96

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.

Robotnik
  • 2,645
Manu
  • 1,203
  • 2
  • 9
  • 9

8 Answers8

77

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

For example if you have five input files, use n=5 and add [4:0].

DAG
  • 103
evilsoup
  • 14,056
66

I think the best option for wav 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?

If you have many wav files in the current dir

$ sox '*' long.wav

Warning, the last file named in sox gets overwritten, so in this case - if long.wav exists, it will be destroyed and then made new from the others.

bmike
  • 3,070
26

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
  • 235,242
8

Another option is to use concat

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

5

I seen people posting similar answers, but never this exact answer which works great.

sox "file*" output.wav

The above command will combine all the files and use the * symbol as a wildcard. You use the quotes to avoid having your shell perform the expansion. Instead we let sox perform the expansion that way any of your parameters will be applied to every audio file.

Goddard
  • 201
4

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
  • 1,036
0

Because ffmpeg was taking too long using filter-complex, I switched to ecasound (apt install ecasound):

ecasound -f:s16_le,1,44100 -a:1 -i:playat,0.5,"0001.wav" \
                           -a:2 -i:playat,6.16,"0002.wav" \
                           -a:3 -i:playat,12.57,"0003.wav" \
                           -a:all -t:-1 -o "output.wav"

ffmpeg took 110 seconds to combine 500 wav files of 2 second each, ecasound took 8 seconds.

-1

You could use shntool for wav-files.

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