Get Started

Chopping and changing audio files

Some of your audio files may have sections of redundancy. For example, you might find at the beginning of each file, there's a few seconds of static.

Instead of wasting compute trying to transcribe static, you can remove it.

Since an AudioSegment is iterable, and measured in milliseconds, you can use slicing to alter the length.

To get the first 3-seconds of wav_file, you'd use wav_file[:3000].

You can also add two AudioSegment's together using the addition operator. This is helpful if you need to combine several audio files.

To practice both of these, we're going to remove the first four seconds of part1.wav, and add the remainder to part2.wav. Leaving the end result sounding like part_3.wav.

This is a part of the course

“Spoken Language Processing in Python”

View Course

Exercise instructions

  • Import part_1.wav and part_2.wav and save them to part_1 and part_2 respectively.
  • Remove the first 4-seconds of part_1 using slicing and save the new audio to part_1_removed.
  • Add part_1_removed to part_2 and save it to part_3.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

from pydub import AudioSegment

# Import part 1 and part 2 audio files
part_1 = AudioSegment.from_file(____)
part_2 = ____.____(____)

# Remove the first four seconds of part 1
part_1_removed = part_1[____:]

# Add the remainder of part 1 and part 2 together
part_3 = ____ + ____

This exercise is part of the course

Spoken Language Processing in Python

AdvancedSkill Level
4.7+
3 reviews

Learn how to load, transform, and transcribe speech from raw audio files in Python.

Not all audio files come in the same shape, size or format. Luckily, the PyDub library by James Robert provides tools which you can use to programmatically alter and change different audio file attributes such as frame rate, number of channels, file format and more. In this chapter, you'll learn how to use this helpful library to ensure all of your audio files are in the right shape for transcription.

Exercise 1: Introduction to PyDubExercise 2: Import an audio file with PyDubExercise 3: Play an audio file with PyDubExercise 4: Audio parameters with PyDubExercise 5: Adjusting audio parametersExercise 6: Manipulating audio files with PyDubExercise 7: Turning it down... then upExercise 8: Normalizing an audio file with PyDubExercise 9: Chopping and changing audio files
Exercise 10: Splitting stereo audio to mono with PyDubExercise 11: Converting and saving audio files with PyDubExercise 12: Exporting and reformatting audio filesExercise 13: Manipulating multiple audio files with PyDubExercise 14: An audio processing workflow

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free