awerks's picture
ffmpeg
6f97c27
raw
history blame
1.88 kB
import gradio as gr
import subprocess
import os
import tempfile
import os
import stat
def create_video(video_file: gr.inputs.File, subtitles_file: gr.inputs.File, font_name:str = "ProbaPro-Bold", font_size:int = 22, border_style: int = 1):
if border_style == 4:
border_command = f"BorderStyle=4,BackColour=&H30000000,Outline=0.5,Shadow=0"
else:
border_command = f"BorderStyle=1,Outline=1.10,Shadow=0.35"
# Get current permissions
file_stats = os.stat('./ffmpeg')
# Add execute permissions
os.chmod('./ffmpeg', file_stats.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
output_file_handle, out_path = tempfile.mkstemp(suffix=".mp4")
command = [
'./ffmpeg',
'-vsync', '0',
'-init_hw_device', 'cuda=cu:0',
'-filter_hw_device', 'cu',
'-hwaccel', 'cuda',
'-hwaccel_output_format', 'cuda',
'-i', video_file.name,
'-filter_complex', f"alphasrc=s=1080x1080:r=25,format=yuva420p,subtitles=alpha=1:f={subtitles_file.name}:sub2video=1,hwupload,format=cuda[sub];[0:v]hwupload,format=cuda[main];[main][sub]overlay_cuda=shortest=1:repeatlast=0:eof_action=endall,scale_cuda=format=yuv420p",
'-c:a', 'copy',
'-c:v', 'h264_nvenc',
'-preset', 'fast',
'-y',
out_path
]
subprocess.run(command)
os.close(output_file_handle)
return out_path
iface = gr.Interface(
fn=create_video,
inputs=[
gr.inputs.File(label="Video File"),
gr.inputs.File(label="Subtitles File"),
gr.inputs.Textbox(default="ProbaPro-Bold", label="Font Name"),
gr.inputs.Slider(minimum=10, maximum=30, default=22, label="Font Size"),
gr.inputs.Dropdown(choices=[1, 4], default=1, label="Border Style"),
],
outputs=gr.outputs.File() # Replace with appropriate output type
)
iface.launch()