1. Introduction
The concatenation of multiple audio and video files offers convenience, organization, and rudimentary editing. For example, we can concatenate several audio files to create a playlist mix and ensure continuous playback without any interruption between tracks.
In this tutorial, we’ll learn how to concatenate several MP3 files into one.
2. Environment Setup
Before moving forward, let’s ensure we have a few MP3 files that we can use as examples.
To begin with, we create a new directory and download MP3 files using wget:
$ mkdir mp3_dir && cd mp3_dir
$ wget -q -O file1.mp3 https://freetestdata.com/wp-content/uploads/2021/09/Free_Test_Data_100KB_MP3.mp3
$ wget -q -O file2.mp3 https://freetestdata.com/wp-content/uploads/2021/09/Free_Test_Data_500KB_MP3.mp3
$ wget -q -O file3.mp3 https://freetestdata.com/wp-content/uploads/2021/09/Free_Test_Data_1MB_MP3.mp3
The mkdir command creates the mp3_dir directory, while the cd command changes the current directory to the newly created mp3_dir directory.
The -q flag in the wget command suppresses the output, and the -O option specifies the output filename.
Finally, we have three MP3 files (file1.mp3, file2.mp3, and file3.mp3) inside the mp3_dir directory.
3. FFmpeg
FFmpeg is a popular multimedia framework that can be used for tasks like audio and video editing, retrieving information about multimedia files, screen recording, and streaming.
3.1. Installation
We can install FFmpeg via a local package manager such as apt:
$ sudo apt install ffmpeg
Once we ensure the command is available, we can verify the installed version:
$ ffmpeg -version
ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11 (Ubuntu 11.2.0-19ubuntu1)
...
As we can see, ffmpeg now works.
3.2. Basic Usage
The FFmpeg framework comes with a few tools:
- ffprobe gathers information about multimedia files
- ffplay is a simple and portable multimedia player
Let’s find the duration of an MP3 file (file1.mp3) using ffprobe:
$ ffprobe -i file1.mp3 -show_entries format=duration -v quiet -of csv="p=0"
3.996735
Similarly, we can find the duration of other mp3 files.
3.3. Concatenate via ffmpeg
Let’s concatenate all three sample MP3 files:
$ ffmpeg -i "concat:file1.mp3|file2.mp3|file3.mp3" -acodec copy output_ffmpeg.mp3
This command takes three input files specified by the -i option and concatenates them without re-encoding, resulting in the output_ffmpeg.mp3 file.
The concat protocol concatenates the files separated by the pipe (|). The -acodec option specifies the audio codec to be used for the output file, in this case, copy.
Let’s play the output file:
$ ffplay -autoexit -nodisp output_ffmpeg.mp3
This command plays the track without any graphical display (-nodisp) and exits automatically (-autoexit) once the track is finished.
We can remove the -acodec copy option from the ffmpeg command above to enable re-encoding with the default MP3 encoder in FFmpeg, i.e., Lavc58.13.
4. Mp3Wrap
Mp3Wrap is a free command-line utility designed to wrap two or more MP3 files in one large playable file.
4.1. Installation
We can install MP3Wrap using most native package managers:
$ sudo apt-get install mp3wrap
Let’s verify the installation:
$ mp3wrap -v
Mp3Wrap Version 0.5 (2003/Jan/16). See README and COPYING for more!
Written and copyrights by Matteo Trotta - <[email protected]>
...
In this case, the installed version of MP3Wrap is 0.5.
4.2. Concatenate via mp3wrap
The syntax to concatenate the MP3 files is quite straightforward. We can specify the output filename followed by the list of input MP3 files to concatenate.
Let’s wrap all three MP3 files in a single output file:
$ mp3wrap output.mp3 file1.mp3 file2.mp3 file3.mp3
Mp3Wrap Version 0.5 (2003/Jan/16). See README and COPYING for more!
...
33 % --> Wrapping file1.mp3 ... OK
66 % --> Wrapping file2.mp3 ... OK
100 % --> Wrapping file3.mp3 ... OK
Calculating CRC, please wait... OK
output_MP3WRAP.mp3 has been created successfully!
Use mp3splt to dewrap file; download at http://mp3splt.sourceforge.net!
Here, Mp3Wrap creates an output file named output_MP3WRAP.mp3 by concatenating three MP3 files.
Although we provide output.mp3 as the output filename, Mp3Wrap changes it to output_MP3WRAP.mp3. This is to facilitate its complementary program, Mp3Splt. The MP3WRAP string tells Mp3Splt that the file is wrapped and can be split using the -w option.
So, it’s necessary to keep this string in the output filename in case we want to split this file in the future.
Moreover, we can list all wrapped files from a Mp3Wrap output file:
$ mp3wrap -l output_MP3WRAP.mp3
Mp3Wrap Version 0.5 (2003/Jan/16). See README and COPYING for more!
...
List of wrapped files in output_MP3WRAP.mp3:
file1.mp3
file2.mp3
file3.mp3
Furthermore, we can append any other MP3 files to an existing Mp3Wrap output file using the -a option.
5. MP3Cat
MP3Cat is a basic utility for concatenating MP3 files on the command line without re-encoding.
5.1. Installation
Let’s fetch the pre-compiled, ready-to-use binary file from the MP3Cat GitHub repository:
$ wget https://github.com/dmulholl/mp3cat/releases/download/v4.2.2/mp3cat-linux-amd64.zip && unzip mp3cat-linux-amd64.zip
Thus, we download the binary file and extract the downloaded archive using the unzip command.
Then, we can move this extracted mp3cat file to the bin directory, i.e., /usr/local/bin/:
$ sudo cp mp3cat-linux-amd64/mp3cat /usr/local/bin/
Now, we should be able to use the mp3cat command from anywhere on the system.
5.2. Concatenate via mp3cat
We concatenate MP3 files with MP3Cat by providing a list of input files with or without output filename:
$ mp3cat --out output.mp3 file1.mp3 file2.mp3 file3.mp3
This command concatenates three MP3 files into one output file specified by the –out option, i.e., output.mp3.
We can obtain the same result even if we remove –out output.mp3 from the above command. The default output filename for MP3Cat is output.mp3.
Additionally, we can concatenate all MP3 files from a directory using the –dir (-d) option:
$ mp3cat --dir ./ --out all_together.mp3
Here, we concatenate all the MP3 files from the current working directory into one file, all_together.mp3.
Moreover, we can overwrite an existing output file using the –force (-f) option.
6. SoX
SoX, short for Sound eXchange, is the Swiss army knife of sound processing.
6.1. Installation
Let’s quickly install SoX with mp3 support:
$ sudo apt-get install -y sox libsox-fmt-mp3
In addition to installing SoX, this command also deploys libsox-fmt-mp3, the package containing the SoX MP2 and MP3 format libraries.
6.2. Concatenate via sox
The concatenation action is the default for SoX.
Thus, to concatenate MP3 files with SoX, we can provide a list of input files followed by the output filename.
Let’s give it a try:
$ sox file1.mp3 file2.mp3 file3.mp3 output_sox.mp3
This command concatenates three specified files together and writes them to the output_sox.mp3 file.
We get the same result with the –combine concatenate operation:
$ sox --combine concatenate file1.mp3 file2.mp3 file3.mp3 output_sox.mp3
In case we don’t specify the output filename, SoX uses the last provided filename as the output filename.
Additionally, we can set the verbosity level to 3 for details on the processing of MP3 files:
$ sox -V3 --combine concatenate file1.mp3 file2.mp3 file3.mp3 output_sox.mp3
This way, the command provides information about the processing details of each input file, including sample rate, precision, duration, and file size.
Output files from SoX have the same format as the input file, including the sample rate and encoding.
7. Conclusion
In this article, we learned multiple ways to concatenate several .mp3 files into one .mp3 file.
First, we installed and tested the FFmpeg multimedia framework to concatenate MP3 files. Then, we checked out MP3Wrap, which joins MP3 files into a special wrapped format without re-encoding.
We also used MP3Cat, another CLI utility, to join MP3 files without re-encoding. MP3Cat can also detect all MP3 files from a directory and concatenate them into one large file. Finally, we used SoX to concatenate with the same encoding as the input MP3 files.
Although we can select any method depending on our preferences and needs, MP3Wrap and MP3Cat are usually the simplest as they are specifically designed for concatenating MP3 files.