File size: 769 Bytes
3c36fb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from yt_dlp import YoutubeDL
import uuid

def download_youtube(url:str):
    output_audio_folder = f"./cached/audio"
    # file_path = output_audio_folder + '/%(title)s.%(ext)s'
    file_path = output_audio_folder + f'/{str(uuid.uuid4())}'
    # f"{file_path}.%(ext)s"
    # url = "https://www.youtube.com/watch?v=WtpPolBLRN0"


    yt = YoutubeDL(params={'postprocessors': [{  # Post-process to convert to MP3
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',  # Convert to mp3
                'preferredquality': '0',  # '0' means best quality, auto-determined by source
            }],
            'outtmpl': f"{file_path}.%(ext)s",
            })

    with yt as ydl:
        ydl.download(url)

    return f"{file_path}.mp3"