How to transcode a YouTube video to MP3 in Python

Learn how to download a YouTube video and transcode it to MP3 using Python and YouTube_DL from within a Python script.

How to transcode a YouTube video to MP3 in Python
Picture by Freestocks, Pexels.
3 minutes to read

If you regularly watch YouTube videos that you want to listen to on your drive to work, you could consider downloading them and converting them to MP3. This is done through a process called transcoding, which is the process of converting a video file from one format to another. In this article, we’ll look at how to transcode a YouTube video to MP3 in Python using the YouTube_DL package.

The YouTube_DL package is a Python package that allows you to download videos from YouTube, and it can also be used to download videos from other sites, such as Vimeo, SoundCloud, and Facebook. It’s a very useful package for downloading videos, and it’s also very easy to use.

YouTube_DL is a command-line tool, so it’s slightly awkward to use from within another Python script, but it is possible. In this article, we’ll look at how to do this to download a YouTube video and transcode it to MP3.

Install YouTubeDL

To get started, open a Jupyter notebook and install the youtube_dl package using Pip, the Python package manager. You can do this by running the following code. Depending on your setup, you may also need to install ffmpeg, which you can do using the apt package manager on Ubuntu and Debian Linux workstations.

!pip3 install youtube_dl
Requirement already satisfied: youtube_dl in /conda/envs/data-science-stack-2.5.1/lib/python3.7/site-packages (2021.12.17)

Import the packages

Next, you’ll need to import youtube_dl plus the unicode_literals module from the __future__ package.

from __future__ import unicode_literals
import youtube_dl

Configure the options

The YouTube_DL package requires that we define some options before we can use it. These options are defined in a dictionary, and they include the format which we’ll set to bestaudio/best, plus some details on how we want to postprocess the video once it’s downloaded. We’ll use the FFmpegExtractAudio postprocessor to extract the audio from the video, and we’ll set the preferredcodec to mp3 and the preferredquality to 192.

options = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}

Download the YouTube video and transcode to MP3

To download the file itself you first need to obtain the YouTube URL of the video. I’ve stored this in a variable called url. You can then use the youtube_dl.YoutubeDL function to download the video and transcode it to MP3 using with.

url = "https://www.youtube.com/watch?v=DZZFiGBX2CA"
with youtube_dl.YoutubeDL(options) as downloader:
    downloader.download(["" + url + ""])
[youtube] DZZFiGBX2CA: Downloading webpage
[download] Destination: Alan Trouser Press-DZZFiGBX2CA.m4a
[download] 100% of 436.02KiB in 00:0660KiB/s ETA 00:003
[ffmpeg] Correcting container in "Alan Trouser Press-DZZFiGBX2CA.m4a"
[ffmpeg] Destination: Alan Trouser Press-DZZFiGBX2CA.mp3
Deleting original file Alan Trouser Press-DZZFiGBX2CA.m4a (pass -k to keep)

After the code has run, you should find that YouTube_DL has downloaded the file, transcoded it to MP3, and saved it in the same directory as your Jupyter notebook.

Matt Clarke, Saturday, August 27, 2022

Matt Clarke Matt is an Ecommerce and Marketing Director who uses data science to help in his work. Matt has a Master's degree in Internet Retailing (plus two other Master's degrees in different fields) and specialises in the technical side of ecommerce and marketing.