如何精简并排拼接双视频的FFmpeg命令并提升性能
Great job putting together that working FFmpeg command! Let's walk through simplifying it and making it run faster without losing functionality.
Step 1: Trim Unnecessary Filter Steps
Your original command has redundant pad + crop combinations that can be eliminated by calculating the direct crop coordinates on the scaled input videos. Here's the simplified version:
ffmpeg -y -i i1.mov -i i2.mp4 -filter_complex \ "[0:v]scale=1824:1026,crop=1641:923:170:52,scale=1920:1080[vl]; \ [1:v]scale=2016:1134,crop=1641:923:57:106,scale=1920:1080[vr]; \ [vl][vr]hstack=shortest=1" \ -c:v libx264 -crf 23 -preset veryfast output.mp4
What Changed:
- Removed redundant
padfilters: The originalpadfollowed by acropwas unnecessary. Instead, we adjusted thecropcoordinates to target the exact region you wanted directly on the scaled input, skipping the intermediate padding step. - Merged sequential
cropsteps: For the second input, we combined two back-to-backcropoperations into a single one by calculating the absolute coordinates from the original scaled video.
If you want even more brevity (while keeping readability), you can factor out repeated scale targets using filter variables, though for two inputs, it's often clearer to keep each chain explicit.
Step 2: Performance Optimizations
Now let's make this command run faster:
Reorder Filters for Efficiency:
Whenever possible, docropbeforescale. Cropping reduces the number of pixels early in the pipeline, which means less work for the subsequentscalefilter. If your source videos allow it (i.e., you can crop the original footage to the desired region first), adjust the chain to crop the original input before scaling—this cuts down on pixel processing upfront.Use Hardware-Accelerated Encoding:
If you have compatible hardware (NVIDIA, AMD, Intel), swaplibx264with a hardware encoder for massive speed gains:- NVIDIA:
-c:v h264_nvenc -preset p7(adjust preset fromp1(slow/best) top7(fast/good)) - Intel:
-c:v h264_qsv -preset veryfast - AMD:
-c:v h264_amf -preset fast
- NVIDIA:
Tweak Preset & CRF:
You're already usingveryfast, which is a great balance. If you need more speed, trysuperfastorultrafast(minor quality hit). To compensate, lower yourcrfvalue by 1-2 (e.g.,crf 21) to retain similar visual quality.Leverage Threading:
Add-threads autoto let FFmpeg use all available CPU cores efficiently (most modern versions do this by default, but it never hurts to explicitly set it).Skip Unnecessary Scaling:
If your final output doesn't need two full 1920x1080 streams side-by-side, scale each input to half the width first, thenhstack—this reduces the total pixel count processed. For example, scale each input to 960x1080,hstackgives you 1920x1080 total, avoiding scaling to 1920x1080 per input.
Final Optimized Command Example (with NVIDIA Encoding)
ffmpeg -y -threads auto -i i1.mov -i i2.mp4 -filter_complex \ "[0:v]scale=1824:1026,crop=1641:923:170:52,scale=960:1080[vl]; \ [1:v]scale=2016:1134,crop=1641:923:57:106,scale=960:1080[vr]; \ [vl][vr]hstack=shortest=1,scale=1920:1080" \ -c:v h264_nvenc -crf 22 -preset p7 output.mp4
This version is cleaner, runs faster, and maintains your desired output quality.
内容的提问来源于stack exchange,提问作者Zurechtweiser

