python youtube_dl custom path 1

python youtube_dl custom path

import os

def download_audio(request):
    SAVE_PATH = '/'.join(os.getcwd().split('/')[:3]) + '/Downloads'

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'outtmpl':SAVE_PATH + '/%(title)s.%(ext)s',
    }

    link = request.GET.get('video_url')

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(["https://www.youtube.com/watch?v="+link])

Here is what the above code is Doing:
1. We are creating a dictionary named ydl_opts.
2. We are setting the format to bestaudio/best.
3. We are setting the postprocessors to extract audio from the video.
4. We are setting the preferred codec to mp3.
5. We are setting the preferred quality to 192.
6. We are setting the output template to the Downloads folder.
7. We are creating a YoutubeDL object with the ydl_opts dictionary.
8. We are downloading the video with the YoutubeDL object.

Similar Posts