You are not logged in.

#1 2012-08-15 12:19:20

scottro
Member
From: NYC
Registered: 2002-10-11
Posts: 465
Website

[SOLVED] Trouble with new ffmpeg cropping syntax

I'm having trouble understanding the new version of ffmpeg cropping syntax.   Say I wanted to take 20 pixels off the bottom, it used to be -cropbottom 20.  Now, one uses -vf crop= and some syntax that I am not getting.

This works

-vf crop=in_w:in_h-10:0:0


However, I'm confused as to why it works.  (Example found by googling).  I'm not sure how one designates top and bottom with the new syntax.  The man page refers to taking equal amounts off top and bottom.  (If anyone is kind enough to look at the man page for me, just search for in_w on the page and the crop section begins above it.)  I understand that by using in_w and/or in_h with no arguments, one just uses the original width or eight, but I'm not sure how the h-10 takes 20 pixels off the bottom.  Would +10 take it off the top?

I'm not sure what I'm missing here, but I suspect it's pretty obvious.   At any rate, the example I used looks as if it's just taking 10 pixels, and the person who answered the person asking the question just told them to look at the manual for more details.  (Hopefully, the OP in that thread is better at grasping the manual than i am)



http://ffmpeg.org/pipermail/ffmpeg-user … 01750.html

is the post from the ffmpeg thread I used.

Last edited by scottro (2012-08-16 15:04:30)

Offline

#2 2012-08-16 07:23:33

ronnylov
Member
Registered: 2010-12-21
Posts: 65

Re: [SOLVED] Trouble with new ffmpeg cropping syntax

-vf crop=width:height:x:y

Width and height is the dimesions of the cropped image after cropping. X and y is the coordinate of the upper left corner of the cropping rectangle where x=0 corresponds to left edge of original frame and y=0 the top edge of the original frame.

Example:
Original frame is 1280x720. You want to crop 10 pixels from top and bottom but leave the width uncropped:
-vf crop=1280:700:0:10
Here the new height after cropping is 700 pixels and the upper left corner of the cropped image is placed at x=0 and y=10 on the original frame. Since the new height is 20 pixels less than the original it will crop 10 pixels at top and bottom. See it like a crop tool where you first set the dimensions of the cropping rectangle and then place the rectangle somewhere in the original picture where the coordinates x and y is where to place the cropping rectangle based on upper left corner of the rectangle.

I think your example is only cropping 10 pixels from bottom because you place the cropping rectangle at top left of the original image and then reduce the height of the cropped image to 10 pixels less than the original. So the post in your link is probably wrong, because I don't see how that should work. To crop only 20 pixels from bottom (as the poster asked for) he should use:
-vf crop=in_w:in_h-20:0:0

To crop 20 pixels from bottom and 20 pixels from top:
-vf crop=in_w:in_h-40:0:20

I am not at my home computer now (forced to use Windows XP at work) so I can not check the man page.
But I use to look here: http://ffmpeg.org/ffmpeg.html
Documentation for crop: http://ffmpeg.org/ffmpeg.html#crop
There you can see some examples too which should explain it better than I can do.

By the way it is more optimal to crop insteps of 8 pixels (because of how video encoding works). So instead of cropping 20 pixels you should crop 24 pixels to gain some quality at the border and getting smaller file size (the final image should be mod8 or even better mod16).

Offline

#3 2012-08-16 11:24:07

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,913
Website

Re: [SOLVED] Trouble with new ffmpeg cropping syntax

I made an ugly one-liner to autocrop videos in mplayer:

#!/bin/sh
mplayer `mplayer -ao null -ss $1 -frames 100 -vf cropdetect -vo null "$2" | awk -F '[()]' '{print $2}' | uniq | grep -Ev 'End of file' | tail -2` "$2"

this is used like this:
mplayer 60 blabla.avi

The 60 is the startsecond, where it should search for borders.


***edit*** and now i realize you are talking about ffmpeg. duh!

Last edited by Rasi (2012-08-16 11:25:44)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#4 2012-08-16 12:12:52

scottro
Member
From: NYC
Registered: 2002-10-11
Posts: 465
Website

Re: [SOLVED] Trouble with new ffmpeg cropping syntax

Firstly, @Rasi, thanks for the thought anyway--some of the syntax may be the same.   

@ronnylov--Ok, your explanation makes more sense--that is, do -20, rather than -10.

The man page actually only gives examples for removing the same amount from top and bottom, that is, removing 20 pixels from top and 20 from the bottom.  Judging from the fact that many people seem to have questions about the new syntax, I am beginning to think I should file an RFE to put in an example for bottom or crop topping only, or different amounts from top and bottom.

So, your answer _almost_ solves it for me--I apologize, I thought I'd said in my post that I've looked at the man page and was still confused.  Its syntax only shows top AND bottom, not OR. 

Their specific example is actually a bit different---it used 2*20 rather than -40.    And that's why I'm confused.  Why does -20 take it from the bottom, rather than the top?

