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:
@@ -25,8 +25,6 @@ func parseProgressLine(line string) *progressUpdate {
|
|||||||
if line == "" || !strings.HasPrefix(line, "[download]") {
|
if line == "" || !strings.HasPrefix(line, "[download]") {
|
||||||
return nil
|
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)
|
m := progressRE.FindStringSubmatch(line)
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -38,12 +36,10 @@ func parseProgressLine(line string) *progressUpdate {
|
|||||||
|
|
||||||
u := &progressUpdate{pct: pct}
|
u := &progressUpdate{pct: pct}
|
||||||
|
|
||||||
// Extract speed and ETA from remaining parts
|
|
||||||
rest := line
|
rest := line
|
||||||
if idx := strings.Index(rest, "%"); idx >= 0 {
|
if idx := strings.Index(rest, "%"); idx >= 0 {
|
||||||
rest = rest[idx+1:]
|
rest = rest[idx+1:]
|
||||||
}
|
}
|
||||||
// Look for "at <speed>"
|
|
||||||
if atIdx := strings.Index(rest, " at "); atIdx >= 0 {
|
if atIdx := strings.Index(rest, " at "); atIdx >= 0 {
|
||||||
afterAt := rest[atIdx+4:]
|
afterAt := rest[atIdx+4:]
|
||||||
if spaceIdx := strings.Index(afterAt, " "); spaceIdx >= 0 {
|
if spaceIdx := strings.Index(afterAt, " "); spaceIdx >= 0 {
|
||||||
@@ -51,7 +47,6 @@ func parseProgressLine(line string) *progressUpdate {
|
|||||||
rest = afterAt[spaceIdx:]
|
rest = afterAt[spaceIdx:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Look for "ETA <duration>"
|
|
||||||
if etaIdx := strings.Index(rest, "ETA "); etaIdx >= 0 {
|
if etaIdx := strings.Index(rest, "ETA "); etaIdx >= 0 {
|
||||||
etaStr := strings.TrimSpace(rest[etaIdx+4:])
|
etaStr := strings.TrimSpace(rest[etaIdx+4:])
|
||||||
u.eta = etaStr
|
u.eta = etaStr
|
||||||
@@ -63,9 +58,16 @@ func parseProgressLine(line string) *progressUpdate {
|
|||||||
// readProgress reads yt-dlp stderr and sends progress updates to the channel.
|
// readProgress reads yt-dlp stderr and sends progress updates to the channel.
|
||||||
// The caller is responsible for closing the updates channel.
|
// The caller is responsible for closing the updates channel.
|
||||||
func readProgress(stderr io.ReadCloser, updates chan<- *domain.DownloadJob, job *domain.DownloadJob) {
|
func readProgress(stderr io.ReadCloser, updates chan<- *domain.DownloadJob, job *domain.DownloadJob) {
|
||||||
|
var stderrBuf strings.Builder
|
||||||
scanner := bufio.NewScanner(stderr)
|
scanner := bufio.NewScanner(stderr)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
|
|
||||||
|
if stderrBuf.Len() < 4096 {
|
||||||
|
stderrBuf.WriteString(line)
|
||||||
|
stderrBuf.WriteByte('\n')
|
||||||
|
}
|
||||||
|
|
||||||
parsed := parseProgressLine(line)
|
parsed := parseProgressLine(line)
|
||||||
if parsed == nil {
|
if parsed == nil {
|
||||||
continue
|
continue
|
||||||
@@ -75,4 +77,5 @@ func readProgress(stderr io.ReadCloser, updates chan<- *domain.DownloadJob, job
|
|||||||
job.ETA = parsed.eta
|
job.ETA = parsed.eta
|
||||||
updates <- job
|
updates <- job
|
||||||
}
|
}
|
||||||
|
job.StderrLog = stderrBuf.String()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -315,7 +315,7 @@ func (c *Client) StartDownload(job *domain.DownloadJob, downloadDir string) (<-c
|
|||||||
<-progressDone
|
<-progressDone
|
||||||
if err != nil {
|
if err != nil {
|
||||||
job.Status = domain.StatusFailed
|
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 {
|
} else {
|
||||||
job.Status = domain.StatusCompleted
|
job.Status = domain.StatusCompleted
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,6 +128,7 @@ type DownloadJob struct {
|
|||||||
FilePath string
|
FilePath string
|
||||||
Title string
|
Title string
|
||||||
QuotaApplied bool
|
QuotaApplied bool
|
||||||
|
StderrLog string
|
||||||
CancelCh chan struct{}
|
CancelCh chan struct{}
|
||||||
Cmd *exec.Cmd
|
Cmd *exec.Cmd
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user