How to Download and Trim MP3s from YouTube with Python

Python is a powerful and versatile programming language that makes it easy to automate all sorts of tasks – including downloading and editing audio files. In this tutorial, we‘ll walk through how to fetch the audio from any YouTube video in MP3 format and trim it to a specific time range, all with a relatively short Python script.

Whether you want to snip out your favorite joke from a comedy podcast, extract a memorable quote from an interview, or cut a song down to just the chorus, this technique will help you precisely capture the audio clip you need from a larger YouTube video.

We‘ll be using a few key Python libraries:

  • youtube-dl to handle downloading the audio from a YouTube URL
  • pydub to open the downloaded MP3 and allow us to manipulate it
  • ffmpeg (not a Python library, but a required dependency of pydub)

Don‘t worry if you‘ve never used these tools before – I‘ll walk you through the entire process step-by-step. Before diving into the code, though, there‘s one important note to make about copyright.

A Word of Caution on Copyright

Whenever you‘re downloading audio or video from YouTube, it‘s critical that you only grab content that you have the rights to use. This usually means sticking to media that is in the public domain or licensed under Creative Commons.

For the purposes of this tutorial, we‘ll use a copyright-free recording of Beethoven‘s Symphony No. 9 as our example audio. I encourage you to follow along with the same clip so you don‘t run into any legal snags.

With that out of the way, let‘s jump into setting up your environment!

Step 1: Installing Dependencies

The first thing you‘ll need is Python 3. If you don‘t already have it installed, go grab the latest version from python.org. This tutorial will work with any version of Python 3.

Next up is ffmpeg, the Swiss-army knife of media editing. It powers the pydub library we‘ll use to slice up our audio.

Installing ffmpeg is a slightly different process depending on your operating system. Find your OS below and follow those steps:

Linux

If you‘re on a Debian-based distro like Ubuntu, you can install ffmpeg from the command line with:

sudo apt update
sudo apt install ffmpeg

macOS

The easiest way to install ffmpeg on macOS is using Homebrew. If you don‘t have Homebrew set up, paste this into your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Then you can install ffmpeg with:

brew install ffmpeg

Windows

Installing ffmpeg on Windows is a little more involved. Go to the ffmpeg downloads page and grab the release build that matches your system architecture (32-bit vs 64-bit).

Unzip the downloaded folder, rename it to just "ffmpeg" and move it to the C:\ drive. Then you need to add C:\ffmpeg\bin to your system‘s PATH environment variable:

  1. Open the Start search and type "env"
  2. Select "Edit the system environment variables"
  3. Click the "Environment Variables" button
  4. Under "System variables", find the "Path" row and click "Edit"
  5. Click "New" and paste in C:\ffmpeg\bin
  6. Click OK to save

To make sure it worked, open a new command prompt and run ffmpeg -version. You should see version info printed out.

Step 2: Setting Up the Python Script

Create a new Python file in your favorite code editor and name it something like yt_trimmer.py. This is where we‘ll write the code to download and trim our audio.

First, we need to install the Python libraries we‘ll be using. From the command line, run:

pip install youtube-dl pydub 

Now open up yt_trimmer.py and add the following imports at the top:

import os
from youtube_dl import YoutubeDL
from pydub import AudioSegment

We‘ll use os for some basic file management, youtube_dl to grab the audio from YouTube, and pydub to edit the resulting MP3.

Step 3: Downloading the Audio from YouTube

Alright, let‘s get to the fun part! We‘ll start by defining a function to download the audio from a YouTube video given its URL.

def download_audio(url):
    print("Downloading audio from " + url)

    ydl_opts = {
        ‘format‘: ‘bestaudio/best‘,
        ‘postprocessors‘: [{
            ‘key‘: ‘FFmpegExtractAudio‘,
            ‘preferredcodec‘: ‘mp3‘,
            ‘preferredquality‘: ‘192‘,
        }],
        ‘outtmpl‘: ‘%(title)s.%(ext)s‘
    }

    with YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url)
        filename = ydl.prepare_filename(info)
        ydl.download([url])

    return filename

There‘s a fair bit to unpack here. The ydl_opts dictionary configures youtube-dl with a few key settings:

  • format: Tells youtube-dl to grab the best audio-only stream
  • postprocessors: Instructs youtube-dl to convert the audio to an MP3
  • outtmpl: Sets the output filename template to the video‘s title

Inside the with block, we initialize a YoutubeDL object with our options and use it to download the audio. The extract_info call fetches metadata about the video, which we use to get the auto-generated output filename.

Finally, we return that filename for use later in the script.

Step 4: Trimming the Audio Clip

With our audio downloaded, it‘s time to trim out the segment we want. We‘ll define another function for this:

