BoxOfColors commited on
Commit
7a2a7f5
·
1 Parent(s): 065a652

transcode_for_browser: overwrite in-place to suppress Gradio fallback converter

Browse files
Files changed (1) hide show
  1. app.py +15 -4
app.py CHANGED
@@ -159,7 +159,8 @@ def strip_audio_from_video(video_path: str, output_path: str) -> None:
159
  def _transcode_for_browser(video_path: str) -> str:
160
  """Re-encode uploaded video to H.264/AAC MP4 so the browser preview widget can play it.
161
 
162
- Returns the transcoded path on success, or the original path if transcoding fails.
 
163
  Only called on upload — not during generation.
164
  """
165
  if video_path is None:
@@ -167,7 +168,14 @@ def _transcode_for_browser(video_path: str) -> str:
167
  try:
168
  probe = ffmpeg.probe(video_path)
169
  has_audio = any(s["codec_type"] == "audio" for s in probe.get("streams", []))
170
- out_path = video_path.rsplit(".", 1)[0] + "_preview.mp4"
 
 
 
 
 
 
 
171
  kwargs = dict(
172
  vcodec="libx264", preset="fast", crf=18,
173
  pix_fmt="yuv420p", movflags="+faststart",
@@ -178,10 +186,13 @@ def _transcode_for_browser(video_path: str) -> str:
178
  else:
179
  kwargs["an"] = None
180
  # map 0:v:0 explicitly to skip non-video streams (e.g. data/timecode tracks)
181
- ffmpeg.input(video_path).output(out_path, map="0:v:0", **kwargs).run(
182
  overwrite_output=True, quiet=True
183
  )
184
- return out_path
 
 
 
185
  except Exception as e:
186
  print(f"[transcode_for_browser] failed, using original: {e}")
187
  return video_path
 
159
  def _transcode_for_browser(video_path: str) -> str:
160
  """Re-encode uploaded video to H.264/AAC MP4 so the browser preview widget can play it.
161
 
162
+ Overwrites the original file in-place so Gradio serves the H.264 version
163
+ and does NOT trigger its own slow fallback conversion.
164
  Only called on upload — not during generation.
165
  """
166
  if video_path is None:
 
168
  try:
169
  probe = ffmpeg.probe(video_path)
170
  has_audio = any(s["codec_type"] == "audio" for s in probe.get("streams", []))
171
+ # Check if already H.264 — skip transcode if so
172
+ video_streams = [s for s in probe.get("streams", []) if s["codec_type"] == "video"]
173
+ if video_streams and video_streams[0].get("codec_name") == "h264":
174
+ print(f"[transcode_for_browser] already H.264, skipping")
175
+ return video_path
176
+ # Write to a temp file first, then replace original in-place.
177
+ # This avoids Gradio's own fallback converter triggering on the H.265 source.
178
+ tmp_path = video_path + ".tmp.mp4"
179
  kwargs = dict(
180
  vcodec="libx264", preset="fast", crf=18,
181
  pix_fmt="yuv420p", movflags="+faststart",
 
186
  else:
187
  kwargs["an"] = None
188
  # map 0:v:0 explicitly to skip non-video streams (e.g. data/timecode tracks)
189
+ ffmpeg.input(video_path).output(tmp_path, map="0:v:0", **kwargs).run(
190
  overwrite_output=True, quiet=True
191
  )
192
+ import os as _os
193
+ _os.replace(tmp_path, video_path)
194
+ print(f"[transcode_for_browser] transcoded to H.264 in-place: {video_path}")
195
+ return video_path
196
  except Exception as e:
197
  print(f"[transcode_for_browser] failed, using original: {e}")
198
  return video_path