Last active 1611817310

A simple flask server to encode a specified video/audio file and stream the chunks with a generator

S Smith revised this gist 1566286339. Go to revision

1 file changed, 29 insertions

ffmpeg_flask_stream.py(file created)

@@ -0,0 +1,29 @@
1 + #!/usr/bin/env python3
2 + from subprocess import Popen, PIPE
3 + from flask import Flask, Response
4 + import os.path
5 +
6 + app = Flask(__name__)
7 + PREFIX = "/media/storage"
8 + FORMAT = ("mp3", "audio/mpeg")
9 +
10 + def ffmpeg_generator(fn):
11 + process = Popen(["ffmpeg", "-hide_banner", "-loglevel", "panic", "-i", fn, "-f", FORMAT[0], "-"], stdout=PIPE)
12 + while True:
13 + data = process.stdout.read(1024)
14 + if not data:
15 + break
16 + yield data
17 +
18 + @app.route("/")
19 + @app.route("/favicon.ico")
20 + def nothing():
21 + return "", 404
22 +
23 + @app.route("/<path:path>")
24 + def play(path):
25 + path = os.path.normpath("/" + path).lstrip("/")
26 + return Response(ffmpeg_generator(os.path.join(PREFIX, path)), mimetype=FORMAT[1])
27 +
28 + if __name__ == "__main__":
29 + app.run()
Newer Older