def trim_audio(filename, start_time, end_time):
    print("Trimming " + filename + " from " + start_time + " to " + end_time)

    audio = AudioSegment.from_mp3(filename)

    start_sec = convert_time_to_seconds(start_time) * 1000
    end_sec = convert_time_to_seconds(end_time) * 1000

    trimmed_audio = audio[start_sec:end_sec]

    output_filename = os.path.splitext(filename)[0] + "_trimmed.mp3"
    trimmed_audio.export(output_filename, format="mp3")

    print("Trimmed audio saved as " + output_filename)
    return output_filename

This function takes three parameters:

  • filename: The name of the MP3 file to trim
  • start_time: The start time of the desired clip in MM:SS format
  • end_time: The end time of the desired clip in MM:SS format

We start by loading the full MP3 file into an AudioSegment object using pydub.

Next, we need to convert our start_time and end_time into milliseconds, which is what pydub expects. To do this, I‘ve defined a small utility function:

def convert_time_to_seconds(time):
    parts = time.split(":")
    minutes = int(parts[0])
    seconds = int(parts[1])
    return minutes * 60 + seconds

This simply splits the time string at the ":" and converts the minute and second parts to integers before doing the conversion to total seconds.

Back in trim_audio, we multiply those second values by 1000 to get milliseconds. Then we use pydub‘s slice syntax to extract the trimmed segment:

trimmed_audio = audio[start_sec:end_sec]

Finally, we generate a filename for our output by taking the original filename minus the ".mp3" extension and appending "_trimmed.mp3". We pass this filename to pydub‘s export function to write out the trimmed MP3.

Step 5: Putting it All Together

We‘ve defined all the key pieces of our audio trimming pipeline – now we just need to tie it together with a main function:

def main():
    # URL of the YouTube video to download
    video_url = "https://www.youtube.com/watch?v=8OAPLk20epo"

    # Download the audio from the YouTube video
    filename = download_audio(video_url)

    # Specify the start and end times of the section to trim (in MM:SS format)
    start_time = "9:51" 
    end_time = "14:04"

    # Trim the audio and save it to a new file
    trimmed_filename = trim_audio(filename, start_time, end_time)

    print("Trimmed audio saved as " + trimmed_filename)

if __name__ == "__main__":
    main()

In main, we hardcode the YouTube URL of our example Beethoven‘s 9th Symphony video. We pass this to our download_audio function to grab the full audio.

Then we specify the start_time and end_time of our desired clip, which in this case will extract the famous "Ode to Joy" section of the 4th movement.

We pass the downloaded filename and trim times to our trim_audio function, which snips out that segment and saves it to a new "_trimmed.mp3" file.

Finally, we print out the name of the generated trimmed audio file.

Go ahead and run the script from your terminal with:

python yt_trimmer.py

After a few seconds (depending on your internet speed), you should see output like:

Downloading audio from https://www.youtube.com/watch?v=8OAPLk20epo
[youtube] 8OAPLk20epo: Downloading webpage
[youtube] 8OAPLk20epo: Extracting video information
[youtube] 8OAPLk20epo: Downloading MPD manifest
[download] Destination: Symphony No. 9 "Choral"- IV. Presto Allegro assai (Ode to Joy).webm
[download] 100% of 20.17MiB in 00:05
[ffmpeg] Destination: Symphony No. 9 "Choral"- IV. Presto Allegro assai (Ode to Joy).mp3
Deleting original file Symphony No. 9 "Choral"- IV. Presto Allegro assai (Ode to Joy).webm (pass -k to keep)
Trimming Symphony No. 9 "Choral"- IV. Presto Allegro assai (Ode to Joy).mp3 from 9:51 to 14:04
Trimmed audio saved as Symphony No. 9 "Choral"- IV. Presto Allegro assai (Ode to Joy)_trimmed.mp3

Congrats! You‘ve just downloaded and trimmed your first YouTube audio clip with Python. You should see the full "Symphony No. 9 ‘Choral‘- IV. Presto Allegro assai (Ode to Joy).mp3" file along with the trimmed "Symphony No. 9 ‘Choral‘- IV. Presto Allegro assai (Ode to Joy)_trimmed.mp3" in the same directory as your script.

Next Steps

While this basic script gets the job done, there are lots of ways you could extend it:

  • Accept the YouTube URL and trim times as command-line arguments instead of hardcoding them
  • Allow the user to input the trim times in HH:MM:SS format to enable grabbing clips longer than an hour
  • Automatically generate a timestamped name for the trimmed clip to make it easier to catalog
  • Validate that the provided YouTube URL is actually a YouTube video and that the trim times are in the proper format
  • Build a simple GUI with Tkinter to make a nice shareable app
  • Batch process a list of YouTube URLs and trim times

The possibilities are endless! I‘ve put the full code from this tutorial up on GitHub so you can easily fork it and make your own tweaks.

I hope this has been a helpful introduction to working with audio in Python. The combination of youtube-dl and pydub is incredibly powerful for all sorts of YouTube audio processing pipelines.

As you explore further, just remember to be mindful of copyright and only use these tools on audio you have permission to modify. Happy coding!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *