Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -28,62 +28,21 @@ 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 |
-
import subprocess
|
32 |
-
import os
|
33 |
-
|
34 |
def video_edit(srt, input_video, color, font, font_size, input_audio):
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
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}_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 |
-
ffmpeg_color='&HFFFF00&'
|
61 |
-
# Build subtitle style string for FFmpeg
|
62 |
-
subtitle_style = f"FontName={font},FontSize={font_size},PrimaryColour={ffmpeg_color}"
|
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:
|
|
|
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 |
+
print(input_video)
|
33 |
+
input_video_name = input_video.split(".mp4")[0]
|
34 |
+
video = VideoFileClip(input_video)
|
35 |
+
audio = AudioFileClip(input_audio)
|
36 |
+
video = video.with_audio(audio)
|
37 |
+
print(video)
|
38 |
+
output_video_file = input_video_name + '_subtitled' + ".mp4"
|
39 |
+
subtitles = pysrt.open(srt, encoding="utf-8")
|
40 |
+
subtitle_clips = create_subtitle_clips(subtitles, video.size, int(font_size), f'{font}.ttf', color, False)
|
41 |
+
final_video = CompositeVideoClip([video]+ subtitle_clips)
|
42 |
+
final_video.write_videofile(output_video_file, codec="libx264", audio_codec="aac", logger=None, preset = "faster", fps=24)
|
43 |
+
print('final')
|
44 |
+
video.close()
|
45 |
+
audio.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
return output_video_file
|
47 |
|
48 |
with gr.Blocks() as demo:
|