File size: 2,905 Bytes
183b9ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68647bf
 
183b9ba
68647bf
183b9ba
68647bf
 
 
 
183b9ba
 
 
 
 
 
 
 
 
 
afe296a
 
183b9ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68647bf
 
 
 
 
 
 
 
 
 
183b9ba
 
 
d2d6202
 
 
 
 
68647bf
d2d6202
183b9ba
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
import gradio as gr

import torch
from torchaudio.backend.common import AudioMetaData
from df.enhance import enhance, load_audio, save_audio
from df.io import resample
from libdf import DF
from df.model import ModelParams
from df import config
import moviepy.editor as mp
import numpy as np

try:
    config.load('config.ini')
except Exception as e:
    print(e)
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

p = ModelParams()
df = DF(
    sr=p.sr,
    fft_size=p.fft_size,
    hop_size=p.hop_size,
    nb_bands=p.nb_erb,
    min_nb_erb_freqs=p.min_nb_freqs,
)


print("Device - ", DEVICE)
model = torch.load(("model.pth"), map_location=torch.device('cpu'))
model.to(DEVICE)
model.eval()

def identity(video_path):
    print(video_path)
    # audio = mp.AudioFileClip(x)
    # wav_file = x
    # audio.write_audiofile(wav_file)
    video = mp.VideoFileClip(video_path)
    audio = video.audio
    wav_file = "tmp.wav"
    audio.write_audiofile(wav_file)
    print("Wav stored.")
    meta = AudioMetaData(-1, -1, -1, -1, "")
    sr = config("sr", 48000, int, section="df")
    sample, meta = load_audio(wav_file, sr)
    len_audio = (meta.num_frames/meta.sample_rate)/60
    max_min = 1
    if len_audio  % max_min < 0.1:
        num_chunks = len_audio // max_min
    else:
        num_chunks = len_audio // max_min + 1
    if len_audio and not num_chunks:
        num_chunks += 1
    print(f"Total length of audio = {len_audio} chunks = {num_chunks}")
    estimate = []
    split_tensors = torch.tensor_split(sample, int(num_chunks), dim = 1)
    for i in range(len(split_tensors)):
        enhanced = enhance(model, df, split_tensors[i])
        enhanced = enhance(model, df, enhanced)
        lim = torch.linspace(0.0, 1.0, int(sr * 0.15)).unsqueeze(0)
        lim = torch.cat((lim, torch.ones(1, enhanced.shape[1] - lim.shape[1])), dim=1)
        enhanced = enhanced * lim
        enhanced = resample(enhanced, sr, meta.sample_rate)
        estimate.append(enhanced)
    estimate = tuple(estimate)
    enhanced = torch.cat(estimate, dim = -1)
    sr = meta.sample_rate
    save_audio("enhanced_aud.wav", enhanced, sr)
    audio = mp.AudioFileClip('enhanced_aud.wav')
    video = mp.VideoFileClip(video_path)
    final_video = video.set_audio(audio)
    final_video.write_videofile("output_video.mp4", 
            codec='libx264', 
            audio_codec='aac', 
            temp_audiofile='temp-audio.m4a', 
            remove_temp=True
            )
    return "output_video.mp4"

demo = gr.Interface(
    fn=identity,
    title="AudioPure - THE BEST AUDIO DENOISER",
    description="AudioPure is the only platform you need for removing all kinds of background noise from your videos!!",
    examples=[['audiopure_og.mov']],
    cache_examples=True,
    inputs=gr.Video(label="Input Video", sources="upload"),
    outputs=gr.Video(label="Output Video"),
    )
demo.launch()