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/"')

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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().
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)
Thank you so much! it really helped me a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.