Couldn't Add Watermark (Using Scale 2 Ref) Performing Hardware Transcoding with FFMPEG
In this article, we will delve into the issue of adding a watermark using the scale 2 ref feature during hardware transcoding with FFMPEG. We will explore the key concepts, provide context, and offer solutions to this problem. This topic is particularly relevant for those working with video processing and FFMPEG in a global context.
Background and Context
FFMPEG is a powerful, open-source tool used for handling multimedia data. It is widely used for video and audio transcoding, as well as for streaming and recording. The software supports various hardware acceleration technologies, such as NVIDIA's CUDA, Intel's QSV, and AMD's VCE, to improve transcoding performance.
One of the features of FFMPEG is the ability to add watermarks to videos. Watermarks are often used for branding or copyright purposes. To accomplish this, FFMPEG utilizes the `overlay` and `scale2ref` filters. The `scale2ref` filter is particularly useful because it allows users to scale the watermark image proportionally to the dimensions of the input video.
The Problem: Scale 2 Ref with Hardware Transcoding
When attempting to add a watermark using the `scale2ref` filter during hardware transcoding, users may encounter issues. Specifically, the following FFMPEG command:
ffmpeg -y -init_hw_device cuda=cuda:0 -filter_hw_device cuda -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i ./input.avi -i ./watermark.png -filter_complex "[1:v]format=nv12,scale=w=iw/10:h=ih/10[wm]; [0:v][wm]overlay=W-w-10:H-h-10:format=auto,hwdownload=extra_hw_frames=32" -c:a copy ./output.mp4May yield an error similar to:
"No such filter: 'scale2ref'"This issue arises because not all filters are supported during hardware transcoding. In this case, the `scale2ref` filter is unavailable when using hardware acceleration.
Solution: Use Scale and Pad Filters Instead
A workaround for this issue is to replace the `scale2ref` filter with the `scale` and `pad` filters. This solution allows users to maintain the functionality of scaling the watermark proportionally based on the input video's dimensions.
The updated FFMPEG command would look like this:
ffmpeg -y -init_hw_device cuda=cuda:0 -filter_hw_device cuda -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i ./input.avi -i ./watermark.png -filter_complex "[1:v]format=nv12,scale=w=iw/10:h=ih/10[wm]; [0:v]scale=-2:720,pad=w=iw+(iw/10):h=ih+(ih/10):x=(iw-iw/10)/2:y=(ih-ih/10)/2[v]; [wm][v]overlay=W-w-10:H-h-10:format=auto,hwdownload=extra_hw_frames=32" -c:a copy output.mp4This command first scales the watermark and the input video separately. Then, it pads the input video to maintain its aspect ratio, ensuring the watermark is scaled proportionally. The watermark is then overlaid on the padded input video.
- FFMPEG is a popular multimedia processing tool that supports hardware acceleration for improved transcoding performance.
- The `scale2ref