Sandra Schlichting
Sandra Schlichting

Reputation: 25996

How to script this, so output is used as input?

I would like to script this command

ffmpeg -i concat:file1.mp3\|file2.mp3 -acodec copy output.mp3

which merges file1.mp3 and file2.mp3 to become output.mp3.

The problem is that I have a lot more than 2 files that I would like to merge.

Example

ffmpeg -i concat:file1.mp3\|file2.mp3 -acodec copy output1.mp3
ffmpeg -i concat:output1.mp3\|file3.mp3 -acodec copy output2.mp3
ffmpeg -i concat:output2.mp3\|file4.mp3 -acodec copy output3.mp3
ffmpeg -i concat:output3.mp3\|file5.mp3 -acodec copy output4.mp3

output4.mp3 is the result I am looking for.

The files are not actually nicely called "file" adn then a number, but ls lists them in the order they should be merged in.

Question

How can this be scripted, so I can execute it in a directory with either an even or odd number of files?

Upvotes: 2

Views: 171

Answers (2)

Op De Cirkel
Op De Cirkel

Reputation: 29493

if ffmpeg supports more then two files and no file contains |, and there are not too many, you can do: ffmpeg -i concat:"$(ls|tr '\n' '|')" -acodec copy out.mp3

if not:

for cfile in *.mp3; do
  ffmpeg -i concat:myout.mp3tmp1\|$cfile -acodec copy myout.mp3tmp2
  mv myout.mp3tmp2 myout.mp3tmp1
done
mv myout.mp3tmp1 <your final file name>

Upvotes: 3

Kerrek SB
Kerrek SB

Reputation: 477150

If you can just concatenate all files in one wash, that'd be best. But a generic answer for your Bash question:

ffmpeg -i concat:file1.mp3\|file2.mp3 -acodec copy output1.mp3

for i in $(seq 1 10); do
    ffmpeg -i concat:output${i}.mp3\|file$((i + 2)).mp3 -acodec copy output$((i + 1)).mp3
done

Here 10 is two less than your total number of input files.

Upvotes: 1

Related Questions