Written by Christian Ahmer | 11/13/2023

Cutting and glueing video files with ffmpeg

In the world of video editing and processing, FFmpeg stands as a powerful and versatile command-line tool that allows users to manipulate videos in various ways. This guide provides a professional walkthrough of two common tasks: extracting a segment from a video and appending multiple videos together.

Extracting a Segment from a Video with FFmpeg

Extracting a specific part of a video is a straightforward task with FFmpeg. The command for this operation requires specifying the start time, the duration of the clip, the input file, and the output file, along with a command to copy the video and audio codec. Here's how to do it in the terminal:

ffmpeg -ss '00:00:00' -t '01:18:01' -i 'A_video.mp4' -vcodec copy -acodec copy 'Part_of_A_video.mp4'

In this command:

  • -ss specifies the start time of the segment.
  • -t indicates the duration of the segment to be extracted.
  • -i designates the input file.
  • -vcodec copy -acodec copy instructs FFmpeg to copy the original video and audio codecs, avoiding re-encoding.
  • The last part specifies the output file name.

This method is particularly efficient as it does not re-encode the video, preserving the original quality and saving processing time.

Appending Videos Together with FFmpeg

To merge several videos into one without re-encoding, the videos must be in the same format. Here’s the step-by-step process:

1. Create a List of Videos

Firstly, you need to define a list of video files to be concatenated. Create a text file (e.g., list.txt) with the content like this:

file 'This_is_a_video.mp4'
file 'This_is_another_video.mp4'

Each line points to a file you wish to join, prefixed with the word file.

2. Concatenate the Videos

Once you have your list, use the following FFmpeg command to concatenate the videos, again in the terminal:

ffmpeg -f concat -safe 0 -i list.txt -c copy all_parts_as_a_video.mp4

In this command:

  • -f concat tells FFmpeg that the following inputs should be concatenated.
  • -safe 0 is a necessary flag to allow FFmpeg to process file paths specified in the list.
  • -i points to the input list file.
  • -c copy indicates that the codec should be copied, not re-encoded.
  • The last part is the name of the output file.

This technique is especially efficient when dealing with large files or when you need to process videos quickly because it directly copies the data streams, hence termed as "just gluing together."

By following these steps, you can easily extract portions of videos or merge multiple clips into one without the need for complex video editing software. FFmpeg does the job with precision and speed, making it an invaluable tool for professionals in video production and editing.

You see: For simple or advanced tasks you do not even need a graphical video editing tool.