Download YouTube video using Python to a certain directory

前端 未结 10 2167
醉话见心
醉话见心 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:39

    youtube_dl has a giant list of options: https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL.py#L128-L278

    But I don't see any that control the output directory. So you can move the file afterward. For that, see: How to move a file in Python.

    0 讨论(0)
  • 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'])
    
    0 讨论(0)
  • 2020-12-07 17:42

    downloading videos from youtube in python 3.x for the reference you can check https://python-pytube.readthedocs.io/en/latest/user/quickstart.html#downloading-a-video

    from pytube import YouTube
    import os
    
    def downloadYouTube(videourl, path):
    
        yt = YouTube(videourl)
        yt = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
        if not os.path.exists(path):
            os.makedirs(path)
        yt.download(path)
    
    downloadYouTube('https://www.youtube.com/watch?v=zNyYDHCg06c', './videos/FindingNemo1')
    
    0 讨论(0)
  • 2020-12-07 17:48
    from __future__ import unicode_literals
    import youtube_dl
    
    ydl_opts = {
        'outtmpl': 'yourPathToDirectory/%(title)s.%(ext)s',
    
    }
    
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(['https://www.youtube.com/watch?v=XpYGgtrMTYs'])
    

    ydl_opts is the what we need to play with. We can declare multiple attributes/parameters inside this for customisation. For this example, I've added a single attribute outtmpl which is used to define custom directory. We can further customize this to declare filename, etc.

    0 讨论(0)
  • 2020-12-07 17:49

    I have created a library that makes it simpler.

    To install:

    $ pip install mhyt
    # or
    $ sudo pip install mhyt
    

    And use it like:

    from mhmovie.youtubemhyt import yt_download
    
    yt_download("url","file.format","format"ismucic=True)
    
    0 讨论(0)
  • 2020-12-07 17:50

    It will save the file where your .py application is in. for example, if your .py program is in your desktop folder and you run your app from the desktop, the output will save on your desktop. the only thing you need is to save your .py file in Desktop and then open a command line and go in Desktop using cd command after it run your .py file using python YOURAPP.py but if you want to download it and then save it in another place, you need to download it like you do now (in your temporary place) then moves it via file libraries in python.

    0 讨论(0)
提交回复
热议问题