diff --git a/goutubedl.go b/goutubedl.go index df21d78..cfee4cc 100644 --- a/goutubedl.go +++ b/goutubedl.go @@ -132,8 +132,9 @@ type Info struct { ThumbnailBytes []byte `json:"-"` Thumbnails []Thumbnail `json:"thumbnails"` - Formats []Format `json:"formats"` - Subtitles map[string][]Subtitle `json:"subtitles"` + Formats []Format `json:"formats"` + Subtitles map[string][]Subtitle `json:"subtitles"` + AutomaticCaptions map[string][]*Caption `json:"automatic_captions"` // Playlist entries if _type is playlist Entries []Info `json:"entries"` @@ -183,6 +184,13 @@ type Subtitle struct { Bytes []byte `json:"-"` } +type Caption struct { + Ext string `json:"ext"` + URL string `json:"url"` + Name string `json:"name"` + Bytes []byte `json:"-"` +} + func (f Format) String() string { return fmt.Sprintf("%s:%s:%s abr:%f vbr:%f tbr:%f", f.FormatID, @@ -217,14 +225,15 @@ var TypeFromString = map[string]Type{ // Options for New() type Options struct { - Type Type - PlaylistStart uint // --playlist-start - PlaylistEnd uint // --playlist-end - Downloader string // --downloader - DownloadThumbnail bool - DownloadSubtitles bool - DownloadSections string // --download-sections - Impersonate string // --impersonate + Type Type + PlaylistStart uint // --playlist-start + PlaylistEnd uint // --playlist-end + Downloader string // --downloader + DownloadThumbnail bool + DownloadSubtitles bool + DownloadSubtitlesLang []string + DownloadSections string // --download-sections + Impersonate string // --impersonate ProxyUrl string // --proxy URL http://host:port or socks5://host:port UseIPV4 bool // -4 Make all connections via IPv4 @@ -476,6 +485,22 @@ func infoFromURL( } } + for _, lang := range options.DownloadSubtitlesLang { + if _, ok := info.AutomaticCaptions[lang]; !ok { + continue + } + + for _, caption := range info.AutomaticCaptions[lang] { + resp, respErr := get(caption.URL) + if respErr == nil { + buf, _ := io.ReadAll(resp.Body) + resp.Body.Close() + caption.Bytes = buf + } + } + + } + if options.DownloadSubtitles { for _, subtitles := range info.Subtitles { for i, subtitle := range subtitles {