NeuralFalcon commited on
Commit
4498df6
·
verified ·
1 Parent(s): f4236fd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+
4
+
5
+ def get_video_duration(video_path):
6
+ """Return video duration as (hours, minutes, seconds)"""
7
+ try:
8
+ result = subprocess.run(
9
+ ["ffprobe", "-v", "error", "-show_entries",
10
+ "format=duration", "-of",
11
+ "default=noprint_wrappers=1:nokey=1", video_path],
12
+ stdout=subprocess.PIPE,
13
+ stderr=subprocess.PIPE,
14
+ text=True
15
+ )
16
+ duration = float(result.stdout.strip())
17
+
18
+ hours = int(duration // 3600)
19
+ minutes = int((duration % 3600) // 60)
20
+ seconds = int(duration % 60)
21
+
22
+ return hours, minutes, seconds
23
+ except Exception as e:
24
+ print("Error:", e)
25
+ return None
26
+
27
+
28
+ def edit(video_path, keep_silence_up_to=0.2,high_quality=False,save_folder="./Remove-Silence/"):
29
+ os.makedirs(save_folder, exist_ok=True)
30
+
31
+ base_name = os.path.splitext(os.path.basename(video_path))[0]
32
+ save_path = os.path.join(save_folder, f"{base_name}_ALTERED.mp4")
33
+
34
+ cmd = [
35
+ "auto-editor",
36
+ os.path.abspath(video_path),
37
+ "--margin", f"{keep_silence_up_to}sec",
38
+ "--no-open",
39
+ "--temp-dir", "./temp/",
40
+ "-o", save_path
41
+ ]
42
+
43
+ if high_quality:
44
+ cmd.extend([
45
+ "-c:v", "libx264",
46
+ "-b:v", "12M",
47
+ "-profile:v", "high",
48
+ "-c:a", "aac",
49
+ "-b:a", "192k"
50
+ ])
51
+
52
+ try:
53
+ print("Running command:", " ".join(cmd))
54
+ subprocess.run(cmd, check=True)
55
+
56
+ save_path = os.path.abspath(save_path)
57
+
58
+ # get durations
59
+ input_duration = get_video_duration(os.path.abspath(video_path))
60
+ output_duration = get_video_duration(save_path)
61
+
62
+ # prepare logs
63
+ logs = f"""original Video Duration: {input_duration[0]} Hour {input_duration[1]} Minute {input_duration[2]} Second
64
+ After Removing Silences: {output_duration[0]} Hour {output_duration[1]} Minute {output_duration[2]} Second"""
65
+
66
+ return save_path, logs
67
+ except Exception as e:
68
+ print("Error:", e)
69
+ return None, None
70
+
71
+ import gradio as gr
72
+
73
+ def ui():
74
+ with gr.Blocks(title="Remove Silence From Video") as demo:
75
+ gr.Markdown("## 🎬 Remove Silence From Video")
76
+ gr.Markdown("Upload an .mp4 video, and silent parts will be removed automatically.")
77
+
78
+ with gr.Row():
79
+ with gr.Column():
80
+ video_input = gr.File(label="Upload Video")
81
+ run_btn = gr.Button("Process Video")
82
+ with gr.Accordion('🎥 Video Settings', open=False):
83
+ silence_value = gr.Number(label="Keep Silence Upto (In seconds)", value=0.2)
84
+ high_quality = gr.Checkbox(value=False, label='Export in High Quality')
85
+ with gr.Column():
86
+ output_file = gr.File(label="Download Video File")
87
+ duration_info = gr.Textbox(label="Duration", interactive=False)
88
+
89
+ # hook function to button
90
+ run_btn.click(
91
+ fn=edit,
92
+ inputs=[video_input, silence_value,high_quality],
93
+ outputs=[output_file, duration_info]
94
+ )
95
+
96
+ return demo
97
+
98
+
99
+ # demo = ui()
100
+ # demo.queue().launch(debug=True, share=True)
101
+
102
+
103
+ import click
104
+ @click.command()
105
+ @click.option("--debug", is_flag=True, default=False, help="Enable debug mode.")
106
+ @click.option("--share", is_flag=True, default=False, help="Enable sharing of the interface.")
107
+ def main(debug, share):
108
+ demo=ui()
109
+ demo.queue().launch(debug=debug, share=share)
110
+ if __name__ == "__main__":
111
+ main()