That's the part where I'm still unclear (your statement that that other post was incorrect solved the other part of my question--I didn't understand why it should be -10, rather than -20.  Perhaps the person who answered that post on that mailing list made a typo). 

So, the remaining question is, what if I wanted to remove from the top, rather than the bottom.  (My confusion lies in the fact that it seems as if the h-20 would start with the top, rather than the bottom)

P.S.  So far, you're doing a better job than the man page, so that statement of yours, that they explain it better, is, IMHO, incorrect.

Last edited by scottro (2012-08-16 12:13:45)

Offline

#5 2012-08-16 14:30:52

ronnylov
Member
Registered: 2010-12-21
Posts: 65

Re: [SOLVED] Trouble with new ffmpeg cropping syntax

To crop only from bottom you can reduce height of image the same amount of pixels as you move the cropping area down.
Lets say we want to crop only 20 pixels from top.

-vf crop=in_w:in_h-20:0:20
(where in_w equals the input image width and in_h equals input image height)

Coordinates in pictures start at upper left corner and goes from left to right and from up to down. And the coordinates start with 0, not 1.
So in_h-20 just mean that you decrease the height of the video by 20 pixels and 0:20 indicates where to start crop it.

Sometimes I use Avidemux, select video filter and crop as a help tool to get the values I should put in ffmpeg. There you can crop from left, top, right and bottom. When you apply it you can see the new width and height of the cropped video. Put these values of the new size first and then the cropleft and croptop values. That is what I am doing.
-vf crop=outputwidth:outputheight:cropleft:croptop

So if your original is 1280x720 and you crop 10 pixels from left, 20 from top, 30 from right and 40 pixels from bottom you get a new width of 1280-cropleft-cropright=1280-10-30=1220 and the new height is 720-croptop-cropbottom=720-20-40=660. So the size after cropping is 1220x660 and the upper left corner of the cropped image is placed at x=cropleft=10 and y=croptop=20:
-vf crop=1220:660:10:20

Best way is to make some tests and see what happens.

Edit: By the way I am not in any way resposible for the change of this function in ffmpeg. I am just an ordinary user that had to find out this syntax myself with trial and error. I am happy if I can help reducing the confusion regarding the new syntax.

Last edited by ronnylov (2012-08-16 14:55:34)

Offline

#6 2012-08-16 15:04:03

scottro
Member
From: NYC
Registered: 2002-10-11
Posts: 465
Website

Re: [SOLVED] Trouble with new ffmpeg cropping syntax

I think you've solved my question, and thank you.  Just to make sure I have this correct.

An in_h-20 would start at the bottom, because I'm using 0:0.   An in_h -2:0:20 means I start at the 20th pixel down and work my way upwards. 

Thank you VERY much.  As for the change in function, part of my googling indicated that it was, though it made syntax more complex, necessary because the old -cropbottom, -croptop and so on had some issues that had to be addressed. (I didn't look deeply into that aspect, and may have it wrong,but it seems to me that I came across that in my searching for examples)

Once again thank you.  (Trying to figure out if there's a mark thread solved tool, but I don't see one, though I see I can edit the post title, so I will do it there.

Offline

#7 2012-08-16 15:37:15

ronnylov
Member
Registered: 2010-12-21
Posts: 65

Re: [SOLVED] Trouble with new ffmpeg cropping syntax

Not sure if I understand what you mean here and I think there was a typo in your post.

It always counts from up to down and from left to right. Just set the desired size and how much to crop from left and from top.
If you want to crop only the bottom you just do not crop anyhting at top and set the height smaller than the original.
If you want to crop from top you still set the height smaller than the original but also the amount of cropping from top.
If you want both top and bottom cropped you still set height smaller than original but a smaller value of the cropping from top (you have to calculate how much).

Same ting applies for cropping the sides but you count from left to right.

There are also some default assumptions so if you leave out x:y and only set width and height then I think it assumes that you want to crop all sides equal.

Offline

#8 2012-08-16 15:41:14

scottro
Member
From: NYC
Registered: 2002-10-11
Posts: 465
Website

Re: [SOLVED] Trouble with new ffmpeg cropping syntax

Ah, OK, now I get it.   So, for bottom,  use in_h-<number> and leave x:y at 0:0

For top use the in_h-<number>, but use the x:y coordinates, correct?

(For me, most common use would be bottom only--my wife's first language isn't English, so sometimes, she's bought something with hardcoded English subtitles that she doesn't want.)

Once again many thanks.

Last edited by scottro (2012-08-16 15:42:56)

Offline

#9 2012-08-16 17:59:29

ronnylov
Member
Registered: 2010-12-21
Posts: 65

Re: [SOLVED] Trouble with new ffmpeg cropping syntax

Yes, correct!

(My first language is Swedish)

Offline

#10 2012-08-16 18:16:59

scottro
Member
From: NYC
Registered: 2002-10-11
Posts: 465
Website

Re: [SOLVED] Trouble with new ffmpeg cropping syntax

Your English is excellent--I hadn't realized you weren't a native speaker.   

Any failure on my part to understand was due to my lack of knowledge (or lack of sleep, to be kinder to myself) and not at all due to lack of clarity on your part.

Offline

Board footer

Powered by FluxBB