diff --git a/backend/cmd/ytdebug/main.go b/backend/cmd/ytdebug/main.go index 4829b06..3633072 100644 --- a/backend/cmd/ytdebug/main.go +++ b/backend/cmd/ytdebug/main.go @@ -12,6 +12,7 @@ import ( "sync" "time" + "git.netflux.io/rob/clipper/media" "git.netflux.io/rob/clipper/youtube" youtubev2 "github.com/kkdai/youtube/v2" @@ -49,9 +50,9 @@ func main() { switch { case audioOnly: - formats = youtube.SortAudio(formats) + formats = media.FilterYoutubeAudio(formats) case videoOnly: - formats = youtube.SortVideo(formats) + formats = media.FilterYoutubeVideo(formats) } fmt.Println("In descending order of preference:") diff --git a/backend/media/fetch.go b/backend/media/fetch.go index 086110b..f7adeee 100644 --- a/backend/media/fetch.go +++ b/backend/media/fetch.go @@ -75,11 +75,11 @@ func (s *FetchMediaSetService) Fetch(ctx context.Context, id string) (*MediaSet, return nil, errors.New("no format available") } - // just the audio for now - - // grab an audio stream from youtube - // TODO: avoid possible panic - format := SortYoutubeAudio(video.Formats)[0] + formats := FilterYoutubeAudio(video.Formats) + if len(video.Formats) == 0 { + return nil, errors.New("no format available") + } + format := formats[0] sampleRate, err := strconv.Atoi(format.AudioSampleRate) if err != nil { @@ -126,12 +126,11 @@ func (s *FetchMediaSetService) FetchAudio(ctx context.Context, id string) (Fetch return nil, fmt.Errorf("error fetching video: %v", err) } + formats := FilterYoutubeAudio(video.Formats) if len(video.Formats) == 0 { return nil, errors.New("no format available") } - - // TODO: avoid possible panic - format := SortYoutubeAudio(video.Formats)[0] + format := formats[0] stream, _, err := s.youtube.GetStreamContext(ctx, video, &format) if err != nil { diff --git a/backend/media/youtube.go b/backend/media/youtube.go index a82a2df..0d13849 100644 --- a/backend/media/youtube.go +++ b/backend/media/youtube.go @@ -7,10 +7,9 @@ import ( youtubev2 "github.com/kkdai/youtube/v2" ) -// SortYoutubeAudio returns the provided formats ordered in descending preferred -// order. The ideal candidate is opus-encoded stereo audio in a webm container, -// with the lowest available bitrate. -func SortYoutubeAudio(inFormats youtubev2.FormatList) youtubev2.FormatList { +// FilterYoutubeAudio returns the provided formats ordered in descending preferred +// order. It may have fewer items than the original slice, or none at all. +func FilterYoutubeAudio(inFormats youtubev2.FormatList) youtubev2.FormatList { var formats youtubev2.FormatList for _, format := range inFormats { if format.FPS == 0 && format.AudioChannels > 0 { @@ -33,11 +32,9 @@ func SortYoutubeAudio(inFormats youtubev2.FormatList) youtubev2.FormatList { return formats } -// SortYoutubeVideo returns the provided formats ordered in descending preferred -// order. The ideal candidate is video in an mp4 container with a low -// bitrate, with audio channels (needed to allow synced playback on the -// website). -func SortYoutubeVideo(inFormats youtubev2.FormatList) youtubev2.FormatList { +// FilterYoutubeVideo returns the provided formats ordered in descending preferred +// order. It may have fewer items than the original slice, or none at all. +func FilterYoutubeVideo(inFormats youtubev2.FormatList) youtubev2.FormatList { var formats youtubev2.FormatList for _, format := range inFormats { if format.FPS > 0 && format.ContentLength > 0 { diff --git a/backend/media/youtube_test.go b/backend/media/youtube_test.go index c687f54..a4406d6 100644 --- a/backend/media/youtube_test.go +++ b/backend/media/youtube_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestSortAudio(t *testing.T) { +func TestFilterAudio(t *testing.T) { formats := []youtubev2.Format{ { MimeType: `audio/webm; codecs="opus"`, @@ -42,7 +42,7 @@ func TestSortAudio(t *testing.T) { }, } - sortedFormats := media.SortYoutubeAudio(formats) + sortedFormats := media.FilterYoutubeAudio(formats) assert.Equal(t, formats[1], sortedFormats[0]) assert.Equal(t, formats[4], sortedFormats[1]) @@ -51,7 +51,7 @@ func TestSortAudio(t *testing.T) { assert.Equal(t, formats[2], sortedFormats[4]) } -func TestSortVideo(t *testing.T) { +func TestFilterVideo(t *testing.T) { formats := []youtubev2.Format{ { MimeType: `audio/webm; codecs="opus"`, @@ -79,7 +79,7 @@ func TestSortVideo(t *testing.T) { }, } - sortedFormats := media.SortYoutubeVideo(formats) + sortedFormats := media.FilterYoutubeVideo(formats) assert.Len(t, sortedFormats, 3) assert.Equal(t, formats[1], sortedFormats[0])