简体   繁体   English

FFmpeg 自动同步音视频

[英]FFmpeg Automatically Synchronize Audio and Video

I have two separate streams, audio (line-in, pulseaudio) and video (ip-camera, rtsp), which I would like to automatically synchronize.我有两个独立的流,音频(line-in,pulseaudio)和视频(ip-camera,rtsp),我想自动同步。

The best I came up with so far is ffmpeg with -itsoffset but the offset actually varies.到目前为止,我想出的最好的是带有ffmpeg-itsoffset但偏移量实际上是不同的。

Is there any way to do this reliably automated on command-line (doesn't necessarily need to be ffmpeg)?有什么方法可以在命令行上可靠地自动化(不一定需要是 ffmpeg)?

The camera as well as the computer run an ntp client and the input delay of pulseaudio is negligible, so this should be solvable.相机和电脑运行ntp客户端,pulseaudio的输入延迟可以忽略不计,应该可以解决。

I found openRTSP ( livemedia-utils on Debian, live-media on Arch) that fetches a parameter o ... (using an arbitrary RTSP example source)我发现openRTSPlivemedia-utils ,Arch 上的live-media )获取参数o ...(使用任意 RTSP 示例源)

$ openRTSP -r rtsp://109.98.78.106
Created new TCP socket 3 for connection
Connecting to 109.98.78.106, port 554 on socket 3...
...remote connection opened
[...]
o=- 1604163122724055 1 IN IP4 109.98.78.106
[...]

... that seem to be the cameras UTC system time in microseconds extracted from the underlying RTC stream. ...这似乎是从底层 RTC stream 中提取的相机 UTC 系统时间(以微秒为单位)。

Eg:例如:

$ date -d@$( echo $(openRTSP -r rtsp://109.98.78.106 2>&1 | grep -Po '(?<=o=-\s)\d+' | head -n1 ) / 1000000 | bc )
Sat Oct 31 05:55:45 PM CET 2020

The cameras we use feature NTP functionality.我们使用的摄像机具有 NTP 功能。 I hence set up a local NTP server on the recording computer to serve as time source for the cameras.因此,我在录制计算机上设置了一个本地 NTP 服务器,作为摄像机的时间源。

From the delay...从延迟...

time_camera () {
  # Returns cameras system time as embedded in the RTC stream in nanoseconds after UNIX 0
  echo $(($(openRTSP -r ${STREAM_CAMERA} 2>&1 | grep -Po '(?<=o=-\s)\d+' | head -n1)000))
  }
time_local () {
  # Returns local system time in nanoseconds after UNIX 0
  date +%s%N
}
vdelay=$(($(time_local) - $(time_camera)))

... I can estimate how long the frame took to arrive. ...我可以估计帧到达需要多长时间。 You might fine tune this to your needs.您可以根据自己的需要对此进行微调。

For me, it is around (900 +- 200) ms and matches the audio-video offset really well.对我来说,它大约是 (900 +- 200) 毫秒,并且非常匹配音频-视频偏移量。

As mentioned above, I use Pulseaudio and can hence (regularly) set the input latency offset directly without having to mess with ffmpeg's -itsoffset via:如上所述,我使用 Pulseaudio,因此可以(定期)直接设置输入延迟偏移,而不必通过以下方式弄乱 ffmpeg 的-itsoffset

# See: pacmd list-cards
pacmd set-port-latency-offset <card> <port> $((vdelay/1000))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM