How to Trim a Video Using FFMpeg

Trimming video is a common task in video editing and it is crucial to include this feature in a video editing tool. Learn how to use FFmpeg to trim videos using the ‘trim’ filter, -ss, and -sseof options in this article.
by Josephine Loo ·

Contents

    Video content has proven to generate more engagement than static content like text and photos on social media, resulting in higher clicks and longer average watch times. With the growing usage of video content for social media marketing, there is no doubt that there is a high demand for a versatile video editing tool.

    If you're building a video editing tool, including a trimming feature is crucial. In this article, I will guide you on how to use FFmpeg to trim a video, using the video below as an example:

    …and also introduce Bannerbear, a video generation API that complements the trimming feature and enhances the capabilities of your video editing tool.

    What is FFmpeg

    FFmpeg is a complete, cross-platform solution to record, convert, and stream audio and video. It can decode, encode, transcode, mux, demux, stream, filter, and play media files in any format. It is also highly portable—it compiles and runs in a wide variety of build environments, machine architectures, and configurations like Linux, Mac OS X, Microsoft Windows, etc.

    FFmpeg contains multiple tools for end-users to convert, play, and analyze media files and libraries for developers to use in different applications, such as libavcodec, libavutil, libavformat, libavfilter, libavdevice, libswscale, and libswresample. When you download the FFmpeg packages/executable files to your machine, these libraries will be downloaded automatically.

    Installing FFmpeg

    Before you begin, make sure to download FFmpeg from the official website. You can follow this step-by-step guide to install FFmpeg on your Mac, Windows, or Ubuntu/Linux. For reference, the FFmpeg version used in this tutorial is v6.0.

    Method 1: Using the ‘trim’ Filter

    FFmpeg’s libavfilter library offers various filters to help you transform videos. Using the trim filter, you can trim the input video by specifying the duration or the start/end time of your desired output video.

    Trim by Duration

    You can trim the input video to a specific duration using the duration parameter in seconds. By default, the trimming will start from the beginning of the video.

    The command below trims the input video to 3 seconds, starting from the beginning using the trim filter:

    ffmpeg -i input.mp4 -vf trim=duration=3 output.mp4
    

    In the command above, -vf trim=duration=3 applies the trim filter and sets the duration to 3 seconds.

    Result:

    Note that when using a filter in your FFmpeg command, the video will be re-encoded. Therefore, you can’t add -c copy to the command to stream copy the video (which skips the re-encoding process). However, you can still specify -c:a copy to only copy the audio stream:

    ffmpeg -i input.mp4 -vf trim=duration=3 -c:a copy ouput.mp4
    

    Trim by End Time

    You can also use the end parameter to trim a video up to a specific end time. This parameter works similarly to the duration parameter and trims the video from the beginning to the specified time. For example, the command below trims the input video to end at the 3-second mark:

    ffmpeg -i input.mp4 -vf trim=end=3 output.mp4
    

    Result:

    Trim by Start Time

    If you want to trim the video starting from a specific time, use the start parameter. The command below trims the video starting from the 3-second mark instead of the beginning:

    ffmpeg -i input.mp4 -vf trim=start=3,setpts=PTS-STARTPTS output.mp4
    

    Slightly different from the last two commands, setpts=PTS-STARTPTS is added to the command to adjust the timestamps of the output video. This resets the timestamps of the output video to start from 00:00:00. Otherwise, the video would start from 00:00:03 and the first three seconds would be black screens.

    Result:

    Trim by Start and End Times

    Combining both start and end parameters, you can trim specific parts of the video. The command below trims the input video from the 3-second mark to the 6-second mark as the output:

    ffmpeg -i input.mp4 -vf trim=start=3:end=6,setpts=PTS-STARTPTS output.mp4
    

    Alternatively, you can use the shorthand notation that omits the parameter name:

    ffmpeg -i input.mp4 -vf trim=3:6,setpts=PTS-STARTPTS output.mp4
    

    Result:

    Both the start and end parameters set the timestamp in the seconds unit. You can also set the timestamp in the timebase units using start_pts and end_pts or trim the video by the number of frames using start_frame and end_frame.

    Method 2: Using the -ss Option

    The seeking (-ss) option can be used to seek within the input video while performing any task and there are several ways to use this option to trim a video:

    Input Seeking

    The seeking (-ss) option can be used before -i <input_video> to seek the specified start time in the input video file and save only the frames after the time as output.

    The command below seeks from the 00:00:03 timestamp, resulting in an output video that is trimmed from the 3-second mark to the end:

    ffmpeg -ss 00:00:03 -i input.mp4 output.mp4
    

    Result:

    To specify the duration of the trimmed segment, we can use -t followed by the desired duration in seconds. For instance, the command below trims 3 seconds of the input video starting from 00:00:03:

    ffmpeg -ss 00:00:03 -t 3 -i input.mp4 output.mp4
    

    Alternatively, you can use -to to indicate the ending timestamp. The command below trims the video from 00:00:03 to 00:00:06:

    ffmpeg -ss 00:00:03 -to 00:00:06 -i input.mp4 output.mp4
    

    Result:

    Output Seeking

    You can also use the seeking (-ss) option after specifying the input file with -i. However, this approach processes the entire input video and discards frames after the specified start time. Therefore, it may be less efficient for large files compared to input seeking.

    Here’s the command that trims the input video from 00:00:03 to the end, using output seeking:

    ffmpeg -i input.mp4 -ss 00:00:03 output.mp4
    

    To trim 3 seconds of the input video starting from 00:00:03, use this command:

    ffmpeg -i input.mp4 -ss 00:00:03 -t 3 output.mp4
    

    And to trim the video from 00:00:03 to 00:00:06, use this command:

    ffmpeg -i input.mp4 -ss 00:00:03 -to 00:00:06 output.mp4
    

    Note: All three commnads above should give you results similar to using input seeking.

    Method 3: Using the -sseof Option

    The -sseof option is similar to -ss but instead of trimming the input video from the beginning, it trims the video from the end. It takes a negative value that indicates a position before the end of the file. This can be useful when you want to process or trim the last few seconds of a video.

    Here’s an example that trims the last 2 seconds of the input video as output:

    ffmpeg -sseof -2 -i input.mp4 output.mp4
    

    You can also use the HH:mm:ss format:

    ffmpeg -sseof -00:00:02 -i input.mp4 output.mp4
    

    Result:

    Using Bannerbear API

    In addition to FFmpeg, you can use Bannerbear's Video Generation API. The API allows you to integrate video generation capabilities into your application by making a simple API request. With Bannerbear, you can merge videos with different formats, as well as other media files like MP3 and PNG, into an MP4 video of your desired resolution simply by using the API. This would be useful for users who want to merge different videos together after trimming them.

    To use the Bannerbear API to merge videos, you need to provide the necessary data (eg. desired resolution, asset/video URLs, transtition effect, etc.) in your API request. Here's an example using JavaScript:

    var data = {
      "width" : 800,
      "height" : 500,
      "transition" : "fade",
      "soundtrack_url" : "https://ffmpeg-bb.s3.ap-southeast-1.amazonaws.com/audio.mp3",
      "inputs" : [
        {
          "asset_url": "https://ffmpeg-bb.s3.ap-southeast-1.amazonaws.com/input1.mp4"
        },
        {
          "asset_url": "https://ffmpeg-bb.s3.ap-southeast-1.amazonaws.com/input2.wmv",
          "trim_to_length_in_seconds": 3
        },
        {
          "asset_url": "https://ffmpeg-bb.s3.ap-southeast-1.amazonaws.com/image.png",
          "trim_to_length_in_seconds": 3
        }
      ]
    }
    
    fetch('https://api.bannerbear.com/v2/movies', {
      method: 'POST',
      body: JSON.stringify(data),
      headers: {
        'Content-Type' : 'application/json',
        'Authorization' : `Bearer ${API_KEY}`
      }
    })
    

    And here's the result:

    🐻 Bear Tips: You can refer to the API reference to learn more about how to use the API.

    Conclusion

    FFmpeg is a powerful tool for developers who want to build video editing functions for their applications. Besides trimming, you can also use it to merge videos, add subtitles, resize videos, etc. However, there is a learning curve to understanding its vast number of commands and options.

    If you prefer a simpler approach or want to streamline the video generation process, Bannerbear API is an excellent alternative. With Bannerbear, you can integrate video generation capabilities into your application easily by making a simple API request with the necessary data. If you haven’t already, register a free account and check it out yourself!

    About the authorJosephine Loo
    Josephine is an automation enthusiast. She loves automating stuff and helping people to increase productivity with automation.

    How to Automatically Add Watermark to Instagram Reels For Reposting Using Bannerbear (Node.js Tutorial)

    Posting a mix of original and user-generated content, including properly credited reposts, can be an effective social media strategy for media companies to engage with their audience and keep their feed dynamic and interesting. Let's learn how to streamline this process using Bannerbear in Node.js!

    Ecommerce Localization: How to Generate Images with Local Currencies and Languages

    Localized content improves customer experiences while improving your business standing. Here's how to automatically localize pricing graphics with nocode tools.

    How to Automatically Add Personalized Images to Your Beehiiv Newsletter

    Learn how to enhance your Beehive newsletter with personalized images using Bannerbear. In this article, we will learn how to automatically add personalized images to Beehiiv newsletters to make the content more relevant and engaging for your subscribers.

    Automate & Scale
    Your Marketing

    Bannerbear helps you auto-generate social media visuals, banners and more with our API and nocode integrations

    How to Trim a Video Using FFMpeg
    How to Trim a Video Using FFMpeg