2

I am trying to concatenate several media files. Each file contains video and audio. I need to add black-screen silent frames after each mediafile (except last one).

I mean file + gap + file + gap + file.

I found very useful answer here, but it works for audio only.

Then I changed command. It works too, but with no sound. Here is the command:

ffmpeg -i video-1489396334138.webm -i video-1-1489396340932.webm -i video-1489396346168.webm -f lavfi -i "color=c=black:s=640x480:r=25" \ -filter_complex " [3]trim=duration=3.932[g0];[3]trim=duration=3.168[g1]; [0][g0][1][g1][2]concat=n=5:v=1:a=0" output.webm

I know it's because of a=0. But when I change it to a=1, ffmpeg throws an error:

[Parsed_trim_0 @ 0x4448080] Media type mismatch between the 'Parsed_trim_0' filter output pad 0 (video) and the 'Parsed_concat_2' filter input pad 1 (audio)

[AVFilterGraph @ 0x4447600] Cannot create the link trim:0 -> concat:1

Probably that's because our lavfi input does not contain any audio. So, the question is, how to fix that?

frutality
  • 133
  • 1
  • 5

3 Answers3

1

You'll have to add and trim the audio segments as well, plus add the audio from the source videos i.e.

Add -f lavfi -i anullsrc to the inputs.

Trim these

[4]atrim=duration=3.932[a0];[4]atrim=duration=3.168[a1];

And add all audio to the concat filter.

[0:v][0:a][g0][a0][1:v][1:a][g1][a1][2:v][2:a]concat=n=5:v=1:a=1

Gyan
  • 34,439
  • 6
  • 56
  • 98
0

How to concatenate videos with a little 4 second silence intermission between each one overlaid over a image pic.jpg:

#Create a silence.mp4 file with audio silence over any given image: 
#note this is all one line with the backslashes removed.
ffmpeg -loop 1 -i /home/youruser/pic.jpg -f lavfi \
    -i anullsrc=channel_layout=5.1:sample_rate=48000 -t 4 \
    -c:v libx264 -t 4 -pix_fmt yuv420p -vf scale=480:320 -y silence.mp4

#transform silence.mp4 to silence.ts so that its type is 
#fluid and conforms to the original
ffmpeg -i silence.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts silence.ts

#loop over all the mp4 files numerically making sure '10' doesn't come before '2'
for f in $(ls ./*.mp4 | sort -V)
do
    #Transform all the given mp4 files to ts files.
    ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
    if [ $counter -eq 0 ]; then
        all_transport_files="$(basename $f.ts)"
    else 
        #inject the silence in between the previous and next
        all_transport_files="$all_transport_files|silence.ts|$(basename $f.ts)"
    fi
    #keep track of which one we're on, first is not like the others
    ((counter++))
    echo $counter
done

#final concate string with all videos in order, with silence between
concat_and_all_files="concat:$all_transport_files"

#Join all the ts files back together into output.mp4
ffmpeg -async 1 -i "$concat_and_all_files" -c copy -bsf:a aac_adtstoasc output.mp4

Voila, all your video files separated by some silence. If this isn't working for you, then you need to make sure the channel_layout, sample_rate, frame rate, and other attributes are identical for all videos and including the silence.mp4. Audio and Video codec hell is a landfill-fire of proprietary obfuscation and abuse from people trying to eliminate competitors from the video space.

Eric Leschinski
  • 6,828
  • 6
  • 45
  • 51
0

Following on previous answers, I ran this line:

ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -f lavfi -i anullsrc -f lavfi -i "color=c=black:s=1920x1080:r=30" -filter_complex \
       "[3]atrim=duration=1[ga1];[3]atrim=duration=1[ga2];[4]trim=duration=1[gv1];[4]trim=duration=1[gv2];
        [0:v][0:a][gv1][ga1][1:v][1:a][gv2][ga2][2:v][2:a]concat=n=5:v=1:a=1" -strict -2 out.mp4

because of my input files, I had to add the extra -strict -2 as suggested by a warning from ffmpeg.

As I needed it for many videos, I wrote python script calling ffmpeg: https://gist.github.com/marc-moreaux/87115abc57c36eb5a8bb46bc77e8a2ce

  • You must be using a really old `ffmpeg` for you to need `-strict -2` to encode AAC audio. The FFmpeg AAC encoder hasn't been considered experimental since early 2015. – llogan Feb 22 '19 at 18:20