Resizing in FFmpeg

….I had more loose ends to tie up than I thought! This is another post that I had been asked for some clarification on, so I thought I had better get it done!

This post has been written with the assistance of Larry Compton at DME Resources

There are many reasons why DVR  Manufacturers, Installers and Owners use image recording dimensions that may not be how the original image should be portrayed. The main one, from a manufacturing perspective, is that the people designing this type of equipment have very little understanding on its future possible evidential use and the consequences.

Never Trust DVR Manufacturers Specs

One of the most common examples is footage recorded with a pixel dimension of 720 x 240/288, and during the encoding a Display Aspect Ratio is set to 4:3. The subsequent player then ignores the pixel measurements and adjusts the picture to a 4:3 aspect ratio.

FFplay

FFplay retains the vertical size but reduces the horizontal by half

Here I have a piece of H264 Video playing in FFplay, I can see from the FFmpeg console that it has a 720×240 pixel size but with a SAR of 1:2 which results on a DAR of 3:2.

Taken from the Red site:

With digital, several types of aspect ratios exist: the display aspect ratio (DAR), the pixel aspect ratio (PAR) and the storage or sample aspect ratio (SAR). DAR refers to the viewable image dimensions, PAR refers to the physical dimensions of each pixel, and SAR refers to the number of horizontal versus vertical pixels.

To work out the DAR from a SAR of 1:2:

720 x 1 = 720

240 x 2=480

720/480=1.5

1.5 = 3:2

Have a read through this to read more on a few other examples!

It is always a bit hit and miss on how the player deals with the resizing. SMplayer retains the horizontal measurements and resizes the vertical.

SMplayer

So, we have a video frame of 720×240 but its being resized automatically by the player.

The actual image, without any aspect ratio correction would look like this… squished!

frame00020

First off, in order to make decisions based on the data, it would be beneficial to get as much information from the video stream as possible. This information would be validated against other software if required.

For the purpose of this example, lets suggest that we just require to get this into a working standard, for demonstrative and presentation purposes only, and we have identified that it should be set to a display aspect ratio of 4:3.

ffmpeg -i Test.mp4 -vf “scale=720:ih*2,setdar=4:3” -c:v rawvideo -f avi Test-UC-Resized-720×480.avi

The above ffmpeg command has a filter chain which is identified by the -vf command. Inside the quotations is the scale filter, followed by the aspect ratio filter. I could have set the original scale to 720:480 but, to highlight some of the various options, I have chosen ‘ih*2’ instead of the number; ih*2 stands for Input Height x 2.

I have then placed these settings into the resulting filename to help me identify what my new avi is set at.

Here are some more links with information of Aspect Ratio and FFmpeg resizing and filters.

https://trac.ffmpeg.org/wiki/Scaling%20(resizing)%20with%20ffmpeg

https://ffmpeg.org/ffmpeg-filters.html#scale

http://trac.ffmpeg.org/wiki/FilteringGuide

http://www.animemusicvideos.org/guides/avtechbeta/aspectratios.html

Lastly, its worth mentioning that, on many occasions you need to output image frames as they are (not resized) and then batch resize in Photoshop.

ffmpeg -i Test.mp4 -f image2 -pix_fmt rgb24 -start_number 0 FrameFolder\Frame_%05d.tiff

This will then output every frame as an uncompressed Tiff image starting at Frame Zero.

If you are running a batch action in Photoshop to create your image sequence, including the resizing or framing, and you want to add the Filename (which is the original frame number), this is a handy script. Add Filename as Text Layer.

Whilst we are on the subject of Still Images from frames – what about fields… and what if you wanted to resize through FFmpeg?

So, lets say you have a piece of interlaced mpeg2 video and you wanted to output all fields. Then resize to full frame, before saving as images. Just to make things more interesting, lets say that it is PAL SD which should be viewed at 16:9 Widescreen!

Start with : 25 FPS Interlaced Mpeg2 720×576 with 16:9 Display Aspect Ratio, 502 Frames

ffmpeg -i yourfile.mpg -vf “yadif=1:0:0,scale=1024/576” -sws_flags lanczos -f image2 -pix_fmt rgb24 -start_number 0 Folder\Field_%05d.tiff

The important part of this command is the Yadif deinterlacer, followed by the scaler. The sequence of 3 digits after the Yadif name details the Mode:Parity:Selection

You can see more on the Yadif filter here:

http://ffmpeg.org/ffmpeg-all.html#yadif-1

Basically, this command now results in a 1004 images of every Field, automatically resized to Full Widescreen PAL. The method of scaling is defined by the -sws_flags. In the example above I used the lanczos algorithm.

In Summary, I hope this has highlighted:

1) Issues with pixel dimensions and automatic resizing to an aspect ratio

2) How to resize and transcode a file that has incorrect parameters for your intended use

3) How to extract all the frames as images

4) How to place the frame number with the image within a Photoshop batch action

5) How to extract each field and then resize using a specific scaler

As always, I hope some of the info helps….

Advertisements
By Spreadys Posted in EEPIP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s