PHP video manipulation

PHP video manipulation overview

After uploading videos to Cloudinary, they can be manipulated in many ways.

The syntax for transforming and delivering videos is generally the same as that for images, and you can apply the majority of available image transformations to video as well. For example, you can resize, crop, rotate, set video quality and format or use auto quality and/or auto_format, add text or image overlays to your videos, and more.

There are also a number of special options you can use for transforming and delivering video content. For example, you can adjust their size, shape, speed, duration, quality, and appearance. There are also some features that are specific to audio.

This section introduces you to the basics of PHP video streaming and manipulation. For complete details on all video manipulation functionality, see Video manipulation and delivery and the Video transformation reference.

PHP video transformation functionality

In addition to transformation features that are equally relevant for images and video, such as resizing, cropping, rotating, adding text or image overlays, and setting video quality or format, there are a variety of special transformations you can use for video. For example, you can:

You can optionally specify all of the above transformations within a cl_video_tag method, which automatically generates an HTML5 video tag including the transformation URL sources for the main formats supported by web browsers (webm, mp4 and ogv), as well as a poster thumbnail image. This enables the browser to automatically select and play the video format it supports. The video files are created dynamically when first accessed by your users.

For details, see the video tag documentation and the HTML5 Video Player blog post.

PHP video transformation code examples

This section provides examples of using PHP to apply some of the video transformation features mentioned in the previous section.

Example 1:

The following example resizes the dog video to 40% of it's original size and rotates it by 20 degrees. It also adds a semi-transparent cloudinary logo in the bottom right corner, using a southeast gravity with adjusted x and y coordinates to reach the corner of the rotated video.

PHP:
cl_video_tag("dog", array("transformation"=>array(
    array("width"=>0.4, "angle"=>20),
    array("overlay"=>"cloudinary_icon", "opacity"=>50, 
    "width"=>60, "gravity"=>"south_east", "y"=>15, "x"=>60)
    )));

Example 2:

The following example adjusts the brightness of a skiing video, and sets its radius to max in order to give a telescope-like effect. It then appends a copy of the video in reverse, and the plays forward again, but in slow motion.

cl_video_tag("ski_jump", array("transformation"=>array(
    array("overlay"=>"video:ski_jump", "flags"=>"splice", "effect"=>"reverse"),
    array("overlay"=>"video:ski_jump", "flags"=>"splice", "effect"=>"accelerate:-50"),
    array("effect"=>"brightness:10", "radius"=>"max")
    )));

Example 3:

The following example generates a <video> tag for a video that will loop continuously in an HTML5 video player with default controls. When the video loads a default poster is displayed based on the middle frame of the video. The tag automatically includes the source URLs for .webm, .mp4, and .ogg formats, with the requested transformations: use the first 10 seconds of the original video, crop to 360X480 using pad cropping, generate at 70% quality to control file size.

cl_video_tag("dog", array(
"loop"=>true, "controls"=>true,
"transformation" => array(
   "height"=>360, "width"=>480, "quality"=>70, "duration"=>"10", "crop"=>"pad"),
"fallback_content"=>"Your browser does not support HTML5 video tags."
));

The above code results in the following HTML:

<video controls='1' loop='1' poster='https://res.cloudinary.com/demo/video/upload/c_pad,du_10,h_360,q_70,w_480/dog.jpg'>
  <source src='https://res.cloudinary.com/demo/video/upload/c_pad,du_10,h_360,q_70,w_480/dog.webm' type='video/webm'>
  <source src='https://res.cloudinary.com/demo/video/upload/c_pad,du_10,h_360,q_70,w_480/dog.mp4' type='video/mp4'>
  <source src='https://res.cloudinary.com/demo/video/upload/c_pad,du_10,h_360,q_70,w_480/dog.ogv' type='video/ogg'>
  Your browser does not support HTML5 video tags.
</video>