18

I have a couple of M4A (sound) files on a Mac that I want to combine into a single sound file. Can this be done with a Terminal command? Is there such a thing?

Hahnemann
  • 4,378
  • 6
  • 40
  • 64

5 Answers5

29

Yeah, ffmpeg doesn't work, contrary to what the internet echo chamber will tell you. At least not that way. It's incredible how many have to drool their wisdumb and waste everyone's time.

Here. Prove me wrong with a link, but this is what you want and this is the only place you'll see it. Tres simple.

ffmpeg -i file1.m4a -acodec copy file1.aac

ffmpeg -i file2.m4a -acodec copy file2.aac

cat file1.aac file2.aac >>filenew.aac

ffmpeg -i filenew.aac -acodec copy -bsf:a aac_adtstoasc filenew.m4a

I compiled my own ffmpeg for the extra libs, so I hope that that is one of the default ones. If not, it's definitely worth the hassle. I haven't been able to validate the above on a second system, but on my old Hardy Heron Ubuntu grunt system the joined file has all the right m4a meta data and tags and there is no change in audio quality.

methode
  • 5,348
  • 2
  • 31
  • 42
Kafaraqgatri
  • 422
  • 4
  • 3
  • This only worked for me when I didn't skip any steps ;) –  Nov 06 '15 at 01:10
  • 1
    Step-by-step and works; this should be the accepted answer. – Quetzalcoatl Feb 10 '18 at 17:07
  • In Mar 2018 this worked for me as a way to convert multiple m4a files to into a continuous recording in mp3 format. The final line was `ffmpeg -i audio-all.aac -acodec libmp3lame audio-all.mp3`. – Scott Veirs Mar 22 '18 at 03:47
  • Doesn't work with ffmpeg verrsion 3.4.2 (June 2018); the files from the first commands to create aac files are zero size. The concat method linked to in Nick Haddad's answer worked for me. – nickcrabtree Jun 12 '18 at 07:02
  • 1
    Works as prescribed with ffmpeg version 4.2.2 -- installed using Homebrew. – gpano Mar 20 '20 at 08:43
  • sadly, the `.m4a` files cannot simply be renamed to `.aac`, in case someone else wondered – Magne May 19 '21 at 20:01
  • Also works with the concat protocol, instead of external concatenate with `cat` program: `ffmpeg -i "concat:input1.aac|input2.aac|input3.aac" -c copy -bsf:a aac_adtstoasc filenew.aac`. Note that my output is to aac format: all players could play the result very well in m4a too, except Audacious 3.9, which decided to play back at half the speed. – db-inf Dec 02 '21 at 10:53
6

FFMPEG can help you with this. Check out their How To Contatenate media files article.

Nick Haddad
  • 8,767
  • 3
  • 34
  • 38
  • 3
    I installed FFMPEG using Brew and ran the following command: `ffmpeg -i "concat:first.m4a|second.m4a" -c copy out.m4a` However, `out.m4a` contains only `first.m4a`. The sound `second.m4a` is missing. The same occurs when I do this via the `cat` command. – Hahnemann Aug 26 '13 at 13:05
  • 4
    The other method: `ffmpeg -f concat -i <(for f in ./*.m4a; do echo "file '$PWD/$f'"; done) -c copy output.m4a` did work. – Rmano Jul 01 '15 at 19:59
  • 1
    @Rmano it gives me error like `[concat @ 0x7ff8ec802600] Unsafe file name '/.../audio/1.m4a' /dev/fd/63: Operation not permitted` – He Yifei 何一非 Aug 05 '17 at 14:10
  • 3
    @Arefly In newer versions of ffmpeg you need to add the `-safe 0` parameter. – Wowfunhappy May 08 '18 at 23:16
  • 2
    The `concat` option in conjunction with `-safe 0` worked for me. In fact, my final formulation used process substitution as suggested by @Rmano. – wjv Jan 27 '20 at 07:37
  • Turned the method from these comments into a separate answer: https://stackoverflow.com/a/75740382/2958070 – Ben Mar 15 '23 at 03:54
2

github.com/mifi/lossless-cut is for audio/video editing using ffmpeg. Works fine for merging mp3/mp4/m4a with drag and drop.

sudo snap install losslesscut

1

Are you trying to have the audio playback one after another, or playback on top of one another at the same time?

Either way FFmpeg can do the trick. The way I read your question, you want them playing back at the same time... If that's the case, try:

ffmpeg -i [input1] -i [input2] -filter_complex "[0:a][1:a]amerge=inputs=2[a]" -map [a] -c:a libfdk_aac out.m4a

Note: this is going to compress the audio to AAC. It is one of the best audio encoders, so you won't really notice a difference, but if you need to maintain quality, you can always go uncompressed with pcm_s16le. However, I think you would then be better off going out to a .wav file.

occvtech
  • 453
  • 3
  • 10
1

Gleaned from the comments of https://stackoverflow.com/a/31033308/2958070:

ffmpeg -f concat -safe 0 -i <(for f in ./*.m4a; do echo "file '$PWD/$f'"; done) -c copy output.m4a

Here's the version of my ffmpeg that brew installed

$ ffmpeg -version
ffmpeg version 5.1.2 Copyright (c) 2000-2022 the FFmpeg developers
built with Apple clang version 14.0.0 (clang-1400.0.29.202)
configuration: --prefix=/usr/local/Cellar/ffmpeg/5.1.2_4 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
libavutil      57. 28.100 / 57. 28.100
libavcodec     59. 37.100 / 59. 37.100
libavformat    59. 27.100 / 59. 27.100
libavdevice    59.  7.100 / 59.  7.100
libavfilter     8. 44.100 /  8. 44.100
libswscale      6.  7.100 /  6.  7.100
libswresample   4.  7.100 /  4.  7.100
libpostproc    56.  6.100 / 56.  6.100
Ben
  • 5,952
  • 4
  • 33
  • 44