Trimming a video allows you to eliminate unnecessary or unwanted parts that are not relevant to your desired content. By cutting video at specified points, you can remove specific portions, like the first few seconds of a video, or divide a single video into multiple files. To accomplish this task using JavaScript, you'll need a video editing API. Here's how to do that using Node.js in just a few lines of code:
const Creatomate = require('creatomate');
const client = new Creatomate.Client('Your API Key');
client.render({
source: new Creatomate.Source({
outputFormat: 'mp4',
elements: [
new Creatomate.Video({
source: 'https://cdn.creatomate.com/demo/mountains.mp4',
trimStart: 1,
trimDuration: 3,
}),
],
}),
}).then((renders) => {
console.log(renders);
});
How it works: In order to perform video editing, install the Creatomate package from NPM. In this example, we're dealing with a single video in the scene, so there's only a single video element involved. We can reference any publicly hosted video URL without worrying about its format since the API will handle the conversion to MP4 automatically.
To trim the video, we make use of the "trim_start" and "trim_duration" properties. These properties enable us to specify the timestamps at which we would like to slice the video. In this case, we slice the video starting from 1 second into the beginning and keep a maximum duration of 3 seconds.
If you're interested in handling more advanced video editing tasks, be sure to explore the code video editor as well. This online tool not only provides the ability to programmatically trim videos but also streamlines a wide range of video editing tasks necessary for any kind of Node.js video processing. Check it out below.