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

Fix: transcode uploaded video to H.264/AAC on upload for browser preview

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py CHANGED
@@ -156,6 +156,31 @@ def strip_audio_from_video(video_path: str, output_path: str) -> None:
156
  overwrite_output=True, quiet=True
157
  )
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  # ------------------------------------------------------------------ #
160
  # Temp directory registry — tracks dirs for cleanup on new generation #
161
  # ------------------------------------------------------------------ #
@@ -2719,6 +2744,13 @@ with gr.Blocks(title="Generate Audio for Video", css=_SLOT_CSS, js=_GLOBAL_JS) a
2719
  hf_slot_vids, hf_slot_waves,
2720
  )
2721
 
 
 
 
 
 
 
 
2722
  # ---- Cross-tab video sync ----
2723
  _sync = lambda v: (gr.update(value=v), gr.update(value=v))
2724
  taro_video.change(fn=_sync, inputs=[taro_video], outputs=[mma_video, hf_video])
 
156
  overwrite_output=True, quiet=True
157
  )
158
 
159
+ 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 #
186
  # ------------------------------------------------------------------ #
 
2744
  hf_slot_vids, hf_slot_waves,
2745
  )
2746
 
2747
+ # ---- Browser-safe transcode on upload ----
2748
+ # Re-encodes uploaded video to H.264 baseline so all browsers can preview it.
2749
+ # Each tab transcodes independently; cross-tab sync propagates the result.
2750
+ taro_video.upload(fn=_transcode_for_browser, inputs=[taro_video], outputs=[taro_video])
2751
+ mma_video.upload(fn=_transcode_for_browser, inputs=[mma_video], outputs=[mma_video])
2752
+ hf_video.upload(fn=_transcode_for_browser, inputs=[hf_video], outputs=[hf_video])
2753
+
2754
  # ---- Cross-tab video sync ----
2755
  _sync = lambda v: (gr.update(value=v), gr.update(value=v))
2756
  taro_video.change(fn=_sync, inputs=[taro_video], outputs=[mma_video, hf_video])