package main import ( "context" "time" "github.com/lrstanley/go-ytdlp" ) type DownloadOptions struct { EmbedThumbnail bool IncludeSubtitles bool } type ProgressUpdate struct { Status ytdlp.ProgressStatus Percent string ETA time.Duration Filename string Phase string } func downloadVideo(out_dir, url string, opts DownloadOptions, progressChan chan<- ProgressUpdate) { defer close(progressChan) var lastPhase string dl := ytdlp.New(). SetWorkDir(out_dir). FormatSort("res,ext:mp4:m4a"). RecodeVideo("mp4"). ProgressFunc(100*time.Millisecond, func(prog ytdlp.ProgressUpdate) { // Detect phase transition -- differentiate "downloading" as the main download // and "post processing" when the file name changes, preventing it from appearing "reset" phase := "downloading" if prog.Status == ytdlp.ProgressStatusDownloading && prog.Percent() == 0.0 { // If we already had progress, it's likely post-processing if lastPhase == "downloading" { phase = "post-processing" } } else if prog.Status != ytdlp.ProgressStatusDownloading { phase = "post-processing" } lastPhase = phase progressChan <- ProgressUpdate{ Status: prog.Status, Percent: prog.PercentString(), ETA: prog.ETA(), Filename: prog.Filename, Phase: phase, } }). Output("%(title)s.%(ext)s") if opts.EmbedThumbnail { dl = dl.EmbedThumbnail() } if opts.IncludeSubtitles { dl = dl.CompatOptions("no-keep-subs") dl = dl.EmbedSubs() dl = dl.SubLangs("en,en*") dl = dl.WriteAutoSubs() dl = dl.WriteSubs() } _, err := dl.Run(context.TODO(), url) if err != nil { panic(err) } }