In today’s world, YouTube has become one of the most popular video-sharing platforms in the world. With millions of videos uploaded every day, there are bound to be some videos that you would like to save as an MP3 file so that you can listen to them offline. In this article, we will create a Python script that will allow you to download and convert any videos from YouTube to MP3 files. But, It is important to keep in mind the copyright laws and terms of service when downloading YouTube videos. Don’t try to use the downloaded video or audio for commercial purposes without the creator’s permission.
Table of Contents
The modules we will be using to create this script are called “pytube” & “moviepy.” Pytube is a lightweight, simple-to-use Python library that allows you to download YouTube videos and moviepy is a video editing library that can read and write common video and audio files. Pytube can download videos in various formats, including MP4, WebM, and 3GP. So we will use pytube to download the videos and moviepy to convert the downloaded file to mp3 format.
Before we start, we need to make sure that pytube and moviepy are installed on our system. To install pytube, open the terminal or command prompt and enter the following command:
pip install pytube && pip install moviepy
Once the modules are installed, we can start writing our Python script.
But where do we write the code?
This will create a Python file named “filename.py” and open the file editor. Now you can start writing the code on your file.
In case, you are on a Windows machine, then you can write Python code directly on Python idle or you can use IDE like pycharm or you can write on a notepad and save the file with “.py” extension. Choose your convenient way and start writing the code.
If you don’t have Python or pycharm installed on your Windows machine and need help with downloading and installing, then you can refer to the article – How to install python on windows (Cmd & pycharm).
Writing the youtube to mp3 script:
Firstly we need to import the downloaded modules pytube and moviepy to our Python file. To do this, use the command:
from pytube import YouTube from moviepy.editor import *
Here we imported the class YouTube from the pytube module and Everything (*) from the moviepy.editor
Now I’ll write the rest of the code and explain it to you at the end.
from pytube import YouTube from moviepy.editor import * # Get the video URL video_url = input("Enter the URL: ") # create a YouTube object and get the audio stream yt = YouTube(video_url) # Get the audio stream of the video audio_stream = yt.streams.filter(only_audio=True).first() # download the audio stream audio_file = audio_stream.download() # convert the audio file to MP3 audio = AudioFileClip(audio_file) audio.write_audiofile(audio_file.replace(".mp4", ".mp3")) print("Conversion Completed!")
The script starts by asking the user to enter the YouTube video URL. The URL is stored in the video_url variable. We then create a pytube.YouTube object using the video_url variable, which represents the YouTube video.
We use the filter() method to get the audio-only stream of the video using the only_audio=True argument, and then use the first() method to get the first audio-only stream returned by the filter() method. We save the audio stream to the audio_file variable using the download() method.
Next, we create an AudioFileClip object using the audio_file variable. We then use the write_audiofile() method to write the audio data to an MP3 file with the same name as the original MP4 file, but with the .mp3 extension instead of .mp4.
At the end of the script, we print a message “Conversion Completed!” to indicate that the video has been converted to an MP3 file successfully.
You can also customize this script further like changing the output file name, path, etc by passing the appropriate arguments to the download() method. Refer to the official documentation of the modules pytube and moviepy to know more.
Executing the Script:
Linux users can run the code directly on the terminal by using the following command:
When you run this script, it will prompt you to enter the URL of the YouTube video you want to download and convert. The script will then download the video as an MP4 file using pytube, and convert the MP4 file to an MP3 file using moviepy. The resulting MP3 file will be saved in the same directory as the script.
Take a look at the output:
Here we can notice two output files, one with a .mp4 extension (Before conversion) and the other with a .mp3 extension(After conversion). We can remove the original MP4 file, if you don’t need it, by adding a single line of code to our script.
NOTE: We need to import the os module before using this code
If you are a Windows user, then we recommend you to use Python idle or pycharm IDE for a better experience.
NOTE: pycharm users need to download the modules separately on the pycharm application. To do so, click the tab “Python packages” on the bottom left and type the name of the modules.
Once installed, Write and run the code from the pycharm application. Take a look at the output screenshot below.
For Python idle, open a new file and write the code. The execution process is the same as the pycharm.
In case, you use Notepad, then Save the script with a .py extension, and then run it in the command prompt using the same command “python3 scriptname.py”.
Instead of using some websites, now we can use our own Python script to easily convert any YouTube video to an MP3 file. With just a few lines of code, you can now download and save your favorite music or podcasts as MP3 files and listen to them offline. This is a useful tool for anyone who wants to listen to YouTube content without an internet connection. Try to customize the code further and extend its feature by adding some more lines of code to this script.
If you have any doubts regarding this, feel free to comment below. Also, if you need any other scripts like this, mention them in the comment section. Don’t forget to subscribe to our newsletter so that you get notified whenever we upload new content. See you all in the next post. Cheers!