Download video in mp3 format using pytube

前端 未结 5 1243
清歌不尽
清歌不尽 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-08 08:08

    here is a mildly more slim and dense format for downloading an mp4 video and converting from mp4 to mp3:

    Download will download the file to the current directory or location of the program, this will also convert the file to mp3 as a NEW file.

    from pytube import YouTube
    import os
    import subprocess
    import time
    
    while True:
        url = input("URL: ")
    
        # Title and Time
        print("...")
        print(((YouTube(url)).title), "//", (int(var1)/60),"mins")
        print("...")
    
        # Filename specification
        # Prevents any errors during conversion due to illegal characters in name
        _filename = input("Filename: ")
    
        # Downloading
        print("Downloading....")
        YouTube(url).streams.first().download(filename=_filename)
        time.sleep(1)
    
        # Converting
        mp4 = "'%s'.mp4" % _filename
        mp3 = "'%s'.mp3" % _filename
        ffmpeg = ('ffmpeg -i %s ' % mp4 + mp3)
        subprocess.call(ffmpeg, shell=True)
    
        # Completion
        print("\nCOMPLETE\n")
    

    This is an infinite loop that will allow the renaming, download, and conversion of multiple URLs.

提交回复
热议问题