FFmpeg - Concenating audio files with spacing

2

Please look image:

enter image description here

Each audio clip has same codec/sample_rate, but has different duration and each space is also different. There is always space between clips, no overlapping. So there is no reason for reencoding, since this is basically a stream copy job. It is important because I should preserve original quality for each clip.

Now, lets think i found what i looked for. I gave this command a pseudo name "-start_at", it inserts the clip to audio stream at given time. With this pseudo command, i will set the time that each clip should begin. And rest of the job should be done by ffmpeg command/filter/script etc.

ffmpeg -start_at 00.00 -i audio1.opus  -start_at 06.30 -i audio2.opus  -start_at 21.15 -i audio3.opus  -start_at 26.35 -i audio4.opus

Just think ffmpeg will fill the gaps with silent sounds automatically.

So, Is there a ffmpeg command/filter for this? Or a ffmpeg gui/script etc.?

dandidondi

Posted 2015-06-23T05:02:55.783

Reputation: 73

Could you make 5 second silent audio file and just concatenate using the blank file as the spacer. – HSchmale – 2015-06-23T05:08:49.193

Answers

4

FFmpeg is capable of handling this. What you need to use is filter_complex with filter chaining. You can create a silent audio with aevalsrc. To create a 5 sec silent audio,

aevalsrc=0:d=5

So the following command will work for you.

ffmpeg -i input_audio_1 -i input_audio_2 -i input_audio_3 -filter_complex "
aevalsrc=0:d=10[s1];
aevalsrc=0:d=15[s2];
aevalsrc=0:d=20[s3];
[s2][1:a]concat=n=2:v=0:a=1[ac1];
[s3][2:a]concat=n=2:v=0:a=1[ac2];
[0:a][s1][ac1][ac2]amix=inputs=4[aout]" -map [aout] output_audio

Here I assumed each audio is having a length of 5 sec and re-encoding is optional like,

-c:a libmp3lame -ac 2 -b:a 128k

As amix cause overlapping of audios I have appended a silent audio to each of the input audio. You can also try amerge and adelay where doc itself has a clear explanation.

Hope this helps you!

Chamath

Posted 2015-06-23T05:02:55.783

Reputation: 412

Unfortunately there is no fixed spaces or fixed durations for clips. Also i dont want to do reencoding even if it does not affect quality much. I updated my first post, please take a look at picture. – dandidondi – 2015-06-23T07:22:46.443

I edited my answer. You can possibly skip the re-encoding. And also you can adjust the durations according to your inputs. This does not require equal durations. Still you need to calculate the durations and feed them to the command accordingly. You can use ffprobe to calculate the durations. – Chamath – 2015-06-23T07:46:01.670

but amix filter still does re-encoding. – dandidondi – 2015-06-23T15:49:25.430

2FYI: Re-encoding is a necessary evil whenever attempting to process source content through filters. -c copy attempts should result in Streamcopy requested for output stream 0:0, which is fed from a complex filtergraph. Filtering and streamcopy cannot be used together. messages. To preserve the source audio quality, output your audio to a lossless codec then pipe that through to a suitable encoder, e.g.: -map [aout] -c:a pcm_s16le -ac 2 -ar 44100 - | ffmpeg -i - -c:a libmp3lame -b:a 320k ConcatenatedAudio.mp3 – Mr. What – 2015-06-27T00:26:38.350

1

Here is audio concat example:

ffmpeg -y -f s16be -i /dev/zero -af "[in]anullsink;amovie=1.wav[a1];amovie=silence.wav[a2];amovie=2.wav[a3];amovie=4.wav[a4];[a1][a2][a3][a4]concat=n=4:v=0:a=1[out]" -shortest -t 12 output.wav

Notice couple of things you need to know here:

  1. I started with a clean input /dev/zero, it's nice, yet, can there is probably a nicer way of doing it.
  2. You need to know the total duration of the clip before making it (t=12)
  3. I premade the silence.wav file. You can use filters such as anullsrc (see nb_samples on the documentation) or other but this is quite pain-in-the-ass if you ask me.
  4. Sometimes, by using this approach, FFmpeg does not know when to stop, and yet, it will not create empty silence track since the -shortest param is applied.

E.G.

Posted 2015-06-23T05:02:55.783

Reputation: 310