Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,25 @@
|
|
| 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 |
-
subtitle_file_path = subtitle_file.replace(" ", "\\ ")
|
| 13 |
video_input_stream = ffmpeg.input(input_video)
|
| 14 |
-
|
| 15 |
-
input_video_name = os.path.splitext(os.path.basename(input_video))[0]
|
| 16 |
-
output_video = f"/tmp/output-{input_video_name}.mp4"
|
| 17 |
-
subtitle_track_title = os.path.splitext(os.path.basename(subtitle_file))[0]
|
| 18 |
-
|
| 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 |
|
|
|
|
|
|
|
| 26 |
ffmpeg.run(stream, overwrite_output=True)
|
| 27 |
return output_video
|
| 28 |
|
| 29 |
-
def video_demo(video, subtitle,
|
| 30 |
if subtitle is not None:
|
| 31 |
-
|
| 32 |
-
processed_video_path = add_subtitle_to_video(video, subtitle, subtitle_language, soft_subtitle)
|
| 33 |
return processed_video_path
|
| 34 |
else:
|
| 35 |
return video
|
|
@@ -71,6 +61,5 @@ with gr.Blocks() as demo:
|
|
| 71 |
unsafe_allow_html=True
|
| 72 |
)
|
| 73 |
|
| 74 |
-
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
| 4 |
+
import ffmpeg # Ensure ffmpeg-python is installed
|
| 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_hard_subtitle_to_video(input_video, subtitle_file, subtitle_language):
|
|
|
|
| 12 |
video_input_stream = ffmpeg.input(input_video)
|
| 13 |
+
output_video = f"/tmp/output-{os.path.splitext(os.path.basename(input_video))[0]}.mp4"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
processed_video_path = add_hard_subtitle_to_video(video, subtitle, subtitle_language)
|
|
|
|
| 23 |
return processed_video_path
|
| 24 |
else:
|
| 25 |
return video
|
|
|
|
| 61 |
unsafe_allow_html=True
|
| 62 |
)
|
| 63 |
|
|
|
|
| 64 |
if __name__ == "__main__":
|
| 65 |
+
app.launch()
|