File size: 4,334 Bytes
4498df6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
663d576
 
5a1a456
ee6840f
 
 
 
4498df6
ee6840f
 
 
 
 
5c51163
33a74c4
5c51163
33a74c4
ee6840f
 
 
 
4498df6
 
663d576
4498df6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import subprocess


def get_video_duration(video_path):
    """Return video duration as (hours, minutes, seconds)"""
    try:
        result = subprocess.run(
            ["ffprobe", "-v", "error", "-show_entries",
             "format=duration", "-of",
             "default=noprint_wrappers=1:nokey=1", video_path],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )
        duration = float(result.stdout.strip())
        
        hours = int(duration // 3600)
        minutes = int((duration % 3600) // 60)
        seconds = int(duration % 60)
        
        return hours, minutes, seconds
    except Exception as e:
        print("Error:", e)
        return None


def edit(video_path, keep_silence_up_to=0.2,high_quality=False,save_folder="./Remove-Silence/"):
    os.makedirs(save_folder, exist_ok=True)

    base_name = os.path.splitext(os.path.basename(video_path))[0]
    save_path = os.path.join(save_folder, f"{base_name}_ALTERED.mp4")

    cmd = [
        "auto-editor",
        os.path.abspath(video_path),
        "--margin", f"{keep_silence_up_to}sec",
        "--no-open",
        "--temp-dir", "./temp/",
        "-o", save_path
    ]

    if high_quality:
        cmd.extend([
            "-c:v", "libx264",
            "-b:v", "12M",
            "-profile:v", "high",
            "-c:a", "aac",
            "-b:a", "192k"
        ])

    try:
        print("Running command:", " ".join(cmd))
        subprocess.run(cmd, check=True)

        save_path = os.path.abspath(save_path)

        # get durations
        input_duration = get_video_duration(os.path.abspath(video_path))
        output_duration = get_video_duration(save_path)

        # prepare logs
        logs = f"""original Video Duration:  {input_duration[0]} Hour {input_duration[1]} Minute {input_duration[2]} Second
After Removing Silences:  {output_duration[0]} Hour {output_duration[1]} Minute {output_duration[2]} Second"""

        return save_path, logs
    except Exception as e:
        print("Error:", e)
        return None, None

import gradio as gr

def ui():
    custom_css = """.gradio-container { font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, sans-serif; }"""
    with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
        gr.HTML("""
        <div style="text-align:center; margin:20px auto; max-width:800px;">
            <h1 style="font-size:2.4em; margin-bottom:6px;">
                ✂️ Remove Silence From Video 
            </h1>

            <p style="font-size:1.05em; color:#555; margin:0 0 10px;">
                Upload an .mp4 video, and silent parts will be removed automatically.
            </p>

            <p style="font-size:0.9em; color:#777;">
                Install locally on your computer, enjoy unlimited runs with no waiting queue 
                <a href="https://github.com/NeuralFalconYT/Automatically-Remove-Silence-From-Video" target="_blank" style="text-decoration:none;">
                    Download Link

                </a>
            </p>
        </div>
        """)
        with gr.Row():
            with gr.Column():
                video_input = gr.File(label="Upload a Video File")
                run_btn = gr.Button("Process Video")
                with gr.Accordion('🎥 Video Settings', open=False):
                    silence_value = gr.Number(label="Keep Silence Upto (In seconds)", value=0.2)
                    high_quality = gr.Checkbox(value=False, label='Export in High Quality')
            with gr.Column():
                output_file = gr.File(label="Download Video File")
                duration_info = gr.Textbox(label="Duration", interactive=False)

        # hook function to button
        run_btn.click(
            fn=edit,
            inputs=[video_input, silence_value,high_quality],
            outputs=[output_file, duration_info]
        )

    return demo


# demo = ui()
# demo.queue().launch(debug=True, share=True)


import click
@click.command()
@click.option("--debug", is_flag=True, default=False, help="Enable debug mode.")
@click.option("--share", is_flag=True, default=False, help="Enable sharing of the interface.")
def main(debug, share):
    demo=ui()
    demo.queue().launch(debug=debug, share=share)
if __name__ == "__main__":
    main()