Download YouTube video using Python to a certain directory

前端 未结 10 2166
醉话见心
醉话见心 2020-12-07 17:08

I have tried the following code to download a video in YouTube and it is working, but I want to save the video at a particular location. Now it is saving the video in

10条回答
  •  囚心锁ツ
    2020-12-07 17:41

    You should put it inside ydl_opts:

    ydl_opts = {
        'outtmpl': os.path.join(download_path, '%(title)s-%(id)s.%(ext)s'),
    }
    

    In your case, download_path should be 'C:/Users/Desktop'. Use %(title)s.%(ext)s instead of %(title)s-%(id)s.%(ext)s if you prefer a file name without video ID.

    Or you can just os.chdir(path) to change the directory to where you want the download to be before you start your download.

    from __future__ import unicode_literals
    import youtube_dl
    import os
    
    ydl_opts = {}
    os.chdir('C:/Users/Desktop')
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(['https://www.youtube.com/watch?v=n06H7OcPd-g'])
    

提交回复
热议问题