Change the output name when download with youtube-dl using python

22k views Asked by At

I tried to follow the tutorial to download a video from youtube:

import youtube_dl
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=Bdf-PSJpccM'])

But i see only when using the command(in command line) with option -o we can change the output video name. So, how to add change output name option embedded in python script? I think it should be add to ydl_opts, but i don't know the syntax, can anybody help?

2

There are 2 answers

2
Mohammad Yusuf On BEST ANSWER

Try like this:

import youtube_dl
ydl_opts = {'outtmpl': 'file_path/file_name'}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=Bdf-PSJpccM'])

Substitute the desired filename and filepath in ydl_opts. file_path/file_name

0
Cleber Alcântara On

Just complementing @MYGz answer, the outtmpl can be formatted according to the video data. You can get more information here: https://github.com/rg3/youtube-dl/issues/5192#issuecomment-78843396.