Convert your favourite Youtube videos into mp3 using Python

28 Apr 2020

One of my favourite TV shows is Parks and Recreation. If you don’t know it watch it. One of the main characters portrayed by Chris Pratt is a wannabe rock star. His band, Mouse Rat, created few quite good rock songs. Recently I re-watched this TV show and I tried to buy the soundtrack containing mentioned rock hits. Unfortunately I wasn’t able to find it on Amazon, Apple Store and NBC store so I looked for it on Youtube and did what every developer would do, created a simple python script that converts Youtube video into mp3. Why homemade script and not one of bazillion websites that convert Youtube videos? Because first few sites I tested were filled with crap (ads, malware, redirects, etc.). Ok, let’s start.

Prerequisites

First of all you will need ffmpeg. I’m using MacOS and the easiest way of installing it is running those two commands

brew tap homebrew-ffmpeg/ffmpeg
brew install homebrew-ffmpeg/ffmpeg/ffmpeg

Installation

Once you have ffmpeg installed you can pull code and install dependencies.

git https://github.com/wilgucki/get-the-music.git
cd get-the-music
# now you need to do the virtual environment magic followed by
pip install -r requirements.txt

Usage

Using script is easy. Just run

python -m get_mp3 --url "<youtube url>"

It will download mp4 file and convert it to mp3. File will be saved under the Youtube video title in current directory.

Final notes

  • This is very simple script. It lacks some validations so you might see some raw errors if you make a mistake in video url.
  • Downloaded video quality won’t be the best. I will have to work on this.
  • At the moment there’s no way to save video and mp3 file in a different location. It’s on my TODO list.

Enjoy :)