How to join/merge many mp3 files?

62

27

Searching Google on how to join/merge many mp3 files, it suggests that I should just cat them together.

That might "work", but clearly it is not the correct way to do it, as each header and set of IDv3 tags will also be concatenated.

Does a Linux program exist that can be scripted to join/merge many mp3?

Can mplayer/mencoder/ffmpeg do it?

Sandra

Posted 2011-07-23T21:19:26.970

Reputation: 1 893

Answers

83

This will concatenate two mp3 files, and the resulting metadata will be that of the first file:

ffmpeg -i "concat:file1.mp3|file2.mp3" -acodec copy output.mp3

This is because, for ffmpeg, the whole "concat:" part is a single "input file", and its metadata will be of the first concatenated file. If you want to use metadata from the second file instead, you have to add it as a dummy input file and map its metadata to that of the output:

ffmpeg -i "concat:file1.mp3|file2.mp3" -i file2.mp3 -acodec copy test.mp3 -map_metadata 0:1

If you want to construct your metadata from the two metadatas, you'll have to do it by hand. You can dump a file's metadata with

ffmpeg -i file1.mp3 -f ffmetadata file1.metadata

After dumping both metadatas and constructing new metadata, you can add it to the output file with -metadata, and you can disable metadata copying by setting a -map_metadata mapping from a negative input file number. This sets a name value and no other metadata:

ffmpeg -i "concat:file1.mp3|file2.mp3" -acodec copy -metadata "title=Some Song" test.mp3 -map_metadata 0:-1

Ambroz Bizjak

Posted 2011-07-23T21:19:26.970

Reputation: 4 265

https://trac.ffmpeg.org/wiki/Concatenate – DmitrySandalov – 2015-04-08T23:52:13.597

1Just wanted to clarify a few things for others: -acodec means that audio codec will be used and copy mean that there will only be muxing and demuxing, but not encoding/transcoding (i.e. very fast and no quality loss) – Housemd – 2018-02-04T05:28:20.257

When several files have to be concatenated, as in the original question, the following snippet might come in handy to construct the string "file1.mp3|file2.mp3|file3.mp3"etc: s=""; for i in file*.mp3; do s="$s|$i"; done; echo ${s:1} – Immanuel Weihnachten – 2018-03-03T23:53:17.443

1It would be great if there was a GUI program where I can drag and drop multiple files (e.g. sorted chronologically for recordings recorded sequentially, and perhaps some files edited) and it will then output one file. – James Ray – 2018-11-15T04:13:29.447

1This is a good answer, however in almost every case, the resulting MP3 file has a short (sub-second) gap between where the files were joined. So if you're trying to merge MP3 tracks to create a seamless playback album / set / whatever, this doesn't work quite right. – The Lizard – 2018-12-11T17:59:00.877

FYI this requires a certain version of ffmpeg. If it says file not found concat... try upgrading and running it again – Sameer – 2012-09-09T23:14:01.613

Works fine in ffmpeg 0.10.4 in Gentoo. What are you using? Are you sure you haven't just mistyped the command? – Ambroz Bizjak – 2012-09-10T00:05:29.090

Ambroz I was saying that I had an old version and had to upgrade, then it worked. – Sameer – 2012-09-14T20:58:06.127

Typing this in a DOS box in XP doesn't work: "'file2.mp3' is not recognized as an internal or external command, operable program or batch file". Is the command Linux-specific? – OverTheRainbow – 2012-10-18T16:54:29.710

@OverTheRainbow the argument is supposed to be "concat:file1.mp3|file2.mp3", without the backslash. The backslash is an escape character to prevent the unix shell from treating | as a pipe. I've changed it to use double quotes instead. – Ambroz Bizjak – 2012-10-19T21:19:57.777

It's worth noting the mp3s have to have the same formats - I had one mp3 at 16000hz and another at 44000hz - they merged without error, didnt play - it wasn't until I used mp3val to see the issues and I had to re-encode my source files - then they merged fine – Rob – 2013-10-28T13:37:51.843

13

Mp3Wrap - Command-line utility that wraps multiple MP3 files into a single, playable MP3, without losing filenames or ID3 information, and without reencoding. Also supports archiving non-audio data such as playlists, info files, and cover images inside the MP3. These files can be unpacked later (using mp3splt, e.g.); ordinary MP3 decoders can play the entire audio stream as one long track.

lig

Posted 2011-07-23T21:19:26.970

Reputation: 266

This no longer works property - try concatenating a few files and inspect the output file in mp3val - it will report sync errors. This means that it will not play after the first chunk in Chrome 64+ (see https://bugs.chromium.org/p/chromium/issues/detail?id=794782 and https://bugs.chromium.org/p/chromium/issues/detail?id=806601#c10)

– Housemd – 2018-02-04T06:24:18.920

Doesn't seem to work on Windows. No metadata preserved. – Gruber – 2019-06-04T06:31:04.357

1Thanks for this recommendation.. much lighter/simpler solution then ffmpeg, since I was combining 100+ mp3's and wanted the option of splitting back to originals. – joshbaptiste – 2014-05-17T02:29:53.140

11

This will concatenate a folder full of MP3 into a single MP3 file:

1) Save a list of the MP3 files to concatenate, e.g.,

$ cat mylist.txt
file '/tmp/01.mp3'
file '/tmp/02.mp3'
file '/tmp/03.mp3'
file '/tmp/04.mp3'
file '/tmp/05.mp3'
file '/tmp/06.mp3'
file '/tmp/07.mp3'

2) Run the following command (-safe 0 is not required if mylist.txt uses relative paths instead):

$ ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp3

Miles Wolbe

Posted 2011-07-23T21:19:26.970

Reputation: 508

4

If you want to concat all mp3 files of the current directory:

function join_by { local IFS="$1"; shift; echo "$*"; }
files=(*.mp3)
ffmpeg -i "concat:`join_by "|" $files`" -acodec copy output.mp3

hansaplast

Posted 2011-07-23T21:19:26.970

Reputation: 273

1

Based on Miles Wolbe's answer, here is a one-liner that worked for me:

ls *.mp3 | \
    sed -e "s/\(.*\)/file '\1'/" | \
    ffmpeg -protocol_whitelist 'file,pipe' -f concat -i - -c copy output.mp3

Andy Balaam

Posted 2011-07-23T21:19:26.970

Reputation: 90

0

zsh:

audio-join() ffmpeg -i "concat:${(j:|:)@[2,-1]}" -acodec copy $1

audio-join output.mp3 *.mp3

HappyFace

Posted 2011-07-23T21:19:26.970

Reputation: 224

We expect answers to include explanations.  (Not all of the answers on this page are good examples.) – Scott – 2019-08-10T21:55:20.397

0

If you need scripting, you're probably better off using the ffmpeg solution. However, if you ever just need an application to do stuff like that, you could try out Audacity. It's open source and cross platform. I haven't used it to join mp3s, but I've used it to crop sections out of an mp3 and fade them out at the end. I'm be willing to bet you can join mp3s and cross-fade them into each other with it as well.

Brandon

Posted 2011-07-23T21:19:26.970

Reputation: 644

4Audacity, as far as I know, works on the raw sound data, and concatenating mp3's with it will result in a degradation due to transcoding. – Ambroz Bizjak – 2011-07-23T22:28:59.520