【问题标题】:ffmpeg switch inputs / mapping on-the-fly / while recordingffmpeg 切换输入/即时映射/录制时
【发布时间】:2020-08-02 20:21:53
【问题描述】:

我有一个 rtsp 视频源 stream1 和一个音频源,我目前合并并使用以下方式发送到 rtmp-server:

stream1="rtsp://streamurl1"

/usr/bin/ffmpeg                    \
    [...]
    -i "$stream1"                  \
    [...]
    -itsoffset $AUDIOVIDEOOFFSET   \
    -f pulse                       \
    -i default                     \
    [...]
    -vcodec copy                   \
    -map 0:v -map 1:a              \
    [...]
    -f flv "rtmp://streamingserver"

我现在想添加第二个视频源 stream2 并在 stream1stream2 之间来回切换而不中断音频。两个流是相同的/来自相同的摄像机。

ffmpeg 有什么健全的方法可以做到这一点吗?或者你会建议怎么做?

只需停止进程并使用stream2 而不是stream1 重新启动它即可,但会导致流中断几秒钟,这是我希望改进的当前最坏情况。

【问题讨论】:

  • 您是否正在对视频流进行编码?两个流的分辨率相同吗?
  • @Gyan 不,是的。只需从两个相同的相机通过。我相应地更新了上面的问题。

标签: ffmpeg streaming on-the-fly


【解决方案1】:

我无法在合理的时间内让它与纯 ffmpeg 一起工作,但 nginx-rtmp 模块开箱即用。

基本上是apt install libnginx-mod-rtmp nginx,加..

rtmp {
        server {
                listen 1935;
                chunk_size 4096;

                application live {
                        live on;
                        record off;
                        # Only localhost is allowed to publish
                        allow publish 127.0.0.1;
                        deny publish all;
                }
        }
}

.. 到 nginx.conf,systemctl restart nginxnginx 充当rtmp://localhost/live 上的本地环回设备。

如果您现在有 n 个要多路复用的流,则可以创建 n 个 systemd 服务camera_i.service

[Unit]
Description=Send stream i to local nginx loopback device
# List all other
Conflicts=camera_1.service, camera_2.service, [...]

[Service]
ExecStart=/usr/bin/ffmpeg -i rtsp://mystreamurl_i -c copy -f flv rtmp://localhost/live
Type=simple
Restart=on-failure
KillSignal=SIGINT
SuccessExitStatus=255

[Install]
WantedBy=xsession.target

并通过启动camera_i.service 切换到流i

systemctl --user start camera_i.service

Conflicts= 选项中列出的所有其他服务都将被 systemd 自动终止,从而有效地多路复用相机流。

然后可以使用以下方式使用记录器使用生成的流:

/usr/bin/ffmpeg                    \
    [...]
    -use_wallclock_as_timestamps 1 \
    -fflags +genpts                \
    -i "rtmp://localhost/live"     \
    [...]
    -itsoffset $AUDIOVIDEOOFFSET   \
    -f pulse                       \
    -i default                     \
    [...]
    -vcodec copy                   \
    -map 0:v -map 1:a              \
    [...]
    OUTPUT

结果是一个相当平滑的翻转和连续的音频。

如果您不使用-vsync 0,则可能不需要-use_wallclock_as_timestamps 1 -fflags +genpts。但我还没有测试过。


附录

正如 ffmpeg 邮件列表中所建议的那样,zmq 过滤器据说可以即时更改过滤器设置。 这个想法是覆盖两个流并切换顶部流的不透明度,从而有效地切换流。

我无法让它工作,但任何人都可以尝试:

# compiled with --enable-libzmq
ffmpeg -i INPUT -filter_complex 'null[main];movie=INPUT2,zmq,lumakey@toggle=tolerance=1,[main]overlay,realtime' OUTPUT
echo lumakey@toggle tolerance 0 | zmqsend

zmqsend 的结果来自:

git clone https://github.com/FFmpeg/FFmpeg
cd FFmpeg/
./configure
cd tools/
gcc zmqsend.c -o zmqsend -I.. `pkg-config --libs --cflags libzmq libavutil`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2014-09-06
    • 2013-10-22
    • 1970-01-01
    相关资源
    最近更新 更多