Tidy code, improve naming

This commit is contained in:
Rob Watson 2021-09-24 07:37:49 +02:00
parent c71c3d251a
commit 658ffa4344
1 changed files with 27 additions and 16 deletions

View File

@ -23,13 +23,14 @@ import (
const ( const (
SizeOfInt16 = 2 SizeOfInt16 = 2
EncodedAudioCodec = "pcm_s16le" rawAudioCodec = "pcm_s16le"
EncodedAudioFormat = "s16le" rawAudioFormat = "s16le"
EncodedAudioSampleRate = 48000 rawAudioSampleRate = 48000
thumbnailPrescaleWidth = -1
thumbnailPrescaleHeight = 120
thumbnailWidth = 30 thumbnailWidth = 30
thumbnailHeight = 100 thumbnailHeight = 100
videoItag = 18
) )
// YoutubeClient wraps the youtube.Client client. // 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) streamReader := io.TeeReader(stream, encodedAudioFile)
var errOut bytes.Buffer 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.Stdin = streamReader
cmd.Stdout = rawAudioFile cmd.Stdout = rawAudioFile
cmd.Stderr = &errOut cmd.Stderr = &errOut
@ -187,9 +188,10 @@ func (d *Downloader) downloadAudio(ctx context.Context, video *youtubev2.Video,
}, nil }, nil
} }
func thumbnailGridSize(seconds int) (int, int) { func thumbnailGridSize(msecs int) (int, int) {
x := int(math.Floor(math.Sqrt(float64(seconds)))) secs := msecs / 1000
if x*x < seconds { x := int(math.Floor(math.Sqrt(float64(secs))))
if x*x < secs {
return x + 1, x return x + 1, x
} }
return x, x return x, x
@ -217,11 +219,22 @@ func (d *Downloader) downloadVideo(ctx context.Context, video *youtubev2.Video,
if err != nil { if err != nil {
return nil, fmt.Errorf("could not parse video duration: %s", err) return nil, fmt.Errorf("could not parse video duration: %s", err)
} }
durationSecs := durationMsecs / 1000 gridSizeX, gridSizeY := thumbnailGridSize(durationMsecs)
gridSizeX, gridSizeY := thumbnailGridSize(durationSecs)
var errOut bytes.Buffer 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.Stdin = streamReader
cmd.Stderr = &errOut 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) return nil, fmt.Errorf("error processing video: %v", err)
} }
duration := time.Duration(durationMsecs) * time.Millisecond
return &media.Video{ return &media.Video{
Bytes: format.ContentLength, Bytes: format.ContentLength,
ThumbnailWidth: thumbnailWidth, ThumbnailWidth: thumbnailWidth,
ThumbnailHeight: thumbnailHeight, ThumbnailHeight: thumbnailHeight,
Duration: duration, Duration: time.Duration(durationMsecs) * time.Millisecond,
}, nil }, nil
} }