-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (49 loc) · 1.91 KB
/
app.py
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
import subprocess
from pathlib import Path
from typing import Optional
from yt_dlp import YoutubeDL
DOWNLOAD_DIR = Path.home() / "Downloads"
DOWNLOAD_DIR.mkdir(exist_ok=True)
AUDIO_FORMATS = [".wav", ".mp3"]
FFMPEG_CODEC = "wav"
FFMPEG_QUALITY = "320"
DEMUCS_MODEL = "htdemucs"
DEMUCS_DEVICE = "cpu"
# Función para descargar archivo wave de YouTube
def download_audio_from_youtube(url: str, output_dir: Path = DOWNLOAD_DIR) -> Optional[Path]:
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': FFMPEG_CODEC,
'preferredquality': FFMPEG_QUALITY,
}],
}
with YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
audio_file = Path(ydl.prepare_filename(info_dict)).with_suffix(f".{FFMPEG_CODEC}")
if audio_file.exists():
return audio_file
else:
print(f"File {audio_file} does not exist. If the path contains spaces, please try again after surrounding the entire path with quotes \"\".")
return None
# Función para separar el audio en componentes usando Demucs
def separate_audio_with_demucs(audio_file: Path):
if audio_file.exists():
command = [
"demucs",
"-n", DEMUCS_MODEL,
"-d", DEMUCS_DEVICE,
str(audio_file)
]
subprocess.run(command, check=True)
print("Separación completa. Archivos guardados en el directorio de descargas.")
else:
print(f"File {audio_file} does not exist. If the path contains spaces, please try again after surrounding the entire path with quotes \"\".")
# Ejemplo de uso
if __name__ == "__main__":
url = input("Ingrese la URL del video de YouTube: ")
audio_file = download_audio_from_youtube(url)
if (audio_file):
separate_audio_with_demucs(audio_file)