Skip to content

Commit df595d7

Browse files
committed
improve early exit feedback
1 parent a877bc0 commit df595d7

2 files changed

Lines changed: 40 additions & 20 deletions

File tree

html/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ $(function(){
137137
case 'error':
138138
$("#output").show();
139139
$("#spinner").hide();
140-
$("#status").prepend("Error: " + msg.Value + "\n");
140+
var $job = updateJob(msg);
141+
$job.find('.status').prepend("Error: " + msg.Value.Msg + "\n");
141142
break;
142143
case 'unknown':
143144
$("#output").show();
@@ -153,7 +154,7 @@ $(function(){
153154
case 'info':
154155
$("#output").show();
155156
$("#spinner").hide();
156-
var $job = updateJob(msg);
157+
updateJob(msg);
157158
break;
158159
case 'link_stream':
159160
if(!isPlaying()) {

internal/ytworker/download.go

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const (
3434
KeyCompleted = "completed"
3535
KeyUnknown = "unknown"
3636
KeyInfo = "info"
37+
KeyError = "error"
3738
KeyLinkStream = "link_stream"
3839
)
3940

@@ -150,6 +151,12 @@ func (yt *Download) Work(j *jobs.Job) {
150151
err = yt.download(ctx, id, yt.OutCh, url)
151152
if err != nil {
152153
slog.Error("download() error", "error", err)
154+
val := Misc{
155+
Id: id,
156+
Msg: err.Error(),
157+
}
158+
m := util.Msg{Key: KeyError, Value: val}
159+
yt.OutCh <- m
153160
return
154161
}
155162

@@ -190,9 +197,7 @@ func (yt *Download) download(ctx context.Context, id int64, outCh chan<- util.Ms
190197
// Faster youtube downloads: this combined with -S proto:dash ensures that we get dash https://github.com/yt-dlp/yt-dlp/issues/7417
191198
//"--extractor-args", "youtube:formats=duplicate",
192199

193-
// Added 2025-01-18 after finding very poor quality audio from the dash settings above.
194-
// It seems Youtube has moved from 48kb/s to 32kb/s in their LQ files and it sounds awful!
195-
// prefer best audio-only format, otherwise fallback to best any format
200+
// prefer best audio-only format, otherwise fallback to best any format
196201
"-f", "bestaudio/best",
197202
}
198203

@@ -220,24 +225,38 @@ func (yt *Download) download(ctx context.Context, id int64, outCh chan<- util.Ms
220225
return err
221226
}
222227

223-
infoFileName := ""
224-
count := 0
225-
for {
228+
infoFileName := diskFileNameTmp + ".info.json"
226229

227-
infoFileName = diskFileNameTmp + ".info.json"
228-
229-
time.Sleep(500 * time.Millisecond)
230-
_, err := os.Stat(infoFileName)
231-
if err == nil {
232-
break
233-
} else if !os.IsNotExist(err) {
234-
return err
230+
infoCheck := func() error {
231+
ticker := time.NewTicker(500 * time.Millisecond)
232+
count := 0
233+
for {
234+
select {
235+
case line := <-cmdOutCh:
236+
misc := Misc{
237+
Id: id,
238+
Msg: line,
239+
}
240+
m := util.Msg{Key: KeyUnknown, Value: misc}
241+
outCh <- m
242+
case <-ticker.C:
243+
_, err := os.Stat(infoFileName)
244+
if err == nil {
245+
return nil
246+
} else if !os.IsNotExist(err) {
247+
return err
248+
}
249+
if count > 20 {
250+
return fmt.Errorf("waited too long for info file")
251+
}
252+
count++
253+
}
235254
}
255+
}
236256

237-
if count > 20 {
238-
return fmt.Errorf("waited too long for info file")
239-
}
240-
count++
257+
err = infoCheck()
258+
if err != nil {
259+
return err
241260
}
242261

243262
raw, err := os.ReadFile(infoFileName)

0 commit comments

Comments
 (0)