Download YouTube video using Python to a certain directory

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

    I found out a really cool python module that allows you to download videos from youtube easily. TO install it:

    pip install pytube
    

    Now, You can download your video like this -

    from pytube import YouTube
    yt = YouTube("https://www.youtube.com/watch?v=n06H7OcPd-g")
    yt = yt.get('mp4', '720p')
    yt.download('/path/to/download/directory')
    

    Boom, Now you can easily scrape such videos using Python easily; Now, We Drink!

    Update 1:

    Thanks to @Chiramisu's comment, You can use the following one-liner to download the highest quality video:

    YouTube('video_url').streams.first().download('save_path')
    

    For Windows, please specify path with double backslashes ex:

    YouTube('video_url').streams.first().download('C:\\Users\\username\\save_path')
    

    Update 2:

    If pytube doesn't seem to work for you, try using youtube-dl:

    sudo -H pip install --upgrade youtube-dl
    

    Now Download Videos:

    from __future__ import unicode_literals
    import youtube_dl
    
    ydl_opts = {}
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])
    

    More info on ytdl in python here.

提交回复
热议问题