diff --git a/format.go b/format.go index 104f1e9..9dfaf3e 100644 --- a/format.go +++ b/format.go @@ -34,6 +34,28 @@ type Format struct { Subtitles []*Subtitle } +// InitVideo initializes the Format.Video completely. +// The Format.Video.Chunklist pointer is, by default, nil because an additional +// request must be made to receive its content. The request is not made when +// initializing a Format struct because it would probably cause an intense overhead +// since Format.Video.Chunklist is only used sometimes +func (f *Format) InitVideo() error { + if f.Video.Chunklist == nil { + resp, err := f.crunchy.Client.Get(f.Video.URI) + if err != nil { + return err + } + defer resp.Body.Close() + + playlist, _, err := m3u8.DecodeFrom(resp.Body, true) + if err != nil { + return err + } + f.Video.Chunklist = playlist.(*m3u8.MediaPlaylist) + } + return nil +} + func (f *Format) Download(downloader Downloader) error { if _, err := os.Stat(downloader.Filename); err == nil && !downloader.IgnoreExisting { return fmt.Errorf("file %s already exists", downloader.Filename)