Last updated: Oct-31-2023
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:
c_fill,g_auto:facesPad with blurred background
c_pad,b_blurred:400:15Keep the right half
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_150sets 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.5sets the width to half the original width.
iwas values sets the dimension to the initial height or initial width of the original video respectively. For example,
w_iwsets 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:
asignifies the relative width and
bthe relative height (e.g.,
- a decimal value representing the ratio of the width divided by the height (e.g.,
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_200is 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.
- 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
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:
|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.|
|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.|
|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.|
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.
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:
You could deliver the
c_crop transformation shown above as follows:
The following sections explain how each of the crop modes behave.
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).
Fill a 250-pixel square with the Rubik's cube video:
Fill a 250-pixel square with the top-left part (gravity northwest) of the Rubik's cube video:
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).
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.
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.
Crop the dog video to a width of 200 pixels and a height of 150 pixels, with north gravity:
Crop the dog video to a width of 450 pixels and an aspect ratio of 2.5:
You can specify a region of the original video to crop by giving the
y coordinates of the top left corner of the region together with the
height of the region. You can also use percentage based numbers instead of the exact coordinates for
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 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.
Scale the dog video to a width of 150 pixels (maintains the aspect ratio by default):
Scale the dog video to a width and height of 150 pixels without maintaining the aspect ratio:
Scale the dog video to a width of 25% (maintains the aspect ratio by default):
Scale the dog video to a width of 100, changing the aspect ratio to 1:2:
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.
Resize the dog video to fit within a width and height of 250 pixels while retaining the aspect ratio:
Resize the dog video to fit within a 150-pixel square defined by aspect ratio and height:
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.
Limit the dog video to a width and height of 250 pixels while retaining the aspect ratio:
Limit the dog video to a 150-pixel square defined by aspect ratio and height:
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.
Resize and pad the dog video with a black background to a width and height of 250 pixels:
Resize and pad the dog video with a black background to a rectangle of height of 150 pixels, and aspect ratio 2:1:
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
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.
Limit the dog video to a bounding box of 400 x 150 pixels, and pad with a green background:
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:
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.
The basic gravity value is specified by giving a compass direction to include:
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 north:
- With gravity set to south_east:
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.
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
- 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 423error 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_urlparameter 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_autocropping 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
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):
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):
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
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):
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:
- 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:birdstries to keep all birds in the frame, whereas
g_auto:birdfocuses on the most prominent bird.
g_auto:<object>is not available to accounts that use alternative data centers.
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.
(Center gravity) g_auto:dog
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 CodeSandbox 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
This code is also available in GitHub.
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 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.
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.