4

I can execute mjpg-streamer using raspberry-pi 3 terminal.

And Below is command that I use.

mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 800x448" -o "output_http.so -p 8090 -w /usr/local/share/mjpg-streamer/www/"

And now I want to execute it on python 3. So I try to do it using os.system() and subprocess.call() but It failed to execute it and webcam go wrong after running code so I have to reboot raspberry-pi 3. even os.system() works well when code is like os.system('python3 test.py').

Is it not possible to execute mjpg-streamer using pathon 3 code?

below is my code.

import os

os.system('mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 800x448" -o "output_http.so -p 8090 -w /usr/local/share/mjpg-streamer/www/"')
MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
allentando
  • 105
  • 8

1 Answers1

4

you can try use subprocess that allows to save stdout and stderr too:

    import subprocess
    ### define the command
    command = 'mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 800x448" -o "output_http.so -p 8090 -w /usr/local/share/mjpg-streamer/www/"'
    ### execute the command and save stdout and stderr as variables
    output, error = subprocess.Popen(command, universal_newlines=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

and you will have stdout saved in "output" and "stderr" in "error" variable.

BTW: it would be advisable to use the listed format

cccnrc
  • 1,195
  • 11
  • 27
  • Thank you so much!! It works!! Can I ask you how to stop this after executing it? I use another variable to execute 'killall mjpg_streamer' but I think there are some functions to stop subprocess.Popen.communicate(). – allentando Jun 17 '19 at 15:19
  • 2
    you can take the ID of the process that you are executing and kill it based on PID, as example : `proc = subprocess.Popen(command, universal_newlines=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)` to take it and execute it as before: `output,error = proc.communicate()` and then stop the process based on the ID with (you need `import os` and `import signal`): `os.killpg(os.getpgid(proc.pid), signal.SIGTERM)` – cccnrc Jun 17 '19 at 15:27
  • Thank you so much! it really helped me a lot – allentando Jun 19 '19 at 11:54