Concatenate multiple WAV files using single command, without extra file

37

10

I want to concatenate multiple WAV files into a single WAV file using FFMPEG.

I have used the following command and it generates the required file.

Command:

ffmpeg -f concat -i mylist.txt -c copy output.wav

File :

#mylist.txt
file '1.wav'
file '2.wav'
file '3.wav'
file '4.wav'

But as you can see the problem is that I have to create a text file to specify the list of WAV files to concatenate.

I can do all these tasks, but I would prefer a single command something that looks like

ffmpeg -i 1.wav -i 2.wav -i 3.wav -i 4.wav output.wav 

or

ffmpeg -i "concat:1.wav|2.wav|3.wav|4.wav" -c copy output.wav

I have tried these two simple commands but they return just the voice of 1.wav Please help me write a single command( or correct the above 2 commands ) that achieves the desired result.

Please don't suggest other Media Encoders/Editors, I want to use FFMPEG only, as it is already installed and used at other places.

Manu

Posted 2013-04-24T08:09:52.040

Reputation: 523

Answers

38

You could try using the concat filter; it requires re-encoding, and so will take more system resources (a pretty tiny amount on any vaguely modern computer in this particular case), but PCM -> PCM audio should be mathematically lossless. In your case, you would use something like:

ffmpeg -i input1.wav -i input2.wav -i input3.wav -i input4.wav \
-filter_complex '[0:0][1:0][2:0][3:0]concat=n=4:v=0:a=1[out]' \
-map '[out]' output.wav

If you have five input files, use n=5.

evilsoup

Posted 2013-04-24T08:09:52.040

Reputation: 10 085

4Note: Don't customize [0:0][1:0][2:0][3:0] to match the number of files. Use that exactly as it says. The only thing you should change is the input files and n=4. – Keavon – 2015-03-07T22:40:23.523

9@Keavon: are you sure? Here ffmpeg (version N-49352-gc46943e x64) complains Too many inputs specified for the "concat" filter. when I try to process only 2 inputs with the original code. It works though when I remove [2:0][3:0]... which makes sense to me since in this case there are no such things as streams 2:0 and 3:0... – Bigue Nique – 2016-04-22T19:03:26.943

1I agree with @BigueNique. I too ran into the same error and though it feels a little silly, wrote some code to generate the proper sequence given n. – Ryan DuVal – 2019-05-12T18:54:37.483

20

I think the best option is to use sox, not ffmpeg:

$ sox short1.wav short2.wav short3.wav long.wav

Solution comes from How do I append a bunch of .wav files while retaining (not-zero-padded) numeric ordering?

Héctor M. Monacci

Posted 2013-04-24T08:09:52.040

Reputation: 311

1This may work for .wav, but sox does not seem to support .mp3. – Aaron Franke – 2018-03-07T19:46:14.997

1Question was precisely about .wav – Héctor M. Monacci – 2018-03-16T07:13:28.240

1I know, I'm just adding a note for anyone who tries to use this method with non-wav files. – Aaron Franke – 2018-03-16T07:20:43.443

the question explicitly stated that a solution is required using ffmpeg only! – Manu – 2018-04-03T08:00:32.747

sox can support .mp3 files too – Benjamin – 2018-05-28T18:55:35.720

Be sure to put your desired output-file-name on the end of that argument list or you'll overwrite the last file listed! Luckily, I had another copy. Pressing enter too soon with sox is dangerous. It worked good for me, though. Thanks Héctor! – LonnieBest – 2018-09-26T06:00:41.850

11

The FFmpeg wiki mentions using the concat protocol is not possible with all file types. It works fine with most MPEG containers and bitstreams, but obviously not WAV files with PCM audio.

You don't necessarily have to create a temporary file and use that. With Bash (or other shells that support process substitution), you can do everything in a single command:

ffmpeg -f concat -i <( for f in *.wav; do echo "file '$(pwd)/$f'"; done ) output.wav

The process substitution <( ) creates a file—or, to be precise, a file descriptor—on the fly, which ffmpeg can read. This file will contain the path to every .wav file in the current directory. We have to prefix it with $(pwd) to get the correct path relative to your current directory, and not relative to the file descriptor.

Of course, the command within the substitution can be changed to something else.

slhck

Posted 2013-04-24T08:09:52.040

Reputation: 182 472

2

If you get Unsafe file name error you would need -safe 0 option before -i. ref. https://stackoverflow.com/q/38996925/297679

– Nobu – 2017-11-27T21:07:48.057

2

Another option is to use concat

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

Ivan Malyshev

Posted 2013-04-24T08:09:52.040

Reputation: 208

1

Welcome to superuser: While this may or may not answer the question,(an answer has been accepted) it would be a better answer if you could provide some explanation why your solution works with detail and an easy to follow instructions. If you feel your answer is correct do these things and re-edit. Please take a couple of minutes and read:- http://superuser.com/help .Answering: http://superuser.com/help/how-to-answer, again welcome to superuser.Thankyou

– mic84 – 2018-03-24T07:34:01.193

2I tested this and it does not appear to work – Nick Long – 2019-08-07T13:56:55.477

2This does not work. It only ends up using the first file. – Brad – 2019-12-28T21:43:43.630

I also tested it and it also only ends up using the first file. – MikeSchinkel – 2020-02-20T04:45:08.287

1

In Win8's Cmd.EXE .BAT script:

Rem Get number of source files
For /F %%A In ('Dir *.3gp /B /A-D ^| Find /C /V ""') Do      Set FilCnt=%%A

Rem Build list of source filenames
Set Lst=
For    %%A In (     *.3gp                          ) Do Call Set Lst=%%Lst%% -i %%A

Rem Concat and Convert sources to target
FFMPeg.EXE %Lst% -filter_complex concat=n=%FilCnt%:v=0:a=1 -vn Output.OGG

This way, you don't bother with the source file names or the concat-count parameter.

Bilbo

Posted 2013-04-24T08:09:52.040

Reputation: 41

-1

You could use shntool for wav-files.

shnjoin -r none 01.wav 02.wav ...

Patrik Wallander

Posted 2013-04-24T08:09:52.040

Reputation: 11