49 lines
1.1 KiB
Python
49 lines
1.1 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)
|
||
|
||
track = Track(
|
||
artist=info["artists"][0], # type: ignore
|
||
title=info["title"], # type: ignore
|
||
duration=info["duration"], # type: ignore
|
||
filepath=info["requested_downloads"][-1]["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()
|