Lenylvt commited on
Commit
23a7dc1
1 Parent(s): 46df01e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()