Skip to content

Commit

Permalink
Better ffmpeg exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAplin committed May 11, 2024
1 parent 6baee5f commit c964dca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
5 changes: 3 additions & 2 deletions nndownload/ffmpeg_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ def convert(self, name: AnyStr, duration: float):
while True:
if self.proc.stdout is None:
continue
prev_line = stdout_line
if stdout_line:
prev_line = stdout_line
stdout_line = self.proc.stdout.readline().decode("utf-8", errors="replace").strip()
out_time_data = self.REGEX_OUT_TIME.search(stdout_line)
if out_time_data is not None:
out_time = self.get_timedelta(out_time_data.group(1))
progress.update(out_time.total_seconds() - progress.n)
continue
if stdout_line == "" and self.proc.poll() is not None:
if not stdout_line and self.proc.poll() is not None:
progress.refresh()
progress.close()
exit_code = self.proc.poll()
Expand Down
23 changes: 15 additions & 8 deletions nndownload/nndownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,14 +1297,21 @@ def perform_native_hls_dl(session: requests.Session, filename: AnyStr, duration:
# Video and audio
if len(tasks) > 1:
stream_filenames = [task["filename"] for task in tasks]
video_convert = FfmpegDL(streams=stream_filenames,
input_kwargs={},
output_path=filename,
output_kwargs={
"vcodec": "copy",
"acodec": "copy",
})
video_convert.convert(name='Merging audio and video', duration=duration)

try:
video_convert = FfmpegDL(streams=stream_filenames,
input_kwargs={},
output_path=filename,
output_kwargs={
"vcodec": "copy",
"acodec": "copy",
})
video_convert.convert(name='Merging audio and video', duration=duration)
except FfmpegDLException as error:
raise FormatNotAvailableException(f"ffmpeg failed to download the video or audio stream with the following error: \"{error}\"")
except Exception:
raise FormatNotAvailableException("Failed to download video or audio stream")

for stream_filename in stream_filenames:
os.remove(stream_filename)
# Only audio or video
Expand Down

0 comments on commit c964dca

Please sign in to comment.