Basic progress reader with logging
This commit is contained in:
parent
43e2592de8
commit
93736abc24
|
@ -8,7 +8,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -190,13 +189,18 @@ func (d *Downloader) downloadAudio(ctx context.Context, video *youtubev2.Video,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func thumbnailGridSize(msecs int) (int, int) {
|
type progressReader struct {
|
||||||
secs := msecs / 1000
|
io.Reader
|
||||||
x := int(math.Floor(math.Sqrt(float64(secs))))
|
total, exp int
|
||||||
if x*x < secs {
|
|
||||||
return x + 1, x
|
|
||||||
}
|
}
|
||||||
return x, x
|
|
||||||
|
func (pw *progressReader) Read(p []byte) (int, error) {
|
||||||
|
n, err := pw.Reader.Read(p)
|
||||||
|
pw.total += n
|
||||||
|
|
||||||
|
log.Printf("[ProgressReader] Read %d of %d (%.02f%%) bytes from the provided reader", pw.total, pw.exp, (float32(pw.total)/float32(pw.exp))*100.0)
|
||||||
|
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) downloadVideo(ctx context.Context, video *youtubev2.Video, outPath string, thumbnailOutPath string) (*media.Video, error) {
|
func (d *Downloader) downloadVideo(ctx context.Context, video *youtubev2.Video, outPath string, thumbnailOutPath string) (*media.Video, error) {
|
||||||
|
@ -215,13 +219,14 @@ func (d *Downloader) downloadVideo(ctx context.Context, video *youtubev2.Video,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error fetching video stream: %v", err)
|
return nil, fmt.Errorf("error fetching video stream: %v", err)
|
||||||
}
|
}
|
||||||
|
reader := progressReader{Reader: stream, exp: int(format.ContentLength)}
|
||||||
|
|
||||||
videoFile, err := os.Create(outPath)
|
videoFile, err := os.Create(outPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating video file: %v", err)
|
return nil, fmt.Errorf("error creating video file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = io.Copy(videoFile, stream); err != nil {
|
if _, err = io.Copy(videoFile, &reader); err != nil {
|
||||||
return nil, fmt.Errorf("error processing video: %v", err)
|
return nil, fmt.Errorf("error processing video: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue