13

I have 33 audio files, each about 11 seconds long, and I'd like to merge them into one lossless file. How can I do this efficiently (i.e. without cut-pasting in Audacity 33 times)?

5 Answers 5

9

You can do this with ffmpeg and sox:

for i in *.mp3 *.ogg *.flac
do
  ffmpeg -i "$i" "$i.wav"
done

sox *.wav combined.wav
2
  • This worked perfectly! Thank you for the help.
    – ashastral
    Commented Aug 1, 2010 at 0:11
  • 1
    sox doesn't work for OGG: /usr/local/Cellar/sox/14.4.2_1/bin/sox FAIL formats: no handler for detected file type 'opus'
    – The Onin
    Commented May 7, 2018 at 22:18
11

Assuming you want to merge them alphabetically, by filename:

for f in ./*.{ogg,flac,mp3}; do echo "file '$f'" >> inputs.txt; done
ffmpeg -f concat -i inputs.txt output.wav

The for loop puts all the filenames in a file called inputs.txt, one-per-line, and the second one uses ffmpeg's concat demuxer to merge the files. It is possible to use printf instead of the loop like so:

printf "file '%s'\n" ./*.{ogg,flac,mp3} > inputs.txt

Assuming a modern shell, you can also use command substitution to do the whole thing in a single line.

ffmpeg -f concat -i <(printf "file '%s'\n" ./*.{ogg,flac,mp3}) output.wav
7
  • 1
    I believe this is the 'correct' ffmepg answer as it will not re-encode anything. Commented Aug 28, 2013 at 0:59
  • Does not work if a file name contains ' Commented Nov 11, 2014 at 6:21
  • ^ I'm talking about 1st command, didn't try other two yet. Commented Nov 11, 2014 at 8:27
  • 1
    I get an error when I attempt the one-liner, but this works for me: ffmpeg -f concat -safe 0 -i <(for f in ./*.flac; do echo "file '$PWD/$f'"; done) output.flac
    – Peter
    Commented Apr 10, 2020 at 22:02
  • a) Edit the file inputs.txt if some files of the type (ogg,flac,mp3) do not exist. b) If 'Unsafe file name' is reported then add the following parameter : -safe 0 example : ffmpeg -f concat -safe 0 -i inputs.txt output.flac Commented Feb 15, 2023 at 13:41
4

If you start with only lossless files, you can use use shntool:

shntool join *.flac
1
  • I get this error: shntool [join]: warning: unsupported format 0xfffe (Unknown) while processing file: [file1.flac]
    – bonh
    Commented Sep 14, 2019 at 4:06
2

However, be aware that the shntool join will insist on joining them in collating sequence (alphabetical) order EVEN IF you specify them otherwise.

shntool join fileB.flac fileA.flac

will put A before B.

You can either rename the files first or use the -r parameter ("-r ask" will prompt for the order).

Frankly I find this irritating...

Also you can force the output mode, so if you're joining FLACs:

shntool join *.flac -o flac

will result in joined.flac rather than joined.wav

1
  • -r none will turn off the reordering.
    – Tim Smith
    Commented Jul 28, 2023 at 7:18
1

It seems that the Sound Juicer that comes with Ubuntu writes broken FLAC files, which result in no MD5 signature in the file. MAKE A COPY of the directory containing the files you want to concatenate, then run the script below.
echo fixing broken FLAC files
find . -type f|grep .flac$ |while read file
do
flac -f --decode "$file" -o temp.wav
flac -f -8 temp.wav -o "$file"
done
rm temp.wav

Then run
shntool join *.flac
as above.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .