FFmpeg 和 CUDA - 缩放视频和叠加图像

FFmpeg 和 CUDA - 缩放视频和叠加图像

我想将输入视频缩放到 1920x1080,然后叠加图像

我使用下一个命令:

ffmpeg -i /video1.mp4 -i /overlay.png \
        -filter_complex "[0:v][1:v] overlay=100:820:enable='between(t,3,8)',fps=fps=60" \
        -pix_fmt yuv420p -c:a copy /output.mp4

现在我想做同样的事情,但使用 Nvidia CUDA。因此我将命令重写为:

ffmpeg -y \
  -hwaccel cuda -hwaccel_output_format cuda -i video1.mp4 \
  -i overlay.png \
  -filter_complex "[0:v]hwupload_cuda,scale_cuda=w=1920:h=1080:format=nv12:interp_algo=lanczos,hwdownload[base]; [base][1:v]overlay=100:820:enable='between(t,3,8)',fps=fps=60,format=yuv420p" \
  -c:v h264_nvenc -c:a copy output.mp4

我发现hwupload_cuda主题。我对和了解很多hwdownload。但我遇到了下一个错误:

Impossible to convert between the formats supported by the filter 'graph 0 input from stream 0:0' and the filter 'auto_scale_0'
Error reinitializing filters!
Failed to inject frame into filter network: Function not implemented
Error while processing the decoded data for stream #1:0

完整日志:

ffmpeg version N-109856-gf8d6d0fbf1 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 11 (Ubuntu 11.3.0-1ubuntu1~22.04)
  configuration: --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --disable-static --enable-shared --enable-decoder='mjpeg,png' --enable-demuxer=image2 --enable-protocol=file --enable-zlib
  libavutil      58.  1.100 / 58.  1.100
  libavcodec     60.  2.100 / 60.  2.100
  libavformat    60.  2.100 / 60.  2.100
  libavdevice    60.  0.100 / 60.  0.100
  libavfilter     9.  1.100 /  9.  1.100
  libswscale      7.  0.100 /  7.  0.100
  libswresample   4.  9.100 /  4.  9.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.83.100
  Duration: 00:00:26.55, start: 0.000000, bitrate: 6220 kb/s
  Stream #0:0[0x1](und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, unknown/bt709/unknown, progressive), 1920x1080, 6077 kb/s, 60.04 fps, 60 tbr, 15360 tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
Input #1, png_pipe, from 'overlay.png':
  Duration: N/A, bitrate: N/A
  Stream #1:0: Video: png, rgba(pc), 406x110 [SAR 2835:2835 DAR 203:55], 25 fps, 25 tbr, 25 tbn
Stream mapping:
  Stream #0:0 (h264) -> hwupload_cuda:default
  Stream #1:0 (png) -> overlay
  format:default -> Stream #0:0 (h264_nvenc)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
Impossible to convert between the formats supported by the filter 'graph 0 input from stream 0:0' and the filter 'auto_scale_0'
Error reinitializing filters!
Failed to inject frame into filter network: Function not implemented
Error while processing the decoded data for stream #1:0
Conversion failed!

答案1

使用时-hwaccel cuda -hwaccel_output_format cuda,解码过程采用CUDA加速,解码后的视频位于GPU中。

  • hwupload_cuda从过滤器链的开头删除。

第二个问题是format=nv12overlay过滤器不兼容。

  • format=nv12用。。。来代替format=yuv420p

以下命令应该有效:

ffmpeg -y -hwaccel cuda -hwaccel_output_format cuda -i video1.mp4 -i overlay.png -filter_complex "[0:v]scale_cuda=w=1920:h=1080:format=yuv420p:interp_algo=lanczos,hwdownload[base];[base][1:v]overlay=100:820:enable='between(t,3,8)',fps=fps=60,format=yuv420p" -c:v h264_nvenc -c:a copy output.mp4


注意:
由于 Windows / Linux 兼容性问题,我将所有内容放在一行中。

相关内容