ffmpeg 切换输入/即时映射/录制时

ffmpeg switch inputs / mapping on-the-fly / while recording

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

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"

I would now like to add a second video source stream2 and switch between stream1 and stream2 back and forth without interrupting the audio. Both streams are identical / come from identical cameras.

有什么明智的方法可以用 ffmpeg 做到这一点吗?或者您建议如何做?

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

我无法在合理的时间内让它与纯 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 个系统服务 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`