package ytdlp import ( "sort" "github.com/noderunners/dlbot/internal/media" ) func (f Format) size(duration Float) int64 { switch { case f.Filesize > 0: return int64(f.Filesize) case f.FilesizeAprx > 0: return int64(f.FilesizeAprx) case f.TBR > 0 && duration > 0: return int64(float64(f.TBR) * 1000 / 8 * float64(duration)) } return 0 } func (f Format) hasVideo() bool { return f.VCodec != "none" && f.Ext != "mhtml" } func (f Format) hasAudio() bool { return f.ACodec != "none" } func (f Format) audioOnly() bool { return f.VCodec == "none" && f.ACodec != "none" && f.ACodec != "" } func isH264(codec string) bool { return len(codec) >= 4 && (codec[:4] == "avc1" || codec[:4] == "h264") } // MaxHeight returns the tallest video format, or 0 when heights are unknown. func (i *Info) MaxHeight() int { h := int(i.Height) for _, f := range i.Formats { if f.hasVideo() && int(f.Height) > h { h = int(f.Height) } } return h } // HasAudio reports whether an audio track is likely. Unknown counts as yes // so that we do not hide the audio buttons on sites with sparse metadata. func (i *Info) HasAudio() bool { if len(i.Formats) == 0 { return true } for _, f := range i.Formats { if f.hasAudio() { return true } } return false } // SubtitleLangs returns the available subtitle languages, manual ones // first, then automatic captions, each group sorted. func (i *Info) SubtitleLangs() (manual, auto []string) { for l := range i.Subtitles { if l != "live_chat" { manual = append(manual, l) } } for l := range i.AutomaticCaptions { if _, dup := i.Subtitles[l]; !dup { auto = append(auto, l) } } sort.Strings(manual) sort.Strings(auto) return manual, auto } // EstimateSize guesses the output size of a profile in bytes from the // format list. It returns 0 when the metadata is not good enough to guess. // The estimate mirrors the selection in profileArgs: prefer h264 and the // tallest format under the height cap, plus the best audio track when the // video format carries none. func (i *Info) EstimateSize(p media.Profile) int64 { switch p.Kind() { case media.KindThumb, media.KindSubs: return 0 case media.KindAudio: if a := i.bestAudio(); a != nil { return a.size(i.Duration) } return 0 } v := i.pickVideo(p.Height()) if v == nil { if i.Filesize > 0 { return int64(i.Filesize) } return int64(i.FilesizeAprx) } total := v.size(i.Duration) if total == 0 { return 0 } if !v.hasAudio() { if a := i.bestAudio(); a != nil { total += a.size(i.Duration) } } return total } func (i *Info) pickVideo(maxHeight int) *Format { var best *Format better := func(a, b *Format) bool { // is a better than b if isH264(a.VCodec) != isH264(b.VCodec) { return isH264(a.VCodec) } if a.Height != b.Height { return a.Height > b.Height } return a.size(i.Duration) > b.size(i.Duration) } for idx := range i.Formats { f := &i.Formats[idx] if !f.hasVideo() { continue } if maxHeight > 0 && int(f.Height) > maxHeight { continue } if best == nil || better(f, best) { best = f } } if best == nil && maxHeight > 0 { // Nothing under the cap: yt-dlp falls back to the smallest above it. for idx := range i.Formats { f := &i.Formats[idx] if f.hasVideo() && (best == nil || f.Height < best.Height) { best = f } } } return best } func (i *Info) bestAudio() *Format { var best *Format for idx := range i.Formats { f := &i.Formats[idx] if !f.audioOnly() { continue } if best == nil || f.size(i.Duration) > best.size(i.Duration) { best = f } } return best }