Hardware Accelerated Scale Crop and Rotate in FFmpeg
Performing video transformations like scaling, cropping, and rotation can severely bottleneck your CPU if handled by software filters. To maximize performance, you can combine these operations into a single pipeline that runs entirely on your graphics hardware (GPU). This article demonstrates how to achieve this using NVIDIA CUDA and Intel Quick Sync Video (QSV) hardware acceleration, ensuring your video frames never leave the GPU memory until the process is complete.
The Key Concept: Keeping Frames in GPU Memory
In a standard FFmpeg command, frames are decoded by the GPU, sent to the CPU (RAM) for filtering, and sent back to the GPU (VRAM) for encoding. This constant data transfer over the PCIe bus slows down processing.
To prevent this bottleneck, you must use hardware-accelerated decoders, filters, and encoders together. By chaining hardware-specific filters, FFmpeg processes scaling, cropping, and rotation directly in VRAM.
Method 1: NVIDIA CUDA (NVDEC / NVENC)
NVIDIA GPUs can handle crop, scale, and rotation operations inside GPU memory. For the most efficient pipeline, the crop is handled during decoding, while scaling and rotation are handled by CUDA filters.
Use the following command structure:
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -c:v h264_cuvid -crop 20x20x20x20 -i input.mp4 -vf "scale_cuda=1920:1080,transpose_cuda=1" -c:v h264_nvenc output.mp4How it works: *
-hwaccel cuda -hwaccel_output_format cuda: Forces FFmpeg to
keep decoded frames inside CUDA memory. *
-c:v h264_cuvid -crop 20x20x20x20: Uses the hardware
decoder to crop the video immediately upon decoding. The crop format is
topxtoxbottomxleft pixels. *
scale_cuda=1920:1080: Scales the cropped frame to 1080p
using NVIDIA hardware. * transpose_cuda=1: Rotates the
frame 90 degrees clockwise on the GPU. * -c:v h264_nvenc:
Encodes the final output directly from VRAM using NVIDIA’s hardware
encoder.
Method 2: Intel Quick Sync Video (QSV)
If you are using an Intel processor with integrated graphics or an
Intel Arc GPU, you can use the Intel Quick Sync Video (QSV) pipeline.
Intel offers a highly integrated filter called vpp_qsv
(Video Processing Pipeline) that can scale, crop, and rotate inside a
single filter call.
Use the following command structure:
ffmpeg -hwaccel qsv -c:v h264_qsv -i input.mp4 -vf "vpp_qsv=w=1920:h=1080:cw=100:ch=100:cx=0:cy=0:rotate=90" -c:v h264_qsv output.mp4How it works: *
-hwaccel qsv -c:v h264_qsv: Decodes the input video using
Intel hardware acceleration. * vpp_qsv: This single filter
processes all three transformations simultaneously: *
w=1920:h=1080: Scales the output width and height. *
cw=100:ch=100:cx=0:cy=0: Crops the video (Crop Width, Crop
Height, Crop X offset, Crop Y offset). * rotate=90: Rotates
the video 90 degrees clockwise (supports 90,
180, and 270 degrees). *
-c:v h264_qsv: Encodes the video using Intel’s QSV hardware
encoder.