How-to Trim a Media File

Before you start

This guide assumes that you understand the basics of making a Transcode API request. We recommend completing the Getting Started with Transcoding Media guide first.

Trim a media file

This guide shows you how to use the Media Transcode API to trim the beginning or end off of a media file. Perhaps there are bars and tone at the start or some leading black to be sliced off.

You can picture this operation like the timeline of a non-linear editor, where you want to remove the first few seconds:

The Transcode API uses the segment parameter that can be applied to an input to make this operation possible.

The segment parameter has the following properties

  • segment
    • start
      • The time in seconds to begin transcoding relative to the file start (media before the start value is skipped). Fractional seconds are valid, for example 10.5. If this parameter is omitted, the input file begins at the start of the media.
    • duration
      • The time in seconds to stop transcoding relative to the segment start. Fractional seconds are valid, for example 10.5. If this parameter is omitted, the media is trimmed from start to end of the media.

Example with a public URL

The following example trims the input file so that the output file's first frame is from the 30-second mark of the input.

"inputs": [
    {
       "source": {
            "url": "https://example.com/videos/input.mp4"
       },
       "segment": {
            "start": 30
       }
    }
],

Example with cloud storage

The same is possible with files located in the storage object. Read more about Media Input and Output.

"inputs": [
    {
       "source": {
            "ref": "my-storage-id",
            "filename": "input.mp4"
       },
       "segment": {
            "start": 30
       }
    }
],

Example trimming multiple files

It is also possible to trim and stitch at the same time. Read our guide on How to Stitch/Concatenate Multiple Files to see more detail. Here is an example trimming the beginning off of the first file and the end off of the second file in the sequence:

"inputs": [
    {
       "source": {
            "url": "https://example.com/videos/intro.mp4"
       },
       "segment": {
            "start": 30
       }
    },
    {
       "source": {
            "url": "https://example.com/videos/main_content.mp4"
       },
       "segment": {
            "duration": 300
       }
     }
],