View Full Version : Automatic calculation of height when resizing video by max width
wthreex
31st January 2020, 15:55
In MeGui 'One Click Enocoder' There is a resize automation which calculate height according to max width :
https://i.imgur.com/NpgGaGk.png
So if the video has 1280x720 pixel and the input for max width was 640 it automatically set the height to 360
My question is how inside of avisynth script i can do the same thing ? Everytime i have to manually set the width and height like:
Spline36Resize(640,360)
How can i tell to avisynth to calculate the height by width? something like :
Spline36Resize(640, someMethod to calculate height )
hello_hello
31st January 2020, 16:57
CropResize (https://forum.doom9.org/showthread.php?t=176667).
You can specify just the output width, or just the output height, and CropResize will do the rest.
Or you can specify both a width and height, and CropResize will crop the picture if necessary to avoid distorting it, or you can tell it to add borders instead. The bottom line is, it does exactly what you want. See the second post in the thread for lots of screenshots.
Your example above would simply look like this (resizing without specifying any cropping).
CropResize(640)
Spline36Resize is the default anyway, but to do it while specifying the Avisynth resizing method.
CropResize(640, Resizer="Spline36")
To do it manually in a single line:
Spline36Resize(640, round(640 * height(last) / width(last) / 4.0) * 4) # rounds the height to mod4 dimensions if necessary
or
Spline36Resize(640, round(640 * height(last) / width(last) / 2.0) * 2) # rounds the height to mod2 dimensions if necessary
Another way. You'd simply change the value for NewWidth as required.
A = last
VideoWidth = A.width()
VideoHeight = A.height()
VideoAspect = float(VideoWidth) / float(VideoHeight)
NewWidth = 640
Spline36Resize(NewWidth, round(NewWidth / VideoAspect / 4.0) * 4) # rounds the height to mod4 dimensions if necessary
or
Spline36Resize(NewWidth, round(NewWidth / VideoAspect / 2.0) * 2) # rounds the height to mod2 dimensions if necessary
manolito
31st January 2020, 17:08
If you want to keep the aspect ratio then you first need to determine the AR of your source. For a 1280x720 clip the AR is 1.777777.
To automatically calculate the height for a width of 640 the formula is 640 * 720 / 1280 which makes the height 360 pixels. The AVS call looks like this:
Spline36Resize(640, ((640*720/1280) - (640*720/1280) % 4))
This makes sure that the new value for height is Mod4 (change it if you have different needs). I hope I got the brackets right... :scared:
hello_hello was faster than me...
wthreex
31st January 2020, 20:01
Thanks guys, @hello_hello i tried you suggestion: Spline36Resize(640, round(640 * height(last) / width(last) / 2.0) * 2) and it simply worked, However i couldnt work with your 'CropResize' package, How to import your codes into script ? because there no .dll file and i usually import plugins by 'LoadPlugin' method
hello_hello
31st January 2020, 20:01
manolito,
Sorry if it looked like a copied you a bit. I posted about using CropResize, and then edited the post to add a manual method, and then thought..... maybe it's easier to do in a single line.... but took a while to finish editing the post because I kept getting distracted, so you probably beat me to it.
hello_hello
31st January 2020, 20:17
Thanks guys, @hello_hello i tried you suggestion: Spline36Resize(640, round(640 * height(last) / width(last) / 2.0) * 2) and it simply worked, However i couldnt work with your 'CropResize' package, How to import your codes into script ? because there no .dll file and i usually import plugins by 'LoadPlugin' method
http://avisynth.nl/index.php/Import#Import
Import("D:\plugins\CropResize 2020-01-30.avsi") etc in the script, or if you have Avisynth installed, just stick it in the Avisynth plugins folder and it'll auto-load. If you use Import, maybe remove the date from the file name so it's just called CropResize.avsi (rename the script) so if at some stage I update it again, you won't need to edit your old scripts with the new name to make them work.
The script also need some plugins to be loaded for additional functions, such as color conversion or auto-cropping. There's details at the top of the help file.
Edit. If you want to use the other scripts, such as "CropResize Wrapper Functions.avsi", you need to import or auto-load them too. The wrapper functions script is the one containing the abbreviated function names, so you can use CR() instead of CropResize() and do things such as enabling the default cropping preview and setting info-true simply by adding "pi" to the function name. ie piCR() or piCropResize(). I use them a bit as it saves some typing, but the wrapper functions script needs to be loaded too. There's also a third script called "CropResize Resizer Functions.avsi", but you don't need that one at this stage. It won't hurt to load it though.
I noticed you use MeGUI. So do I, but I also have Avisynth installed and I put all the plugins in the installed Avisynth plugins folder. If you don't have Avisynth installed and you install it, when MeGUI's portable Avisynth runs it'll auto-load the plugins and avsi scripts in the "installed" Avisynth plugins folder too. It saves a lot of messing about using LoadPlugin() and Import() all the time. In case you don't know, with Avisynth installed, the location of the auto-loading plugins folder will be something like "C:\Program Files\AviSynth\plugins" or whatever the Program Files folder is for newer versions of Windows. "C:\Program Files (x86)\AviSynth\plugins".
Probably best to install Avisynth+ as that's what MeGUI uses.
https://www.videohelp.com/software/AviSynth-Plus
While you're there you can download the portable version too, and upgrade MeGUI to Avisynth+ 3.4, as it's a bit behind. To do that you need to close MeGUI and replace Avisynth.dll and DeviL.dll in the "MeGUI\tools\avs" folder with the new versions. Rename the existing dlls something like "Avisynth.dll.old" or similar if you don't want to delete them. Do the same for the 6 dlls in the "MeGUI\tools\avs\plugins" folder then restart MeGUI. It should replace the copies of Avisynth.dll in other locations, such as the MeGUI folder, with the new version.
hello_hello
31st January 2020, 21:29
PS. I thought I'd add, because I tried it myself and got it wrong the first few times, To do the same resizing manually with an anamorphic source you can add the source pixel aspect ratio into the equation. The source pixel aspect ratio is (source height x source display aspect ratio / source with).
ie for a 16:9 NTSC DVD it'd be 480 x (16 / 9) / 720 = 1.185185
So for a 16:9 anamorphic source you can resize to square pixels like this:
Spline36Resize(640, round(640 * height(last) / width(last) / 1.185185 / 4.0) * 4)
Like the original equation though, you really only need to divide the new width by the display aspect ratio for the new height, and for a 16:9 DVD that's un-suprisingly 16:9, so you don't need to use the original width and height to calculate the display aspect ratio, and you simply do this.
Spline36Resize(640, round(640 / (16.0 / 9.0) / 4.0) * 4)
PS. Be careful with Avisynth math as it does integer math for integers, so for Avisynth, 16 / 9 = 1, whereas 16.0 / 9 = 1.77777
wthreex
31st January 2020, 22:45
I appreciate your work, The autoload thing didnt work for me but the import method worked.
wonkey_monkey
31st January 2020, 23:09
ie for a 16:9 NTSC DVD it'd be 480 x (16 / 9) / 720 = 1.185185
It might (should) be 480 x (16 / 9) / 704 = 1.212121. The outer 16 pixels (18 pixels for PAL) are not meant to contribute to the display image.
hello_hello
31st January 2020, 23:55
It might (should) be 480 x (16 / 9) / 704 = 1.212121. The outer 16 pixels (18 pixels for PAL) are not meant to contribute to the display image.
I was hoping to avoid getting into that. :)
I've compared quite a few 16:9 DVDs (PAL) with their HD versions and I'm pretty sure they mostly use the whole 720 width as 16:9. Of course I've come across exceptions, and while I haven't been able to make any direct comparisons. I suspect DVDs from the BBC generally use an ITU or mpeg4 DAR, and there's definitely old 16:9 DVDs with a lot of black down the sides that are ITU.
I tend to ignore the 704 thing for those DVDs and use 20:11 for the display aspect ratio, as I've rarely come across a DVD where I need to crop exactly 8 pixels from each side, but it works out to the same pixel aspect ratio.
On the other hand, I'm not sure I've ever come across a 4:3 DVD that's really 4:3 for the 720 width. I tend to use 15:11 for those.
I haven't been able to make many NTSC DVD comparisons so things may be different.
As a side note, I encoded some Bluray extras a while back with a 720x480 resolution and they should be 20:11, but when I compared scenes from the movie itself the extras were neither 20:11 or 16:9. They were about halfway in between, if I remember correctly.
Maybe whoever authored them thought 20:11 is technically correct, but they'll probably be displayed as 16:9 over HDMI, so he went for the middle ground. Just a theory....
hello_hello
1st February 2020, 00:06
I appreciate your work, The autoload thing didnt work for me but the import method worked.
I don't know why because it works for me and I explained to someone else how to do the same with MeGUI a little while ago and it worked for him. Is Avisynth installed, because it checks the registry for the location of the plugins folder when it runs. The portable version does the same, it doesn't look for the folder directly.
I completely forgot, because I still have Avisynth 2.6 installed (I still need to test things with it now and then), for Avisynth plus you can specify the autoload directory in a script, or clear the directory, so make sure MeGUI isn't adding anything like that to a scrpt. There's info on autoloading here:
http://avisynth.nl/index.php/AviSynth%2B#Plugin_Autoloader
It handy to have Avisynth installed anyway, because then you can open scripts in other programs such as MPC-HC or AvsPmod and not just MeGUI.
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.