Omnibus commited on
Commit
d7dae2d
1 Parent(s): 6e55932

Create vc.py

Browse files
Files changed (1) hide show
  1. vc.py +75 -0
vc.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from pathlib import Path
4
+ import scipy
5
+ from pytube import YouTube
6
+ from pydub import AudioSegment
7
+ from TTS.api import TTS
8
+ import uuid
9
+
10
+ uid = uuid.uuid4()
11
+
12
+ device = "cuda" if torch.cuda.is_available() else "cpu"
13
+
14
+ def custom_bark(inp, in_aud, trim_aud=None):
15
+ speaker_wav=Path(f"{uid}-tmp_aud.mp4")
16
+ if trim_aud != None:
17
+ speaker_wav=Path(f"{uid}-trim.wav")
18
+ tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False).to(device)
19
+ tts.tts_to_file(inp, speaker_wav=speaker_wav, language="en", file_path=f"{uid}-output.wav")
20
+ return (f"{uid}-output.wav")
21
+
22
+ def load_video_yt(vid):
23
+ yt = YouTube(vid)
24
+ vid = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename=f"{uid}-tmp.mp4")
25
+ vid_aud = yt.streams.filter(only_audio=True)[0].download(filename=f"{uid}-tmp_aud.mp4")
26
+ print (f'Video Length: {yt.length}')
27
+ return vid, vid_aud, f"{uid}-tmp_aud.mp4"
28
+
29
+ def trim_clip(clip, start_t, end_t):
30
+ clip = Path(f"{uid}-tmp_aud.mp4")
31
+ song = AudioSegment.from_file(f"{uid}-tmp_aud.mp4", format="mp4")
32
+ start_min = int(start_t.split(":",1)[0])
33
+ start_sec = int(start_t.split(":",1)[1])
34
+ end_min = int(end_t.split(":",1)[0])
35
+ end_sec = int(end_t.split(":",1)[1])
36
+ start = ((start_min*60)+start_sec)*1000
37
+ end = ((end_min*60)+end_sec)*1000
38
+ song_clip = song[start: end]
39
+ song_clip.export(f"{uid}-trim.wav", format="wav")
40
+ print("New Audio file is created and saved")
41
+
42
+ return f"{uid}-trim.wav"
43
+
44
+ with gr.Blocks() as app:
45
+ with gr.Box():
46
+ with gr.Row():
47
+ in_text = gr.Textbox(lines = 6, max_lines = 20)
48
+ with gr.Column():
49
+ alt_go_btn = gr.Button()
50
+ out_audio = gr.Audio(interactive=False)
51
+ with gr.Box():
52
+ with gr.Row():
53
+ with gr.Column():
54
+
55
+ in_aud_mic = gr.Audio(source='microphone')
56
+ in_aud_file = gr.Audio(label = 'Audio Source', source='upload', interactive = True)
57
+ aud_file = gr.File(interactive=False,visible=False)
58
+ with gr.Row():
59
+ start_time = gr.Textbox(label = "Start", value = "0:00", placeholder = "0:23")
60
+ end_time = gr.Textbox(label = "End", value = "0:01", placeholder = "1:12")
61
+ trim_clip_btn = gr.Button("Trim Clip")
62
+ trim_aud = gr.Audio(label = 'Trimmed Audio Source', source='upload', interactive = False)
63
+ with gr.Column():
64
+ in_aud_yt = gr.Textbox(label="YouTube URL")
65
+ load_yt_btn = gr.Button("Load URL")
66
+ yt_vid = gr.Video(interactiv=False)
67
+
68
+
69
+
70
+
71
+ load_yt_btn.click(load_video_yt, in_aud_yt, [yt_vid,in_aud_file,aud_file])
72
+ trim_clip_btn.click(trim_clip,[aud_file, start_time, end_time],trim_aud)
73
+ alt_go_btn.click(custom_bark, [in_text,in_aud_file,trim_aud], out_audio)
74
+
75
+ app.launch()