How to Trim a Video using PHP?

Video trimming is the process of removing irrelevant or unnecessary parts of your footage. It's possible to cut a video at certain points to cut out parts, like the first few seconds, or to divide the video up into separate files. To do this in PHP, you'll need to use a video editing API. Here is how you can accomplish that with just a few lines of code in your PHP application:

Input Code
// Don't forget to install the required library using Composer:
// composer require creatomate/creatomate
$client = new Creatomate\Client('Your API Key');

$output = $client->render([
 'source' => new Creatomate\Source([
  'output_format' => 'mp4',
  'elements' => [
   [
    'type' => 'video',
    'source' => 'https://cdn.creatomate.com/demo/mountains.mp4',
    'trim_start' => 1,
    'trim_duration' => 3
   ]
  ]
 ])
]);

var_dump($output);
Output Video

How it works: Start by installing the Creatomate package from Packagist. In this example, we're working with just one video in the scene, so there's only a single element involved. When specifying a video element, you can use any publicly hosted URL without worrying about its format because the API will automatically convert it to MP4.

To perform the actual trimming, use the "trim_start" and "trim_duration" properties to specify the timestamps where to cut the video. In this case, we slice the video starting from 1 second at the beginning and keep a maximum duration of 3 seconds.

If you want to handle more advanced video editing tasks, make sure to explore the code video editor too. This online tool not only lets you programmatically trim videos but also simplifies a wide range of video editing tasks needed for any type of PHP video processing. You can check it out below.

Edit this Code Example in the Video Editor

Easily design your own video templates using the online editor. Then, export your templates as JSON and integrate them into your PHP application to create any kind of video – completely through code.

Related PHP Video Editing Questions

Start automating today

Start with a full-featured trial with 50 credits, no credit card required.
Get started for free