52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
import yt_dlp
|
|
from pathlib import Path
|
|
from config import settings
|
|
|
|
def is_playlist(query: str) -> bool:
|
|
return "playlist" in query or "list=" in query
|
|
|
|
def is_youtube_url(query: str) -> bool:
|
|
return query.startswith("http")
|
|
|
|
def max_duration_filter(max_seconds):
|
|
def _filter(info, *, incomplete):
|
|
duration = info.get('duration')
|
|
if duration and duration > max_seconds:
|
|
return f"Skipping: {info.get('title')} is longer than {max_seconds//60} minutes"
|
|
return _filter
|
|
|
|
def download_song(query: str):
|
|
Path(settings.MUSIC_DIR).mkdir(parents=True, exist_ok=True)
|
|
|
|
if is_youtube_url(query):
|
|
yt_query = query
|
|
noplaylist = False
|
|
elif is_playlist(query):
|
|
yt_query = query
|
|
noplaylist = False
|
|
elif len(query.split()) == 1:
|
|
yt_query = f"ytsearch50:{query}"
|
|
noplaylist = True
|
|
else:
|
|
yt_query = f"ytsearch1:{query}"
|
|
noplaylist = True
|
|
|
|
ydl_opts = {
|
|
'format': 'bestaudio/best',
|
|
'outtmpl': f"{settings.MUSIC_DIR}/%(title)s.%(ext)s",
|
|
'postprocessors': [{
|
|
'key': 'FFmpegExtractAudio',
|
|
'preferredcodec': 'mp3',
|
|
}, {
|
|
'key': 'FFmpegMetadata',
|
|
}],
|
|
'match_filter': max_duration_filter(10 * 60), # 5 minutes in seconds
|
|
'noplaylist': noplaylist,
|
|
'quiet': False,
|
|
'verbose': True,
|
|
}
|
|
|
|
print("yt-dlp options:", ydl_opts)
|
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
result = ydl.download([yt_query])
|
|
return {"downloaded": "Check music folder for downloaded files"} |