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("

Text to SRT Converter

", unsafe_allow_html=True) gr.Markdown("

⚠️ Note: The processing can take some time depending on the video length and size.

", 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( ''' ''', unsafe_allow_html=True ) if __name__ == "__main__": demo.launch()