Hev832 commited on
Commit
20a9565
1 Parent(s): c680ff9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +249 -95
main.py CHANGED
@@ -1,104 +1,258 @@
1
- import argparse
2
- import gc
3
- import hashlib
4
- import json
5
- import os
6
- import shlex
7
- import subprocess
8
- from contextlib import suppress
9
- from urllib.parse import urlparse, parse_qs
10
-
11
  import gradio as gr
12
  import librosa
13
- import numpy as np
14
- import soundfile as sf
15
- import sox
16
  import yt_dlp
17
- from pedalboard import Pedalboard, Reverb, Compressor, HighpassFilter
18
- from pedalboard.io import AudioFile
19
- from pydub import AudioSegment
20
-
21
-
22
-
23
-
24
- def get_youtube_video_id(url, ignore_playlist=True):
25
- """
26
- Examples:
27
- http://youtu.be/SA2iWivDJiE
28
- http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu
29
- http://www.youtube.com/embed/SA2iWivDJiE
30
- http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US
31
- """
32
- query = urlparse(url)
33
- if query.hostname == 'youtu.be':
34
- if query.path[1:] == 'watch':
35
- return query.query[2:]
36
- return query.path[1:]
37
-
38
- if query.hostname in {'www.youtube.com', 'youtube.com', 'music.youtube.com'}:
39
- if not ignore_playlist:
40
- # use case: get playlist id not current video in playlist
41
- with suppress(KeyError):
42
- return parse_qs(query.query)['list'][0]
43
- if query.path == '/watch':
44
- return parse_qs(query.query)['v'][0]
45
- if query.path[:7] == '/watch/':
46
- return query.path.split('/')[1]
47
- if query.path[:7] == '/embed/':
48
- return query.path.split('/')[2]
49
- if query.path[:3] == '/v/':
50
- return query.path.split('/')[2]
51
-
52
- # returns None for invalid YouTube url
53
- return None
54
-
55
-
56
- def yt_download(link):
57
- ydl_opts = {
58
- 'format': 'bestaudio',
59
- 'outtmpl': '%(title)s',
60
- 'nocheckcertificate': True,
61
- 'ignoreerrors': True,
62
- 'no_warnings': True,
63
- 'quiet': True,
64
- 'extractaudio': True,
65
- 'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3'}],
66
- }
67
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
68
- result = ydl.extract_info(link, download=True)
69
- download_path = ydl.prepare_filename(result, outtmpl='%(title)s.mp3')
70
-
71
- return download_path
72
-
73
-
74
- def raise_exception(error_msg, is_webui):
75
- if is_webui:
76
- raise gr.Error(error_msg)
77
- else:
78
- raise Exception(error_msg)
79
-
80
-
81
-
82
- with gr.Blocks(theme=gr.themes.Soft) as app:
83
- gr.HTML("<h1> youtube downloader </h1>")
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
- def download_from_url(url, audio_name):
87
- ydl.download([url])
88
 
