Spaces:
Running
Running
File size: 2,751 Bytes
23a7dc1 dab027f 23a7dc1 dab027f 23a7dc1 dab027f 23a7dc1 dab027f 23a7dc1 dab027f 23a7dc1 dab027f 23a7dc1 fc9312a 7f1b106 fc9312a 7f1b106 fc9312a 7f1b106 fc9312a 7f1b106 fc9312a 23a7dc1 7f1b106 23a7dc1 dab027f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import gradio as gr
import os
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
import ffmpeg # Ensure ffmpeg-python is installed
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_hard_subtitle_to_video(input_video, subtitle_file, subtitle_language):
video_input_stream = ffmpeg.input(input_video)
output_video = f"/tmp/output-{os.path.splitext(os.path.basename(input_video))[0]}.mp4"
# Hard subtitle process
stream = ffmpeg.output(video_input_stream, output_video, vf=f"subtitles={subtitle_file}")
ffmpeg.run(stream, overwrite_output=True)
return output_video
def video_demo(video, subtitle, subtitle_language):
if subtitle is not None:
processed_video_path = add_hard_subtitle_to_video(video, subtitle, subtitle_language)
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__":
app.launch() |