例如
// Common overlay image input
String commonOverlayImage = '/storage/emulated/0/DCIM/Camera/IMG_20230530_201349.jpg';
// First execution
String backgroundImage1 = 'storage/emulated/0/DCIM/Camera/IMG_20230530_201359.jpg'; // width: 2448, height: 3264
String outputPath1 = '/storage/emulated/0/DCIM/Camera/outputPath1.jpg';
String command1 = '-y -i $backgroundImage1 -i $commonOverlayImage -filter_complex "[1:v]scale=500:500[ovrl];[0:v][ovrl]overlay=100:100" $outPutPath1';
// Second execution
String backgroundImage2 = '/storage/emulated/0/Download/images.jpeg'; // width: 736, height: 272
String outputPath2 = '/storage/emulated/0/DCIM/Camera/outputPath2.jpg';
String command2 = '-y -i $backgroundImage2 -i $commonOverlayImage -filter_complex "[1:v]scale=500:500[ovrl];[0:v][ovrl]overlay=100:100" $outPutPath2';
前两次执行工作正常,但问题是 commonOverlayImage 在宽度、高度和位置上看起来也不同,但它们具有相同的值,这是因为 backgroundImage1 和 backgroundImage1 尺寸不同
我怎样才能使它们固定并且无论 backgroundImage 尺寸是多少总是看起来一样
答案1
ffmpeg -i "2448x3264.jpg" -i "1920x1920.jpg" -lavfi "
[1][0]scale2ref=w=iw*500/2448:h=iw*500/2448[l][b];
[b][l]overlay=w*100/500:h*100/500
" out.jpg -y
iw
→ 输入宽度 = 2448,因此w
→ 输出宽度 = 2448 * 500 / 2448 = 500,与scale=500:500
如果输入宽度 = 736,则输出宽度 = 736 * 500 / 2448 = 150,与scale=150:150
如果添加旋转:
ffmpeg -i "736x272.jpg" -i "1920x1920.jpg" -lavfi "
[1][0]scale2ref=iw*500/2448:iw*500/2448[l][b];
[l]pad='hypot(iw,ih)':ow:-1:-1:color=black@0,rotate=1:c=none[l];
[b][l]overlay=W*500/2448*350/500-w/2:x
" out3.jpg -y
其中 350 = 100 + 500 / 2 覆盖图片中心