py-dj/download_service.py
2025-04-11 08:57:10 +02:00

49 lines
1.1 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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()