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.
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.
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.
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.
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.
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/cld_rubiks_guy.mp4").resize(
crop().width(200).height(200)
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/cld_rubiks_guy.mp4").resize(
crop().width(200).height(200)
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/cld_rubiks_guy.mp4").resize(
crop().width(200).height(200)
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/cld_rubiks_guy.mp4").resize(
crop().width(200).height(200)
);
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).
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(fill().width(250).height(250));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(fill().width(250).height(250));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(fill().width(250).height(250));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(fill().width(250).height(250));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(
fill()
.width(250)
.aspectRatio("1.0")
.gravity(compass("north_west"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(
fill()
.width(250)
.aspectRatio("1.0")
.gravity(compass("north_west"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(
fill()
.width(250)
.aspectRatio("1.0")
.gravity(compass("north_west"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(
fill()
.width(250)
.aspectRatio("1.0")
.gravity(compass("north_west"))
);
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).
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.
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/g_auto_demo.mp4")
.videoEdit(trim().startOffset("7.0"))
.resize(
fillPad()
.height(400)
.aspectRatio("9:16")
.gravity(autoGravity())
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/g_auto_demo.mp4")
.videoEdit(trim().startOffset("7.0"))
.resize(
fillPad()
.height(400)
.aspectRatio("9:16")
.gravity(autoGravity())
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/g_auto_demo.mp4")
.videoEdit(trim().startOffset("7.0"))
.resize(
fillPad()
.height(400)
.aspectRatio("9:16")
.gravity(autoGravity())
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/g_auto_demo.mp4")
.videoEdit(trim().startOffset("7.0"))
.resize(
fillPad()
.height(400)
.aspectRatio("9:16")
.gravity(autoGravity())
);
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.
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
crop()
.width(200)
.height(150)
.gravity(compass("north"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
crop()
.width(200)
.height(150)
.gravity(compass("north"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
crop()
.width(200)
.height(150)
.gravity(compass("north"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
crop()
.width(200)
.height(150)
.gravity(compass("north"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(crop().width(450).aspectRatio(2.5));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(crop().width(450).aspectRatio(2.5));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(crop().width(450).aspectRatio(2.5));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(crop().width(450).aspectRatio(2.5));
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:
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/cld_rubiks_guy.mp4").resize(
crop().width(150).height(150).x(10).y(80)
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/cld_rubiks_guy.mp4").resize(
crop().width(150).height(150).x(10).y(80)
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/cld_rubiks_guy.mp4").resize(
crop().width(150).height(150).x(10).y(80)
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/cld_rubiks_guy.mp4").resize(
crop().width(150).height(150).x(10).y(80)
);
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.
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(150).height(150));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(150).height(150));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(150).height(150));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(150).height(150));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(100).aspectRatio("1:2"));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(100).aspectRatio("1:2"));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(100).aspectRatio("1:2"));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(100).aspectRatio("1:2"));
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.
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(fit().height(150).aspectRatio("1.0"));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(fit().height(150).aspectRatio("1.0"));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(fit().height(150).aspectRatio("1.0"));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(fit().height(150).aspectRatio("1.0"));
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.
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(limitFit().width(250).height(250));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(limitFit().width(250).height(250));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(limitFit().width(250).height(250));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(limitFit().width(250).height(250));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitFit().height(150).aspectRatio("1.0")
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitFit().height(150).aspectRatio("1.0")
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitFit().height(150).aspectRatio("1.0")
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitFit().height(150).aspectRatio("1.0")
);
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.
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
pad()
.width(250)
.height(250)
.background(color("black"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
pad()
.width(250)
.height(250)
.background(color("black"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
pad()
.width(250)
.height(250)
.background(color("black"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
pad()
.width(250)
.height(250)
.background(color("black"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
pad()
.height(150)
.aspectRatio("2:1")
.background(color("black"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
pad()
.height(150)
.aspectRatio("2:1")
.background(color("black"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
pad()
.height(150)
.aspectRatio("2:1")
.background(color("black"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
pad()
.height(150)
.aspectRatio("2:1")
.background(color("black"))
);
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.
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(
pad()
.width(480)
.height(320)
.background(blurred().intensity(400).brightness(15))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(
pad()
.width(480)
.height(320)
.background(blurred().intensity(400).brightness(15))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(
pad()
.width(480)
.height(320)
.background(blurred().intensity(400).brightness(15))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("cld_rubiks_guy.mp4").resize(
pad()
.width(480)
.height(320)
.background(blurred().intensity(400).brightness(15))
);
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.
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitPad()
.width(400)
.height(150)
.background(color("green"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitPad()
.width(400)
.height(150)
.background(color("green"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitPad()
.width(400)
.height(150)
.background(color("green"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitPad()
.width(400)
.height(150)
.background(color("green"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitPad()
.width(100)
.aspectRatio(0.66)
.background(color("green"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitPad()
.width(100)
.aspectRatio(0.66)
.background(color("green"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitPad()
.width(100)
.aspectRatio(0.66)
.background(color("green"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
limitPad()
.width(100)
.aspectRatio(0.66)
.background(color("green"))
);
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: 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:
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
fill()
.width(250)
.height(250)
.gravity(compass("north"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
fill()
.width(250)
.height(250)
.gravity(compass("north"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
fill()
.width(250)
.height(250)
.gravity(compass("north"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
fill()
.width(250)
.height(250)
.gravity(compass("north"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
fill()
.width(250)
.height(250)
.gravity(compass("south_east"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
fill()
.width(250)
.height(250)
.gravity(compass("south_east"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
fill()
.width(250)
.height(250)
.gravity(compass("south_east"))
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(
fill()
.width(250)
.height(250)
.gravity(compass("south_east"))
);
Automatic gravity selection ensures that the most interesting areas are selected as the main focus throughout the duration of each video. Each video is analyzed to find the optimal region, allowing you to adjust the size or aspect ratio to fit all of your requirements. As the optimal region of the video could be moving from frame to frame, the cropped area will adjust accordingly. To adjust the behavior of the automatic cropping algorithm to focus on faces within a video, you can also specify an additional focal preference.
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.
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:
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("ship.mp4").resize(
fill()
.width(300)
.aspectRatio(ar1X1())
.gravity(autoGravity())
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("ship.mp4").resize(
fill()
.width(300)
.aspectRatio(ar1X1())
.gravity(autoGravity())
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("ship.mp4").resize(
fill()
.width(300)
.aspectRatio(ar1X1())
.gravity(autoGravity())
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("ship.mp4").resize(
fill()
.width(300)
.aspectRatio(ar1X1())
.gravity(autoGravity())
);
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, this rollercoaster video automatically adjusts the aspect ratio and padding to keep the two people in the frame as much as possible:
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/Roller_Coaster.mp4").resize(
fillPad()
.width(400)
.aspectRatio("1.0")
.gravity(autoGravity())
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/Roller_Coaster.mp4").resize(
fillPad()
.width(400)
.aspectRatio("1.0")
.gravity(autoGravity())
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/Roller_Coaster.mp4").resize(
fillPad()
.width(400)
.aspectRatio("1.0")
.gravity(autoGravity())
);
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("docs/Roller_Coaster.mp4").resize(
fillPad()
.width(400)
.aspectRatio("1.0")
.gravity(autoGravity())
);
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.
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. 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.
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.
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.
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.
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(100)).delivery(dpr("2.0"));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(100)).delivery(dpr("2.0"));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(100)).delivery(dpr("2.0"));
// This SDK requires imports from @cloudinary/url-gen. Learn more in the SDK docs.new CloudinaryVideo("dog.mp4").resize(scale().width(100)).delivery(dpr("2.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.