使用带有 h264_nvenc 编码器的 ffmpeg 时遇到音频延迟

使用带有 h264_nvenc 编码器的 ffmpeg 时遇到音频延迟

我正在尝试使用桌面复制 API 和 GPU D3D11 纹理以 ffmpeg D3D11VA 帧@60 fps 的形式捕获我的 Windows10 桌面。所需命令在此处给出。除此之外,我还试图拦截我的麦克风音频和桌面音频。为此,我使用此命令,

ffmpeg -f dshow -fflags nobuffer -i audio="Microphone (Realtek High Definition Audio)" -f dshow -fflags nobuffer -i audio="Stereo Mix (Realtek High Definition Audio)" -filter_complex ddagrab=framerate=60;amix=inputs=2:duration=longest:dropout_transition=0 -c:v h264_nvenc -preset:v slow -tune ll -profile:v high -rc:v vbr -b:v 6M -maxrate:v 6M -minrate:v 5M -bufsize:v 6M output.mp4
libavutil      58.  2.100 / 58.  2.100
  libavcodec     60.  3.100 / 60.  3.100
  libavformat    60.  3.100 / 60.  3.100
  libavdevice    60.  1.100 / 60.  1.100
  libavfilter     9.  3.100 /  9.  3.100
  libswscale      7.  1.100 /  7.  1.100
  libswresample   4. 10.100 /  4. 10.100
  libpostproc    57.  1.100 / 57.  1.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, dshow, from 'audio=Microphone (Realtek High Definition Audio)':
  Duration: N/A, start: 9251.652000, bitrate: 1411 kb/s
  Stream #0:0: Audio: pcm_s16le, 44100 Hz, 2 channels, s16, 1411 kb/s
Guessed Channel Layout for Input Stream #1.0 : stereo
Input #1, dshow, from 'audio=Stereo Mix (Realtek High Definition Audio)':
  Duration: N/A, start: 9252.378000, bitrate: 1411 kb/s
  Stream #1:0: Audio: pcm_s16le, 44100 Hz, 2 channels, s16, 1411 kb/s
Stream mapping:
  Stream #0:0 (pcm_s16le) -> amix
  Stream #1:0 (pcm_s16le) -> amix
  ddagrab:default -> Stream #0:0 (h264_nvenc)
  amix:default -> Stream #0:1 (aac)
Press [q] to stop, [?] for help
Output #0, mp4, to 'output.mp4':
  Metadata:
    encoder         : Lavf60.3.100
  Stream #0:0: Video: h264 (Main) (avc1 / 0x31637661), d3d11(pc, gbr/bt709/iec61966-2-1, progressive), 1600x900 [SAR 1:1 DAR 16:9], q=2-31, 60 fps, 15360 tbn
    Metadata:
      encoder         : Lavc60.3.100 h264_nvenc
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc60.3.100 aac
frame=  170 fps= 46 q=23.0 size=     512kB time=00:00:03.43 bitrate=1220.6kbits/s dup=8 drop=1 speed=0.924x

[q] command received. Exiting.

frame=  202 fps= 50 q=23.0 Lsize=     719kB time=00:00:03.48 bitrate=1691.4kbits/s dup=8 drop=1 speed=0.864x
video:666kB audio:47kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.901717%
[aac @ 0000022af0d5c100] Qavg: 242.688

输入 #0 开始时间:9251.65200,输入 #2 开始时间:9252.378000,这两个音频输入的延迟最小。但没有关于 ddagrab 开始时间的信息,可能是因为它充当过滤器而不是输入设备。无论如何,这只是一个观察结果。

桌面捕获过程结束后,当我观看 output.mp4 时,遇到了音频延迟的问题。

我尝试以 30fps 的速度捕捉,但没有任何改善。

我还观察到,如果我从输入设备中删除立体声混音(桌面内部音频),一切都会正常工作,即我的麦克风音频和视频同步。

ffmpeg -y -filter_complex ddagrab=framerate=60 -f dshow -fflags nobuffer -i audio="Microphone (Realtek High Definition Audio)" -c:v h264_nvenc -preset:v slow -tune ll -profile:v high -rc:v vbr -b:v 6M -maxrate:v 6M -minrate:v 5M -bufsize:v 6M output.mp4

一种可能的解决方案是使用 -itsoffset 选项延迟我的视频。这些堆栈溢出和超级用户解决方案仅适用于预先录制的视频,

https://stackoverflow.com/questions/63237572/ffmpeg-delay-video-in-overlay

在 ffmpeg 中,如何仅延迟 .mp4 视频的音频而不转换音频?

但我正在进行捕获和编码(即同时进行)。

我是视频编辑和 ffmpeg 的新手。我正在使用 gyan.dev 的最新版本。

ffmpeg version 6.0-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
  configuration: 
--enable-gpl --enable-version3 --enable-static --disable-w32threads 
--disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls 
--enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma 
--enable-libsnappy --enable-zlib --enable-librist --enable-libsrt 
--enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray 
--enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libdav1d 
--enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e 
--enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 
--enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl 
--enable-libopenjpeg --enable-libvpx --enable-mediafoundation 
--enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi 
--enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg 
--enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec 
--enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 
--enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo 
--enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug 
--enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame 
--enable-libshine --enable-libtheora --enable-libtwolame 
--enable-libvo-amrwbenc --enable-libilbc --enable-libgsm 
--enable-libopencore-amrnb --enable-libopus --enable-libspeex 
--enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite 
--enable-libmysofa --enable-librubberband --enable-libsoxr 
--enable-chromaprint
  libavutil      58.  2.100 / 58.  2.100
  libavcodec     60.  3.100 / 60.  3.100
  libavformat    60.  3.100 / 60.  3.100
  libavdevice    60.  1.100 / 60.  1.100
  libavfilter     9.  3.100 /  9.  3.100
  libswscale      7.  1.100 /  7.  1.100
  libswresample   4. 10.100 /  4. 10.100
  libpostproc    57.  1.100 / 57.  1.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

相关内容