Files
Media-download-telegram-bot/internal/ytdlp/estimate.go
T
MichilisandClaude Fable 5.2 3b361c17a1 Phase 1: core bot
Link in, card, download, send, in private chats and groups.

- cmd/core with an embedded download worker
- Telegram front end on telego v1.13.0 (Bot API 10.4): link card with
  thumbnail and format buttons, one live status message, /dl /audio /thumb
  /info /queue /cancel, group support with requester-only buttons
- Database backed queue: paid and free lanes, leases with heartbeats, one
  automatic retry, cancel, queue position and wait estimate
- yt-dlp and ffprobe behind a Runner interface with classified, redacted
  errors mapped to friendly messages
- SQLite via modernc with goose migrations, portable schema
- Local file storage behind a Storage interface
- go-i18n with an English catalog, no literal text in handlers
- Free tier limits enforced before queueing and again on the real file
- Bot token and webhook secret are stripped from all log output
- Tests: URL parsing, job lifecycle, worker, yt-dlp wrapper, and end to end
  flows against a fake Bot API server; opt-in test with the real yt-dlp

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 07:33:32 +00:00

156 lines
3.5 KiB
Go

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
}