89
- def youtube_to_wav(url, audio_name):
90
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
91
- download_from_url(url, audio_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
- # Gradio interface
94
- iface = gr.Interface(
95
- fn=youtube_to_wav,
96
- inputs=["text", "text"],
97
- outputs="text",
98
- live=True,
99
- capture_session=True
100
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- # Launch Gradio interface
103
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch, os, traceback, sys, warnings, shutil, numpy as np
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
  import librosa
4
+ import asyncio
5
+ import rarfile
6
+ import edge_tts
7
  import yt_dlp
8
+ import ffmpeg
9
+ import gdown
10
+ import subprocess
11
+ import wave
12
+ import soundfile as sf
13
+ from scipy.io import wavfile
14
+ from datetime import datetime
15
+ from urllib.parse import urlparse
16
+ from mega import Mega
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ now_dir = os.getcwd()
19
+ tmp = os.path.join(now_dir, "TEMP")
20
+ shutil.rmtree(tmp, ignore_errors=True)
21
+ os.makedirs(tmp, exist_ok=True)
22
+ os.environ["TEMP"] = tmp
23
+ from lib.infer_pack.models import (
24
+ SynthesizerTrnMs256NSFsid,
25
+ SynthesizerTrnMs256NSFsid_nono,
26
+ SynthesizerTrnMs768NSFsid,
27
+ SynthesizerTrnMs768NSFsid_nono,
28
+ )
29
+ from fairseq import checkpoint_utils
30
+ from vc_infer_pipeline import VC
31
+ from config import Config
32
+ config = Config()
33
 
 
 
34
 
35
+ def vc_single(
36
+ sid,
37
+ vc_audio_mode,
38
+ input_audio_path,
39
+ input_upload_audio,
40
+ vocal_audio,
41
+ tts_text,
42
+ tts_voice,
43
+ f0_up_key,
44
+ f0_file,
45
+ f0_method,
46
+ file_index,
47
+ index_rate,
48
+ filter_radius,
49
+ resample_sr,
50
+ rms_mix_rate,
51
+ protect
52
+ ): # spk_item, input_audio0, vc_transform0,f0_file,f0method0
53
+ global tgt_sr, net_g, vc, hubert_model, version, cpt
54
+ try:
55
+ logs = []
56
+ print(f"Converting...")
57
+ logs.append(f"Converting...")
58
+ yield "\n".join(logs), None
59
+ if vc_audio_mode == "Input path" or "Youtube" and input_audio_path != "":
60
+ audio, sr = librosa.load(input_audio_path, sr=16000, mono=True)
61
+ elif vc_audio_mode == "Upload audio":
62
+ selected_audio = input_upload_audio
63
+ if vocal_audio:
64
+ selected_audio = vocal_audio
65
+ elif input_upload_audio:
66
+ selected_audio = input_upload_audio
67
+ sampling_rate, audio = selected_audio
68
+ duration = audio.shape[0] / sampling_rate
69
+ audio = (audio / np.iinfo(audio.dtype).max).astype(np.float32)
70
+ if len(audio.shape) > 1:
71
+ audio = librosa.to_mono(audio.transpose(1, 0))
72
+ if sampling_rate != 16000:
73
+ audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
74
+ elif vc_audio_mode == "TTS Audio":
75
+ if tts_text is None or tts_voice is None:
76
+ return "You need to enter text and select a voice", None
77
+ asyncio.run(edge_tts.Communicate(tts_text, "-".join(tts_voice.split('-')[:-1])).save("tts.mp3"))
78
+ audio, sr = librosa.load("tts.mp3", sr=16000, mono=True)
79
+ input_audio_path = "tts.mp3"
80
+ f0_up_key = int(f0_up_key)
81
+ times = [0, 0, 0]
82
+ if hubert_model == None:
83
+ load_hubert()
84
+ if_f0 = cpt.get("f0", 1)
85
+ audio_opt = vc.pipeline(
86
+ hubert_model,
87
+ net_g,
88
+ sid,
89
+ audio,
90
+ input_audio_path,
91
+ times,
92
+ f0_up_key,
93
+ f0_method,
94
+ file_index,
95
+ # file_big_npy,
96
+ index_rate,
97
+ if_f0,
98
+ filter_radius,
99
+ tgt_sr,
100
+ resample_sr,
101
+ rms_mix_rate,
102
+ version,
103
+ protect,
104
+ f0_file=f0_file
105
+ )
106
 
107
+ def download_audio(url, audio_provider):
108
+ logs = []
109
+ os.makedirs("dl_audio", exist_ok=True)
110
+ if url == "":
111
+ logs.append("URL required!")
112
+ yield None, "\n".join(logs)
113
+ return None, "\n".join(logs)
114
+ if audio_provider == "Youtube":
115
+ logs.append("Downloading the audio...")
116
+ yield None, "\n".join(logs)
117
+ ydl_opts = {
118
+ 'noplaylist': True,
119
+ 'format': 'bestaudio/best',
120
+ 'postprocessors': [{
121
+ 'key': 'FFmpegExtractAudio',
122
+ 'preferredcodec': 'wav',
123
+ }],
124
+ "outtmpl": 'result/dl_audio/audio',
125
+ }
126
+ audio_path = "result/dl_audio/audio.wav"
127
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
128
+ ydl.download([url])
129
+ logs.append("Download Complete.")
130
+ yield audio_path, "\n".join(logs)
131
 
132
+ def change_audio_mode(vc_audio_mode):
133
+ if vc_audio_mode == "Input path":
134
+ return (
135
+ # Input & Upload
136
+ gr.Textbox.update(visible=True),
137
+ gr.Checkbox.update(visible=False),
138
+ gr.Audio.update(visible=False),
139
+ # Youtube
140
+ gr.Dropdown.update(visible=False),
141
+ gr.Textbox.update(visible=False),
142
+ gr.Textbox.update(visible=False),
143
+ gr.Button.update(visible=False),
144
+ # Splitter
145
+ gr.Dropdown.update(visible=True),
146
+ gr.Textbox.update(visible=True),
147
+ gr.Button.update(visible=True),
148
+ gr.Button.update(visible=False),
149
+ gr.Audio.update(visible=False),
150
+ gr.Audio.update(visible=True),
151
+ gr.Audio.update(visible=True),
152
+ gr.Slider.update(visible=True),
153
+ gr.Slider.update(visible=True),
154
+ gr.Audio.update(visible=True),
155
+ gr.Button.update(visible=True),
156
+ # TTS
157
+ gr.Textbox.update(visible=False),
158
+ gr.Dropdown.update(visible=False)
159
+ )
160
+ # elif vc_audio_mode == "Upload audio":
161
+ # return (
162
+ # # Input & Upload
163
+ # gr.Textbox.update(visible=False),
164
+ ## gr.Checkbox.update(visible=True),
165
+ # gr.Audio.update(visible=True),
166
+ # Youtube
167
+ # gr.Dropdown.update(visible=False),
168
+ # gr.Textbox.update(visible=False),
169
+ # gr.Textbox.update(visible=False),
170
+ # gr.Button.update(visible=False),
171
+ # Splitter
172
+ # gr.Dropdown.update(visible=True),
173
+ # gr.Textbox.update(visible=True),
174
+ # gr.Button.update(visible=False),
175
+ # gr.Button.update(visible=True),
176
+ # gr.Audio.update(visible=False),
177
+ # gr.Audio.update(visible=True),
178
+ # gr.Audio.update(visible=True),
179
+ # gr.Slider.update(visible=True),
180
+ # gr.Slider.update(visible=True),
181
+ # gr.Audio.update(visible=True),
182
+ # gr.Button.update(visible=True),
183
+ # TTS
184
+ # gr.Textbox.update(visible=False),
185
+ $ gr.Dropdown.update(visible=False)
186
+ )
187
+ elif vc_audio_mode == "Youtube":
188
+ return (
189
+ # Input & Upload
190
+ # gr.Textbox.update(visible=False),
191
+ # gr.Checkbox.update(visible=False),
192
+ # gr.Audio.update(visible=False),
193
+ # Youtube
194
+ gr.Dropdown.update(visible=True),
195
+ gr.Textbox.update(visible=True),
196
+ gr.Textbox.update(visible=True),
197
+ gr.Button.update(visible=True),
198
+ # Splitter
199
+ # gr.Dropdown.update(visible=True),
200
+ # gr.Textbox.update(visible=True),
201
+ # gr.Button.update(visible=True),
202
+ # gr.Button.update(visible=False),
203
+ # gr.Audio.update(visible=True),
204
+ # gr.Audio.update(visible=True),
205
+ # gr.Audio.update(visible=True),
206
+ # gr.Slider.update(visible=True),
207
+ # gr.Slider.update(visible=True),
208
+ # gr.Audio.update(visible=True),
209
+ # gr.Button.update(visible=True),
210
+ # TTS
211
+ # gr.Textbox.update(visible=False),
212
+ # gr.Dropdown.update(visible=False)
213
+ )
214
+ # elif vc_audio_mode == "TTS Audio":
215
+ # return (
216
+ # # Input & Upload
217
+ # gr.Textbox.update(visible=False),
218
+ # gr.Checkbox.update(visible=False),
219
+ # gr.Audio.update(visible=False),
220
+ # Youtube
221
+ # gr.Dropdown.update(visible=False),
222
+ # gr.Textbox.update(visible=False),
223
+ # gr.Textbox.update(visible=False),
224
+ # gr.Button.update(visible=False),
225
+ # Splitter
226
+ # gr.Dropdown.update(visible=False),
227
+ # gr.Textbox.update(visible=False),
228
+ # gr.Button.update(visible=False),
229
+ # gr.Button.update(visible=False),
230
+ # gr.Audio.update(visible=False),
231
+ # gr.Audio.update(visible=False),
232
+ # gr.Audio.update(visible=False),
233
+ # gr.Slider.update(visible=False),
234
+ # gr.Slider.update(visible=False),
235
+ # gr.Audio.update(visible=False),
236
+ # gr.Button.update(visible=False),
237
+ # TTS
238
+ # gr.Textbox.update(visible=True),
239
+ # gr.Dropdown.update(visible=True)
240
+ )
241
+
242
+ with gr.Blocks() as app:
243
+ gr.Markdown(
244
+ "# <center> YOUTUBE DOWNLOADER\n"
245
+ )
246
+
247
+ with gr.TabItem("Inference"):
248
 
249
+ with gr.Row():
250
+ with gr.Column():
251
+ vc_audio_mode = gr.Dropdown(label="Input voice", choices=["Youtube"], allow_custom_value=False, value="Youtube")
252
+ vc_download_audio = gr.Dropdown(label="Provider", choices=["Youtube"], allow_custom_value=False, visible=False, value="Youtube", info="Select provider (Default: Youtube)")
253
+ vc_link = gr.Textbox(label="Youtube URL", visible=True, info="Example: https://www.youtube.com/watch?v=Nc0sB1Bmf-A", placeholder="https://www.youtube.com/watch?v=...")
254
+ vc_log_yt = gr.Textbox(label="Output Information", visible=False, interactive=True)
255
+ vc_download_button = gr.Button("Download Audio", variant="primary", visible=True)
256
+ vc_audio_preview = gr.Audio(label="Downloaded Audio Preview", visible=True)
257
+ )
258
+ app.queue().launch()