# MQTT won't send stop command to ffmpeg spawn node

**URL:** <https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290>\
**Category:** General\
**Created:** [8 November 2022 22:37 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290 "2022-11-08T22:37:26Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorymathis13](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorymathis13/32/66999_2.png) [@jorymathis13](https://discourse.nodered.org/u/jorymathis13)\
**Post date:** [8 November 2022 22:37 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/1 "2022-11-08T22:37:26Z")

</div>

Hey @kevinGodell could you please help me again sir? I'm using a function node with an MQTT IN node attached. I can send an MQTT messgae to "start" the ffmpeg spawn node of yours, but when I try to stop the ffmpeg spawn node, it essentially ignores it. Even when the flow is idle or has been redeployed, and I send the stop message through MQTT, it actually starts the stream. I've tried to analyze your code in the node itself to see if there's something to give me a clue, but honestly I'm not experienced enough to know how your kill process is executed.

Here is a couple of different examples of the coding I've attempted with the function node:

**#1**

```auto
var payload = msg.payload; //get payload

if (msg.payload == "on") {
    msg.action = {
        "command": "start"
    };
    return msg;
}
 else if (msg.payload == "off") {
    msg.action = {
        "command": "stop"
    };
    return msg;
}
return null;

```

**#2**

```auto
var payload = msg.payload; //get payload

if (msg.payload == "on") {
    msg.action = {
        "command": "start"
    };
    return msg;
}
 else if (msg.payload == "off") {
    msg.SIGKILL = {
        "command": "stop"
    };
    return msg;
}
return null;

```

Here is what my flow looks like. I have the function node notionally acting like PiR sensor to trigger the camera when motion is detected. The debug output shows when the spawn node was started with the mqtt, but it shows how it was ignored when I tried to stop it. The kill process was done via the direct inject.

 ![Function_Debug_8Nov22_1619](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/9/c93ba5b44fc12656791732b1e00ecf7b188044d8.png)

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [8 November 2022 23:36 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/2 "2022-11-08T23:36:08Z")

</div>

> [@jorymathis13](#):
>
> #1

That block of code worked for me in a function node to detect on/off and then send the correct message to start/stop ffmpeg.

I think that on windows, you may need a different kill code because sometimes ffmpeg does not want to exit normally. If you could include more details about the ffmpeg command and your operating system and even the version of ffmpeg that could help to diagnose the issue. Maybe a screenshot of the ffmpeg node settings window, too.

---

<div class="post-metadata">

**Author:** ![jorymathis13](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorymathis13/32/66999_2.png) [@jorymathis13](https://discourse.nodered.org/u/jorymathis13)\
**Post date:** [9 November 2022 16:35 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/3 "2022-11-09T16:35:06Z")

</div>

Thank you for testing on your end. I'm running my node-red on the latest Ubuntu release, Ubuntu 22.04.1 LTS with ffmpeg version 4.4.2 running.

Here is a screen shot of the ffmpeg node config window.

 ![ffmpeg_Node_Config_9Nov22_1025](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08e95433cf62cb91c08ab8fad404261ffd722b19.png)

I've attempted again this morning to get ffmpeg to stop streaming, but it keeps ignoring my command. As you can see from the debug output, it sees the command and processes it, but nothing happens as far as the process stopping.

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [9 November 2022 23:50 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/4 "2022-11-09T23:50:02Z")

</div>

I can't really see the flaw. Maybe there is a typo somewhere in your code. Try this function node. It checks payload for `on` or `off` and then passes the action command to my ffmpeg node and promptly starts and stops it.

 ![Screen Shot 2022-11-09 at 5.47.31 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/ad25b840e1040b596a306bdb5a740dc0d413f9b0.png)

```auto
[{"id":"4475d1eeed5ddc6d","type":"inject","z":"0528fb314ac39109","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":410,"y":520,"wires":[["3b3f823455d5f232"]]},{"id":"3b3f823455d5f232","type":"function","z":"0528fb314ac39109","name":"","func":"const { payload } = msg;\n\nif (payload === 'on') {\n return { action: { command: 'start'} };\n}\n\nif (payload === 'off') {\n return { action: { command: 'stop' } };\n}\n\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":580,"wires":[["b0bbbcc64e2253d3"]]},{"id":"06f068924b6aee93","type":"inject","z":"0528fb314ac39109","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":410,"y":580,"wires":[["3b3f823455d5f232"]]},{"id":"3893a0d24ca73e02","type":"inject","z":"0528fb314ac39109","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"junk","payloadType":"str","x":410,"y":640,"wires":[["3b3f823455d5f232"]]},{"id":"b0bbbcc64e2253d3","type":"debug","z":"0528fb314ac39109","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":580,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![jorymathis13](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorymathis13/32/66999_2.png) [@jorymathis13](https://discourse.nodered.org/u/jorymathis13)\
**Post date:** [15 November 2022 15:25 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/5 "2022-11-15T15:25:46Z")

</div>

I think my problem is in the MQTT messaging. I can get the direct injects to work just fine, it's just when I put the MQTT node in place of the direct inject nodes, is where my issues stem.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2022 15:42 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/6 "2022-11-15T15:42:26Z")

</div>

In that case there must be a difference between the messages that work and those that don't. Send them to debug nodes set to output the complete message and check them carefully.

---

<div class="post-metadata">

**Author:** ![jorymathis13](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorymathis13/32/66999_2.png) [@jorymathis13](https://discourse.nodered.org/u/jorymathis13)\
**Post date:** [15 November 2022 17:51 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/7 "2022-11-15T17:51:25Z")

</div>

I added a debug node to each output of the function node to see exactly what is going on.

 ![MQTT VidTest_15Nov22_1136](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/c/ec31ec994dd0e8381d713b977a0805dbd4b4953f.png)

Here is the output from debug when I send the "on" message from MQTT

 ![Function_Debug_15Nov22_1138](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/a/fafd3db3f666fad3147fa00a42b2ba08954383f4.png)

Here is the code that I have set in my function node, as well as the debug output when I attempt to send the "off" message via MQTT to stop the video, which isn't working. I am seeing "undefined" in the debug 6 node, which is what I'm assuming is my hiccup.

 ![ffmpeg_Node_Config_15Nov22_1141](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/7605cfd3db77588e3b502780f24d2c26b3b86eb7.png)

Thank you for your willingness to assist!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2022 18:14 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/8 "2022-11-15T18:14:06Z")

</div>

In future please copy/paste code rather then screenshot, then I could have editted it.  
It should be  
`const payload = msg.payload`

---

<div class="post-metadata">

**Author:** ![jorymathis13](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorymathis13/32/66999_2.png) [@jorymathis13](https://discourse.nodered.org/u/jorymathis13)\
**Post date:** [15 November 2022 18:20 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/9 "2022-11-15T18:20:29Z")

</div>

Thank you for the quick reply Colin, I'll set that and see what she turns out.

---

<div class="post-metadata">

**Author:** ![jorymathis13](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorymathis13/32/66999_2.png) [@jorymathis13](https://discourse.nodered.org/u/jorymathis13)\
**Post date:** [15 November 2022 20:45 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/10 "2022-11-15T20:45:49Z")

</div>

Holy cow, I just figured out my issue and fixed it!

**Previous code used:**

```auto
var payload = msg.payload; //get payload

if (msg.payload == "on") {
    msg.action = {
        "command": "start"
    };
    return msg;
}
 else if (msg.payload == "off") {
    msg.action = {
        "command": "stop"
    };
    return msg;
}
return null;

```

**Code that Works:**

```auto

const payload = msg.payload

if (msg.payload == "on") {
    msg.action = {
        "command": "start"
    };
    return [msg,null];
}
else if (msg.payload == "off") {
    msg.action = {
        "command": "stop"
    };
    return [null,msg];
}
return null;

```

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [15 November 2022 20:50 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/11 "2022-11-15T20:50:13Z")

</div>

nice. So you had 2 outputs on the function node?

---

<div class="post-metadata">

**Author:** ![jorymathis13](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorymathis13/32/66999_2.png) [@jorymathis13](https://discourse.nodered.org/u/jorymathis13)\
**Post date:** [15 November 2022 20:58 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/12 "2022-11-15T20:58:19Z")

</div>

Yes sir, and that was where my hang up was kicking me in the teeth. I looked on one of the pages that Steve Cope has:

> **[Using the Node-Red Function Node- Beginners Guide](https://stevesnoderedguide.com/node-red-functions)**
>
> The function node is used to run JavaScript code. The function node accepts a msg object as input and can return 0 or more message objects as output.

and studied his example for using two outputs and saw that my return statements were wrong. I went back to my code of my function node and added them the way he has them and low and behold it worked like magic!!!

Sorry if I seem overly excited but I've been struggling with this for a good while now. Nearly 2 months of trying to get this work, and now it does!!!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2022 22:07 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/13 "2022-11-15T22:07:08Z")

</div>

It is better to use triple = as you originally had.  
`if (msg.payload === "on") {`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 November 2022 08:53 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/14 "2022-11-16T08:53:54Z")

</div>

The more node-red way to do this would be to use a Switch node to split the messages down two paths dependent on on the value in msg.payload and then a Change node in each leg to set the action, both then feeding the next node.

---

<div class="post-metadata">

**Author:** ![jorymathis13](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jorymathis13/32/66999_2.png) [@jorymathis13](https://discourse.nodered.org/u/jorymathis13)\
**Post date:** [17 November 2022 14:48 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/15 "2022-11-17T14:48:40Z")

</div>

I used a switch node in my initial design that I posted in a previous post, but I couldn't get the MQTT messages at that time to start the video streams. That's why I went with the function node in this instance of testing.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 January 2023 14:49 UTC](https://discourse.nodered.org/t/mqtt-wont-send-stop-command-to-ffmpeg-spawn-node/70290/16 "2023-01-16T14:49:01Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
