Convert Video to MP4 Format in PHP
Building your own MP4 video conversion workflow? You can skip a few laborious steps in that process using the ready-to-run PHP code examples provided below. Our free-to-use Video to MP4 conversion API will quickly detect and convert a variety of (common) input video file formats — and in your request you can also (optionally) standardize your output videos’ width & height, framerate, and quality by customizing a few intuitive request parameters.
Our first step is to install the client SDK, which we can do by running the following command:
composer require cloudmersive/cloudmersive_video_api_client
Next, we can implement the below PHP code examples to structure our API call. To authenticate your requests for free, you’ll need a free-tier API key (which you can get by registering a free account on our website):
<?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->videoConvertToMp4($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->videoConvertToMp4: ', $e->getMessage(), PHP_EOL;
}
?>
That’s all there is to it! Please note that it’s recommended to use the File URL upload option if your videos exceed 2GB in size.