View Full Version : Weird crop request
mrwhitethc
17th July 2006, 23:57
Ok here goes, I have a movie in TS format, that I would like to put on DVD, no big deal, it's already 16:9 MPEG2 & AC3 5.1 sound. Resize to 720x480, no problem right? Well during the movie there is a single scene I would like to zoom to a specific part on the left, cutting off say 1/2 the screen to the right, keeping the aspec ratio, 16:9, and slowly zoom back out to the full size again. Anyone know how I would create this effect with Avisynth and a filter? I suppose I could cut the small scene out, it has a nice black cut at the start and end and isn't terriblly important but I would like to keep it. Thanks for all the help.
IanB
19th July 2006, 01:03
Function TryThis(clip Clip, float f) {
# Select an F% window from the centre of an image
Clip
Ox = (100-f) / 200.0 * Width()
Oy = (100-f) / 200.0 * Height()
Dx = f / 100.0 * Width()
Dy = f / 100.0 * Height()
Return LanczosResize(Width(), Height(), Ox, Oy, Dx, Dy)
}
...Source(...)
A=Trim(0, 999) # Bit before
C=Trim(2000, 0) # Bit after
Animate(1800, 1899, "TryThis", 50.0, 100.0) # Transition
Trim(1000, 1999) # Bit fiddled with
A + Last + C # Paste back togetherThe function TryThis() will crop an F percent window from the centre of the image and resize it back to the original size. Calling it inside Animate allows for a smooth transition over a range of frames. In this case frames 0 to 999 and 2000 to the end are untouched. Frames 1000 to 1799 are windowed 50%, frames 1800 to 1899 smoothly transition from 50% to 100% windowing and frames 1900 to 1999 are the full 100% window.
Adjust the crop parameter formulae of the resize to suit your needs, you probably want Ox = 0 for a left edge basis.
Didée
19th July 2006, 09:31
@ IanB
On request, lately I tried to make a simple Zoom-around-in-a-big-frame function, posted >here< (http://forum.gleitz.info/showthread.php?p=277807#post277807), which is quite similar to your function here.
However, ZoomedTravel() at times bails out with "Crop: YUV images can only be cropped by even numbers". How can that be, when the cropping is done by a resizer? I think that function is scripted correctly, but perhaps I did overlook something?
IanB
19th July 2006, 10:45
Opps! Now heres a nasty bugPClip CreateResizeH(PClip clip, double subrange_left, double subrange_width, int target_width,
ResamplingFunction* func, IScriptEnvironment* env)
{
const VideoInfo& vi = clip->GetVideoInfo();
if (subrange_left == 0 && subrange_width == target_width && subrange_width == vi.width) {
return clip;
} else if (subrange_left == int(subrange_left) && subrange_width == target_width
&& subrange_left >= 0 && subrange_left + subrange_width <= vi.width)
{
return new Crop(int(subrange_left), 0, int(subrange_width), vi.height, 0, clip, env);
} else {
return new FilteredResizeH(clip, subrange_left, subrange_width, target_width, func, env);
}
}The optimization fails to test for YUV restrictions when substituing a crop. It will be fixed for the next release.
:EDIT: Fixed in 2.5.7 CVS
squid_80
19th July 2006, 11:10
The resizers check the cropping arguments and if the cropped video size matches the requested target size and the cropping arguments are ints, they skip resizing and call crop directly. Unfortunately it doesn't always work, e.g.
colorbars(pixel_type="yv12")
bilinearresize(636,480,1,0,-3,0) # 640-1-3=636, no resizing necessary
..gives the error: "Crop: YUV images can only be cropped by even numbers (left side)."
As a workaround I sometimes add a very small amount to one of the cropping arguments, for example change the line above to bilinearresize(636,480,1.00001,0,-3,0).
Oh, IanB found it. nevermind.
foxyshadis
19th July 2006, 12:13
I don't know if you run Debug avisynth often, but I meant to tell you, I always get this error in resample_functions.cpp:
#ifdef DEBUG
env->ThrowError("Resizer: [Internal Error] Got Zero Coefficient");
#endif
Not sure what it means, it runs fine in release mode. (So I just called it X_DEBUG to kill the error.) EKG.avs seems to cause it, I'd have to play around to see what else does.
IanB
19th July 2006, 12:30
@foxyshadis, Thanks, I had forgotten about that error. It means the resample pattern generator has produced a set of pixel coefficients that sum to zero. It usually happens at the left/top edge and causes a dark first row/column in the production version.
mrwhitethc
21st July 2006, 13:17
Wow you know how to make a guys brain hurt :) First off, thanks guys for the initial info, here's some more to fill in some questions I have. First off a screen cap (http://i5.tinypic.com/20kehed.jpg) it's HD material @ 1280x1088, which means for a proper ratio it needs to be expanded to 1920x1088 and cropped 8 from the bottom to 1920x1080 then resized for dvd 720x480, but everyone knows that.
so would that be:
BilinearResize(1920,1088).Crop(0,0,0,-8).BilinearResize(720,480)
but then wouldn't the script from above be zooming into the 720x480 portion? Or would I have to do 3 trim funtions, the original resize, the zoom and then back to the original resize? And how would i go about to get the right frame numbers for the trims? Thanks again
Mr_Odwin
21st July 2006, 14:32
There's no need to resize twice. Cropping the 8 pixels from the bottom and then resizing to 720x480 gives the same effect but saves quality and will be quicker.
mrwhitethc
22nd July 2006, 17:48
AWSOME IT WORKED, thank you IanB and all others, this worked like a charm, now my only other question is, does this function, the IanB one, zoom in on the original 1280x1088 image or is it zooming in on the resized 720x480 image? Anyway you guys made my day, thanks again.
IanB
23rd July 2006, 09:08
The optional 4 subpixel crop parameters of the resizers apply to the input image size. So for your 1280x1088 example you may need to apply some scaling into the Ox/Dx calculations depending on the effect you want to project.
It cost you nothing to tweak the script and examine the result. It is usually called trial and error. Make it your best friend :D
mrwhitethc
26th July 2006, 00:45
I don't think I phrased that right last time. I messed around with and was finally able to zoom in & then expand on the secion I needed, this is the .avs I used:
LoadPlugin("DGDecode.dll")
Function TryThis(clip Clip, float f) {
# Select an F% window from the centre of an image
Clip
Ox = 0
Oy = (145-f) / 290.0 * Height()
Dx = f / 145.0 * Width()
Dy = 0
Return LanczosResize(Width(), Height(), Ox, Oy, Dx, Dy)
}
mpeg2source("test.d2v")
Loadplugin("decomb.dll")
Telecide(order=1,guide=1,post=2,vthresh=30)
Decimate()
Crop(0,0,0,-32)
AddBorders(0, 16, 0, 16, $000000)
BilinearResize(720,480)
A=Trim(0, 33629) # Bit before
C=Trim(34001, 0) # Bit after
Animate(33807, 33907, "TryThis", 50.0, 145.0) # Transition
Trim(33630, 34000) # Bit fiddled with
A + Last + C # Paste back together
Because of the resize to DVD res; was that zooming in on the 1080 the 480 video? Thanks again.
IanB
27th July 2006, 04:39
Watch setting Dy=0 some versions of Avisynth don't default this parameter correctly. From version 2.5.7 the behaviour is explicitly defined to be the same as crop() for Dx, Dy <= 0.0, i.e Dy = (Dy <= 0) ? Width()+Dy : Dy
Also with a bit more clever mathematics and script shuffling you could avoid resizing the zoomed part of your clip twice. Also consistant use of 1 resizer may help to maintain the look and feel of your encodes.
Hint:
global int TargetWidth = 720
global int TargetHeight = 480
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.