Programmable Media

Video resizing and cropping

Last updated: Nov-13-2024

Responsive design and art direction generally requires displaying videos at a variety of sizes, often much smaller than the original.

If you deliver full size videos and rely on browser-side resizing (using CSS or HTML width and height attributes), users are forced to download unnecessarily large files. Therefore, videos should always be delivered from the server at their final size.

When you use any of the Cloudinary resizing transformations, the sizing (scaling/cropping) is performed on the server side, and the asset is always delivered to the browser at the requested size.

Here are some examples of different cropping or resizing techniques used on the same video:

Auto-crop with focus on faces
c_fill,g_auto:faces
Pad with blurred background
c_pad,b_blurred:400:15
Keep the right half
c_crop,x_0.5

Resize dimensions

You can set the target dimensions of your resized video by specifying width, height, and/or the target aspect ratio as qualifiers of your resize transformation.

  • Using an integer value for w (width) or h (height) sets the new dimension to that number in pixels. For example, w_150 sets the width to exactly 150 pixels.
  • Using a decimal value for width or height sets the new dimension relative to the original dimension. For example, w_0.5 sets the width to half the original width.
  • Using ih or iw as values sets the dimension to the initial height or initial width of the original video respectively. For example, w_iw sets the width to the same value as the original width of the video. This may be useful when applying chained transformations or setting the dimensions of an overlay.
  • Aspect ratios are specified using the ar (aspect ratio) parameter, as follows:
    • a:b where a signifies the relative width and b the relative height (e.g., ar_4:3 or ar_16:9).
    • a decimal value representing the ratio of the width divided by the height (e.g., ar_1.33 or ar_2.5). 1.0 is a perfect square.
  • In most cases, you will specify both width and height or width/height along with an aspect ratio to define the exact required dimensions. However, in rare cases, you may choose to specify only one of these 3 resize qualifiers, and Cloudinary will automatically determine the missing dimension as follows:

    • If you provide only width or only height, then the other dimension is automatically calculated to deliver the original aspect ratio. For example, if your original asset is 400*600, then specifying c_crop,w_200 is the same as specifying c_crop,w_200,h_300. Supported for all resize and crop modes.
    • If you provide only the aspect ratio: If ar > 1, the original width is maintained and the height is cropped to deliver the requested ratio. If ar < 1, the original height is maintained, and the width is cropped accordingly. Supported for cropping modes only.

Note
If you want to resize only one dimension, and keep the other dimension at its original size (rather than the automatic determination described above), you can specify only width or only height, and add the fl_ignore_aspect_ratio flag qualifier.

Resize and crop modes

When changing the dimensions of an uploaded video by setting the video's height, width, and/or aspect ratio, you need to decide how to resize or crop the video to fit into the requested size. Use the c (crop/resize) parameter for selecting the crop/resize mode. Cloudinary supports the following video resize/crop modes:

Crop/resize mode Behavior
Cropping modes If the requested dimensions have a different aspect ratio than the original, these modes crop out part of the video.
fill Resizes the video to fill the specified dimensions without distortion. The video may be cropped as a result.
fill_pad Same as fill, but avoids excessive cropping by adding padding when needed. Supported only with automatic cropping.
crop Extracts a region of the specified dimensions from the original video without first resizing it.
Resize modes These modes adjust the size of the delivered video without cropping out any elements of the original video.
scale Resizes the video to the specified dimensions without necessarily retaining the original aspect ratio.
fit Resizes the video to fit inside the bounding box specified by the dimensions, maintaining the aspect ratio.
limit Same as fit, but only scales down the video.
pad Resizes the video to fit inside the bounding box specified by the dimensions, maintaining the aspect ratio, and applies padding if the resized video does not fill the whole area.
lpad Same as pad, but only scales down the video.

Note
When creating dynamic delivery URLs, if you specify only the width and/or height parameters, but no cropping mode (no c_<mode>), the video is scaled to the new dimensions by default. However, there is no default cropping mode when using the Cloudinary SDK helper methods (see Embedding videos in web pages), so a cropping mode must be explicitly set.

Some of the cropping modes keep only a certain part of the original video in the resulting video. By default, the center of the video is kept in the crop, but this is not always ideal. To keep the parts of the video that are important to you, you can use the gravity parameter. For example, you can specify to keep faces, or gravitate towards an automatically determined area of interest. You can also guide the crop towards areas of your video defined by compass points, for example, north to keep the top part of the video, or south_east to keep the bottom-right part.

Resize and crop mode examples

The following examples show the same video resized to a width and height of 200 pixels, using different methods of resizing and cropping.

The original video is 224 x 400 pixels:

Resizing the video to 200 x 200 pixels, using crop, scale, fill and pad results in the following videos:

