BoxOfColors commited on
Commit
ba77b8a
·
1 Parent(s): c3068ef

Fix: handle no-audio video in transcode_for_browser; fallback on error

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -160,26 +160,32 @@ def _transcode_for_browser(video_path: str) -> str:
160
  """Re-encode *video_path* to H.264 baseline + AAC so all browsers can preview it.
161
 
162
  Returns the path to the transcoded file (written alongside the original).
163
- If the input is already browser-safe the re-encode is still cheap (~1s for
164
- short clips) and ensures consistent behaviour across all upload sources.
165
  """
166
  if video_path is None:
167
  return video_path
168
- out_path = video_path.rsplit(".", 1)[0] + "_browser.mp4"
169
- (
170
- ffmpeg
171
- .input(video_path)
172
- .output(
173
- out_path,
 
174
  vcodec="libx264", preset="fast", crf=18,
175
  profile="baseline", level="3.0",
176
  pix_fmt="yuv420p",
177
- acodec="aac", audio_bitrate="128k",
178
  movflags="+faststart",
179
  )
180
- .run(overwrite_output=True, quiet=True)
181
- )
182
- return out_path
 
 
 
 
 
 
 
183
 
184
  # ------------------------------------------------------------------ #
185
  # Temp directory registry — tracks dirs for cleanup on new generation #
 
160
  """Re-encode *video_path* to H.264 baseline + AAC so all browsers can preview it.
161
 
162
  Returns the path to the transcoded file (written alongside the original).
163
+ Falls back to returning the original path if transcoding fails.
 
164
  """
165
  if video_path is None:
166
  return video_path
167
+ try:
168
+ # Probe to check if audio stream exists
169
+ probe = ffmpeg.probe(video_path)
170
+ has_audio = any(s["codec_type"] == "audio" for s in probe.get("streams", []))
171
+ out_path = video_path.rsplit(".", 1)[0] + "_browser.mp4"
172
+ stream = ffmpeg.input(video_path)
173
+ output_kwargs = dict(
174
  vcodec="libx264", preset="fast", crf=18,
175
  profile="baseline", level="3.0",
176
  pix_fmt="yuv420p",
 
177
  movflags="+faststart",
178
  )
179
+ if has_audio:
180
+ output_kwargs["acodec"] = "aac"
181
+ output_kwargs["audio_bitrate"] = "128k"
182
+ else:
183
+ output_kwargs["an"] = None # no audio track
184
+ stream.output(out_path, **output_kwargs).run(overwrite_output=True, quiet=True)
185
+ return out_path
186
+ except Exception as e:
187
+ print(f"[transcode_for_browser] failed, using original: {e}")
188
+ return video_path
189
 
190
  # ------------------------------------------------------------------ #
191
  # Temp directory registry — tracks dirs for cleanup on new generation #