How to trim video using FFmpeg?

Trimming video with FFmpeg is easy. The overall process involves installing FFmpeg on your computer, then offering the source video file as input with the -i flag, and then specifying the timestamps where the video needs to be trimmed.

Following are the step-by-step instructions-

Trim video using FFmpeg with re-encoding

Use the -ss flag offered by FFmpeg to extract a section of your input and then search within it to browse and change the input video’s area.

The following steps show how to use the -ss flag to cut the video:

Step 1: Install FFmpeg

If you don’t have FFmpeg installed on your computer, you must install it first. You can download FFmpeg from the official website and follow the installation instructions.

We have the following guides to help you get started

  1. Install FFmpeg on Windows
  2. Install FFmpeg on MacOS
  3. Install FFmpeg on Unix

Step 2: Browse the Folder

  1. Search Command Prompt or Terminal in the Start menu or Finder and press Enter.
  2. Use the cd command to navigate the folder containing your input video file. For example, type cd Desktop/Videos and hit Enter if your video file is in the Videos folder on your desktop.

Step 4: Trim the Video with FFmpeg

  1. Run the following command to trim a video in FFmpeg:

    ffmpeg -i inputfile.mp4 -ss 00:00:20 -t 00:00:40 -c copy outputfile.mp4

  2. Each flag has its own meaning as follows

  • -i inputfile.mp4: Indicates the input file name and its extension.
  • -ss 00:00:20: States the start time of the desired clip. In this example, we begin the video at 20 seconds.
  • -t 00:00:40: Choose how long you want the clip extracted. In this case, we are selecting a 40-second clip.
  • -c copy: It will copy audio and video streams without re-encoding. This process is faster than re-encoding but only works if the output format is exactly the same as the input format.
  • outputfile.mp4: Indicates the output file name with extension.
  1. After running the above command, change the filename from inputfile.mp4 to outputfile.mp4.

  2. FFmpeg will extract the desired clip from the input file and save it as a new output file when you run the above command by pressing Enter.

Step 5: Inspect the Output

In order to determine whether the video has been cut or not, play the output file.

And, it’s done! with FFmpeg, you may trim a video by following these easy steps.

When to trim a video?

There are situations when you might wish to cut a video as follows.

1. Remove undesired footage

There can be certain portions of a movie that you’ve captured that are pointless or undesirable. By trimming the video, you can get rid of these bits and maintain exactly what you need.

2. Highlight specific content

Trimming can allow you to identify a particular area of a video you’d prefer to focus on and produce a smaller video clip that focuses on the information you want to highlight.

3. Create a trailer

When making a teaser or trailer for a lengthier video, trimming can be helpful. You can make a preview that captures the attention of the entire video by picking the most interesting or thrilling segments of it and compressing them into a shorter clip.

4. Save storage space

Video files occupy a lot of space on the storage media. While the size of the video file can vary according to the codec, bitrate, and compression settings, 60-second video footage can occupy almost 450MB on your HDD.

By trimming the video, you can save a lot of space on storage media; thus bringing down the overall cost of hosting your videos.

5. Improve loading time

Trimming a video can improve its loading time by reducing the file size. When you trim a video, you remove unwanted or unnecessary portions, which results in a shorter video with a smaller file size. Smaller files require less bandwidth and storage, which enables them to load and buffer more quickly when accessed online.

When viewers stream a video online, their devices need to download and buffer the video data before playback. Smaller video files can be downloaded and buffered faster, leading to reduced waiting times and a smoother streaming experience for viewers. This is particularly important for users with slower internet connections or those accessing the video from mobile devices with limited bandwidth.

Overall, you should crop a video when you believe that doing so will enhance its overall quality or increase the effectiveness with which it fulfills its intended goal.

How to trim 6 seconds off a video using java - ffmpeg?

Keep in mind that for this code to function, FFmpeg must be set up on your system and added to the path.

