uncategorized

How to Cut, Clip and Convert Audio Files in Python

When working on experimental things, you might find yourself in a situation where you are faced with raw audio or video and need to modify it. Working with these media formats requires a few extra steps if you do not have suitable 3rd party software pre-installed, or perhaps you just prefer a command line approach for these operations? Either way, Python is a great tool for this job and next I will show you how.

In my own situation, I wanted to experiment with some speech-to-text services, to test them for quality. I wanted to test specific audio files from youtube, then upload them to the speech-to-text app for transcription. To accomplish this, I used pytube to download the audio file, then ffmpeg library to converted the file to appropriate format, and finally ffmpeg to cut the audio file length - all using a command line! These are the commands I will discuss today.

Prerequisites

Execute this command to ensure you have all necessary modules installed.

1
pip install pytube imageio imageio-ffmpeg

Download Audio from Youtube

If you already have an audio file you want to work with, feel free to skip this first step. Otherwise, you can obtaining your audio sample from Youtube using pytube library.

1
2
3
4
5
6
7
from pytube import YouTube

YouTube('https://youtu.be/your-video-id-here')
.streams
.filter(file_extension='mp4', only_audio=True)
.first()
.download(output_path='out_directory', filename='out_filename')

In the code sample above, replace the following:

  • your-video-id-here - enter the youtube video id you want to work with
  • out_directory - enter local directory path where the file should be saved, for example 'C:/Documents'
  • out_filename - enter a name for the audio file, for example 'audio.mp4'

The output of this command, when successful, is a mp4 audio file saved at the specified location. If the video has no audio, or the audio is not accessible, this command will fail. If that happens, double check your command then try another video id, or use an audio file you already have.

Convert Audio from MP4 to WAV

For my specific use case, I needed an audio file in wav and mp3 formats. However, since my video audio file was an mp4, I needed to first convert the file format. ffmpeg library allows converting audio file formats.

The following command will perform conversion from mp4 to wav format:

1
ffmpeg -i audio.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav

Replace audio with appropriate filename referring to your audio file before and after this operation. If audio.mp4 is not in the current directory, include file path in the command.

Convert Audio from MP4 to MP3

Similarly, ffmpeg can be used to convert mp4 to mp3. For this operation, specify .mp3 as the output file extension, and the library will perform the desired conversion.

Execute the following command using a terminal or command line:

1
ffmpeg -i audio.mp4 audio.mp3

Clip Audio

In a scenario where you want to get a shorter clip of a longer audio file and you know exactly were to clip, you can use ffmpeg for clipping audio.

1
2
3
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

ffmpeg_extract_subclip("audio.mp3", 0, 60, targetname="result/shorter_audio.mp3")

Before executing this command, specify the following parameters:
"audio.mp3" - replace this with a path to the source file
0 - replace this with clip start time, in seconds
60 - replace this with clip end time, in seconds
"result/shorter_audio.mp3" - replace this with desired output file path and name

That’s It!

We hope you enjoyed this tutorial. Please share it with your friends.

If you enjoyed this article please share it with your friends!