使用fluent-ffmpeg对多个摄像头推流,每次实例化一个ffmpeg,为什么start事件只被触发一次
发布于 5 年前 作者 evilArsh 4520 次浏览 来自 问答

这是我在github上的提的issue https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/issues/908 以下是推流代码

//const ffmpeg = require("fluent-ffmpeg");
   ffmpeg('rtsp://admin:123456abc@169.254.35.12:554/11')
            .outputOptions([
                "-vcodec",
                "copy",
                "-r",
                "25",
                "-an"
            ])
            .inputFPS(50)
            .noAudio()
            .aspect("4:3")
            .format("flv")
            .save('rtmp://localhost:9090/live/123')
            .on("start", function (e) {
                _this.notify("[ffmpeg start]" + e);
            })

如果在同一个方法中同时执行两次上述代码,那么其中

.on("start", function (e) {
                _this.notify("[ffmpeg start]" + e);
            })

会执行两次。但是如果先执行一次代码,并且start事件被触发之后,再次执行上述代码,start事件就不会被触发了。 当我每次执行上述代码,fluent-ffmpeg开启一个childprocess来执行外部ffmpeg.exe。 既然每次执行都是开启一个新的进程,所以start事件必然会发生,但是为啥当已经开启一个fmpeg.exe进程然后再次开启一个fmpeg.exe进程,此时stat事件就不会被触发了?

1 回复

问题已经解决了,把.save()变为.outPut(),最后加上.run()。.save()是后两个方法的语法糖

this.ffmpeg(uri)
            .outputOptions([
                "-vcodec",
                "copy",
                "-threads",
                "2",
                "-an"
            ])
            .inputFPS(50)
            .noAudio()
            .aspect("4:3")
            .format("flv")
            .output(this.__getRTMP())
            .on("start", function (e) {
                store.dispatch('setStreamUri', {
                    uri: _this.getHttpFlv(),
                    inputUri: _this.uri
                })
                _this.notify("[ffmpeg 推流开始]" + e);
            })
            .on("end", function () {
                _this.notify("[ffmpeg 结束]" + err);

            })
            .on("error", function (err) {
                _this.notify("[ffmpeg 错误]" + err);
            })
			.run()

回到顶部