Spaces:
Running
Running
import gradio as gr | |
import os | |
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip | |
import ffmpeg # Make sure to install ffmpeg-python | |
def read_subtitle_file(subtitle_path): | |
with open(subtitle_path, 'r', encoding='utf-8') as file: | |
subtitle_content = file.read() | |
return os.path.basename(subtitle_path), subtitle_content | |
def add_subtitle_to_video(input_video, subtitle_file, subtitle_language, soft_subtitle): | |
subtitle_file_path = subtitle_file.replace(" ", "\\ ") | |
video_input_stream = ffmpeg.input(input_video) | |
subtitle_input_stream = ffmpeg.input(subtitle_file) | |
input_video_name = os.path.splitext(os.path.basename(input_video))[0] | |
output_video = f"/tmp/output-{input_video_name}.mp4" | |
subtitle_track_title = os.path.splitext(os.path.basename(subtitle_file))[0] | |
stream = ffmpeg.output( | |
video_input_stream, subtitle_input_stream, output_video, | |
**{"c": "copy", "c:s": "mov_text"}, | |
**{"metadata:s:s:0": f"language={subtitle_language}", | |
"metadata:s:s:0": f"title={subtitle_track_title}"} | |
) | |
ffmpeg.run(stream, overwrite_output=True) | |
return output_video | |
def video_demo(video, subtitle, subtitle_type, subtitle_language): | |
if subtitle is not None: | |
soft_subtitle = "Hard" | |
processed_video_path = add_subtitle_to_video(video, subtitle, subtitle_language, soft_subtitle) | |
return processed_video_path | |
else: | |
return video | |
with gr.Blocks() as demo: | |
# Header and information | |
gr.Markdown("<h1 style='text-align: center;'>Text to SRT Converter</h1>", unsafe_allow_html=True) | |
gr.Markdown("<h3 style='text-align: center; color: #FF5733;'>⚠️ Note: The processing can take some time depending on the video length and size.</h3>", unsafe_allow_html=True) | |
# Inputs section | |
with gr.Row(): | |
with gr.Column(scale=1): | |
video_input = gr.Video(label="Upload Video") | |
with gr.Column(scale=1): | |
subtitle_input = gr.File(label="Upload Subtitle File", file_types=[".srt", ".vtt"]) | |
with gr.Column(scale=1): | |
subtitle_language_input = gr.Textbox(label="Subtitle Language (ISO 639-1, e.g., 'en')") | |
# Submit button | |
with gr.Row(): | |
submit_button = gr.Button("Process Video", elem_id="process_button") | |
# Output video | |
output_video = gr.Video(label="Processed Video", elem_id="output_video") | |
# Button click action | |
submit_button.click(fn=video_demo, inputs=[video_input, subtitle_input, subtitle_language_input], outputs=output_video) | |
# Custom CSS to enhance visual appearance | |
demo.style( | |
''' | |
<style> | |
#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; } | |
#output_video { border: 2px solid #4CAF50; border-radius: 8px; } | |
.gr-column { padding: 10px; } | |
.gr-row { justify-content: center; margin-top: 20px; } | |
</style> | |
''', | |
unsafe_allow_html=True | |
) | |
if __name__ == "__main__": | |
demo.launch() | |