Added InitVideo() function to initialize Format.Video.Chunklist

This commit is contained in:
bytedream 2022-02-21 15:19:26 +01:00
parent 685dbc622e
commit 758b9b59c8

View file

@ -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)