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

AlphaWolf picture AlphaWolf · Dec 20, 2016 · Viewed 10.9k times · Source

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?

Answer

Mohammad Yusuf picture Mohammad Yusuf · Dec 20, 2016

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