avans06 commited on
Commit
4fbf19d
1 Parent(s): 922fe2a

Improve displaying the actual error message on the frontend when yt-dlp fails to download a video.

Browse files
Files changed (1) hide show
  1. src/download.py +15 -12
src/download.py CHANGED
@@ -49,22 +49,25 @@ def _perform_download(url: str, maxDuration: int = None, outputTemplate: str = N
49
 
50
  filename_collector = FilenameCollectorPP()
51
  with redirect_stderr(errStrIO):
52
- with YoutubeDL(ydl_opts) as ydl:
53
- if maxDuration and maxDuration > 0:
54
- info = ydl.extract_info(url, download=False)
55
- entries = "entries" in info and info["entries"] or [info]
 
 
 
56
 
57
- total_duration = 0
58
 
59
- # Compute total duration
60
- for entry in entries:
61
- total_duration += float(entry["duration"])
62
 
63
- if total_duration >= maxDuration:
64
- raise ExceededMaximumDuration(videoDuration=total_duration, maxDuration=maxDuration, message="Video is too long")
65
 
66
- ydl.add_post_processor(filename_collector)
67
- ydl.download([url])
68
 
69
  errMsg = errStrIO.getvalue()
70
  errMsg = [text for text in errMsg.split("\n") if text.startswith("ERROR")] if errMsg else ""
 
49
 
50
  filename_collector = FilenameCollectorPP()
51
  with redirect_stderr(errStrIO):
52
+ for _ in (True,):
53
+ with YoutubeDL(ydl_opts) as ydl:
54
+ if maxDuration and maxDuration > 0:
55
+ info = ydl.extract_info(url, download=False)
56
+ if not info: break
57
+
58
+ entries = "entries" in info and info["entries"] or [info]
59
 
60
+ total_duration = 0
61
 
62
+ # Compute total duration
63
+ for entry in entries:
64
+ if entry: total_duration += float(entry["duration"])
65
 
66
+ if total_duration >= maxDuration:
67
+ raise ExceededMaximumDuration(videoDuration=total_duration, maxDuration=maxDuration, message="Video is too long")
68
 
69
+ ydl.add_post_processor(filename_collector)
70
+ ydl.download([url])
71
 
72
  errMsg = errStrIO.getvalue()
73
  errMsg = [text for text in errMsg.split("\n") if text.startswith("ERROR")] if errMsg else ""