
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 ...
04/08/2026
Dalet, a leading technology and service provider for media-rich organizations, t...
04/07/2026
April 7 2026, 19:00 (PDT) Detective Conan: Fallen Angel of the Highway Opens in...
01/06/2026
January 6 2026, 05:30 (PST) Dolby Sets the New Standard for Premium Entertainment at CES 2026
Throughout the week, Dolby brings to life the latest innovatio...
02/05/2026
Dalet, a leading technology and service provider for media-rich organizations, t...
01/05/2026
Ratings Roundup is a rundown of recent rating news and is derived from press rel...
01/05/2026
BKB Bare Knuckle Boxing ( BKB ), today announced the appointment of Will Wright ...
01/05/2026
Lawo has been at the center of the industry's transition to IP and other next-generation technologies. At NAB 2026, its story was the Edge One AV stagebox, ...
01/05/2026
HBA Media, acting on behalf of NBC Sports and Churchill Downs Incorporated, has announced broadcast and streaming distribution for Kentucky Derby 152, taking pl...
01/05/2026
By Bailey Pennick
One of the most exciting things about the Sundance Film Festi...
01/05/2026
Florals for spring? Groundbreaking. But a playlist that tells you which The Devi...
01/05/2026
One of the world's biggest popstars is headed to El Cl sico. Later this mont...
01/05/2026
Limited-edition model celebrates 15th anniversary
Heritage Audio's range of monitor controllers has just gained a new member, the Baby RAM Black Edition...
01/05/2026
Dumble recreation now available as UAD plug-in
Along with their renowned processing plug-ins, Universal Audio have been steadily introducing emulations of c...
01/05/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
01/05/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
01/05/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
01/05/2026
To celebrate the opening of its new showroom and office, Lightware UK hosted a dedicated launch event at the new London location. The event welcomed partners, c...
01/05/2026
Choice without compromise
The broadcast industrys transformation is accelerating, and traditional broadcasters are having to fundamentally reinvent how they o...
01/05/2026
Beam Dynamics will return to MPTS 2026 with its asset intelligence platform, helping systems integrators, live production teams, media facilities and profession...
01/05/2026
Best-in-class UX design and rapid, scalable delivery for next-generation viewing experiences
Leading video software provider, Synamedia, today announced a coll...
01/05/2026
Compact new cforce MAX lens motor brings unrivaled speed and responsiveness to t...
01/05/2026
Panavision welcomes Fritz Heinzle as Vice President of Sales
Brie Clayton May 1, 2026
0 Comments
Heinzle will support Panavision's global growth s...
01/05/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
01/05/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
01/05/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
01/05/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
01/05/2026
Share
Copy link
Facebook
X
Linkedin
Bluesky
Email...
01/05/2026
LONDON, APRIL 30, 2026 The Post Republic London's Re-recording Mixer and Dialogue Editor Dan Johnson has built a reputation for clean, emotionally resonan...
01/05/2026
Adobe Unveils Powerful New Innovations in Photoshop & Lightroom
Deepa Subramaniam April 30, 2026
0 Comments
Your most tedious creative tasks just got ea...
01/05/2026
Berklee Partners with Santander US to Establish Global Opportunity Fund The $400,000 grant offers students access to experiential learning opportunities withi...
01/05/2026
Student Spotlight: Keziah Thomas The Indian composer, who was named the 2026 student commencement speaker for Berklee College of Music, talks about how shes p...
01/05/2026
Friday 1 May 2026
Hannah Waddingham and Ncuti Gatwa to host the series final tw...
01/05/2026
Friday 1 May 2026
Got plans? Cancel them. Sky Sports Big Weekend is coming
Sky Sports is preparing for a bumper weekend of live action, including Manchester ...
01/05/2026
Friday 1 May 2026
Sky Sports to broadcast all matches from World Sevens Football London edition
Sky Sports will be the exclusive UK broadcaster of the women...
01/05/2026
Back to All News
NIAJ Fest Gets Los Angeles In on the Joke With Free Pop-Up Events
Entertainment
01 May 2026
GlobalUnited States
Link copied to clipboard
...
01/05/2026
RT Sport awarded first pick free-to-air on Wednesday nights
Champions League and Super Cup finals
Highlights on Wednesday nights
RT today (Thursday 30 Apri...
01/05/2026
January 5 2026, 18:30 (PST) NBCUniversal's Peacock to Be First Streamer to ...
30/04/2026
The Professional Women's Hockey League (PWHL) concluded its third regular season on Saturday, reporting growth across attendance, viewership, digital engage...
30/04/2026
NBC Sports will air national MLB coverage on Sundays beginning May 3, with MLB Sunday Leadoff on Peacock and NBCSN at 12:30 p.m. ET, followed by the debut of th...
30/04/2026
Clear-Com has appointed Brian Grahn as Market Outreach Manager of the Americas and Ben Turnwell as Business Development Manager for EMEA live.
Grahn joined Cle...
30/04/2026
ARRI has introduced the cforce MAX, a new lens motor for the Hi-5 lens control system. The cforce MAX is twice as fast as the cforce plus motor it replaces whil...
30/04/2026
Knuerr, Voxtronic, and IHSE will jointly present an integrated control room solu...
30/04/2026
The CW Network and ESPN have announced an agreement to make the ESPN App the exclusive streaming home for all CW Sports live events. CW Sports will continue to ...
30/04/2026
Ed Sheeran's The Loop' tour launched in Auckland in January 2026 before moving on to Australia, with South America and the United States to follow late...
30/04/2026
Audinate has announced Dante Preset Creator, a free online tool for configuring Dante network settings before hardware is available on site. Presets created in ...
30/04/2026
Yahoo Sports has announced the appointment of Jarrod Schwarz as General Manager of Yahoo Sports. Schwarz will oversee product, design, and technology; revenue a...
30/04/2026
Nielsen has released a new report, Get Ready with Media Intelligence: 2026 FIFA World Cup Edition, examining U.S. soccer viewership trends, fan engagement, and ...
30/04/2026
USA Lacrosse and SportsEngine have announced an expanded partnership, naming Spo...
30/04/2026
Telos Alliance will participate in the 2026 Media Production and Technology Show (MPTS), taking place May 13-14 at Olympia London. Rather than exhibiting from a...