Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

php - FFMPEG : Add a fixed size image on a video, regardless of the video width & height (resolution)

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:

enter image description here

and this one

enter image description here

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

enter image description here

and this one

enter image description here

Is it possible to improve this solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...