FFmpeg: Unexpected Image Rotation Overlay with Arbitrary Angle
In this article, we will discuss the issue of unexpected image rotation when overlaying an image on a video with an arbitrary rotation angle using FFmpeg. The user reported that they were trying to overlay an image at position X=150, rotate it by an arbitrary angle of x=100, but the result was not as expected.
FFmpeg Overlay Filter
The overlay filter in FFmpeg is used to overlay one input on top of another input. The syntax for the overlay filter is as follows:
overlay=x=X:y=YWhere X and Y are the coordinates of the top-left corner of the overlay input in the output frame.
Rotating the Overlay Image
To rotate the overlay image, we can use the rotate filter. The syntax for the rotate filter is as follows:
rotate=angle=A:fillcolor=CWhere A is the rotation angle and C is the fill color. We can chain the overlay and rotate filters as follows:
overlay=x=X:y=Y:shortest=1[out];[out]rotate=angle=A:fillcolor=C[out2]User Issue
The user reported that they were trying to overlay an image at position X=150, rotate it by an arbitrary angle of x=100, but the result was not as expected. The user's command was as follows:
ffmpeg -i input.mp4 -i image.png -filter\_complex "[0:v][1:v]overlay=x=150:y=100:shortest=1[out];[out]rotate=angle=100:fillcolor=black[out2]" -map "[out2]" output.mp4However, the user reported that the image was not rotated as expected. The issue was that the user was using the angle option in the rotate filter, which expects a clockwise rotation angle. To rotate the image counterclockwise, the user needs to use the degrees option instead.
Solution
To fix the issue, the user can modify the rotate filter as follows:
ffmpeg -i input.mp4 -i image.png -filter\_complex "[0:v][1:v]overlay=x=150:y=100:shortest=1[out];[out]rotate=degrees=100:fillcolor=black[out2]" -map "[out2]" output.mp4This will rotate the image counterclockwise by 100 degrees as expected.
- The
overlayfilter in FFmpeg is used to overlay one input on top of another input. - To rotate the overlay image, we can use the
rotatefilter. - The
angleoption in therotatefilter expects a clockwise rotation angle. To rotate the image counterclockwise, the user needs to use thedegreesoption instead.