
How to do Cropping and Scaling Using Javascript by Isak March 11, 2016 Howto
The Vidispine transcoder first crops, then rotates, then scales. This post is about how to use Javascript and some equations to emulate doing scaling before cropping. Brush up on your math skills and dive right in.
Let's look at the details of the previous blog post Using scripts to automatically fit video.
As said in the post above, the order of the operations is crop, rotate, scale. Assume this image (or video):
The goal is to rotate this image into the right orientation, and by cropping and padding we will keep the aspect ratio. Finally we would like a 1920 1080 output. First the crop. We crop the sides by 150 pixels each, and pad the top and bottom with 511 pixels each.
Then rotate the image. We now have:
You see that 1742:980 is very close to 16:9. Now we can scale the image.
The shape tag for the operation above:
1920 1080 -511 -511 150 150 #000080 1 1 1
2
3
4
5
6
7
8
9
10
11
12
13
1920
1080
-511
-511
150
150
#000080
1
1
So what if we wanted to scale first, then crop? Fortunately you can switch the order of the operations yourself, with a little bit of algebra. Above, we did:
Original dimension: x
Crop of the two sides: c1, c2
Scaling to size: x
If you start with an image that is 1000 pixels wide, and you want to scale it down to 500 pixels, then crop 100 pixels from each side, that is equivalent to:
Crop by 100 1000 / 500 = 200 pixels on each side. That gives you an image that is 1000 200 200 = 600 pixels.
Then scale the image to 300 pixels.
What if you know what the scaled dimension should be after cropping, and you know how much to crop in the scaled dimension? The scaling of the cropped dimension is x / (x c1 c2). That means, the cropped part in the scaled dimension is
c1 = c1 x / (x c1 c2)
c2 = c2 x / (x c1 c2)
Solving this equation system gives:
c1 = c1 x / ( c1 c2 x )
c2 = c2 x / ( c1 c2 x )
Or, in English. If you have an image that is 1000 pixels wide and you want to scale the image. After scaling, you want to crop 100 pixels from each side, and then the result to be 300 pixels:
Crop by 100 1000 / ( 100 100 300 ) = 200 pixels on each side. That gives you an image that is 1000 200 200 = 600 pixels.
Then scale the image to 300 pixels.
Ok, another example. Let's say you have an video, w h. We want to scale the height so it is 900 pixels. Then crop the sides so the resulting video is 1600 pixels wide.
The scaling of the video is 900 / h. That means that the scaled width is w = 900 / h w. So, in the scaled dimension what we want to crop is ( 1600 w ) / 2 on each side. But cropping is done before scaling. With the equation above, this is what the script could look like:
JavaScript
var inWidth = shape.getVideoComponent().get(0).getResolution().getWidth(); var inHeight = shape.getVideoComponent().get(0).getResolution().getHeight(); var scaling = preset.getVideo().getScaling(); var scaledWidth = scaling.getWidth() / inHeight * inWidth; var cropAfterScale = (scaling.getHeight() - scaledWidth) / 2: var cropBeforeScale = cropAfterScale * inWidth / (2 * cropAfterScale scaledWidth); scaling.setWidth(scaledWidth); scaling.setLeft(cropBeforeScale); scaling.setRight(cropBeforeScale);
1
2
3
4
5
6
7
8
9
10
11
var inWidth = shape.getVideoComponent().get(0).getResolution().getWidth();
var inHeight = shape.getVideoComponent().get(0).getResolution().getHeight();
var scaling = preset.getVideo().getScaling();
var scaledWidth = scaling.getWidth() / inHeight * inWidth;
var cropAfterScale = (scaling.getHeight() - scaledWidth) / 2:
var cropBeforeScale = cropAfterScale * inWidth / (2 * cropAfterScale scaledWidth);
scaling.setWidth(scaledWidth);
scaling.setLeft(cropBeforeScale);
scaling.setRight(cropBeforeScale);
Use this with a preset like:
1600 900 1 1 1
2
3
4
5
6
7
8
1600
900
1
1
Most recent headlines
05/01/2027
Worlds first 802.15.4ab-UWB chip verified by Calterah and Rohde & Schwarz to be ...
07/10/2026
Dalet, a leading technology and service provider for media-rich organizations, today announced the latest Long-Term Supported (LTS) release of Dalet Flex. Build...
06/09/2026
June 9 2026, 23:00 (PDT) Dolby and MagentaTV Bring Fans Closer to the FIFA Worl...
05/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
05/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
05/08/2026
Profuz Digital is set to launch a powerful upgrade to its Profuz LAPIS digital media management platform at IBC this year at Stand 8.A88. France Galop, Canal+ F...
05/08/2026
KRK, a Gibson brand and leader in professional studio monitoring for nearly four decades, launches its new V Series Five Studio Monitors, the latest evolution o...
04/08/2026
Live production in the cloud is a growing trend across the industry, and Major L...
04/08/2026
LAFC co-managing owner will succeed Don Garber on Jan. 1, 2027, as league prepar...
04/08/2026
In 2022, when the PGA TOUR was breaking ground on its new, state-of-the-art broa...
04/08/2026
Provided by POVORA, the new camera angle offers a dealer's-eye view of the c...
04/08/2026
Spiideo will provide the camera setup, with NEP handling broadcast integration and off-site video operations...
04/08/2026
Production team streamed more than 750 youth rugby matches using ATEM switchers,...
04/08/2026
Agreement gives KTC early access to NDI's Advanced SDK for integration across commercial displays, digital signage, and interactive panels...
04/08/2026
Twelve projects across four categories recognize collaborative work in productio...
04/08/2026
Multi-year agreement will introduce SkeleTRACK, semi-automated offside, automate...
04/08/2026
Three mc 56 consoles, dual A__UHD Core engines, RAVENNA networking, and HOME management supported live sound, monitoring, and international broadcast production...
04/08/2026
New contribution products support ultra-low-latency video transport across fixed, remote, and mobile production environments...
04/08/2026
Company will showcase updates across PRIME, Chyron LIVE, Paint, virtual production, newsroom workflows, and venue control...
04/08/2026
Exhibit will feature SilverBack, VersaBrix, VersaFrame, VF-9100, and FiberSaver-10G platforms...
04/08/2026
Platform updates target cloud-based production, high-density ST 2110 monitoring,...
04/08/2026
LMG will take control of Gravity Media's U.S. mobile-unit fleet as the compa...
04/08/2026
Amp-modelling rigs get major upgrade
HeadRush have recently released a major firmware update that brings some powerful new features to their Prime, Core and...
04/08/2026
Generate a choir from a single voice track
Following years of development, Am I Audio have just officially launched their debut plug-in. Created in collabor...
04/08/2026
Official EMT-endorsed reverbs now available to all
Two more of Universal Audio's renowned UAD plug-ins have just gained new native versions that can be ...
04/08/2026
SBS helps Australians understand why every Census counts
Media releases
As Australia prepares for the 2026 Census on Tuesday 11 August, SBS is helping ensur...
04/08/2026
Canada invests in the future of HF communications with Rohde & Schwarz solutions After selection by the Canadian Department of National Defence (DND) as the p...
04/08/2026
Blendkit Expands Beyond Blender
Brie Clayton August 4, 2026
0 Comments
Popular 3D Asset Library Launches Alpha Plugins for Maya, Godot, and Rhino
Blend...
04/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/08/2026
Ikegami has chosen the 11-14 September 2026 International Broadcasting Convention in Amsterdam as the European market launch venue for additions to its UNICAM p...
04/08/2026
Appear (IBC2026 booth 1.C61) will showcase new X & VX Platform capabilities that extend broadcast-grade performance from venue to cloud
Appear ASA (OSE: APR), ...
04/08/2026
LiveU, the global leader in live IP-video solutions, today highlighted the crucial role its EcoSystem played in Broadcast Management Group's (BMG) successfu...
04/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/08/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
04/08/2026
Homecoming Artist Spotlight: Dreion The singer, songwriter, and producer talks about finding his village at Berklee ahead of his Homecoming performance on Sep...
04/08/2026
Tuesday 4 August 2026
Sky Sports agrees deal with SANZAAR for international rugby rights 2026 - 2030
The deal means Sky will have exclusive rights to the inau...
04/08/2026
Kill Tony Launches on Supercast, Giving Fans Premium & Ad-Free Access to Comedy&...
04/08/2026
More details coming soon....
04/08/2026
More details coming soon....
04/08/2026
More details coming soon....
04/08/2026
Surging AI demands are driving the need for massive datasets and context windows that burst past the confines of system memory.
But rising needs aren't me...
04/08/2026
For robotaxis and other autonomous vehicles (AVs), the hardest problems aren'...
04/08/2026
Members of the Open Secure AI Alliance - now more than 120 organizations strong - are developing new guidelines to strengthen agentic AI cybersecurity as the an...