Convert Videos to WEBM Format in PHP

Cloudmersive
2 min readApr 7, 2023

--

Local video conversions can put a lot of strain on our system. Thankfully, using the below code, you can easily implement a free Video Format Conversion API which automatically detects input formats & converts them to WEBM format. The external processing will take a lot of pressure off your system, and intuitive request parameters will allow you to customize the output video’s width & height, framerate, and quality.

We can install the client SDK by running the following command:

composer require cloudmersive/cloudmersive_video_api_client

Lastly, to structure our API call, we can copy and paste from the ready-to-run PHP code examples provided below. To authorize our request, we’ll need a free-tier API key, which we can get by registering a free account on the Cloudmersive website (free accounts provide a limit of 800 API calls per month & zero additional commitments).

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\VideoApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$file_url = "file_url_example"; // string | Optional; URL of a video file being used for conversion. Use this option for files larger than 2GB.
$max_width = 56; // int | Optional; Maximum width of the output video, up to the original video width. Defaults to original video width.
$max_height = 56; // int | Optional; Maximum height of the output video, up to the original video width. Defaults to original video height.
$preserve_aspect_ratio = true; // bool | Optional; If false, the original video's aspect ratio will not be preserved, allowing customization of the aspect ratio using maxWidth and maxHeight, potentially skewing the video. Default is true.
$frame_rate = 56; // int | Optional; Specify the frame rate of the output video. Defaults to original video frame rate.
$quality = 56; // int | Optional; Specify the quality of the output video, where 100 is lossless and 1 is the lowest possible quality with highest compression. Default is 50.

try {
$result = $apiInstance->videoConvertToWebm($input_file, $file_url, $max_width, $max_height, $preserve_aspect_ratio, $frame_rate, $quality);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling VideoApi->videoConvertToWebm: ', $e->getMessage(), PHP_EOL;
}
?>

And that’s it — no more code required!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet