feat: capture yt-dlp stderr on download failures for debugging

Add StderrLog field to DownloadJob and collect all stderr lines during
download. Log the captured stderr output when a download fails to help
diagnose exit code 2 errors (geo-restrictions, auth, etc.).
This commit is contained in:
2026-06-25 16:12:03 +03:30
parent 8408b17ca0
commit cd7aaeebc2
3 changed files with 10 additions and 6 deletions

View File

@@ -25,8 +25,6 @@ func parseProgressLine(line string) *progressUpdate {
if line == "" || !strings.HasPrefix(line, "[download]") {
return nil
}
// Try to extract the simple percentage from the default format:
// [download] 45.2% of 123.45MiB at 2.34MiB/s ETA 00:45
m := progressRE.FindStringSubmatch(line)
if m == nil {
return nil
@@ -38,12 +36,10 @@ func parseProgressLine(line string) *progressUpdate {
u := &progressUpdate{pct: pct}
// Extract speed and ETA from remaining parts
rest := line
if idx := strings.Index(rest, "%"); idx >= 0 {
rest = rest[idx+1:]
}
// Look for "at <speed>"
if atIdx := strings.Index(rest, " at "); atIdx >= 0 {
afterAt := rest[atIdx+4:]
if spaceIdx := strings.Index(afterAt, " "); spaceIdx >= 0 {
@@ -51,7 +47,6 @@ func parseProgressLine(line string) *progressUpdate {
rest = afterAt[spaceIdx:]
}
}
// Look for "ETA <duration>"
if etaIdx := strings.Index(rest, "ETA "); etaIdx >= 0 {
etaStr := strings.TrimSpace(rest[etaIdx+4:])
u.eta = etaStr
@@ -63,9 +58,16 @@ func parseProgressLine(line string) *progressUpdate {
// readProgress reads yt-dlp stderr and sends progress updates to the channel.
// The caller is responsible for closing the updates channel.
func readProgress(stderr io.ReadCloser, updates chan<- *domain.DownloadJob, job *domain.DownloadJob) {
var stderrBuf strings.Builder
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
line := scanner.Text()
if stderrBuf.Len() < 4096 {
stderrBuf.WriteString(line)
stderrBuf.WriteByte('\n')
}
parsed := parseProgressLine(line)
if parsed == nil {
continue
@@ -75,4 +77,5 @@ func readProgress(stderr io.ReadCloser, updates chan<- *domain.DownloadJob, job
job.ETA = parsed.eta
updates <- job
}
job.StderrLog = stderrBuf.String()
}

View File

@@ -315,7 +315,7 @@ func (c *Client) StartDownload(job *domain.DownloadJob, downloadDir string) (<-c
<-progressDone
if err != nil {
job.Status = domain.StatusFailed
slog.Error("download failed", "url", job.URL, "error", err)
slog.Error("download failed", "url", job.URL, "error", err, "stderr", job.StderrLog)
} else {
job.Status = domain.StatusCompleted
}

View File

@@ -128,6 +128,7 @@ type DownloadJob struct {
FilePath string
Title string
QuotaApplied bool
StderrLog string
CancelCh chan struct{}
Cmd *exec.Cmd
}