FAQ  •  Register  •  Login

Advanced aspect ratio conrtol

<<

heyt

Serviio newbie

Posts: 2

Joined: Sun Oct 23, 2016 8:25 pm

Post Sun Nov 20, 2016 7:11 pm

Advanced aspect ratio conrtol

Hi,

with my new LG TV set I was searching for a proper video server. I ended up using Serviio and I am very happy with it.
Sorrowly LG's WebOS 3 is very picky about aspect ratio. Most of my videos are not NTSC/PAL standard or square pixel which is usually no problem if PAR and DAR are handled correctly. But if you don't meet NTSC/PAL standard WebOS will use PAR 1:1 and DAR 1:1 independant of the videos meta data.

To fix the problem I tested around with the DAR="16:9" option in the profiles which will end up in with a nicely 16:9 padded video forced to DAR 4:3. That is actually not the result expected but exactly what I found in the ffmpeg command in the log file.

Since DAR="16:9" is the only aspect ratio function I started to test around ffmpeg directly to transcode an arbitrary file to a DAR 16:9 square pixel output. To do so, I used two steps. First change to square pixel and second pad it to 16:9. Both work independantly of course. Regarding ffmpeg filter options it looks like:
  Code:
Storage Aspect Ratio:   scale="trunc(iw*sar/(1/1)/hsub)*hsub:trunc(ih/vsub)*vsub",setsar="1/1"
PAD:                    pad="trunc(if(lt(dar\,16/9)\,ih*16/9/sar\,iw)/hsub)*hsub:trunc(if(lt(dar\,16/9)\,ih\,iw/(16/9)*sar)/vsub)*vsub:(ow-iw)/2\:(oh-ih)/2:black",setdar="16/9"

or for the complete command line:
  Code:
ffmpeg -i SOURCE.mp4 -vf scale="trunc(iw/hsub)*hsub:trunc(ih/vsub)*vsub",scale="trunc(iw*sar/(1/1)/hsub)*hsub:trunc(ih/vsub)*vsub",setsar="1/1",pad="trunc(if(lt(dar\,16/9)\,ih*16/9/sar\,iw)/hsub)*hsub:trunc(if(lt(dar\,16/9)\,ih\,iw/(16/9)*sar)/vsub)*vsub:(ow-iw)/2\:(oh-ih)/2:black",setdar="16/9" test.mp4

In addition I tested around with autocrop since some old files still need it. With Linux it can be done inline with a command line like this (for windows I am not sure how it could look like):
  Code:
ffmpeg -i SOURCE.mp4 -vf `ffmpeg -ss 60 -i SOURCE.mp4 -f matroska -t 10 -an -vf cropdetect=24:16:0 -y -crf 51 -preset ultrafast /dev/null 2>&1 | grep -o crop=.* | sort -bh | uniq -c | sort -bh | tail -n1 | grep -o crop=.*` test.mp4


I wonder if it could be an option to implement those filter commands for serviio's profiles in one of the next version. Something like AutoCrop="true" and SquarePixel="true" without any options and PAD="X/Y" with X and Y as options - could be great to be more flexible e.g. for my 21:9 PC Monitor. In the pad filter line simply change all 16/9 to any X/Y given. Of course all filter parts are safe regarding container and codec multipliers.

I am interested in any comments.
<<

zip

User avatar

Serviio developer / Site Admin

Posts: 16356

Joined: Sat Oct 24, 2009 12:24 pm

Location: London, UK

Post Tue Nov 22, 2016 8:53 am

Re: Advanced aspect ratio conrtol

Thanks for the examples. The ratio code in Serviio is quite old from the time where FFmpeg didn't really support the functions to do it in command line.

It was built as a part of https://bitbucket.org/xnejp03/serviio/i ... proper-par

I could to potentially revisit this and use your code instead, but it'll need some serious testing. Are you up for it? If yes, please create a new ticket with your solution. Also do you think this is a generic solution or just for your particular scenario?


Basically it should support both, square and non-square pixels (depends on the target codec and renderer).
<<

heyt

Serviio newbie

Posts: 2

Joined: Sun Oct 23, 2016 8:25 pm

Post Sun Nov 27, 2016 2:54 pm

Re: Advanced aspect ratio conrtol

I just revisited the code supplied and did some minor changes to make it more flexible - square or non square pixel for input and output should be no problem now. In addition I added some code for scaling since scaling is heavily influenced by all those ratio changes.

  Code:
Filter module                    Parameter          ffmpeg filter code
AutoCrop                         true/false         `ffmpeg -ss 60 -i SOURCE.EXT -f matroska -t 10 -an -vf cropdetect=24:16:0 -y -crf 51 -preset ultrafast /dev/null 2>&1 | grep -o crop=.* | sort -bh | uniq -c | sort -bh | tail -n1 | grep -o crop=.*`,scale="trunc(iw/hsub)*hsub:trunc(ih/vsub)*vsub"
Storage Aspect Ratio             [w/h]              scale="trunc(iw*sar/([w/h])/hsub)*hsub:trunc(ih/vsub)*vsub",setsar="[w/h]"
Display Aspect Ratio by padding  [w/h]              pad="trunc(if(lt(dar\,[w/h])\,ih*[w/h]/sar\,iw)/hsub)*hsub:trunc(if(lt(dar\,[w/h])\,ih\,iw/([w/h])*sar)/vsub)*vsub:(ow-iw)/2\:(oh-ih)/2:black",setdar="[w/h]"
         
Width                            [width]            scale="trunc([width]/hsub)*hsub:trunc(ow*sar/dar/vsub)*vsub"
Height                           [height]           scale="trunc(oh/sar*dar/hsub)*hsub:trunc([height]/vsub)*vsub"
maxWidth                         [max_width]        scale="trunc(if(gt(iw\,[max_width])\,[max_width]\,iw)/hsub)*hsub:trunc(ow*sar/dar/vsub)*vsub"
maxHeight                        [max_height]       scale="trunc(oh/sar*dar/hsub)*hsub:trunc(if(gt(ih\,[max_height])\,[max_height]\,ih)/vsub)*vsub"


For sure I am up for testing. In addition I can check if the code supplied (only Linux tested for now) will work with a Windows version of FFMPEG. For Autocrop I am sure I will have to find another soultion since the evaluation is done by bash commands for now.

Return to Transcoding

Who is online

Users browsing this forum: No registered users and 3 guests

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.