Pan, Tilt, and Zoom with FFMpeg

Pan, Tilt, and Zoom with FFMpeg

There are times when the content in an image or video is lost in the surrounding details. Live security camera systems have a feature known as PTZ, short for Pan, Tilt, and Zoom. This allows the live cameras a hardware and software way to focus more on an object of interest. With FFMpeg, we can do something similar.

In this post I’ll cover the basics of PTZ. Unlike live security cameras were you might notice a transition from the wide angle to the PTZ refocus, FFMpeg will strictly display the results. Creating a transition effect is outside the scope of this post, maybe another time. With that said, lets step through how to do this with FFMpeg.

To save time in processing, I select a clip segment of 15 seconds that starts at the 20 second point of the original 1920×1080 5 minute video.

ffmpeg -ss 00:00:20 -i L0000006.MOV -t 00:00:15 -c copy L0000006.seg.MOV

The scene in the video has a slight tilt to it. Next, I rotate the video 358 degrees clockwise to level out the horizon. Once consequence is it introduces dead space on the edges of the video.

ffmpeg -i L0000006.seg.MOV -vf rotate=358*(PI/180) L0000006.seg.rot.MOV

I’ll then crop a new video with a 480×270 size starting at the pixel point located at 1338×287.
I used GIMP to determine the exact crop points. This also removes the dead space from the earlier rotate command.

ffmpeg -i L0000006.seg.rot.MOV -filter:v "crop=480:270:1338:287" -c:a copy L0000006.seg.rot.crop.MOV

Finally, I’ll resize the video to 960×540 by multiplying by a factor of 2.

ffmpeg -i L0000006.seg.rot.crop.MOV -vf scale=iw*2:ih*2 L0000006.seg.rot.crop.zoom.MOV

Now I have my result video that has been panned, tilted, and zoomed from the original. For demonstration purposes I’m going to draw a red rectangle on the original video to give a visual of the resulting PTZ video. First I’ll resize my original to the same 960×540 video by dividing it in half.

ffmpeg -i L0000006.seg.MOV -vf scale=iw*.5:ih*.5 L0000006.seg.shrink.MOV

Next, I’ll draw the red rectangle around the target area.

ffmpeg -i L0000006.seg.shrink.MOV -vf drawbox=x=669:y=143:w=240:h=135:color=red@0.5 L0000006.seg.drawbox.MOV

Lastly, I’ll join videos with this command.

ffmpeg -f concat -safe 0 -i mylist.txt -c copy L0000006.final.MOV

The “mylist.txt” file is formatted with the contents of all my videos I want to concatenate.

# this is a comment
file 'L0000006.seg.drawbox.MOV'
file 'L0000006.seg.rot.crop.zoom.MOV'

As a matter of comparison, the results of the PTZ bring out details too faint in the original.

The rectangle overlay was something extra in this post. Later posts will cover this and other overlays in more detail. The ability of FFMpeg to process both images and videos means this function can be applied to image data as well.

I won’t cover all of the Imagemagick commands on how to PTZ, but will share the rectangle command.

convert L0000006.rot.PNG -fill none -stroke green -strokewidth 3 -draw "rectangle 1338,287 1818,557" L0000006.rot.boarder.PNG

The real difference with the Imagemagick command is the specified starting x,y and ending x,y coordinates. Again, this is extra info that I won’t cover here. I’m getting off topic if I continue on.

I hope you have enjoyed this and found it useful. FFMpeg has many feature sets and I plan to cover more of them in future posts. I hope you drop back by again to see these features

 

Comments are closed.