Refactor format parsing

This commit is contained in:
Rob Watson 2021-10-28 03:23:45 +02:00
parent 8c30a2581a
commit 0e2fb5cd47
4 changed files with 20 additions and 23 deletions

View File

@ -12,6 +12,7 @@ import (
"sync" "sync"
"time" "time"
"git.netflux.io/rob/clipper/media"
"git.netflux.io/rob/clipper/youtube" "git.netflux.io/rob/clipper/youtube"
youtubev2 "github.com/kkdai/youtube/v2" youtubev2 "github.com/kkdai/youtube/v2"
@ -49,9 +50,9 @@ func main() {
switch { switch {
case audioOnly: case audioOnly:
formats = youtube.SortAudio(formats) formats = media.FilterYoutubeAudio(formats)
case videoOnly: case videoOnly:
formats = youtube.SortVideo(formats) formats = media.FilterYoutubeVideo(formats)
} }
fmt.Println("In descending order of preference:") fmt.Println("In descending order of preference:")

View File

@ -75,11 +75,11 @@ func (s *FetchMediaSetService) Fetch(ctx context.Context, id string) (*MediaSet,
return nil, errors.New("no format available") return nil, errors.New("no format available")
} }
// just the audio for now formats := FilterYoutubeAudio(video.Formats)
if len(video.Formats) == 0 {
// grab an audio stream from youtube return nil, errors.New("no format available")
// TODO: avoid possible panic }
format := SortYoutubeAudio(video.Formats)[0] format := formats[0]
sampleRate, err := strconv.Atoi(format.AudioSampleRate) sampleRate, err := strconv.Atoi(format.AudioSampleRate)
if err != nil { 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) return nil, fmt.Errorf("error fetching video: %v", err)
} }
formats := FilterYoutubeAudio(video.Formats)
if len(video.Formats) == 0 { if len(video.Formats) == 0 {
return nil, errors.New("no format available") return nil, errors.New("no format available")
} }
format := formats[0]
// TODO: avoid possible panic
format := SortYoutubeAudio(video.Formats)[0]
stream, _, err := s.youtube.GetStreamContext(ctx, video, &format) stream, _, err := s.youtube.GetStreamContext(ctx, video, &format)
if err != nil { if err != nil {

View File

@ -7,10 +7,9 @@ import (
youtubev2 "github.com/kkdai/youtube/v2" youtubev2 "github.com/kkdai/youtube/v2"
) )
// SortYoutubeAudio returns the provided formats ordered in descending preferred // FilterYoutubeAudio returns the provided formats ordered in descending preferred
// order. The ideal candidate is opus-encoded stereo audio in a webm container, // order. It may have fewer items than the original slice, or none at all.
// with the lowest available bitrate. func FilterYoutubeAudio(inFormats youtubev2.FormatList) youtubev2.FormatList {
func SortYoutubeAudio(inFormats youtubev2.FormatList) youtubev2.FormatList {
var formats youtubev2.FormatList var formats youtubev2.FormatList
for _, format := range inFormats { for _, format := range inFormats {
if format.FPS == 0 && format.AudioChannels > 0 { if format.FPS == 0 && format.AudioChannels > 0 {
@ -33,11 +32,9 @@ func SortYoutubeAudio(inFormats youtubev2.FormatList) youtubev2.FormatList {
return formats return formats
} }
// SortYoutubeVideo returns the provided formats ordered in descending preferred // FilterYoutubeVideo returns the provided formats ordered in descending preferred
// order. The ideal candidate is video in an mp4 container with a low // order. It may have fewer items than the original slice, or none at all.
// bitrate, with audio channels (needed to allow synced playback on the func FilterYoutubeVideo(inFormats youtubev2.FormatList) youtubev2.FormatList {
// website).
func SortYoutubeVideo(inFormats youtubev2.FormatList) youtubev2.FormatList {
var formats youtubev2.FormatList var formats youtubev2.FormatList
for _, format := range inFormats { for _, format := range inFormats {
if format.FPS > 0 && format.ContentLength > 0 { if format.FPS > 0 && format.ContentLength > 0 {

View File

@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestSortAudio(t *testing.T) { func TestFilterAudio(t *testing.T) {
formats := []youtubev2.Format{ formats := []youtubev2.Format{
{ {
MimeType: `audio/webm; codecs="opus"`, 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[1], sortedFormats[0])
assert.Equal(t, formats[4], sortedFormats[1]) assert.Equal(t, formats[4], sortedFormats[1])
@ -51,7 +51,7 @@ func TestSortAudio(t *testing.T) {
assert.Equal(t, formats[2], sortedFormats[4]) assert.Equal(t, formats[2], sortedFormats[4])
} }
func TestSortVideo(t *testing.T) { func TestFilterVideo(t *testing.T) {
formats := []youtubev2.Format{ formats := []youtubev2.Format{
{ {
MimeType: `audio/webm; codecs="opus"`, 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.Len(t, sortedFormats, 3)
assert.Equal(t, formats[1], sortedFormats[0]) assert.Equal(t, formats[1], sortedFormats[0])