1

This command line downscales fine my 4k video:

$ ffmpeg -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i input_video.mkv -vf scale_cuda=1920:1080:interp_algo=bicubic -c:v hevc_nvenc -preset slow -rc vbr -rc-lookahead 20 -b:v 1M -bufsize 5M -maxrate 1M -g 250 -an -sn output_video.mkv

But if I try to crop the black bars with -crop=3840:1608:0:276, they remains on video file destination:

$ ffmpeg -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i input_video.mkv -vf -crop=3840:1608:0:276,scale_cuda=1920:804:interp_algo=bicubic -c:v hevc_nvenc -preset slow -rc vbr -rc-lookahead 20 -b:v 1M -bufsize 5M -maxrate 1M -g 250 -an -sn output_video.mkv

1 Answers1

3

crop filter is executed on the CPU, but the decoded frames are in the memory of the GPU device.
For using crop (CPU) filter, we may use hwdownload,crop...,hwupload... (download the frames to the system memory, apply cropping in the CPU, and upload to the GPU for encoding).
But it's inefficient...
We like to look for GPU "device filter" like crop_cuda, but there is none.
Luckily hevc_cuvid decoder and h264_cuvid decoder support -crop argument.

Note that this is a decoder feature, and not a video filter.


Assume the video codec of input_video.mkv is HEVC (H.265), we may use the following command:

ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v hevc_cuvid -crop 276x552x0x0 -i input_video.mkv -vf scale_cuda=1920:804:interp_algo=bicubic -c:v hevc_nvenc -preset slow -rc vbr -rc-lookahead 20 -b:v 1M -bufsize 5M -maxrate 1M -g 250 -an -sn output_video.mkv

In case the input codec is H.264, replace hevc_cuvid with h264_cuvid.


Executing ffmpeg -h decoder=hevc_cuvid (for help), tells us that the -crop syntax is:
Crop (top)x(bottom)x(left)x(right).
That means, number of top rows to remove, number of bottom rows to remove, number of left, and number of right columns to remove.


Testing:

Create synthetic H.265 encoded input (for testing):
ffmpeg -y -f lavfi -i testsrc=size=3840x2160:rate=1:duration=100 -c:v libx265 input_video.mkv

Execute the command:
ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -c:v hevc_cuvid -crop 276x552x0x0 -i input_video.mkv -vf scale_cuda=1920:804:interp_algo=bicubic -c:v hevc_nvenc -preset slow -rc vbr -rc-lookahead 20 -b:v 1M -bufsize 5M -maxrate 1M -g 250 -an -sn output_video.mkv

The output resolution is 1920x804.
enter image description here

Rotem
  • 3,346