0

FFMPEG makes it easy to take multiple inputs and stack them into a mosaic video. I am looking for a way to do the opposite, specifically I would like take a video stream that is composed of four streams stacked into quadrants and split it into four separate videos consisting of the coordinates

video1 = [0, 0.5*w, 0, 0.5*h]
video2 = [0.5*w, w, 0, 0.5*h]
video3 = [0, 0.5*w, 0.5*h, h]
video4 = [0.5*w, w, 0.5*h, h]

I know that I can do this with four separate ffmpeg calls using the crop filter but this seems to be unnecessarily expensive. Is there a way to do this in a single call?

John Allard
  • 3,564
  • 5
  • 23
  • 42

1 Answers1

2

crop filter

You can use four crop filters in one command:

ffmpeg -i input -filter_complex "[0]crop=iw/2:ih/2:0:0[tl];[0]crop=iw/2:ih/2:ow:0[tr];[0]crop=iw/2:ih/2:0:oh[bl];[0]crop=iw/2:ih/2:ow:oh[br]" -map "[tl]" topleft.mp4 -map "[tr]" topright.mp4 -map "[bl]" bottomleft.mp4 -map "[br]" bottomright.mp4

bitstream filter

A bitstream filter is different than a normal filter. A normal filter requires decoding and encoding. A bitstream filter operates on the encoded stream data, and performs bitstream level modifications without performing decoding.

The h264_metadata and hevc_metadata bitstream filters can edit the window cropping offsets in the SPS for H.264 and H.265/HEVC. What this means is that it can change these values without needing to re-encode the video. The file size will remain the same but the player will crop the video according to the crop values you set.

Example for H.264 320x240 input:

ffmpeg -i input.mp4 -bsf:v h264_metadata=crop_right=160:crop_bottom=120 -c copy topleft.mp4 -bsf:v h264_metadata=crop_left=160:crop_bottom=120 -c copy topright.mp4  -bsf:v h264_metadata=crop_right=160:crop_top=120 -c copy bottomleft.mp4 -bsf:v h264_metadata=crop_left=160:crop_top=120 -c copy bottomright.mp4

These fields are set in pixels. Note that some sizes may not be representable if the chroma is subsampled (it basically means you should only use even values for your typical video).

To script this you can use ffprobe to get the width and height.

llogan
  • 121,796
  • 28
  • 232
  • 243