13

I am using ffmpeg for Concatenating two MP3 files together,

I use this command :

ffmpeg -y -i first.mp3 -i second.mp3 -filter_complex "[0:0][1:0] amix=inputs=2:duration=longest" -c:a libmp3lame output.mp3

It works, but there is a little problem, the overlay together!

I want first song plays, and when it finished, second file starts (in output file)

But now they starts at the same time.

How can i change that command, to have a output that contains first song then when the first finished, second plays ?

Plus, I've tried concat command but not worked, I just can use something like what i sent.

Karami
  • 133
  • 1
  • 1
  • 4

3 Answers3

15

To skip re-encoding, use the concat demuxer:

Create a text file

file '/path/to/first.mp3'
file '/path/to/second.mp3'

and then

ffmpeg -f concat -i list.txt -c copy out.mp3

If re-encoding is fine,

ffmpeg -i first.mp3 -i second.mp3 -filter_complex [0:a][1:a]concat=n=2:v=0:a=1 out.mp3
Gyan
  • 34,439
  • 6
  • 56
  • 98
  • thanks but i couldn't use that, is there anyway to use `amix` command? – Karami Jun 04 '17 at 07:07
  • for example adding offset to second sound with using `amix` ? I don't know why `concat` doesn't work for me. – Karami Jun 04 '17 at 07:07
  • there are two commands - which didn't work? – Gyan Jun 04 '17 at 07:13
  • i've tried both of them but mainly i've no idea why they don't work, plus getting trace output is hard for me because i'm using php to doing it. – Karami Jun 04 '17 at 07:15
  • Is there anyway to add a delay to the second mp3 with `amix` ? – Karami Jun 04 '17 at 07:15
  • 1
    Yes, but you have to manually supply the duration, and volume will be reduced. `ffmpeg -i 1.mp3 -i 2.mp3 -filter_complex "[1]adelay=75000|75000[b];[0][b]amix" out.mp3` `75000` is the duration of 2.mp3 in milliseconds. – Gyan Jun 04 '17 at 07:18
  • Oh thanks soo much! is there anyway it copies metadata of 2.mp3 file too? attach it to the output. – Karami Jun 04 '17 at 07:30
  • 1
    Add `-map_metadata 1` – Gyan Jun 04 '17 at 07:31
  • Thanks , how about metadata for 1.mp3? And where to add it? at the last of command? – Karami Jun 04 '17 at 07:50
  • 1
    Add `-map_metadata 0`. if any fields clash, the last specified map_metadata will overwrite the first. – Gyan Jun 04 '17 at 08:00
  • @Mulvya: You should write this up as an answer if he insists on using `ffmpeg`. – dirkt Jun 04 '17 at 08:42
  • 2
    Add `-safe 0` before `-i` if see error: `[concat @ 0x7fcef3000000] Unsafe file name '/path/to/first.mp3' list.txt: Operation not permitted` – Honghao Z Jan 06 '19 at 02:37
  • The option with re-encoding is the only solution among those I tried that results in a correct merged mp3. Could you please supply a command line that allows to speed up re-encoding at the cost of lower sound quality in the output mp3? – AlwaysLearning Feb 08 '19 at 11:48
  • The ffmpeg -copy leaves a tiny glitch (in the one test I tried). The various "cat" methods probably work correctly only when there are no ID tags. With ID info, on the tests I tried, the bigger file looked to various programs like the first file ... and stopped playing at the same length as the first file. I haven't found a good command line solution for the Mac yet, but www.audio-join.com did a nice job of joining two tracks with ID tags for me. (ffmpeg also added 3 seconds of silence at the end for some reason!) – Stan Sieler Apr 20 '20 at 06:35
  • In my comment above, typo...should be www.audio-joiner.com. I've also tried using 'sox' ... has the same problem as ffmpeg (glitch & 3 extra seconds) – Stan Sieler Apr 20 '20 at 06:58
7

Usually,

cat first.mp3 second.mp3 > out.mp3

should just work. You didn't say what goes wrong when you try it.

Alternatively, you can use mp3wrap:

mp3wrap out.mp3 first.mp3 second.mp3 third.mp3 ...

This doesn't re-encode the MP3s like ffmpeg would, it keeps the ID3 tags, and you can split the files again later with mp3split.

I advise against using ffmpeg or similar programs, because reencoding causes loss of quality.

dirkt
  • 16,421
  • 3
  • 31
  • 37
  • You're right ffmpeg is not good , but i have to currently. Is there anyway to use ffmpeg right now? concat doesn't work for me. I have to use `amix` format – Karami Jun 04 '17 at 07:08
  • if you don't re-encode doesn't that mess up the timecode? – niico Dec 11 '19 at 15:14
  • This is the fastest way but will need some tweaking if you have spaces in the filename. – lacostenycoder Aug 07 '20 at 16:45
  • 1
    I know that the question has `ffmpeg` in the title but this solution is so simple and straightforward that it should deserve more praise. Have been using Linux for a while but realizing the real potential of `cat` has eluded me until now. `cat * > out.mp3` worked like a charm, thanks – toraritte Oct 08 '20 at 14:10
  • Perfect solution that will work on 99% of systems with minimal fuss and no encoding issues – crmpicco Jan 15 '21 at 13:42
  • 1
    what does this do to mp3 tags? – cannyboy Nov 23 '21 at 11:08
  • @cannyboy The ID3v1 record is located at the end of the file, the ID3v2 records are at the beginning. So tags from the last (ID3v1) resp. first (IDv2) file should be valid for the final file. Tags copied from other files will be ignored in the best case (and might cause trouble in the worst case), so better remove them first. – dirkt Nov 23 '21 at 12:12
  • "_This doesn't re-encode the MP3s like `ffmpeg` would_" `ffmpeg` wouldn't if you set `-c copy`. – Geremia Dec 23 '21 at 23:24
2

you could also use cat [your files] > concat.mp3 which will produce a mp3 file that has multiple headers and ID3 tags. Afterwards you can correct this by invoking mp3val -f -nb concat.mp3.