pass download status to discord

This commit is contained in:
2026-01-26 20:16:13 -05:00
parent 4bad5f72da
commit aa9dc58259
2 changed files with 80 additions and 36 deletions

View File

@@ -2,7 +2,6 @@ package main
import (
"context"
"fmt"
"time"
"github.com/lrstanley/go-ytdlp"
@@ -13,19 +12,45 @@ type DownloadOptions struct {
IncludeSubtitles bool
}
func downloadVideo(out_dir, url string, opts DownloadOptions) {
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) {
fmt.Printf(
"%s @ %s [eta: %s] :: %s\n",
prog.Status,
prog.PercentString(),
prog.ETA(),
prog.Filename,
)
// 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")