c_crop,h_200,w_200 c_scale,h_200,w_200
c_fill,h_200,w_200 b_black,c_pad,h_200,w_200

You could deliver the c_crop transformation shown above as follows:

The following sections explain how each of the crop modes behave.

fill

The fill cropping mode creates a video with the exact specified dimensions, without distorting the video. This option first scales up or down as much as needed to at least fill both of the specified dimensions. If the requested aspect ratio is different than the original, cropping will occur on the dimension that exceeds the requested size after scaling. You can specify which part of the original video you want to keep if cropping occurs, using the gravity parameter (set to center by default).

See full syntax: c_fill in the Transformation Reference.

Example 1: Fill a specific area with a video

Fill a 250-pixel square with the Rubik's cube video:

Example 2: Fill a specific area with a specific part of a video

Fill a 250-pixel square with the top-right part (gravity northeast) of the Rubik's cube video:

fill_pad

The fill_pad cropping mode tries to prevent a "bad crop" by adding padding to any frames where interesting content is lost if the standard fill mode is applied. This is especially useful if the aspect ratio of the delivered video is considerably different from the original's aspect ratio. It is only supported in conjunction with Automatic cropping (g_auto).

Notes

See full syntax: c_fill_pad in the Transformation Reference.

Example: Compare standard fill mode with fill_pad mode

The video on the left is delivered at 9:16 aspect ratio using the fill crop mode, and the video on the right using the fill_pad crop mode. You'll see the beginning of the video is cropped the same, but the video on the right then pads the video to ensure all of the subjects are shown.

Original video (click to play)

c_fill c_fill_pad

crop

The crop cropping mode extracts a region of the specified dimensions from the original video. No scaling is applied, so applying the crop mode to the same video of different resolutions can provide very different results. You can specify the gravity parameter to select which area or object to extract, or use fixed coordinates cropping.

See full syntax: c_crop in the Transformation Reference.

Example 1: Crop a video to specified dimensions (width and height)

Crop the dog video to a width of 200 pixels and a height of 150 pixels, with north gravity:

Example 2: Crop a video to specified dimensions (aspect ratio and width)

Crop the dog video to a width of 450 pixels and an aspect ratio of 2.5:

Fixed coordinates cropping

You can specify a region of the original video to crop by giving the x and y coordinates of the top left corner of the region together with the width and height of the region. You can also use percentage based numbers instead of the exact coordinates for x, y, w and h (e.g., 0.5 for 50%) . Use this method when you know beforehand what the correct absolute cropping coordinates are, as in when your users manually select the region to crop out of the original video.

To resize the Rubik's cube video to focus mainly on the cube, the video is cropped to a 150x150 region starting at the coordinate x = 10 and y = 80:

scale

The scale resize mode changes the size of the video exactly to the specified dimensions without necessarily retaining the original aspect ratio: all original video parts are visible but might be stretched or shrunk. If only the width or height is specified, then the video is scaled to the new dimension while retaining the original aspect ratio, unless you also include the ignore_aspect_ratio flag.

See full syntax: c_scale in the Transformation Reference.

Example 1: Scale while maintaining aspect ratio

Scale the dog video to a width of 150 pixels (maintains the aspect ratio by default):

Example 2: Scale without maintaining aspect ratio

Scale the dog video to a width and height of 150 pixels without maintaining the aspect ratio:

Example 3: Scale by a percentage

Scale the dog video to a width of 25% (maintains the aspect ratio by default):

Example 4: Scale by setting the aspect ratio and width

Scale the dog video to a width of 100, changing the aspect ratio to 1:2:

fit

The fit resize mode resizes the video so that it takes up as much space as possible within a bounding box defined by the specified dimensions. The original aspect ratio is retained and all of the original video is visible.

See full syntax: c_fit in the Transformation Reference.

Example 1: Fit the video inside a square bounding box

Resize the dog video to fit within a width and height of 250 pixels while retaining the aspect ratio:

Example 2: Use aspect ratio to define a bounding box

Resize the dog video to fit within a 150-pixel square defined by aspect ratio and height:

limit

The limit resize mode behaves the same as the fit mode but only if the original video is larger than the specified limit (width and height), in which case the video is scaled down so that it takes up as much space as possible within a bounding box defined by the specified dimensions. The original aspect ratio is retained and all of the original video is visible. This mode doesn't scale up the video if your requested dimensions are larger than the original video's.

See full syntax: c_limit in the Transformation Reference.

Example 1: Limit the size of the video to a square bounding box

Limit the dog video to a width and height of 250 pixels while retaining the aspect ratio:

Example 2: Use aspect ratio to define a limiting bounding box

Limit the dog video to a 150-pixel square defined by aspect ratio and height:

