Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
4 |
-
import ffmpeg #
|
5 |
|
6 |
def read_subtitle_file(subtitle_path):
|
7 |
with open(subtitle_path, 'r', encoding='utf-8') as file:
|
8 |
subtitle_content = file.read()
|
9 |
return os.path.basename(subtitle_path), subtitle_content
|
10 |
|
11 |
-
def
|
12 |
video_input_stream = ffmpeg.input(input_video)
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
# Hard subtitle process
|
16 |
-
stream = ffmpeg.output(video_input_stream, output_video, vf=f"subtitles={subtitle_file}")
|
17 |
ffmpeg.run(stream, overwrite_output=True)
|
18 |
return output_video
|
19 |
|
20 |
-
def video_demo(video, subtitle, subtitle_language):
|
21 |
if subtitle is not None:
|
22 |
-
|
|
|
23 |
return processed_video_path
|
24 |
else:
|
25 |
return video
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
subtitle_input = gr.File(label="Upload Subtitle File", file_types=[".srt", ".vtt"])
|
38 |
-
with gr.Column(scale=1):
|
39 |
-
subtitle_language_input = gr.Textbox(label="Subtitle Language (ISO 639-1, e.g., 'en')")
|
40 |
-
|
41 |
-
# Submit button
|
42 |
-
with gr.Row():
|
43 |
-
submit_button = gr.Button("Process Video", elem_id="process_button")
|
44 |
-
|
45 |
-
# Output video
|
46 |
-
output_video = gr.Video(label="Processed Video", elem_id="output_video")
|
47 |
-
|
48 |
-
# Button click action
|
49 |
-
submit_button.click(fn=video_demo, inputs=[video_input, subtitle_input, subtitle_language_input], outputs=output_video)
|
50 |
-
|
51 |
-
# Custom CSS to enhance visual appearance
|
52 |
-
demo.style(
|
53 |
-
'''
|
54 |
-
<style>
|
55 |
-
#process_button { background-color: #4CAF50; color: white; border: none; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 8px; }
|
56 |
-
#output_video { border: 2px solid #4CAF50; border-radius: 8px; }
|
57 |
-
.gr-column { padding: 10px; }
|
58 |
-
.gr-row { justify-content: center; margin-top: 20px; }
|
59 |
-
</style>
|
60 |
-
''',
|
61 |
-
)
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
4 |
+
import ffmpeg # Make sure to install ffmpeg-python
|
5 |
|
6 |
def read_subtitle_file(subtitle_path):
|
7 |
with open(subtitle_path, 'r', encoding='utf-8') as file:
|
8 |
subtitle_content = file.read()
|
9 |
return os.path.basename(subtitle_path), subtitle_content
|
10 |
|
11 |
+
def add_subtitle_to_video(input_video, subtitle_file, subtitle_language, soft_subtitle):
|
12 |
video_input_stream = ffmpeg.input(input_video)
|
13 |
+
subtitle_input_stream = ffmpeg.input(subtitle_file)
|
14 |
+
input_video_name = os.path.splitext(os.path.basename(input_video))[0]
|
15 |
+
output_video = f"/tmp/output-{input_video_name}.mp4"
|
16 |
+
subtitle_track_title = os.path.splitext(os.path.basename(subtitle_file))[0]
|
17 |
+
|
18 |
+
if soft_subtitle:
|
19 |
+
stream = ffmpeg.output(
|
20 |
+
video_input_stream, subtitle_input_stream, output_video,
|
21 |
+
**{"c": "copy", "c:s": "mov_text"},
|
22 |
+
**{"metadata:s:s:0": f"language={subtitle_language}",
|
23 |
+
"metadata:s:s:0": f"title={subtitle_track_title}"}
|
24 |
+
)
|
25 |
+
else:
|
26 |
+
stream = ffmpeg.output(
|
27 |
+
video_input_stream, output_video,
|
28 |
+
vf=f"subtitles={subtitle_file}"
|
29 |
+
)
|
30 |
|
|
|
|
|
31 |
ffmpeg.run(stream, overwrite_output=True)
|
32 |
return output_video
|
33 |
|
34 |
+
def video_demo(video, subtitle, subtitle_type, subtitle_language):
|
35 |
if subtitle is not None:
|
36 |
+
soft_subtitle = subtitle_type == "Soft"
|
37 |
+
processed_video_path = add_subtitle_to_video(video, subtitle, subtitle_language, soft_subtitle)
|
38 |
return processed_video_path
|
39 |
else:
|
40 |
return video
|
41 |
|
42 |
+
demo = gr.Interface(
|
43 |
+
fn=video_demo,
|
44 |
+
inputs=[
|
45 |
+
gr.Video(label="Video", interactive=True),
|
46 |
+
gr.File(label="Subtitle", file_types=[".srt", ".vtt"]),
|
47 |
+
gr.Radio(choices=["Soft", "Hard"], label="Subtitle Type"),
|
48 |
+
gr.Textbox(label="Subtitle Language (ISO 639-1 code, e.g., 'en' for English)", default="en"),
|
49 |
+
],
|
50 |
+
outputs=gr.Video(label="Processed Video"),
|
51 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
if __name__ == "__main__":
|
54 |
+
demo.launch()
|