# resizing videos while accounting for cropping and aspect ratio

1. I need help on a project that is way over due. I thought I used to know all this, but back when, it used to be dvd. Anyway. I had a ton of questions about this, and was going to formulate it all into a main topic, but I but decided to hold off on it because i'm behind on my original project plan.

The video was already cropped, so the 1080 and 480 would obviously need to be readjusted.

What I want to do is resize them for the 1920 and 720 width, and adjust the height accordingly and still maintain the aspect ratio. But what escapes me is the calculation to do all this. Any help would be appreciated. Thanks in advance.

Code:
```source       width  height  AR    --  dest
-------------------------------------------------------
progressive  3840   1714    2.240     1920x1080
progressive  3840   1714    2.240     720x480```
2. I think I got it. I have to calculate it like this:

1920 / 2.240AR = 857 (or round it to 864 to evenly divide by 16 or 8 macroblocks)

and

720 / 2.240AR = 321 (or round it to 320 to evenly divide by 16 or 8 macroblocks)

Code:
```source       width  height  AR    --  dest
-------------------------------------------------------
progressive  3840   1714    2.240     1920x864
progressive  3840   1714    2.240     720x320```
3. Assuming square pixels:
Code:
`new height = new width * old height / old width`
4. Thanks for the formula, jagabo. I'll keep that for next time.

After running various encodes, (from the 4k from Tears of Steel) to 1920 and 720, it has occurred to me that the 720 looks too fuzzy and I came to realize that there is more to resizing/cropping/aspect_ratio than was I am currently using. I mean, most dvd's are pretty sharp, coming from the master source of course, but since this is from the 4K 3840 pixels wide (6.3 GB, mov) :: Mirror 1 (Netherlands), it should maintain some level of sharpness, unless I am using a poor "downscale" method. The 4K source should be comparable to hollywoods, for dvd if i'm using 720x480 pixels.

Here's the script i'm using to process:

Code:
```  m = "tearsofsteel_4k_keep.mov"

LWLibavVideoSource(m, cache=true , seek_mode=0 )#.selectevery(2,0)

Spline36Resize( 720, 320 ) # is better quality ?
# BilinearResize( 720, 320 ) # is softer, per some users ?

# turn on both color conversions for better qlty YV12, per some members in old threads ?
ConvertToYUY2( interlaced=false )
ConvertToYV12```
5. The whole purpose of this is so that I can better properly perform test encodes through the Intel quicksync encoders on both my laptops. FWIW, I was unable to process the actual 4K under quicksync's H264/AVC due to some error in processing that I could not understand: "Failed to get free surface for enc. error at encode thread." under my Dell Inspiron i3-2370M, API v1.4 laptop. So I had to switch the encoder to MPEG2 and that worked, but I had to use a large bitrate. Actually, it turned out ok since the source doesn't already consist of the usual mpeg (encoder) mabroblock and pixellation and garbadge noise. The test clip did quiet well when encoded to MPEG2 using 9K bitrate. Actually, shortly after those tests, I came to the idea and tried the H264/AVC encoder again, but this time, since I was downscaling the 4K's 3840 down to 720 pixel width, it might work, and sure enough, it did, and using the same parameters as in the MPEG2, the filesize came out nearly identical, down to the kb, or 200.537kb vs 200.629kb, and both were in very good quality, only just fuzzy looking. There must be some step I am missing in the process to get it to the level of sharpness that we all see in Hollywood DVD's.
6. Spline36Resize is sharper than BilinearResize. The latter is the least sharp of AviSynth's resize filters but gives the most natural look if your source is sharp (no oversharpening halos, least aliased edges). The former will generate oversharpening halos and buzzing edges if you source is sharp. LanczosResize() and Lanczos4Resize() are even sharper.

If your source is sharp Spline36 or Lanczos will be much sharper than hollywood DVDs.
7. Did a lot of testing a while back and I agree with this post - for downscaling to less than about 3/4 original size, plain old Bicubic works best. For small size changes, up or down, use Spline64.
8. Originally Posted by vhelp
Thanks for the formula, jagabo. I'll keep that for next time.

