BoxOfColors commited on
Commit
1a0c4c0
·
1 Parent(s): ec649c1

Fix ffmpeg stream-copy crash in strip_audio_from_video

Browse files

Falls back to re-encoding (libx264) when the source codec can't be
stream-copied into the mp4 container (e.g. VP9/webm, ProRes/mov),
instead of letting ffmpeg.Error crash the request.

Files changed (1) hide show
  1. app.py +19 -5
app.py CHANGED
@@ -293,10 +293,23 @@ def get_video_duration(video_path: str) -> float:
293
  return float(probe["format"]["duration"])
294
 
295
  def strip_audio_from_video(video_path: str, output_path: str) -> None:
296
- """Write a silent copy of *video_path* to *output_path* (stream-copy, no re-encode)."""
297
- ffmpeg.input(video_path).output(output_path, vcodec="copy", an=None).run(
298
- overwrite_output=True, quiet=True
299
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
300
 
301
  def _transcode_for_browser(video_path: str) -> str:
302
  """Re-encode uploaded video to H.264/AAC MP4 so the browser preview widget can play it.
@@ -340,7 +353,8 @@ def _transcode_for_browser(video_path: str) -> str:
340
  print(f"[transcode_for_browser] transcoded to H.264: {out_path}")
341
  return out_path
342
  except Exception as e:
343
- print(f"[transcode_for_browser] failed, using original: {e}")
 
344
  return video_path
345
 
346
 
 
293
  return float(probe["format"]["duration"])
294
 
295
  def strip_audio_from_video(video_path: str, output_path: str) -> None:
296
+ """Write a silent copy of *video_path* to *output_path*.
297
+
298
+ Tries a fast stream-copy first. Falls back to re-encoding when the
299
+ source codec can't be stream-copied into an mp4 container (e.g.
300
+ VP9/webm, ProRes/mov, or any upload that skipped/failed the
301
+ browser-preview H.264 transcode in _transcode_for_browser)."""
302
+ try:
303
+ ffmpeg.input(video_path).output(output_path, vcodec="copy", an=None).run(
304
+ overwrite_output=True, quiet=True
305
+ )
306
+ except ffmpeg.Error as e:
307
+ stderr = e.stderr.decode(errors="replace") if e.stderr else str(e)
308
+ print(f"[strip_audio_from_video] stream-copy failed, re-encoding instead: {stderr[-2000:]}")
309
+ ffmpeg.input(video_path).output(
310
+ output_path, vcodec="libx264", preset="fast", crf=18,
311
+ pix_fmt="yuv420p", an=None,
312
+ ).run(overwrite_output=True, quiet=True)
313
 
314
  def _transcode_for_browser(video_path: str) -> str:
315
  """Re-encode uploaded video to H.264/AAC MP4 so the browser preview widget can play it.
 
353
  print(f"[transcode_for_browser] transcoded to H.264: {out_path}")
354
  return out_path
355
  except Exception as e:
356
+ stderr = e.stderr.decode(errors="replace") if isinstance(e, ffmpeg.Error) and e.stderr else str(e)
357
+ print(f"[transcode_for_browser] failed, using original: {stderr[-2000:]}")
358
  return video_path
359
 
360