First you need to know the parameters of the video file:

ffmpeg -i file.avi 

I have this:

 Input #0, avi, from 'file.avi': Metadata: encoder : Lavf56.36.100 Duration: 00:10:38.49, start: 0.000000, bitrate: 814 kb/s Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 672 kb/s, 25 fps, 25 tbr, 25 tbn, 50 tbc Stream #0:1: Audio: aac (LC) ([255][0][0][0] / 0x00FF), 48000 Hz, stereo, fltp, 128 kb/s 

.avi from .MTS get this:

 for invideo in *.MTS; do ffmpeg -i "$invideo" -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 "Converted/${invideo%%.MTS}.avi"; done; 

Next, you need to make a video of the pictures with the same parameters.

In the graphical editor, create pictures Begin.png and End.png with a resolution of 1280x720.

Create a video from pictures:

 ffmpeg -loop 1 -i Начало2.png -pix_fmt yuv420p -vframes 501 -vf "fade=out:899:1:alpha=1" -sar 1:1 -aspect 16:9 -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 Начало2.avi ffmpeg -loop 1 -i Окончание2.png -pix_fmt yuv420p -vframes 501 -vf "fade=out:899:1:alpha=1" -sar 1:1 -aspect 16:9 -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 Окончание2.avi 

Overlay audio track:

 ffmpeg -i 00019.wav -i Начало2.avi -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 Начало_звук.avi ffmpeg -i 00019.wav -i Окончание2.avi -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 Окончание_звук.avi 

For simplicity, I took the audio track from a video file recorded by the same video camera in silence:

 ffmpeg -i 00019.MTS -c:a pcm_s16le -vn 00019.wav 

We cut to the desired duration:

 ffmpeg -i Начало_звук.avi -ss 00:00:00 -t 00:00:10 -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 Начало_10с.avi ffmpeg -i Окончание_звук.avi -ss 00:00:00 -t 00:00:10 -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 Окончание_10с.avi 

Glue to all files in the current directory:

 for invideo in *.avi; do ffmpeg -i concat:"info/Начало_10с.avi|$invideo|info/Окончание_10с.avi" -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 "${invideo%%.avi}_mod.avi"; done; 

I had errors when sticking because of different SAR, DAR and audio codec of the video files being glued.

I thank the distinguished Sergiks for the tips.

    1 answer 1

    The clips combined through concat must have identical sizes, codecs, pixel format. Where you create videos from the image after -i Начало.png try adding -pix_fmt yuv420p .

    For intermediate videos that you later merge, instead of h264 it is better to use a lossless codec, for example. ffvhuff . Those. translate the original video into this codec, create images in this codec, combine into this codec. And at the end separately, click on h264 - it will be faster and more qualitative. But it will take more disk space while running.

    You can create a video of the desired length from the image in one operation.

    • ffmpeg -loop 1 -i Начало.png -pix_fmt yuv420p -vframes 501 -vf "fade=out:899:1:alpha=1" -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 Начало.avi speed is unchanged relative to libx264, or even slower. With -pix_fmt yuv420p the same errors. I can not put here the full text of what I'm doing, says the length of the message is exceeded. - Ilya
    • ffmpeg -loop 1 -i Начало.png -pix_fmt yuv420p -vframes 501 -vf "fade=out:899:1:alpha=1" -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 Начало.avi ffmpeg -i 00018.wav -i Начало.avi -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 Начало_звук.avi ffmpeg -i Начало_звук.avi -ss 00:00:00 -t 00:00:10 -s hd720 -c:v libx264 -crf 23 Начало_10с.avi for invideo in *.avi; do ffmpeg -i concat:"info/Начало_10с.avi|$invideo|info/Окончание_10с.avi" -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 "${invideo%%.avi}_mod.avi"; done; here The end_10c.avi is doing the same. Line wrap does not display ... - Илья
    • And what will ffmpeg -i Начало_звук.avi and ffmpeg -i любойИз.avi ? - Sergiks
    • one
      Yes. Thank. Understood. There were different SAR, DAR, and audio codecs for the files being glued. - Ilya