Spaces:
Sleeping
Sleeping
| from moviepy import * | |
| import pysrt | |
| import gradio as gr | |
| def time_to_seconds(time_obj): | |
| return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000 | |
| def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug): | |
| subtitle_clips = [] | |
| color_clips=[] | |
| for subtitle in subtitles: | |
| start_time = time_to_seconds(subtitle.start) # Add 2 seconds offset | |
| end_time = time_to_seconds(subtitle.end) | |
| duration = end_time - start_time | |
| video_width, video_height = videosize | |
| max_width = video_width * 0.8 | |
| max_height = video_height * 0.2 | |
| #reshaped_text = arabic_reshaper.reshape(subtitle.text) | |
| #bidi_text = get_display(reshaped_text) | |
| text_clip = TextClip(font, subtitle.text, font_size=fontsize, size=(int(video_width * 0.8), int(video_height * 0.2)) ,text_align="center" ,color=color, method='caption').with_start(start_time).with_duration(duration) | |
| #myclip = ColorClip(size=(int(video_width * 0.8), int(video_height * 0.2)) , color=(225, 0, 0)).with_opacity(0.2).with_start(start_time).with_duration(duration) | |
| subtitle_x_position = 'center' | |
| subtitle_y_position = video_height * 0.68 | |
| text_position = (subtitle_x_position, subtitle_y_position) | |
| subtitle_clips.append(text_clip.with_position(text_position)) | |
| #color_clips.append(myclip.with_position(text_position)) | |
| return subtitle_clips | |
| import subprocess | |
| import os | |
| def video_edit(srt, input_video, color, font, font_size, input_audio): | |
| """ | |
| Burns subtitles into a video using FFmpeg and replaces the audio. | |
| Args: | |
| srt (str): Path to the SRT subtitle file. | |
| input_video (str): Path to the input video file. | |
| color (str): Subtitle color in hex format (e.g., '#FFFF00' for yellow). | |
| font (str): Font name for subtitles. | |
| font_size (int): Font size for subtitles. | |
| input_audio (str): Path to the audio file to replace the video's audio. | |
| Returns: | |
| str: Path to the output video file with burned subtitles and replaced audio. | |
| """ | |
| # Derive output file name from input video | |
| #input_video_base = os.path.splitext(input_video)[0] | |
| output_video_file = f"{input_video_base}_subtitled.mp4" | |
| # Get font directory (assume fonts are in the same directory as the SRT file) | |
| fonts_dir = os.path.dirname(srt) | |
| # Convert color to FFmpeg’s PrimaryColour format (e.g., '#FFFF00' -> '&HFFFF00&') | |
| """"" if not color.startswith('#') or len(color) != 7: | |
| raise ValueError("Color must be a hex code like '#RRGGBB'") | |
| ffmpeg_color = '&H' + color[1:] + '& """" | |
| ffmpeg_color='&HFFFF00&' | |
| # Build subtitle style string for FFmpeg | |
| subtitle_style = f"FontName={font},FontSize={font_size},PrimaryColour={ffmpeg_color}" | |
| # Construct FFmpeg command | |
| cmd = [ | |
| 'ffmpeg', | |
| '-i', input_video, # Input video file | |
| # '-i', input_audio, # Input audio file | |
| '-vf', f"subtitles={srt}:fontsdir={fonts_dir}:force_style='{subtitle_style}'", # Burn subtitles | |
| '-c:v', 'libx264', # Video codec (H.264) | |
| '-c:a', 'aac', # Audio codec (AAC, matching MoviePy’s default) | |
| '-r', '24', # Frame rate (adjust as needed) | |
| '-preset', 'faster', # Encoding speed vs. compression trade-off | |
| '-map', '0:v:0', # Use video from first input (after subtitle filter) | |
| '-map', '1:a:0', # Use audio from second input | |
| '-y', # Overwrite output file if it exists | |
| output_video_file # Output file | |
| ] | |
| # Execute FFmpeg command | |
| try: | |
| subprocess.run(cmd, check=True, stderr=subprocess.PIPE, universal_newlines=True) | |
| except subprocess.CalledProcessError as e: | |
| raise RuntimeError(f"FFmpeg failed: {e.stderr}") | |
| print(f"Video processed successfully: {output_video_file}") | |
| return output_video_file | |
| with gr.Blocks() as demo: | |
| gr.Markdown("Start typing below and then click **Run** to see the progress and final output.") | |
| with gr.Column(): | |
| srt_file = gr.File() | |
| video_in = gr.Video() | |
| color = gr.Text() | |
| font = gr.Text() | |
| font_size = gr.Number() | |
| audio_in = gr.Audio(type = "filepath") | |
| btn = gr.Button("Create") | |
| output_video = gr.Video() | |
| btn.click( | |
| fn=video_edit, | |
| inputs=[srt_file, video_in, color, font, font_size, audio_in], | |
| outputs=output_video | |
| ) | |
| demo.launch(debug=True) |