Spaces:
Running
on
Zero
Running
on
Zero
Funcional youtube fuction
Browse files- src/main.py +41 -33
src/main.py
CHANGED
@@ -7,6 +7,7 @@ import json
|
|
7 |
import os
|
8 |
import shlex
|
9 |
import subprocess
|
|
|
10 |
from contextlib import suppress
|
11 |
from urllib.parse import urlparse, parse_qs
|
12 |
|
@@ -15,10 +16,10 @@ import librosa
|
|
15 |
import numpy as np
|
16 |
import soundfile as sf
|
17 |
import sox
|
18 |
-
import
|
|
|
19 |
from pedalboard import Pedalboard, Reverb, Compressor, HighpassFilter
|
20 |
from pedalboard.io import AudioFile
|
21 |
-
from pydub import AudioSegment
|
22 |
|
23 |
from mdx import run_mdx
|
24 |
from rvc import Config, load_hubert, get_vc, rvc_infer
|
@@ -29,39 +30,46 @@ mdxnet_models_dir = os.path.join(BASE_DIR, 'mdxnet_models')
|
|
29 |
rvc_models_dir = os.path.join(BASE_DIR, 'rvc_models')
|
30 |
output_dir = os.path.join(BASE_DIR, 'song_output')
|
31 |
|
|
|
|
|
|
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def get_youtube_video_id(url, ignore_playlist=True):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
if query.hostname == 'youtu.be':
|
45 |
-
if query.path[1:] == 'watch':
|
46 |
-
return query.query[2:]
|
47 |
-
return query.path[1:]
|
48 |
-
|
49 |
-
if query.hostname in {'www.youtube.com', 'youtube.com', 'music.youtube.com'}:
|
50 |
-
if not ignore_playlist:
|
51 |
-
# use case: get playlist id not current video in playlist
|
52 |
-
with suppress(KeyError):
|
53 |
-
return parse_qs(query.query)['list'][0]
|
54 |
-
if query.path == '/watch':
|
55 |
-
return parse_qs(query.query)['v'][0]
|
56 |
-
if query.path[:7] == '/watch/':
|
57 |
-
return query.path.split('/')[1]
|
58 |
-
if query.path[:7] == '/embed/':
|
59 |
-
return query.path.split('/')[2]
|
60 |
-
if query.path[:3] == '/v/':
|
61 |
-
return query.path.split('/')[2]
|
62 |
-
|
63 |
-
# returns None for invalid YouTube url
|
64 |
-
return None
|
65 |
|
66 |
|
67 |
def yt_download(link):
|
|
|
7 |
import os
|
8 |
import shlex
|
9 |
import subprocess
|
10 |
+
import re
|
11 |
from contextlib import suppress
|
12 |
from urllib.parse import urlparse, parse_qs
|
13 |
|
|
|
16 |
import numpy as np
|
17 |
import soundfile as sf
|
18 |
import sox
|
19 |
+
from pytube import YouTube
|
20 |
+
from pydub import AudioSegment
|
21 |
from pedalboard import Pedalboard, Reverb, Compressor, HighpassFilter
|
22 |
from pedalboard.io import AudioFile
|
|
|
23 |
|
24 |
from mdx import run_mdx
|
25 |
from rvc import Config, load_hubert, get_vc, rvc_infer
|
|
|
30 |
rvc_models_dir = os.path.join(BASE_DIR, 'rvc_models')
|
31 |
output_dir = os.path.join(BASE_DIR, 'song_output')
|
32 |
|
33 |
+
def sanitize_filename(filename):
|
34 |
+
# Remove caracteres inválidos para nomes de arquivo
|
35 |
+
return re.sub(r'[\\/*?:"<>|]', "", filename)
|
36 |
|
37 |
+
def yt_download(link):
|
38 |
+
try:
|
39 |
+
yt = YouTube(link)
|
40 |
+
video = yt.streams.filter(only_audio=True).order_by('abr').desc().first()
|
41 |
+
|
42 |
+
# Sanitiza o título do vídeo para nome de arquivo
|
43 |
+
safe_title = sanitize_filename(yt.title)
|
44 |
+
output_path = f"{safe_title}.mp3"
|
45 |
+
|
46 |
+
# Baixa o áudio
|
47 |
+
temp_file = video.download(filename_prefix="temp_")
|
48 |
+
|
49 |
+
# Converte para MP3 usando pydub
|
50 |
+
audio = AudioSegment.from_file(temp_file)
|
51 |
+
audio.export(output_path, format="mp3")
|
52 |
+
|
53 |
+
# Remove arquivo temporário
|
54 |
+
os.remove(temp_file)
|
55 |
+
|
56 |
+
return output_path
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
raise Exception(f"Erro ao baixar o vídeo: {str(e)}")
|
60 |
+
|
61 |
+
# ATUALIZE a função get_youtube_video_id para usar pytube
|
62 |
def get_youtube_video_id(url, ignore_playlist=True):
|
63 |
+
try:
|
64 |
+
yt = YouTube(url)
|
65 |
+
return yt.video_id
|
66 |
+
except:
|
67 |
+
return None
|
68 |
+
|
69 |
+
# ... (O RESTANTE DO CÓDIGO ORIGINAL PERMANECE IGUAL)
|
70 |
+
# ... (MANTENHA TODAS AS OUTRAS FUNÇÕES COMO preprocess_song, voice_change, etc.)
|
71 |
+
|
72 |
+
# Modifique APENAS as partes de download do YouTube mostradas acima
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
|
75 |
def yt_download(link):
|