KevCaz's Website

Yesterday I needed to combine several audio files and I figured out to do this with command lines. It was not as easy as I expected but I guess I underestimated how little I know about audio files! Anyway, below I explain what I did!

Preparing my audio files

I had several audio files that I recorded with the voice recorder available on my phone. This application saves audio files as .m4a, so basically I had a bunch of .m4a files!

Between every file I recorded I needed to add a blank, so instead of recording a blank, I created blank of 30 seconds with FFmpeg (see this gist):

1
ffmpeg -f lavfi -i anullsrc=r=11025:cl=mono -t 30 -acodec aac out.m4a

Then I had to so some cuts. For instance, I needed to only use the first 1min 30sec part2_all.m4a, so I used FFmpeg one more time:

1
ffmpeg -ss 0 -t 90 -i part2_all.m4a part2c.m4a

and so I obtained part2c.m4a. That being done I had all the files I needed.

Concatenate the files

That was the tricky part, cause it is not super easy to concatenate audio files because of codecs and timestamps… As all my files where all in .m4a I thought I woul be able to use the FFmpeg to concatenate files with same codecs, but it did not work. I try to use other formats but it did not work either. Fortunately, I stumbled into the following answer that gives valuable information about this. The solution proposed is to convert files to Pulse-code modulation, then use a simple file concatenation with cat (cause this is possible with such files) and then convert the file thereby obtained back to .m4a. So this is what I have done:

1
2
3
4
5
6
7
8
9
# convertion to pcm 
for f in *.m4a
do 
  ffmpeg -i ${f%.*}.m4a -c:a pcm_s16le -ac 2 -ar 48000 -f s16le ${f%.*}.pcm
done
# concatenate files 
cat blank.pcm part1.pcm blank.pcm part2c.pcm part2f.pcm blank.pcm part3.pcm blank.pcm part4.pcm blank.pcm > final.pcm
# back to .m4a
ffmpeg -f s16le -ac 2 -ar 48000 -i final.pcm -c:a aac -b:a 192K -ac 2 final.m4a

and so I got final.m4a 💥!

Two last remarks. First, I have also tried with .aac files following this answer , but it did not work. And again I can just blame myself for not knowing enough about this, so I still do really know why… I guess it means that I can learn a lot about audio files! Second, I would like to mention SoX that looks like a very handy tool for this kind of manipulation, but I did not test it yet! Maybe next time!