How to Cut Videos in PHP

Cloudmersive
2 min readApr 12, 2023

--

If we’re just uploading short & sweet clips to our website, complex video processing through in-depth editing applications is often surplus to our needs. It’s generally sufficient to implement simple, one-dimensional video editing services which add value while conserving our processing power.

Using the code examples below, you can easily take advantage of our straightforward “Cut a Video to a Shorter Length” API. The underlying service allows you to cut videos by simply providing a specific start time & desired video length in your request parameters. The examples in this article are provided in PHP, but you can just as easily find additional code examples by visiting our swagger API documentation page.

To install the PHP client using Composer, let’s execute the following command from our command line:

composer require cloudmersive/cloudmersive_video_api_client

With that out of the way, let’s copy in our code examples and provide a free-tier API key to authenticate our request (get one by registering a free account on our website; this provides a limit of 800 API calls per month and no 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.
$start_time = new \DateTime("2013-10-20T19:20:30+01:00"); // \DateTime | Optional; Specify the desired starting time of the cut video in TimeSpan format.
$time_span = new \DateTime("2013-10-20T19:20:30+01:00"); // \DateTime | Optional; Specify the desired length of the cut video in TimeSpan format. Leave blank to include the rest of the video. Maximum time is 4 hours.

try {
$result = $apiInstance->videoCutVideo($input_file, $file_url, $start_time, $time_span);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling VideoApi->videoCutVideo: ', $e->getMessage(), PHP_EOL;
}
?>

As documented above, it’s best to use the $file_url request when our videos regularly exceed 2 GB in size.

That’s all there is to it — now you can quicly slice & dice videos with ease.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet