Table of contents
3. Merge audio to insert blank
2. Video plus background image
This article will use several examples to introduce the comprehensive use of ffmpeg commands, mainly involving audio processing, video processing and audio and video synthesis.
1. Audio Merge
1. Get audio duration
ffprobe -i 1.mp3 -show_entries format=duration -v quiet -of csv="p=0"
2. Merge two pieces of audio
ffmpeg -i 1.mp3 -i 2.mp3 -filter_complex amerge -ac 2 -c:a libmp3lame -q:a 4 output.mp3
Parameter explanation:
-ac: Set the number of channels for the sound
-c:a: specify the audio encoder
libmp3lame: mp3 audio encoder
-q:a: Indicates the output audio quality, generally between 1 and 5 (1 is the highest quality)
3. Merge audio to insert blank
Merges three segments of audio, adding a one-second gap between each segment.
ffmpeg -i 1.mp3 -i 2.mp3 -i 3.mp3 -filter_complex "[1:a]adelay=1000[a1];[2:a]adelay=1000[a2];[0:a][a1][a2]concat=n=3:v=0:a=1" output.mp3
Parameter explanation:
[1:a]adelay=1000[a1]: The audio of the second segment material ([1:v] if it is a video), the playback is delayed by 1000 milliseconds, and the value is assigned to a1 for later use. In the same way, a2 is defined.
[0:a][a1][a2]concat=n=3: Combine the audio, a1 and a2 of the first material, and n=3 means three segments.
v=0:a=1: no sound, only audio.
2. Video plus background image
Put the horizontal version of the video in front of a 1080x1920 vertical version of the background, with the same distance up and down; then put a subtitle picture with a transparent background on the top of the house to achieve the following effects:
ffmpeg -i 1.mp4 -i bg.jpg -i font.png -filter_complex "[0:v]scale=1080:-1[video];[1:v][video]overlay=x=0:y=(1920-h)/2[v1];[v1][2:v]overlay" -codec:a copy output1.mp4
Parameter explanation:
[0:v]scale=1080:-1[video]: Only take the video of the first material, resize it into a width of 1080 according to the proportion, and assign the output value to video.
[1:v][video]overlay=x=0:y=(1920-h)/2[v1]: place the video on the upper layer of the background image, the position is x, y, and the output value is v1.
[v1][2:v]overlay: Place the video of the third material on top of v1.
-codec:a copy: Specifies the codec of the sound, which needs to use the name in the capability set list (the codec is set to "copy" to indicate that no codec is performed).
3. Audio and video synthesis
1. Preserve video sound
The original video has sound. While keeping the sound of the original video unchanged, add background sound and make the volume of the background sound smaller.
ffmpeg -i 1.mp4 -i 1.mp3 -filter_complex "[1:a]volume=0.3[a1];[0:a][a1]amix=inputs=2:duration=first[a]" -map 0:v -map "[a]" -c:v copy -c:a aac -y output.mp4
Parameter explanation:
[1:a]volume=0.3[a1]: the audio volume of the second material is 30%, and the output value is a1;
[0:a][a1]amix=inputs=2:duration=first[a]: the sound of the first material (video) is mixed with the background sound, the duration is the sound of the first material, and the output value is a;
-map 0:v: Video streaming splicing, there is only one video here;
-map "[a]": Audio stream splicing, here take the a just output;
-c:v copy: video encoding unchanged;
-c:a aac : audio encoding acc.
2. Do not keep video sound
Remove the sound of the original video, re-dub, and add 1 second of blank space before the sound.
ffmpeg -i 1.mp4 -i 1.mp3 -filter_complex "[0:v]trim=0:10.1[v];[1:a]adelay=1000[a];[v][a]concat=n=1:v=1:a=1" -c:v libx264 -c:a aac -movflags +faststart output.mp4
Parameter Description:
[0:v]trim=0:10.1[v]: The video takes 1-10.1 seconds, the unit here is seconds, and the output is assigned v;
[1:a]adelay=1000[a]: The audio demo is played in 1000 milliseconds, and the unit here is milliseconds;
[v][a]concat=n=1:v=1:a=1: Audio and video are combined, and the output requires video and audio;
-c:v libx264: use x264 for video encoding;
-c:a aac: audio encoding acc;
-movflags +faststart: This parameter is related to the metadata of mp4, setting it to faststart means that moov will be moved to the front of mdat, and it will be slightly faster when playing online.
4. Merge video
To merge multiple videos, you can first generate a video list file, such as merge_video.txt, with the following content:
file '1.mp4'
file '2.mp4'
file '3.mp4'
file '4.mp4'
Then use this file for video merging:
ffmpeg.exe -f concat -safe 0 -i merge_video.txt -c copy -y merge_video.mp4
Parameter Description:
-f concat: indicates splicing operation;
-safe 0: mark the level of file security check as 0, that is, do not consider security factors;
-c copy: the encoding remains unchanged;
-y: output file overwrite