I have a shell command that I would like to execute, with a python script. It's a combination of ffmpeg
, grep
, gawk
and several other ffmpeg
options.
The command
ffmpeg -i http://0.0.0.0:8080/stream/video.mjpeg -vcodec copy -map 0 -f segment -segment_time 2 -loglevel 40 -segment_format mp4 capture-%05d.mp4 2>&1 | grep --line-buffered -Eo "segment:.+ended" | gawk -F "'" '{print $2; system("")}'
If you run this command on terminal, it should return a string
capture-00001.mp4
Now, my objective is to run this with subprocess
on Python3. Since the command is rather complex with mixed single and double quotes, it needs to be treated correctly otherwise it will complain EOL while scanning string literal
, invalid syntax
, etc, etc.
I have tried several string formating methods but none work. Below is one of the ways I've tried.
Script
import os
import datetime
import subprocess
first = "segment:.+ended"
second = "'"
third = '{print $2; system("")}'
if __name__ == "__main__":
fScript = "ffmpeg -i http://0.0.0.0:8080/stream/video.mjpeg -vcodec copy -map 0 -f segment -segment_time 2 -loglevel 40 -segment_format mp4 capture-%05d.mp4 2>&1 | grep --line-buffered -Eo {} | gawk -F {} {}".format(first, second, third)
try:
result = subprocess.check_output(fScript, shell=True).decode('utf-8')
print(result)
except subprocess.CalledProcessError as e:
print(e.output)