简体   繁体   English

FFMPEG 将来自 image2pipe 的视频与来自 RTMP 的音频同步

[英]FFMPEG sync video from image2pipe with audio from RTMP

I have an rtmp url, and i need to put overlay using opencv and keeping audio.我有一个 rtmp url,我需要使用 opencv 覆盖并保持音频。

It works well, exept for syncronization between audio and video.它运行良好,除了音频和视频之间的同步。

with this command用这个命令

ffmpeg -re -y -f image2pipe -i - -i rtmp://192.168.178.32:1935/opencv-hls/test -map 0:v:0 -vcodec libx264 -g 50 -keyint_min 50 -map 1:a:0 -b:a 128k -f mpegts -codec:v mpeg1video -b:v 700k -s 1024x576 -bf 0 -q 1 http://localhost:3000/mystream

I open an image2pipe where i send my frame with overlay, and i use the same input of opencv as second command for ffmpeg to acquire audio and send all to mpegts url我打开一个image2pipe,在其中发送带有叠加层的帧,并使用opencv的相同输入作为ffmpeg的第二个命令,以获取音频并将所有内容发送到mpegts Z572D4E421E5E6B9BC11D815E8A027112

This is the ffmpeg output这是 ffmpeg output

ffmpeg version 4.1.4-1build2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.2.1-4ubuntu1)
  configuration: --prefix=/usr --extra-version=1build2 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Input #0, image2pipe, from 'pipe:':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 1024x576 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 25 tbn, 25 tbc
Input #1, flv, from 'rtmp://192.168.178.32:1935/opencv-hls/test':
  Metadata:
    Server          : NGINX RTMP (github.com/arut/nginx-rtmp-module)
    displayWidth    : 1024
    displayHeight   : 576
    fps             : 0
    profile         : 
    level           : 
  Duration: 00:00:00.00, start: 4.639000, bitrate: N/A
    Stream #1:0: Audio: aac (LC), 44100 Hz, stereo, fltp
    Stream #1:1: Video: h264 (Constrained Baseline), yuv420p(progressive), 1024x576, 29.58 fps, 29.58 tbr, 1k tbn
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> mpeg1video (native))
  Stream #1:0 -> #0:1 (aac (native) -> mp2 (native))
[swscaler @ 0x55f256d63980] deprecated pixel format used, make sure you did set range correctly
Output #0, mpegts, to 'http://localhost:3000/mystream':
  Metadata:
    encoder         : Lavf58.20.100
    Stream #0:0: Video: mpeg1video, yuv420p(progressive), 1024x576 [SAR 1:1 DAR 16:9], q=2-31, 700 kb/s, 25 fps, 90k tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.35.100 mpeg1video
    Side data:
      cpb: bitrate max/min/avg: 0/0/700000 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: mp2, 44100 Hz, stereo, s16, 128 kb/s
    Metadata:
      encoder         : Lavc58.35.100 mp2

I always have a delay between audio and video and it increese with time.我总是在音频和视频之间有延迟,并且随着时间的推移而增加。 Do you have any idea on how to sync them?您对如何同步它们有任何想法吗?

thanks Andrea谢谢安德里亚

[sorry for spelling mistakes, i am just not bothered to fix it] [抱歉拼写错误,我只是懒得修复它]

Not sure if it is still needed, but I had a similar problem;不确定是否仍然需要它,但我遇到了类似的问题; the audio was starting 1 sec behind the video and drifting further over time.音频比视频晚 1 秒开始,并且随着时间的推移进一步漂移。

The drifting was fixed after I inserted -vsync 2 so the video will drop frames if a duplicate timestamp occured.在我插入-vsync 2后,漂移得到了修复,因此如果出现重复的时间戳,视频将丢帧。 (this might also be a framerate issue, use -vsync 1 instead, to drop or duplicate frames to achieve the requested fps, or use -vsync -1 if you want ffmpeg to auto choose between 1 or 2), for me it was not the audio that was causing the seemingly drift. (这也可能是帧率问题,请改用-vsync 1来丢弃或复制帧以实现请求的 fps,或者如果您希望 ffmpeg 在 1 或 2 之间自动选择,请使用-vsync -1 ),对我来说不是导致看似漂移的音频。 It was the occasion frame loss that caused the frame timestamps to mess up.正是偶然的帧丢失导致了帧时间戳的混乱。

Even tough this might solve timestamp issues, there is a possibility that indeed the audio itself is drifting, in this case -af aresample=async=1 -adrift_threshold <time-in-sec> might do the trick, ffmpeg will drop samples in order for the timestamps to match up, lower values will do a hard (drop/add) of frames and higher values will do a soft (squeez/stretch) of frames.即使很难解决时间戳问题,也有可能音频本身确实在漂移,在这种情况下-af aresample=async=1 -adrift_threshold <time-in-sec>可能会解决问题,ffmpeg 将按顺序丢弃样本为了使时间戳匹配,较低的值将执行硬(删除/添加)帧,较高的值将执行软(压缩/拉伸)帧。

if there is still the 1sec drift at the beginning you can use -itsoffset <value> before the audio input parameter to manually correct it (or dynamically with personnalised code), this will initiate the the stream with an offset, it can also be a negative value to do a reverse offset, making the input start earlier in comparisson with the rest.如果开始时仍有 1 秒的漂移,您可以在音频输入参数之前使用-itsoffset <value>手动更正它(或使用个性化代码动态),这将启动 stream 偏移,它也可以是负值进行反向偏移,使输入与 rest 相比更早开始。 So in your case it would be -itsoffset -1 .因此,在您的情况下,它将是-itsoffset -1

-frame_drop_threshold <nr. of frame-offset> -frame_drop_threshold <nr. of frame-offset> might also be an interesting parameter to check out, but i would use the audio timestamps as main timestamp in this case, since the videoframes might duplicate their timestamps. -frame_drop_threshold <nr. of frame-offset>也可能是一个有趣的检查参数,但在这种情况下我会使用音频时间戳作为主时间戳,因为视频帧可能会重复它们的时间戳。

https://ffmpeg.org/ffmpeg-all.html#Advanced-options [ Advanced-Options 5.11 ] https://ffmpeg.org/ffmpeg-all.html#Advanced-options [Advanced-Options 5.11]

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

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