File size: 1,875 Bytes
21e89c2
 
 
 
6f97c27
 
 
21e89c2
6c59c25
 
0eaefdf
 
 
6c59c25
6f97c27
 
 
 
 
21e89c2
 
63b2acf
21e89c2
63b2acf
 
 
 
21e89c2
63b2acf
21e89c2
63b2acf
 
 
 
 
21e89c2
 
6c59c25
21e89c2
6c59c25
 
 
ab9aa05
6c59c25
4eccfa8
 
6c59c25
 
c233472
6c59c25
63b2acf
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
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()