-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun.py
executable file
·76 lines (58 loc) · 1.76 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# Simple REST wrapper to start / stop streaming
import os
from subprocess import Popen
import sys
from flask import Flask
base_dir = "/opt/webcam/mjpg-streamer/mjpg-streamer-experimental"
mjpg_input = "input_raspicam.so -fps 5"
mjpg_output = "output_http.so -w " + base_dir + "/www"
app = Flask(__name__)
stream = None
def stream_start():
global stream
if stream:
return "Streaming already running"
else:
stream = Popen([
os.path.join(base_dir, 'mjpg_streamer'),
'-i', mjpg_input,
'-o', mjpg_output
], stdout=sys.stdout, stderr=sys.stderr)
return "Streaming started"
def stream_stop():
global stream
if stream:
stream.terminate()
stream.wait()
stream = None
return "Streaming stopped"
else:
return "Streaming not running"
@app.route("/on")
def stream_on():
return stream_start()
@app.route("/off")
def stream_off():
return stream_stop()
def initialize():
global mjpg_input
print("*** Starting WebcamStream")
sys.stdout.flush()
if os.getenv('BALENA'):
# Expunge unexpanded variables from docker-compose
for key, val in os.environ.items():
if key.startswith('WEBCAM_') and val.startswith('${'):
os.environ.pop(key)
# Set Library path for mjpg_streamer
if os.getenv('LD_LIBRARY_PATH'):
os.environ['LD_LIBRARY_PATH'] += ':' + base_dir
else:
os.environ['LD_LIBRARY_PATH'] = base_dir
if os.getenv('WEBCAM_INPUT'):
mjpg_input = os.getenv('WEBCAM_INPUT')
if os.getenv('WEBCAM_START', 'true') == 'true':
stream_start()
if __name__ == '__main__':
initialize()
app.run(debug=False, host="0.0.0.0", port=5200)