Download YouTube video using Python to a certain directory

前端 未结 10 2168
醉话见心
醉话见心 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:52
    Path = "The Path That You Want"
    Location = '%s \%(extractor)s-%(id)s-%(title)s.%(ext)s'.replace("%s ", Path)
    ytdl_format_options = {
    'outtmpl': Location
    }
    
    with youtube_dl.YoutubeDL(ytdl_format_options) as ydl:
         ydl.download(['https://www.youtube.com/watch?v=n06H7OcPd-g'])
    

    I personally don't know the library very well, but here is my knowledge the youtube_dl have ytdl_format_options it gives you the options to add some I don't know what it called but let say parameters like above outtmp1 give you the option to specify the location, id, title, or quiet to see the log or not and there is so much more. almost everything you can get it from this URL:https://github.com/ytdl-org/youtube-dl/blob/master/README.md#format-selection

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

    -> Uninstall pytube (if is present)

    pip uninstall pytube
    

    -> Install:

    pip install pytube3
    

    -> Modify extract.py in environment:

    lib/pytube/extract.py
    

    -> Find and edit line:

    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
    

    -> change it to:

    parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)
    

    -> Use pytube:

    from pytube import YouTube
    import os
    
    
    def downloadYoutube(vid_url, path):
        yt = YouTube(vid_url)
        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)
    
    url = input('Input url:\n')
    path = input('Path to store file:\n')
    downloadYoutube(url, path)
    
    0 讨论(0)
  • 2020-12-07 17:56

    I guess you are a bit confused, try this code, end to end

        from __future__ import unicode_literals
        import youtube_dl
        import urllib
        import shutil
        ydl_opts = {}
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download(['https://www.youtube.com/watch?v=n06H7OcPd-g'])
    
        #Moving your source file to destination folder
        source_file = 'C:\Users\Sharmili Nag\Aahatein - Agnee (splitsvilla 4 theme song) Best audio quality-n06H7OcPd-g.mp4'
        destination_folder = 'C:\Users\Sharmili Nag\Desktop\Aahatein - Agnee (splitsvilla 4 theme song) Best audio quality-n06H7OcPd-g.mp4'
        shutil.move(source_file, destination_folder)
    

    In case this code worked out for you, kindly mark the answer as correct.

    0 讨论(0)
  • 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.

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