-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
52 lines (41 loc) · 1.61 KB
/
app.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
import subprocess
from flask import Flask, Response, request, json
from functools import wraps
from typing import List
app = Flask(__name__)
def args_required(*expected_args: str):
"""Confirm expected request args are present"""
def decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
for expected_arg in expected_args:
if not request.args.get(expected_arg, None):
return Response(json.dumps({'Error': '{} parameter is required'.format(expected_arg)}),
status=200,
mimetype='application/json')
return f(*args, **kwargs)
return wrapper
return decorator
def stream_shell_cmd(cmd: List[str], mimetype: str, chunksize: int = 16384) -> Response:
"""Returns a streaming flask Response of specified mimetype type given a shell command"""
def streamer():
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
while True:
chunk = p.stdout.read(chunksize)
if not chunk:
app.logger.debug(p.stderr.read().decode('utf-8'))
break
yield chunk
return Response(streamer(), 200, mimetype=mimetype)
@app.route('/')
@app.route('/version')
def version():
cmd = ['bin/ffmpeg', '-version']
mimetype = 'text/plain'
return stream_shell_cmd(cmd, mimetype)
# We only need this for local development.
if __name__ == '__main__':
app.run(debug=False)