pad

The pad resize mode resizes the video to fill the specified dimensions while retaining the original aspect ratio and with all of the original video visible. If the proportions of the original video do not match the specified dimensions, padding is added to the video to reach the required size. You can also specify where the original video is placed by using the gravity parameter (set to center by default), and specify the color of the background in the case that padding is added.

See full syntax: c_pad in the Transformation Reference.

Example 1: Resize and pad a video to fill the square defined by width and height

Resize and pad the dog video with a brown background to a width and height of 250 pixels:

Example 2: Resize and pad a video to fill the rectangle defined by aspect ratio

Resize and pad the dog video with a brown background to a rectangle of height of 150 pixels, and aspect ratio 2:1:

Example 3: Pad with blurred video background

You may sometimes need to deliver a video with an aspect ratio very different than the target video player dimensions, for example delivering a portrait video in a landscape-oriented video player. In these cases, you can use a blurred version of the same video as the padding background. To do this, specify the blurred value instead of a color as the background. You can also optionally specify the intensity of the blur effect (range: 1-2000, default 100) and the brightness of the background video (range: -300-100, default 0).

For example, deliver the Rubik's cube video in a 320*480 HTML5 video player by padding the portrait video with a blurred version of the same video at an intensity of 400 and a brightness of 15.

lpad (limit pad)

The lpad resize mode behaves the same as the pad mode but only if the original video is larger than the specified limit (width and height), in which case the video is scaled down to fill the specified dimensions while retaining the original aspect ratio and with all of the original video visible. This mode doesn't scale up the video if your requested dimensions are bigger than the original video's. If the proportions of the original video do not match the specified dimensions, padding is added to the video to reach the required size. You can also specify where the original video is placed by using the gravity parameter (set to center by default), and specify the color of the background in the case that padding is added.

See full syntax: c_lpad in the Transformation Reference.

Example 1: Scale down and pad a video

Limit the dog video to a bounding box of 400 x 150 pixels, and pad with a green background:

Example 2: Scale down and pad a video using aspect ratio

Limit the dog video to a bounding box specified by an aspect ratio of 0.66 with a width of 100 pixels, and pad with a green background:

Example 3: Pad a video on all sides

Apply padding to all sides of the dog video by specifying a bounding box that's square, and has a width of 1.1 times the original width:

Gravity positions for crops

When used with cropping modes that crop out part of a video, the gravity qualifier (g in URLs) specifies which part of the original video to keep when one or both of the requested dimensions is smaller than the original.

See full syntax: g (gravity) in the Transformation Reference.

Compass positions

The basic gravity value is specified by giving a compass direction to include: north_east, north, north_west, west, south_west, south, south_east, east, or center (the default value). The compass direction represents a location in the video, for example, north_east represents the top right corner.

For example, fill a 250-pixel square with the dog video while retaining the aspect ratio:

  • Original video:
  • With gravity set to east:
  • With gravity set to west:

Automatic gravity for crops (g_auto)

Automatic gravity selection is an advanced AI feature that ensures that the most interesting areas are selected as the main focus throughout the duration of each video, allowing you to adjust the size or aspect ratio to fit all of your requirements. When using this feature, each video is analyzed to find the optimal region. As the optimal region of the video may be moving from frame to frame, the cropped area will adjust accordingly while still smoothly transitioning from frame to frame.

You can optionally fine-tune the behavior of the automatic cropping algorithm to instruct it to focus on faces or specific objects within a video.

There are several benefits to using AI auto-gravity for videos:

  • Saves time and effort by eliminating the need for manual cropping of each frame
  • Improves the visual quality of the video by making it appear more focused and professional
  • Makes videos more engaging by allowing the viewer to focus on the important parts of the video
  • Optimizes a video for different screen sizes, such as smartphones, tablets, and computer screens
  • Reduces the file size of the video, making it easier to upload and share

Notes and tips
  • The automatic cropping algorithm analyzes the entire video to determine the areas to focus on, which means it can take several seconds or minutes, depending on the length of the original video (an HTTP 423 error will be returned until the analysis is complete). Therefore, it's recommended to generate the transformation eagerly during upload or using an explicit method call for existing videos, along with an eager_notification_url parameter to notify your application when the content-aware cropping transformation is ready for delivery.
  • Once a video has been analyzed by the automatic cropping algorithm, any subsequent transformations happen on the fly as usual. This includes adjusting the size and aspect ratio.
  • You can only use automatic gravity once per transformation and not within a layer.
  • You can add the getinfo flag (fl_getinfo in URLs) in your transformation to return the proposed g_auto cropping results, including confidence scores in JSON, instead of delivering the transformed video. You can then integrate the g_auto results into an external workflow.
  • There are special transformation counts for videos using g_auto.

