Blog
Video Editing

Using FFmpeg for Video Trimming: a Step-By-Step Guide

Trimming is a core feature for any video editing application. While making it from the ground up can be complicated and time-consuming, with FFmpeg you can implement it with just a few lines of code. Read the article for the specifics.

[navigation]

FFmpeg installation

FFmpeg is a robust and actively developed cross-platform command-line utility that can process audio and video files. To download the latest version of FFmpeg check out its official website where you'll find precompiled binaries for Linux, Windows and macOS. Another way of installing FFmpeg is by using package managers like Homebrew for macOS or APT for Linux. Here is how you can install FFmpeg on macOS with Homebrew:

[code]
brew install ffmpeg
[/code]

A note on different types of encoded frames

Modern video compression formats like H264 (Advanced Video Coding or AVC) and H265 (High Efficiency Video Coding or HEVC) are inter-picture-prediction formats meaning that not only do they compress individual frames but also manage to decrease the video file size even further by finding similarities between neighboring video frames and storing only the incremental data necessary to decode the frame.

The frames that use only intra-frame compression and don't rely on neighboring frames are called I-frames or key-frames. Other kinds of frames are called P-frames and B-frames. Because they rely on data from neighboring frames their size are smaller than that of an I-frame but their decoding time is also longer as it is necessary to find the closest I-frame and sequentially decode all P- or B-frames that stand between a key frame and a frame that we
want to decode. The difference between P-frames and B-frames is that P-frames rely on data from previous frames while B-frames can use both previous and future frame data.

The key information here that needs to remembered as we begin to discuss the actual video trimming is that majority of frames in a typical video file are of P or B type. While it's technically possible to encode a video in a way that it would contain only I-frames it is rarely done this way as resulting file would be way too big.

You can examine the types of frames in your video by using another utility from FFmpeg developers called ffprobe:

[code]
ffprobe -select_streams v -show_frames \
-show_entries frame=pict_type \
-of csv video.mp4
[/code]

Trimming video

To trim a video with FFmpeg we are going to use the following parameters.
-ss or seek time specifier is used to specify the start time of trim range.
-t or duration parameters is used when we want to tell FFmpeg how long the trimmed section of a video should be.
-to or end time parameter is used when end timestamp of trim range is known.
The values of listed parameters can be specified in two formats: one involving hours, minutes, seconds and optionally milliseconds (HOURS:MM:SS.MILLISECONDS or -ss 01:10:34.000), and seconds only (-ss 130.5).

[code]
ffmpeg -i input.mp4 -ss 12 -to 50 -c copy trimmed.mp4
[/code]

The above command will trim the part of the video input.mp4 starting from 12 seconds and ending at 50 seconds and save it as trimmed.mp4. This operation will be very quick as it does not re-encode the video and audio streams because -c copy parameter was used.
The downside of not re-encoding is that you will most likely observe either black frames or frozen first frame at the beginning of a trimmed video or the video will start a bit later than was told to during trimming. This happens because there was no I-frame located at the start time and thus the video stream in resulting video will actually begin as soon as the next I-frame is found.

To prevent this issue, we can skip the -c copy parameter and force FFmpeg to re-encode the video during trimming:

[code]
ffmpeg -i input.mp4 -ss 00:00:12 -t 00:00:10 trimmed.mp4
[/code]

This command will extract the 10 seconds long segment of the video input.mp4 starting from 12th second and encode it as trimmed.mp4 with default encoding parameters. FFmpeg offers a large list of encoder settings. Please check out the official encoding guides for H264 and H265 codecs.

How to use FFmpeg on iOS

Thanks to the FFmpeg being an open-source project, it can be ported to other platforms that lack official support. One of the available distributions of FFmpeg for iOS is called FFmpegKit and is available at https://github.com/arthenica/ffmpeg-kit.

When using FFmpeg on iOS it is up to you how to implement the user interface of the app. You'll have to get an input from a user, assemble the query similar to previous examples and call FFmpeg. It is also worth mentioning that FFmpeg does not provide a playback interface and you'll have to implement a video player in your app using other libraries or frameworks.

A simpler way

Trimming is an important feature, but it alone doesn't even make an MVP. You will need many other functionalities, whether you are making a social network, dating app, video editor, or something else. 

To get ahead of the competition and get to market faster and with less hassle, you can use a Video Editor SDK. It only takes a few lines of code to integrate, and includes a large array of functionalities:

  • Trimming/merging
  • Sound editing
  • Text/picture/GIF overlays
  • Transition effects
  • 3D masks
  • Color filters (LUTs)
  • Slo-mo/rapid
  • Music providers integration
  • etc.

Video Editor SDK supports iOS, Android, Flutter, and React Native, making it accessible to most app developers.

Get Video Editor SDK for Your App  Get Free Trial  

Top