53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
import sys
|
||
|
||
import yt_dlp
|
||
|
||
from music_player import Track
|
||
|
||
ydl_opts = {
|
||
"format": "mp3/bestaudio/best",
|
||
# ℹ️ See help(yt_dlp.postprocessor) for a list of available Postprocessors and their arguments
|
||
"postprocessors": [
|
||
{
|
||
"key": "FFmpegExtractAudio",
|
||
"preferredcodec": "mp3",
|
||
}
|
||
],
|
||
"paths": {"home": "queue"},
|
||
"outtmpl": {"default": "%(artist)s - %(track)s [%(id)s].%(ext)s"},
|
||
}
|
||
|
||
|
||
class DownloadService:
|
||
def __init__(self) -> None:
|
||
self.ydl = yt_dlp.YoutubeDL(ydl_opts)
|
||
|
||
def download(self, url: str) -> Track:
|
||
info = self.ydl.extract_info(url, download=True)
|
||
|
||
try:
|
||
filepath = info["requested_downloads"][-1]["filepath"] # type: ignore
|
||
except KeyError:
|
||
raise ValueError("Could not ")
|
||
track = Track(
|
||
artist=info.get("artist", None), # type: ignore
|
||
title=info.get("title", None), # type: ignore
|
||
duration=info.get("duration", None), # type: ignore
|
||
filepath=filepath, # type: ignore
|
||
)
|
||
|
||
print(f"Finished processing {track}")
|
||
|
||
return track
|
||
|
||
|
||
def main():
|
||
if len(sys.argv) < 2:
|
||
sys.exit(1)
|
||
|
||
qser = DownloadService()
|
||
qser.download(sys.argv[1])
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|