I often need to download videos from Youtube in mp3
format. There are many websites where you can convert Youtube videos to mp3
, but I am giving you a simple Python script that does the same job.
I am using youtube_dl. Command-line program to download videos from YouTube.com and other video sites. It requires the Python interpreter, version 2.6, 2.7, or 3.2+, and it is not platform-specific. It should work on your Unix box, on Windows, or on macOS.
Let's jump to the code:
import youtube_dl
def run():
video_url = input("please enter youtube video url:")
video_info = youtube_dl.YoutubeDL().extract_info(
url = video_url,download=False
)
filename = f"{video_info['title']}.mp3"
options={
'format':'bestaudio/best',
'keepvideo':False,
'outtmpl':filename,
}
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([video_info['webpage_url']])
print("Download complete... {}".format(filename))
if __name__=='__main__':
run()
Just enter the URL of the song and the script will download that song in mp3
format, cool isn't it?
Thank you all.
Latest comments (27)
Here is a python script through which you can download all the videos in a playlist in .mp3 format
Here is a list of top YouTube to MP3 converters:
`from pytube import YouTube
def progress(stream, chunk, bytes_remaining):
total_size = stream.filesize
bytes_downloaded = total_size - bytes_remaining
percentage_of_completion = bytes_downloaded / total_size * 100
print(f"Descărcare: {percentage_of_completion:.2f}% completă.")
def download_mp3(video_url, output_path):
yt = YouTube(video_url)
yt.register_on_progress_callback(progress)
audio_stream = yt.streams.filter(only_audio=True).first()
audio_stream.download(output_path)
print(f"Descărcarea melodiei '{yt.title}' este completă.")
video_url = "youtube.com/watch?v=YsmfWkE6FUg"
output_path = "d://Downloads//uuu"
download_mp3(video_url, output_path)
`
thaks for the post!
i had some problems with the code and i extended it to be able to download single songs and playlists. (sorry for the bad english not nativ)
comment for simplification.
You can now also choose the output!
Here is also the code to download mp3 from youtube in highest possible quality using pytube:
But it I can't edit the mp3 tags of the file download with it. Can anyone tell me what happand?
hi, where does the file go after you download it? it isnt showing up for me anywhere
I've got "WARNING: yaBcFvKEXDo" and it suggested me to install ffmpeg or avconv to fix this, but what should I do when I install it?
why not
pytube
?pypi.org/project/pytube/
or
pytube3
,pytubeX
I used the pytube but I don't know how to download files as mp3, I did download the audio only but the format was mp4.
that's why I am trying this library as I found an explanation how to do it.
Help me how to convert and download to mp4 file.
You can use pytube. It's lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.