0

suppose I have MP3 of around 1 minute duration and increase its duration longer.Duration can be any.

I want to randomly add N seconds of silence to that audio.

I can generate the silence like below

ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t <seconds> -q:a 9 -acodec libmp3lame out.mp3

And I can insert silence to audio like below

ffmpeg -i in.wav -filter_complex "anullsrc,atrim=0:4[s];[0]atrim=0:14[a];[0]atrim=14,asetpts=N/SR/TB[b];[a][s][b]concat=3:v=0:a=1" out.wav

Is there anyway to insert silence at multiple areas.With php I can generate random time to insert based on 60 seconds audio like below.

$arr = [];
$arr[3] = 2; //here 3 is original audio insert time and 2 is the duration of silence
$arr[6] = 3;  //here 6 is original audio insert time and 3 is the duration of silence
$arr[10] = 1;
$arr[12] = 3;

Now I want to insert silence between seconds based on above array like

2 seconds silence at 3rd second
3 seconds silence at 6th second
1 second silence at 10th second

and son on..

Gracie williams
  • 1,287
  • 2
  • 16
  • 39

1 Answers1

2

I assume the location and duration of silences are known to you before ffmpeg execution.

Using as an example,

2 seconds silence at 3rd second
3 seconds silence at 6th second
1 second silence at 10th second

you would run

ffmpeg -i in.wav -af "asetpts='PTS+gte(T\,2)*2/TB+gte(T\,5)*3/TB+gte(T\,9)*1/TB',aresample=async=1" out.wav

The asetpts filter preserves the timestamps of the interval 0 to 2. It increases timestamps of all frames with timestamp 2s or higher by 2 seconds. It further adds 3 seconds to all frames with timestamp >= 5, and another second to all timestamps 9 and higher. T refers to source timestamps.

The aresample filter then plugs all timestamp gaps with silence.

Gyan
  • 85,394
  • 9
  • 169
  • 201