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

Question:

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?

Asked By: U. Watt

||

Answers:

Yes, it is possible to give custom names to the output files and also to put them in a specific folder. You can modify the ydl_opts dictionary to achieve this.

Here’s an example code snippet that shows how to set a custom output filename and download the files in a specific folder:

import youtube_dl

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

In the above code, the outtmpl option is used to set the output filename format. In this example, it is set to ‘/path/to/folder/%(title)s.%(ext)s’, which will save the downloaded files in the folder specified in the outtmpl option with the name of the video and its extension.

You can modify the folder path and the filename format according to your needs.

Answered By: Indra Dwi Aryadi
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])
Answered By: romancedawn

You can use the following code to set custom names to files you download:

#Import the module to download videos
import youtube_dl

#Create a function to save downloaded videos
def downloadVideos(videoURL):
    #Video file location and name
    ydl_opts = {
        'format': 'bestvideo,bestaudio',
        'outtmpl': '/your_path/to/your_folder/%(title)s.%(ext)s',
    }
   
    #Download the video
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([videoURL])

The outtmpl parameter is used to save the file in a specific folder with custom name and extension.

Answered By: Dinux
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.