Apply automatic content-aware gravity by cropping your video with either the fill or fill pad crop modes and setting the gravity transformation parameter to auto (g_auto in URLs).

For example, to crop this video to a square aspect ratio whilst keeping the ship as the main focus throughout, using the fill crop mode:

In some cases, you may find that cropping to a different aspect ratio cuts out interesting parts of the content. If this is the case, consider using the fill pad crop mode with automatic cropping, which uses padding where necessary to keep more of the interesting content in the crop.

For example, using c_fill_pad on this rollercoaster video automatically adjusts the aspect ratio and padding to keep the two people in the frame as much as possible (click any video to see all three videos running simultaneously):

Original video (click to play)

c_fill c_fill_pad

Auto-cropping videos to focus on faces

By default, the automatic cropping algorithm uses a gaze prediction algorithm to identify the most interesting areas of the video. To adjust the algorithm that is used and detect a single face or multiple faces, specify the focal preference. The available options are:

  • g_auto:face: Focuses the crop on the largest face detected in the video.
  • g_auto:faces: Focuses the crop on all the detected faces in the video.

The examples below show the difference between the two face detection options (click any video to see all three videos running simultaneously):

Original video (click to play)

g_auto:faces g_auto:face

Auto-cropping videos to focus on a specified object

To adjust the automatic cropping algorithm so that instead of focusing on the most interesting areas of the video it focuses on a specific object, use g_auto:<object>.

Compare the following videos, where you can see the original, the first cropped one using g_auto and the second cropped one using g_auto:ball (click any video to see all three videos running simultaneously):

Original video (click to play)

g_auto g_auto:ball

If the specified object is not found in the video, the algorithm falls back to the default automatic cropping algorithm.

The available options are currently all from the Large Vocabulary Instance Segmentation (lvis) model, which contains thousands of general objects. Start typing the name of an object or category in the search box to see if it's supported:



Notes
  • If there are many instances of the same object in a frame, the algorithm prioritizes the most prominent specified object.
  • You can add an 's' to any of the objects to prioritize all of the objects. For example, g_auto:birds tries to keep all birds in the frame, whereas g_auto:bird focuses on the most prominent bird.
  • Currently, g_auto:<object> is not available to accounts that use alternative data centers.

Comparison with default gravity

Below is a comparison between the original video of a dog catching a frisbee, and the same video with the aspect ratio inverted. The left video was cropped using default center gravity and the other using automatic gravity focusing on the dog. Watch how the auto cropped (right-hand) video keeps the main subject (the dog) in view at all times, even as it moves across the frame in the original video.

Click any video below to see the comparison in action or use our automatic cropping demo to try it on a variety of samples or on your own videos.

Original video (click to play)
Default crop
(Center gravity)
g_auto:dog

Code explorer: Changing source based on orientation

You could also use automatic cropping to show the correct video depending on the device orientation. If the user lands on the page whilst browsing in portrait orientation, you could set the HTML5 video source to a vertical video that has been automatically cropped. If the user switches to landscape, then the source reverts back to the original landscape video. The sandbox below is a very simple example of how you could do this. Use the "Change Orientation" button to simulate rotating a device, or try it out on mobile. Notice that this time, g_auto is used for the portrait orientation, rather than g_auto:dog.

This code is also available in GitHub.

Tip
Enjoy interactive learning? Check out more code explorers!

Device Pixel Ratio (DPR)

Different devices support different DPR values, which is defined as the ratio between physical pixels and logical pixels. This means that a device with support for a higher DPR uses more physical pixels for displaying a video, resulting in a clearer, sharper video.

DPR illustration

Use the dpr parameter to set the DPR value of the delivered video. The parameter accepts a numeric value specifying the DPR multiplier.

For example, the following URL dynamically generates the video named dog scaled to a width of 100 pixels. Setting the dpr value to 1.0, 2.0 (as in the code example) or 3.0 generates the following videos, while resizing the video to match the required DPR.

DPR 1.0 DPR 2.0 DPR 3.0

Now you can create an HTML video tag with the required dimensions and deliver a video with the resolution that best matches the specified pixel density of your users' devices. The three videos below are all displayed with a width of 200 pixels using the <video> tag width attribute, while you see more details and a better visual result for the last two videos (dpr 2 and dpr 3 respectively) if you view this documentation using a device that supports a higher DPR.

DPR 1.0 (200 width, 331KB) DPR 2.0 (400 width, 437KB) DPR 3.0 (600 width, 474KB)

Note
When setting a DPR value, you must also include a crop/resize transformation specifying a certain width or height.

See full syntax: dpr (DPR) in the Transformation Reference.

✔️ Feedback sent!

Rate this page: