Spaces:
Sleeping
Sleeping
Update app.py
#1
by kavehtaheri - opened
app.py
CHANGED
|
@@ -28,21 +28,62 @@ def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
|
|
| 28 |
#color_clips.append(myclip.with_position(text_position))
|
| 29 |
return subtitle_clips
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
def video_edit(srt, input_video, color, font, font_size, input_audio):
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return output_video_file
|
| 47 |
|
| 48 |
with gr.Blocks() as demo:
|
|
|
|
| 28 |
#color_clips.append(myclip.with_position(text_position))
|
| 29 |
return subtitle_clips
|
| 30 |
|
| 31 |
+
import subprocess
|
| 32 |
+
import os
|
| 33 |
+
|
| 34 |
def video_edit(srt, input_video, color, font, font_size, input_audio):
|
| 35 |
+
"""
|
| 36 |
+
Burns subtitles into a video using FFmpeg and replaces the audio.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
srt (str): Path to the SRT subtitle file.
|
| 40 |
+
input_video (str): Path to the input video file.
|
| 41 |
+
color (str): Subtitle color in hex format (e.g., '#FFFF00' for yellow).
|
| 42 |
+
font (str): Font name for subtitles.
|
| 43 |
+
font_size (int): Font size for subtitles.
|
| 44 |
+
input_audio (str): Path to the audio file to replace the video's audio.
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
str: Path to the output video file with burned subtitles and replaced audio.
|
| 48 |
+
"""
|
| 49 |
+
# Derive output file name from input video
|
| 50 |
+
#input_video_base = os.path.splitext(input_video)[0]
|
| 51 |
+
output_video_file = f"{input_video_base}_subtitled.mp4"
|
| 52 |
+
|
| 53 |
+
# Get font directory (assume fonts are in the same directory as the SRT file)
|
| 54 |
+
fonts_dir = os.path.dirname(srt)
|
| 55 |
+
|
| 56 |
+
# Convert color to FFmpeg’s PrimaryColour format (e.g., '#FFFF00' -> '&HFFFF00&')
|
| 57 |
+
''' if not color.startswith('#') or len(color) != 7:
|
| 58 |
+
raise ValueError("Color must be a hex code like '#RRGGBB'")
|
| 59 |
+
ffmpeg_color = '&H' + color[1:] + '&''''
|
| 60 |
+
|
| 61 |
+
# Build subtitle style string for FFmpeg
|
| 62 |
+
subtitle_style = f"FontName={font},FontSize={font_size},PrimaryColour={&HFFFF00&}"
|
| 63 |
+
|
| 64 |
+
# Construct FFmpeg command
|
| 65 |
+
cmd = [
|
| 66 |
+
'ffmpeg',
|
| 67 |
+
'-i', input_video, # Input video file
|
| 68 |
+
# '-i', input_audio, # Input audio file
|
| 69 |
+
'-vf', f"subtitles={srt}:fontsdir={fonts_dir}:force_style='{subtitle_style}'", # Burn subtitles
|
| 70 |
+
'-c:v', 'libx264', # Video codec (H.264)
|
| 71 |
+
'-c:a', 'aac', # Audio codec (AAC, matching MoviePy’s default)
|
| 72 |
+
'-r', '24', # Frame rate (adjust as needed)
|
| 73 |
+
'-preset', 'faster', # Encoding speed vs. compression trade-off
|
| 74 |
+
'-map', '0:v:0', # Use video from first input (after subtitle filter)
|
| 75 |
+
'-map', '1:a:0', # Use audio from second input
|
| 76 |
+
'-y', # Overwrite output file if it exists
|
| 77 |
+
output_video_file # Output file
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
# Execute FFmpeg command
|
| 81 |
+
try:
|
| 82 |
+
subprocess.run(cmd, check=True, stderr=subprocess.PIPE, universal_newlines=True)
|
| 83 |
+
except subprocess.CalledProcessError as e:
|
| 84 |
+
raise RuntimeError(f"FFmpeg failed: {e.stderr}")
|
| 85 |
+
|
| 86 |
+
print(f"Video processed successfully: {output_video_file}")
|
| 87 |
return output_video_file
|
| 88 |
|
| 89 |
with gr.Blocks() as demo:
|