pythonyoutube-dl

How to format output video/audio name with youtube-dl embed?


I'm writing a python script that downloads videos from a URL using youtube-dl. Here's the code:

def downloadVideos(videoURL):
    ydl_opts = {
        'format': 'bestvideo,bestaudio',
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([videoURL])

The above method downloads an mp4 file for the video and a m4a file for the audio. The only thing is, the names for the files are some random strings. How can I give a custom name for the output files? And is it possible to put these in a specific folder?


Solution

  • def downloadVideos(videoURL):
        ydl_opts = {
            'format': 'bestvideo,bestaudio',
            'outtmpl': 'You can enter your text here -%(title)s.%(ext)s'
        }
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([videoURL])
    

    You need to put outtmpl option within ydl_opts. You can type anything you want inside it. (title) will give you title of the video, (ext) is of course the extension of it. You can define output path as well. So final code should look like this :

    def downloadVideos(videoURL):
        ydl_opts = {
            'format': 'bestvideo,bestaudio',
            'outtmpl': 'path/to/folder/Write Something here - %(title)s.%(ext)s'
        }
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([videoURL])