基于Mobile-FFMPEG的视频多PNG图层缩放重定位效果不符预期
Mobile FFmpeg 叠加静态图片生成目标视频的命令修正方案
原命令的核心问题
- 参数顺序错位:将最终画布分辨率设置
-s 991x903放在输入参数段,按照FFmpeg的参数作用规则,这类输出侧参数会被错误作用于第一个输入的背景素材,而非最终输出,导致全链路画布尺寸计算偏移,所有叠加定位全部错位。 - 缩放逻辑错误:对叠加PNG的缩放计算错误使用
min(素材宽/2, 素材高/2)作为统一宽高值,会将不同宽高比的图片强制拉伸为固定正方形,出现严重变形、比例失真。 - 冗余滤镜干扰:所有素材附加的零角度
rotate滤镜无实际作用,在部分裁剪版的Mobile FFmpeg环境中,该滤镜的透明画布计算逻辑与scale滤镜不兼容,会给透明PNG带入黑边、错误修改素材实际宽高,进一步导致overlay定位偏移。 - 输入时长不匹配:所有叠加层PNG仅配置了
-loop 1无限循环,未与主背景保持一致的10秒总时长,会触发overlay的最短时长判定逻辑异常,出现叠加层提前消失、帧时序不同步的问题。 - 无效参数冗余:scale滤镜上的
eval=frame是为动态尺寸素材设计的逐帧计算参数,对静态图片完全无效,徒增移动端计算开销。
修正后的可直接运行命令
ffmpeg -y \ -loop 1 -t 10 -i /storage/emulated/0/InvitationTemp/image.png \ -loop 1 -t 10 -i /storage/emulated/0/InvitationTemp/image_0.png \ -loop 1 -t 10 -i /storage/emulated/0/InvitationTemp/image_1.png \ -loop 1 -t 10 -i /storage/emulated/0/InvitationTemp/image_2.png \ -loop 1 -t 10 -i /storage/emulated/0/InvitationTemp/image_3.png \ -loop 1 -t 10 -i /storage/emulated/0/InvitationTemp/image_4.png \ -filter_complex \ "[1]scale=w='min(146, iw)':h='min(149, ih)':force_original_aspect_ratio=decrease,fade=in:st=0:d=1:alpha=1[img1]; [2]scale=w='min(129, iw)':h='min(161, ih)':force_original_aspect_ratio=decrease,fade=in:st=1:d=2:alpha=1[img2]; [3]scale=w='min(120, iw)':h='min(109, ih)':force_original_aspect_ratio=decrease,fade=in:st=2:d=3:alpha=1[img3]; [4]scale=w='min(145, iw)':h='min(160, ih)':force_original_aspect_ratio=decrease,fade=in:st=3:d=4:alpha=1[img4]; [5]scale=w='min(212, iw)':h='min(46, ih)':force_original_aspect_ratio=decrease,fade=in:st=4:d=5:alpha=1[img5]; [0][img1]overlay=137:341:shortest=1[bg1]; [bg1][img2]overlay=609:74:shortest=1[bg2]; [bg2][img3]overlay=432:325:shortest=1[bg3]; [bg3][img4]overlay=700:579:shortest=1[bg4]; [bg4][img5]overlay=92:786:shortest=1,format=yuv420p[out]" \ -map "[out]" -s 991x903 -r 30 \ /storage/emulated/0/InvitationEventVideos/video_2021_Feb_09_09_39_38.mp4
优化提示
- 所有overlay的定位坐标基于991x903的标准画布计算,调整素材位置时需确认坐标未超出画布边界,避免素材被裁切。
- scale滤镜的
force_original_aspect_ratio=decrease参数会自动维持素材原宽高比,不会出现拉伸变形,适配不同尺寸的透明PNG素材。 - 移动端运行时可追加编码优化参数降低处理耗时:
-c:v libx264 -crf 23 -preset veryfast,无需调整滤镜逻辑。
内容的提问来源于stack exchange,提问作者najam ulhassan
相关产品推荐
相关产品推荐