Step 1: Create a Java project

  • Select your favorite text editor or integrated development environment (IDE). For
    example, Eclipse, NetBeans, and IntelliJ IDEA are among the more well-liked options.
  • Create a new project in your text editor or IDE.
  • Select Create New Project from the homepage in IntelliJ IDEA.
  • Choose File > New > Java Project in Eclipse.
  • Click File > Create Project in NetBeans.
  • Select the location where you want to save the file.
  • Choose a Java SDK (Software Development Kit) version. A Java SDK can be downloaded and installed from the Oracle website if you don’t already have one.
  • Now, click Finish.

Step 2: Add FFmpeg command to Java code

  • Create a new Java class called TrimVideo. This class will contain the code that is used to trim the video using FFmpeg.
  • In the “TrimVideo” class, add the following code:

    public class TrimVideo {
 public static void main(String[] args) throws IOException, InterruptedException {
    String inputVideoPath = "/path/to/input/video.mp4";
    String outputVideoPath = "/path/to/output/video.mp4";
    // Set start time and duration for the trim
  int startTime = 0; // Start time in seconds
  int duration = 6; // Duration in seconds
    // Build FFmpeg command
 String[] cmd = { 
      -i, inputVideoPath,
      -ss, Integer.toString(startTime),
      -t, Integer.toString(duration),
      -c, copy,
 // Run FFmpeg command
  Process process = new ProcessBuilder(cmd).start();
    System.out.println("Trimmed video saved to: " + outputVideoPath); 
  • You must substitute the actual file paths for your input and output videos for the inputVideoPath and outputVideoPath variables in this code, respectively.

  • To select the beginning and duration of the trim, you may also change the startTime and duration variables.

Step 4: Execute the Java program

  • To run the Java program, navigate to the project directory and type java TrimVideo.

This is how you can trim videos in java using FFmpeg.

How can I trim the video without losing quality?

FFmpeg provides two options -c:v copy and -c:a copy to copy the video and audio streams without re-encoding them in order to reduce a video without sacrificing quality. The video and audio are kept at their original quality using this technique.

Here’s an example:

ffmpeg -i input.mp4 -ss 00:01:30 -to 00:02:30 -c:v copy -c:a copy output.mp4

In this command,

  • input.mp4 is the input video file, output.mp4 is the output video file.
  • -ss 00:01:30 sets the starting point of the trim at 1 minute 30 seconds.
  • -to 00:02:30 sets the end point of the trim at 2 minutes 30 seconds.
  • The -c:v copy and -c:a copy options tell FFmpeg to copy the video and audio streams without re-encoding.

You can change the -ss and -to target the starting point and end point of the trim as per your needs.

Remember that some video codecs might not permit trimming without re-encoding, in which case you might have to re-encode the video to get the desired outcome.

How to trim a video into multiple parts in FFmpeg?

With FFmpeg, we can trim multiple parts of a video in a single command by specifying the start time and duration of each segment using a number of -ss and -t options. These segments will concatenate using the concat filter in FFmpeg.

In the example given below, we will cut the video into three parts:

ffmpeg -i input.mp4 \

-ss 00:00:00 -t 00:00:10 -c:v copy -c:a copy part1.mp4 \

-ss 00:00:10 -t 00:00:20 -c:v copy -c:a copy part2.mp4 \

-ss 00:00:20 -c:v copy -c:a copy part3.mp4

ffmpeg -i "concat:part1.mp4|part2.mp4|part3.mp4" -c copy output.mp4

In above,

  • input.mp4 is the input video file and part1, part2, part3 are the output files.
  • Here, we are using the multiple -ss and -t options to set the start time and duration of each part, and the resulting parts are saved as part1.mp4, part2.mp4, and part3.mp4.
  • In the second line, we are using the concat filter provided by FFmpeg to concatenate the three parts into a single output file named output.mp4.
  • The -c copy option tells the FFmpeg to copy the video and audio streams without re-encoding, preserving the original quality of the video.

Here is how a single video can be divided into several portions.

However, follow these instructions to trim and preserve metadata.