Basic progress reader with logging
This commit is contained in:
parent
43e2592de8
commit
93736abc24
|
@ -8,7 +8,6 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
|
@ -190,13 +189,18 @@ func (d *Downloader) downloadAudio(ctx context.Context, video *youtubev2.Video,
|
|||
}, nil
|
||||
}
|
||||
|
||||
func thumbnailGridSize(msecs int) (int, int) {
|
||||
secs := msecs / 1000
|
||||
x := int(math.Floor(math.Sqrt(float64(secs))))
|
||||
if x*x < secs {
|
||||
return x + 1, x
|
||||
}
|
||||
return x, x
|
||||
type progressReader struct {
|
||||
io.Reader
|
||||
total, exp int
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -215,13 +219,14 @@ func (d *Downloader) downloadVideo(ctx context.Context, video *youtubev2.Video,
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("error fetching video stream: %v", err)
|
||||
}
|
||||
reader := progressReader{Reader: stream, exp: int(format.ContentLength)}
|
||||
|
||||
videoFile, err := os.Create(outPath)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue