SaltProphet commited on
Commit
e3e6bb1
·
verified ·
1 Parent(s): 5affdc2

Create downloader.py

Browse files
Files changed (1) hide show
  1. downloader.py +26 -0
downloader.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yt_dlp
2
+ import os
3
+
4
+ def download_audio(url, output_format="mp3"):
5
+ """
6
+ Downloads audio from a given URL and converts it to the selected format.
7
+ """
8
+ os.makedirs("downloads", exist_ok=True)
9
+
10
+ ydl_opts = {
11
+ 'format': 'bestaudio/best',
12
+ 'outtmpl': os.path.join("downloads", '%(title)s.%(ext)s'),
13
+ 'postprocessors': [{
14
+ 'key': 'FFmpegExtractAudio',
15
+ 'preferredcodec': output_format,
16
+ 'preferredquality': '192',
17
+ }],
18
+ 'noplaylist': True,
19
+ 'quiet': True,
20
+ 'no_warnings': True
21
+ }
22
+
23
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
24
+ info = ydl.extract_info(url, download=True)
25
+ filename = os.path.splitext(ydl.prepare_filename(info))[0] + f".{output_format}"
26
+ return os.path.abspath(filename), info.get('title', 'Unknown title')