Losange bleu

Blogpost

Using Python to download and convert Youtube video into mp3

This is a short explanation of how to use this Python script to download Youtube videos and convert them into mp3. The idea is that you run this script while scrolling on Youtube videos and copy-paste the URL of each video you find interesting as you go.

1. Ipython + needed packages installed

First you need to have ipython 3 installed (Python 3 + ipython). How to install it is beyond the scope of this post but you can find information there.

Once installed, you need to have a few packages installed. To install them, launch ipython: open your terminal and type ipython. If correctly installed, the terminal should display the version of Python and IPython installed, and the following line should start with "In[1]". Once ipython is ready to be used, type the following in your terminal:

!pip install pytube
!pip install moviepy
!pip install youtube_dl

The ! before the command is to run the command outside Python. The other packages should already be coming with Python. If not, you will have an error message while running the program such as "ModuleNotFoundError: No module named [...]". Then, simply follow the same idea to install it:

!pip install _package-name_

2. Download the youtube converter Python script

Once the installation of ipython and packages is done, you can download the script "youtube_converter.py" there on your Desktop.

Click on "Clone or Download" on the Github page, and select "Download ZIP"

Extract the zip and in the extracted folder, you will find the file "Youtube_converter.py". Cut and paste it on your Desktop.

3. Run the program

Ok, we are almost done.

Select the file "youtube_converter.py" and drop it on your terminal (still with ipython activated -see above-), this should write the whole file path on your terminal. Then, type "run " (mind the space after run) before the complete path. The line should look similarly (depending on your OS and path structure):

run /User/Desktop/youtube_converter.py

Congrats, the program just started! Alright, so now, the program will first ask you if you want to paste the URL one by one or directly paste a list of URLs. Type 1 and press Enter to paste Youtube URL one by one. Type 2 and press Enter to paste a list of Youtube URLs.

If you chose to paste URL one by one, pick a Youtube URL, paste it and press Enter. Feel free to add as many urls as you like but always one by one. Once your done, type 1 and press Enter, this will start the conversion process.

If you chose to paste a list of URLs, make sure each URL is separated by a space, paste the whole selection and press Enter to start the conversion.

The program will then ask you to check if you want to create the music folder in the current path or in another path. Type 0 and press enter to create the folder music in the current path (which is writen above the question on your terminal), the mp3 will be converted in there. Type 1 and press enter if you want to define another destination for the folder music. You will have to define the whole new path (not including music). For example, it could be something like user/Desktop/personal_stuff. Writing this path would create a folder music in the folder personal_stuff in your Desktop. Mind that depending on your system, you might have to write a longer path.

4. Enjoy the output!

That's it. Now you just have to wait until your computer does the work. Go to the music folder to listen to the output!

The program will ask you if you want to paste more ULRs (the program will start again), just press Ctrl + C to stop.

Feel free to improve the code (pull request) or to fork it.

Edit: Some Youtube videos won't download. If so, try closing your terminal and running the program again. Make sure you're not messing up with the terminal path (play around with the pwd/cd commands to check it before running the script). I suggest you start downloading (type 1 instead of pasting a new url) after copying a few Youtube URLs. It can be frustrating to list a lot of URLs and then find out that they can't be converted eventually.