diff --git a/backend/youtube/youtube.go b/backend/youtube/youtube.go index 1f459b3..6f45307 100644 --- a/backend/youtube/youtube.go +++ b/backend/youtube/youtube.go @@ -23,13 +23,14 @@ import ( const ( SizeOfInt16 = 2 - EncodedAudioCodec = "pcm_s16le" - EncodedAudioFormat = "s16le" - EncodedAudioSampleRate = 48000 + rawAudioCodec = "pcm_s16le" + rawAudioFormat = "s16le" + rawAudioSampleRate = 48000 - thumbnailWidth = 30 - thumbnailHeight = 100 - videoItag = 18 + thumbnailPrescaleWidth = -1 + thumbnailPrescaleHeight = 120 + thumbnailWidth = 30 + thumbnailHeight = 100 ) // YoutubeClient wraps the youtube.Client client. @@ -149,7 +150,7 @@ func (d *Downloader) downloadAudio(ctx context.Context, video *youtubev2.Video, streamReader := io.TeeReader(stream, encodedAudioFile) var errOut bytes.Buffer - cmd := exec.CommandContext(ctx, "ffmpeg", "-i", "-", "-f", EncodedAudioFormat, "-ar", strconv.Itoa(EncodedAudioSampleRate), "-acodec", EncodedAudioCodec, "-") + cmd := exec.CommandContext(ctx, "ffmpeg", "-i", "-", "-f", rawAudioFormat, "-ar", strconv.Itoa(rawAudioSampleRate), "-acodec", rawAudioCodec, "-") cmd.Stdin = streamReader cmd.Stdout = rawAudioFile cmd.Stderr = &errOut @@ -187,9 +188,10 @@ func (d *Downloader) downloadAudio(ctx context.Context, video *youtubev2.Video, }, nil } -func thumbnailGridSize(seconds int) (int, int) { - x := int(math.Floor(math.Sqrt(float64(seconds)))) - if x*x < seconds { +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 @@ -217,11 +219,22 @@ func (d *Downloader) downloadVideo(ctx context.Context, video *youtubev2.Video, if err != nil { return nil, fmt.Errorf("could not parse video duration: %s", err) } - durationSecs := durationMsecs / 1000 - gridSizeX, gridSizeY := thumbnailGridSize(durationSecs) + gridSizeX, gridSizeY := thumbnailGridSize(durationMsecs) var errOut bytes.Buffer - cmd := exec.CommandContext(ctx, "ffmpeg", "-i", "-", "-vf", fmt.Sprintf("fps=1,scale=-1:110,crop=%d:%d,tile=%dx%d", thumbnailWidth, thumbnailHeight, gridSizeX, gridSizeY), "-f", "image2pipe", "-vsync", "0", thumbnailOutPath) + cmd := exec.CommandContext( + ctx, + "ffmpeg", + "-i", + "-", + "-vf", + fmt.Sprintf("fps=1,scale=%d:%d,crop=%d:%d,tile=%dx%d", thumbnailPrescaleWidth, thumbnailPrescaleHeight, thumbnailWidth, thumbnailHeight, gridSizeX, gridSizeY), + "-f", + "image2pipe", + "-vsync", + "0", + thumbnailOutPath, + ) cmd.Stdin = streamReader cmd.Stderr = &errOut @@ -230,12 +243,10 @@ func (d *Downloader) downloadVideo(ctx context.Context, video *youtubev2.Video, return nil, fmt.Errorf("error processing video: %v", err) } - duration := time.Duration(durationMsecs) * time.Millisecond - return &media.Video{ Bytes: format.ContentLength, ThumbnailWidth: thumbnailWidth, ThumbnailHeight: thumbnailHeight, - Duration: duration, + Duration: time.Duration(durationMsecs) * time.Millisecond, }, nil }