Combining Cropped Video or Image Segments Using FFmpeg

Combining Cropped Video or Image Segments Using FFmpeg

FFmpeg is a powerful tool for video processing, and with a few commands, you can achieve impressive results. In this post, we'll give you an overview of a script that crops three segments from a video file with a shift on the centre segment and then combines them side-by-side into a single video. As a bit of background to the project, our client wanted to fit 3 x 49” LCD Video Wall Panels onto a feature wall with the middle panel fitted slightly lower than the left and right panel. The video wall panels are daisy chained together and all run from a single 4K Network Media Player.

There are a numerous methods and applications for preparing a single image or video file for the media player, in this instance as an example we create an oversized video in Canva and then run a FFmpeg test script which is executed Mac Automator. The user can simply drop a video or image on an automator icon and the script will output the correctly formatted mp4 file. 

1. Create an oversized image using Canva.

Create a template with the overall size of 3840px x 1280px. Why we do this: The 4K Network Media Player plugged into one of the screens can output a maximum of 3840px x 2160px content. For the template width we use the maximum width of 3840px. Each LCD video wall panel is 1080px high however due to the shifted middle screen which is approximately 200px we increase the original template height 1080px + 200px = 1280px.

Input video file created on Canva

2. Crop the First Segment

Next, we'll use FFmpeg to crop the first segment for the left LCD panel of our video:

/usr/local/bin/ffmpeg -i input.mp4 -filter:v "crop=1280:1080:0:0" output_1.mp4

Let's break down this command:

/usr/local/bin/ffmpeg : The path to the FFmpeg executable.

-i input.mp4 : Specifies the input video file.

-filter:v "crop=1280:1080:0:0" : Applies a crop filter with the following parameters:

  • 1280:1080 : Width and height of the cropped area.
  • 0:0 : X and Y coordinates of the top-left corner of the crop area.
  • output_1.mp4 : The name of the output file.

This command crops a 1280 x 1080 section from the top-left corner of input.mp4 and saves it as output_1.mp4.

3. Crop the Second Segment (with the shift)

We repeat the process to crop the second segment of the video for the middle LCD Video wall panel. However we shift the cropping zone with a 200px delta as the middle screen is mounted higher that the left and right.

/usr/local/bin/ffmpeg -i input.mp4 -filter:v "crop=1280:1080:1280:200" output_2.mp4

In this command:

  • 1280:1080 : Width and height of the cropped area.
  • 1280:200 : X and Y coordinates of the top-left corner of the crop area.

This crops another 1280x1080 section starting from (1280, 200) and saves it as output_2.mp4.

4. Crop the Third Segment

Finally, we crop the third segment of the video for the right LCD Video Wall Panel.

/usr/local/bin/ffmpeg -i input.mp4 -filter:v "crop=1280:1080:2560:0" output_3.mp4

In this command:

  • 1280:1080 : Width and height of the cropped area.
  • 2560:0 : X and Y coordinates of the top-left corner of the crop area.

This crops a 1280x1080 section starting from (2560, 0) and saves it as output_3.mp4.

 

Individual Video Files

5. Combine the Cropped Segments

Now that we have our three cropped segments saved as individual files, we can combine them side-by-side into a single video.

/usr/local/bin/ffmpeg -i output_1.mp4 -i output_2.mp4 -i output_3.mp4 -filter_complex "[0:v][1:v][2:v]hstack=inputs=3[v]" -map "[v]" output_3840_1080.mp4

This command does the following:

  • -i output_1.mp4 -i output_2.mp4 -i output_3.mp4 : Specifies the input video files.
  • -filter_complex "[0:v][1:v][2:v]hstack=inputs=3[v]": Uses the hstack filter to stack the three video streams horizontally.
  • -map "[v]" : Maps the output of the hstack filter to the output file.
  • output_3840_1080.mp4 : The name of the final output file.
Result output video file

The resulting video, output_3840_1080.mp4, will have a resolution of 3840px x 1080 px, combining all three segments side-by-side with the shift on the centre screen. The LCD Video Wall built in daisy chain will pass the video source across all the screen from the single 4K Media Player. The LCD Video Wall Panels are configured with their position in the video wall matrix so they only display the segment of the video for their position. 

Final Layout Video File

In the aquarium example shown the sharks can swim seamlessly across the screen even though the middle screen is at a different height. With these commands, you can easily crop and combine video segments using FFmpeg. This is just a small example of the powerful capabilities of FFmpeg. Whether you're working on a personal project or a professional production, mastering FFmpeg can greatly enhance your video processing skills. These commands can be built into a bespoke windows application or run quickly and easily via Automator on Mac.