Download video in mp3 format using pytube

前端 未结 5 1242
清歌不尽
清歌不尽 2021-02-08 07:26

I have been using pytube to download youtube videos in python. So far I have been able to download in mp4 format.

yt = pytube.YouTube(\"https://www.youtube.com/         


        
5条回答
  •  梦谈多话
    2021-02-08 08:17

    With this code you will download all the videos from a playlist and saving them with the title from youtube in mp4 and mp4 audio formats.

    i used the code from @scrpy in this question and the hint from @Jean-Pierre Schnyder from this answer

    import os
    import subprocess
    import re
    from pytube import YouTube
    from pytube import Playlist
    
    
    path =r'DESTINATION_FOLER'
    playlist_url = 'PLAYLIST_URL'
    
    play = Playlist(playlist_url)
    
    play._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
    print(len(play.video_urls))
    for url in play.video_urls:
        yt = YouTube(url)
        audio = yt.streams.get_audio_only()
        audio.download(path)
        nome = yt.title
        new_filename=nome+'.mp3'
        default_filename =nome+'.mp4'
        ffmpeg = ('ffmpeg -i ' % path default_filename + new_filename)
        subprocess.run(ffmpeg, shell=True)
             
        
    print('Download Complete')
    

提交回复
热议问题