Download video in mp3 format using pytube
Pytube does not support "mp3" format but you can download audio in webm format. The following code demonstrates it.
from pytube import YouTube
yt = YouTube("https://www.youtube.com/watch?v=kn8ZuOCn6r0")
stream = yt.streams.get_by_itag(251)
the itag is unique id to get file with sppecific resolution
stream.download()
For mp3 you have to convert (mp4 or webm) file format to mp3.
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')
I am assuming you are using Python 3 and pytube 9.x, you can use the filter method to "filter", the file extension you are interested in.
For example, if you would like to download mp4 video file format it would look like the following:
pytube.YouTube("url here").streams.filter(file_extension="mp4").first()
if you would like to pull audio it would look like the following:
pytube.YouTube("url here").streams.filter(only_audio=True).all()
Hope that helps anyone landing on this page; rather than converting unnecessarily.
How can I download the video as an audio file, in .mp3 format directly?
I'm afraid you can't. The only files available for direct download are the ones which are listed under yt.streams.all()
.
However, it is straightforward to convert the downloaded audio file from .mp4
to .mp3
format. For example, if you have ffmpeg installed, running this command from the terminal will do the trick (assuming you're in the download directory):
$ ffmpeg -i downloaded_filename.mp4 new_filename.mp3
Alternatively, you can use Python's subprocess
module to execute the ffmpeg command programmatically:
import os
import subprocess
import pytube
yt = pytube.YouTube("https://www.youtube.com/watch?v=WH7xsW5Os10")
vids= yt.streams.all()
for i in range(len(vids)):
print(i,'. ',vids[i])
vnum = int(input("Enter vid num: "))
parent_dir = r"C:\YTDownloads"
vids[vnum].download(parent_dir)
new_filename = input("Enter filename (including extension): ")) # e.g. new_filename.mp3
default_filename = vids[vnum].default_filename # get default name using pytube API
subprocess.run([
'ffmpeg',
'-i', os.path.join(parent_dir, default_filename),
os.path.join(parent_dir, new_filename)
])
print('done')
EDIT: Removed mention of subprocess.call
. Use subprocess.run
(unless you're using Python 3.4 or below)