Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ubergarm committed Aug 18, 2017
0 parents commit 8b34a3c
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.mypy_cache/
zappa_settings.json
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2017 John Leimgruber III <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
ffmpeg-lambda
===
Run ffmpeg inside a lambda for serverless transformations.

## Static Binaries
Grab the `x86_64 build` files from [ffmpeg static builds](https://www.johnvansickle.com/ffmpeg/).

Put `ffmpeg` static binary into into the `bin` subdirectory and delete everything else.

## Dev
```bash
pip install zappa flask
# export AWS secret/key/region etc
zappa init
zappa deploy dev
# make changes as needed
zappa update dev
# watch logs
zappa tail dev
```

## Production
Keep in mind limitations of lambda e.g. max runtime, CPU, and RAM etc.
You may need/want to add the following to your `zappa_settings.json`
```
"binary_support": true,
"cors": true,
```

## Todo
- [ ] production deploy
- [ ] ssl
- [x] streaming response
- [ ] better logging based on debug level
- [ ] standardize on JSON response for all errors/version endpoints etc

## Implications
You can stick almost any smallish `x86_64` statically compiled binary
e.g. `golang` etc into `bin` and call it via http! Cheers!

## Notes
I haven't implemented actual useful transformations in this
version. Sorry. Just pass arguments and keep in mind that ffmpeg
can read in http streams directly or use `stdin` and `stdout`.

## References
* [Miserlou/Zappa](https://github.com/Miserlou/Zappa)
* [ubergarm/pythumbio](https://github.com/ubergarm/pythumbio)
52 changes: 52 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
2 changes: 2 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit 8b34a3c

Please sign in to comment.