After running various encodes, (from the 4k from Tears of Steel) to 1920 and 720, it has occurred to me that the 720 looks too fuzzy and I came to realize that there is more to resizing/cropping/aspect_ratio than was I am currently using. I mean, most dvd's are pretty sharp, coming from the master source of course, but since this is from the 4K 3840 pixels wide (6.3 GB, mov) :: Mirror 1 (Netherlands), it should maintain some level of sharpness, unless I am using a poor "downscale" method. The 4K source should be comparable to hollywoods, for dvd if i'm using 720x480 pixels.

Here's the script i'm using to process:

Code:
```  m = "tearsofsteel_4k_keep.mov"

LWLibavVideoSource(m, cache=true , seek_mode=0 )#.selectevery(2,0)

Spline36Resize( 720, 320 ) # is better quality ?
# BilinearResize( 720, 320 ) # is softer, per some users ?

# turn on both color conversions for better qlty YV12, per some members in old threads ?
ConvertToYUY2( interlaced=false )
ConvertToYV12```
It is so funny that you should post this because at this very moment I am in the middle of resizing ToS for my own encoding tests. One thing, you are starting with a very poor quality source, the MOV version of ToS you linked to is already heavily compressed, it's nowhere near Hollywood studio mastering quality.

If you want a mastering quality source download the uncompressed y4m version (it's about 60gb compressed archive that when uncompressed is about 180gb or the jpeg2000 compressed mxf version (I have both).
9. If you want a mastering quality source download the uncompressed y4m version (it's about 60gb compressed archive that when uncompressed is about 180gb or the jpeg2000 compressed mxf version (I have both).

And as far as the 4K I downloaded, that is good enough for me. And that is better quality than any Holllywood mastered 720x480 DVD. At least all the DVD's in my collection are far poorer quality than the 4K I just downloaded this morning. Believe that!

@ jagabo, and raffriff42, thanks, i'll look into those suggestions next.
10. One thing, you are starting with a very poor quality source, the MOV version of ToS you linked to is already heavily compressed, it's nowhere near Hollywood studio mastering quality.
First, let me say this.. there were two files I downlaoded. The +700MB and then the 4K (6.3GB). The 700MB had a few compression artifacts. You can spot them in the "scope" scene(s) (starting around frame 2458) where the guy on the roof is looking through his gun scope, where on the left and right dark areas, you can see artifacts. Its kind of hard to find artifacts when the video is playing, but a good pair of (trained*) eyes can spot them, and many others fairly quickly throughout the movie.

* 1 the ~700MB clip: there comes a time when you just know where to spot artifacts. you just know where to look. areas like dark patchy sections are good examples, for instance. the training comes with time, time dealing with videos since you can remember.
* 2 the ~700MB clip: another area to pick for artifacts is when small sections appear to shimmer, aprox frame 2360, where they is napping with the newspaper over his head, he begins to get up. if you watch the wall on his left, you will see it start to shimmer during panning-motion as he begins to get up and leave, starting around frame 2930 onward.
* 3 note (in 1 and 2 above) if you encode to low a bitrate, you will get similar artifacts. i realize that these are just nit-picks, but then again, this forum is full of that.

But I disagree with you about the artifacts in the 4K clip. But i'm referring to the 720x480 dvd's, not the 4k masters. As far as I'm concerned, I have a 4k master, but i'm trying to downscale it at least dvd specs, not exact specs, just the pixels.

As for the quality in the 4K i downloaded, there are no noticeable artifacts. Its pretty clean.

Also just want to say, that the purpose of my encodings are not for smallest file size, but smallest file size and undisturbed detail. Bitrate is not as important to me as it once was. Those XviD days are long gone, and its time to change and grow past that. Our encodes should no longer have compression artifact due to poor bitrate setup unless they are already in the sources to begin with (ie, television, dvd's, etc). Acquire the trained eye (to know what to look for during the encoding process) then test, trial and adjust accordingly.
11. Oh, also just noticed that while testing the Bicubicresize() filter, it is fast. I get 4 fps increase in encoding. And when you are encoding from a 4K (or, 3840 width) down to 720, every fps counts towards shorter encoding times. I have not yet tested with the Acer Aspire E 15, i3-7100U cpu laptop. So I imagine things will be different there. But on my i3-2370M cpu (Sandybridge) laptop, I am getting aprox 23 fps with all the resize methods (in script below) except for when using Bicubicresize() I get 27 fps. I guess no one realized the speed benefit for this downscale method.

Code:
```  m = "tearsofsteel_4k_keep.mov"

LWLibavVideoSource(m, cache=true , seek_mode=0 )#.selectevery(2,0)

# Spline36Resize( 720, 320 ) # is better quality
# LanczosResize(  720, 320 ) #
# LanczosRsize(  1920, 1080) #
# BilinearResize(1920, 1080) #
# BilinearResize( 720, 320 ) # is softer, per some users
# Lanczos4Resize( 720, 320 ) #
Bicubicresize(  720, 320 ) #

# turn on both color conversions for better qlty YV12, per some members in old threads ?
ConvertToYUY2( interlaced=false )
ConvertToYV12```
12. Originally Posted by vhelp
As far as I'm concerned, I have a 4k master,
It doesn't work that way, it's not whether by your standards you have a 4k studio quality master, you either do or you don't and you don't. If you don't want to download the 60gb version at least go for the 13.5gb mxf version.

What you have is a poorly encoded version which was encoded using a lossy source (I think it was encoded from the mxf source) which means your dvd and 720p are 3rd generation dupes, of course they look fuzzy.
13. i do not have any plugins that read mxf files. but i will consider downloading that file. Right now, the "4K 3840 pixels wide (6.3 GB, mov)" i download looks pretty clean to me and i don't think it will get any sharper once i downscale it to 720w, but i will give it a shot. i'm just testing things out anyway.
14. While I'd never argue that a higher quality source would potentially give you a higher quality output, in my experience when you're downscaling by a large amount the quality of the source becomes somewhat secondary to the downscaling and any quality loss when re-encoding.

Once you're downscaling beyond a certain point the source will contain detail you can't retain, and increasing it's quality will only increase what you don't retain while having virtually no effect on what you do. I'd be confident unless there's visible compression artefacts in a UHD source, being lossy or lossless would make no difference to the result when downscaling to DVD resolution, and I wouldn't be surprised if it didn't make any difference, even when downscaling to 720p. In fact I wouldn't be surprised if even a small amount of visible compression artefacts in the source had virtually no effect on the sharpness of the downscaled version, because the compression artefacts might effectively become just another form of detail that's lost due to the amount of downscaling.

Every situation is different depending on the amount of downscaling and source quality, so I'd be very reluctant to set rules myself based on whether the source is lossy or not. If it looks clean and sharp then it's clean and sharp.
15. two things come to mind, 1) to maintain mod 16 borders if authoring for stand alone usage (dvd etc) get fitCD, it will do all the math for you. just check or uncheck anamorphic source etc. and 2) spline64 resize works great far as ringing goes if the source is clean. no matter what you will lose some detail downsizing etc, but gradients and what not will be kept as long as enough bitrate is applied. I use fitCD and HCenc when doing my DVD's and find the output good on my 42" tv. if you have a bad source there is only so much you can do, but at SD resolutions I usually end up at 4.5Mbps with MPEG2 and 2Mbps with AVC(x264). just remember 1/2 the resolution don't always mean 1/2 bitrate as each dot will be responsible for more motion on a low res encode than a high res one. same rules apply to 720p and 1080p.
16. It doesn't work that way, it's not whether by your standards you have a 4k studio quality master, you either do or you don't and you don't. If you don't want to download the 60gb version at least go for the 13.5gb mxf version
this is a complete waist of time!!!! just as in the other files, they can't be read.."zip is invalid"
17. Since you are having fun testing resizers, why don't you add Blackman to the list?
18. Originally Posted by vhelp
while testing the Bicubicresize() filter...
By the way, BicubicResize(X, Y, b=-1, c=0) is much sharper than BicubicResize(X, Y).
19. i will add it as soon as i find it and download it if its an avs plugin. never knew about it. thanks.

I wish that website at least split those large files into two or three sections to minimize redownloading when the file mysteriously becomes corrupt after download. its a real pain to have to go through the whole cycle, over and over again. and, it would save the bandwidth on their end. surely, its costing them to keep it/the site up and running. i mean, those are huge files. anyway. gotta get ready for work.
20. jagabo, i read that about the (-1,0) but i could not figure out how to use them. i mean, you know.. bicubicresize(720,320) is the only values allowed, not -1,0
21. You have to specify the width and height. BicubicResize(720, 480, b=-1, c=0). And don't forget, oversharpening creates halos, buzzing (aliased) edges, and increases noise. Professionally mastered video is intentionally not too sharp so as to avoid those problems.
22. ahaa.. i got it you have to enter it bicubicresize(720,320, -1,0) toda!
23. i'm a slow typist.
24. yes, that definately did it! must sharper now. after the 200th encode and i'm getting tired of searching for the folder and dragging into my player. i'm going to update it to include a folder of video files and just let it play from that somehow. like a playlist. ever since i finally found a dspack for delphi xe7, i'm back in the saddle in tools and things anyway.

i think i have to redo the way i resize/crop because there is distortion along the horizontal. lines, especially, are noticable in bicubicxxx filters, for obvious reasons. but i think if i make a small change in the way i resize, it might help just a tad. i choose the 320h because is was evenly dividable in both 16 and 8 macroblocks and keeps the video completely clean of macroblock distortions (from the mpeg/avc compression encode). but i think that if i resize to 321 and crop to 320, or some other value(s) it might help a little......i will have to experiment on this idea some, later. i gotta get ready for work. still sitting here typing...
25. Originally Posted by vhelp
It doesn't work that way, it's not whether by your standards you have a 4k studio quality master, you either do or you don't and you don't. If you don't want to download the 60gb version at least go for the 13.5gb mxf version
this is a complete waist of time!!!! just as in the other files, they can't be read.."zip is invalid"
I have no idea what you are talking about, the y4m is a never before lossy compressed raw render straight from Blender, since it's raw and never before compressed you can shrink it down losslessly using general purpose compressors to about 1/3 the size, the archive is not invalid, i downloaded it and extracted it just fine and ended up with a 180gb y4m file.
26. If you don't want to download the 60gb version at least go for the 13.5gb mxf version.
I was referring to this resource that you suggested: "4K DCP 4096 x 2160 (14 GB, Digital Cinema Package) :: Mirror 1 (Netherlands)".

This is a 13.5GB zip file: https://download.blender.org/demo/movies/ToS/ToS_4k_DCP.zip -- there is no other 13.5gb link. so that must be the one you are referring to.
27. update..

was finally able to download the 13.5GB completely. reading an mxf file is painfully slow. (even slower than encoding x265 in placebo mode.) I am getting about 0.2 fps on my dell inspiron i3-2370M 2.40GHz cpu laptop. that compression (to decompress the frames inside that xml structure) is slooooooow. and on my pc at work, the i7-6700, i get about 2.1 fps. what a waist of money on an upgrade i was planning to spend for just that much improvement.
28. I have decided to transcode the MXF file over to UT (yv12 color space, 4:2:0) at 1920w by 800h in order to make testing encoders more practical and survivable. This will give me at least 30 fps processing speed via quicksync for H264/AVC on this dell laptop and (H264/AVC and H265/HEVC) on the work pc i7-6700 (skylake) and the acer aspire laptop (kabylake) and just work off an newly clean USB3.0 1TB HDD for these systems with the portable avisynth if necessary.

I choose a section of the clip to snip out of the MXF transcode to UT to use for the encoding tests. It contained some areas that were easily spot'able for artifacts, thus, starting from frame 1195 through 4450, or timecode: (0:00:49.792 through 0:03:09.583) (aprox 3min 5sec) are what I am snipping out of the MXF for the testing platform. Later, I will snip the remaining portions into one master file and do final test encodes.

Right now, I am getting 0.9 fps transcode speed on the acer laptop since 10:42pm and its at aprox 1850 frames (1m:17s of the 3m:05s test clip) into the process.
29. Can you share the link to that specific 13.5 GB file?
30. post # 26

its an MXF file. huge. and very sloooooow to process. that's why i decided to transcode it to UT codec.
(i think the decoder to process MXF is very slow or poorly written. i used ffmpeg to transcode it. see below for the method i used, maybe you know faster.)

but that will be an even bigger file.. but way faster to process. worth it to transcode to UT and work off that.

Code:
`ffmpeg  -ss 0:00:49.792  -i "g:\src\tos_picture.mxf.mkv"    -vf scale=1920:800   -vcodec utvideo  -pix_fmt yuv420p  -t 0:03:10  -an -y "tos_picture.mxf.1920x800.avi"`

Statistics