File size: 2,162 Bytes
32ed7fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bf4d73
 
32ed7fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00451f0
32ed7fa
 
 
 
3725ecc
32ed7fa
 
 
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
import moviepy.editor as mp
import librosa
import numpy as np
import gradio as gr
import subprocess


def buffer_n_merge(intervals, buffer=0.1):
    if not intervals: return []

    new_intervals = [intervals[0]]
    new_intervals[0][0] -= buffer
    new_intervals[0][1] += buffer

    for start, end in intervals[1:]:
        start -= buffer
        end += buffer
        if new_intervals[-1][-1] >= start:
            new_intervals[-1][-1] = end
        else:
            new_intervals.append([start, end])
    return new_intervals


def download_and_process_video(in_f, threshold_db, buffer_sec):
    vidpath = in_f.name

    # load the video
    video = mp.VideoFileClip(vidpath)
    # extract audio and convert to mono
    audio = video.audio.to_soundarray(fps=22000)

    # use librosa to get non-silent intervals
    non_silent_intervals = librosa.effects.split(audio[:, 0], top_db=threshold_db) 
    # convert non_silent_intervals from samples to seconds, as librosa works with samples not seconds
    non_silent_intervals_sec = np.array(non_silent_intervals) / 22000

    # Add buffer and merge intervals
    non_silent_intervals_sec = buffer_n_merge(non_silent_intervals_sec.tolist(), buffer=buffer_sec)

    # Process video
    # cut the video using the non-silent intervals and store the clips in a list
    clips = [video.subclip(max(0, start_time), min(end_time, video.duration)) for start_time, end_time in non_silent_intervals_sec]

    output_file = 'my_concatenation.mp4'
    final_clip = mp.concatenate_videoclips(clips)
    final_clip.write_videofile(output_file, codec='libx264', audio_codec='aac', temp_audiofile='temp-audio.m4a', remove_temp=True)
    
    return output_file


iface = gr.Interface(
    fn=download_and_process_video,
    inputs=[
        gr.inputs.File(label="Video File (.mp4 only)", file_count='single', type='file'),
        gr.inputs.Slider(minimum=1, maximum=70, step=1, default=30, label="Threshold (db)"),
        gr.inputs.Slider(minimum=0, maximum=2, step=0.01, default=0.1, label="Buffer (sec)"),
    ],
    outputs=gr.outputs.Video(label="Processed Video"),
    title="Video Silence Remover"
)

iface.launch()