問題描述
這是我通過 PHP 運(yùn)行的向視頻添加圖像的代碼:
exec('ffmpeg -i input.mp4 -i logo.png -filter_complex"[0:v][1:v] overlay=10:10" -pix_fmt yuv420p -c:a copy output.mp4');
它運(yùn)行良好,但問題是,圖像在視頻分辨率上被縮小或放大.例如,在以下圖像中,徽標(biāo)寬度為 50px
,但視頻分辨率不同:
還有這個(gè)
如何防止圖像縮小/放大?
<小時(shí)>更新
感謝
還有這個(gè)
是否可以改進(jìn)此解決方案?
您可以使用 scale2ref 過濾器.
ffmpeg -i input.mp4 -i logo.png -filter_complex[1:v][0:v]scale2ref=(W/H)*ih/8/sar:ih/8[wm][base];[base][wm]overlay=10:10"-pix_fmt yuv420p -c:a copy output.mp4
這會(huì)將徽標(biāo)的高度調(diào)整為視頻高度的 1/8.
您必須將 W/H
替換為圖像的值,例如如果 PNG 是 320x270,那么它應(yīng)該是 scale2ref=(320/270)*ih/8:ih/8
This is my code that adds an image to videos, running via PHP:
exec('ffmpeg -i input.mp4 -i logo.png -filter_complex
"[0:v][1:v] overlay=10:10" -pix_fmt yuv420p -c:a copy output.mp4');
It works well but the problem is, the image is scaled down or up, up on the video resolution. For example in the following images the logo width is 50px
but videos resolution are different:
and this one
How can I prevent the image from scaling down/up ?
Update
Thanks to Mulvya, he proposed these codes
ffmpeg -i input.mp4 -i logo.png -filter_complex
"[1:v][0:v]scale2ref=(W/H)*ih/8:ih/8[wm][base];[base][wm]overlay=10:10"
-pix_fmt yuv420p -c:a copy output.mp4
and
ffmpeg -i input.mp4 -i logo.png -filter_complex
"[1:v][0:v]scale2ref=(W/H)*ih/8:ih/8[wm][base];[wm]setsar=1[wmsar];
[base][wmsar]overlay=10:10"
-pix_fmt yuv420p -c:a copy output.mp4
that works very well, but it doesn't keep the logo aspect ratio. I tried this code on two videos with different resolution and this is the result
and this one
Is it possible to improve this solution?
You can use the scale2ref filter.
ffmpeg -i input.mp4 -i logo.png -filter_complex
"[1:v][0:v]scale2ref=(W/H)*ih/8/sar:ih/8[wm][base];[base][wm]overlay=10:10"
-pix_fmt yuv420p -c:a copy output.mp4
This will resize the logo's height to 1/8th of the height of the video.
You have to replace W/H
with the values for the image e.g. if the PNG is 320x270, then it should be scale2ref=(320/270)*ih/8:ih/8
這篇關(guān)于FFMPEG :在視頻上添加固定大小的圖像,而不管視頻寬度&高度(分辨率)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!