Created
August 20, 2019 07:32
-
-
Save blha303/e7f03977439c24213589fe7d85e54a08 to your computer and use it in GitHub Desktop.
A simple flask server to encode a specified video/audio file and stream the chunks with a generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from subprocess import Popen, PIPE | |
from flask import Flask, Response | |
import os.path | |
app = Flask(__name__) | |
PREFIX = "/media/storage" | |
FORMAT = ("mp3", "audio/mpeg") | |
def ffmpeg_generator(fn): | |
process = Popen(["ffmpeg", "-hide_banner", "-loglevel", "panic", "-i", fn, "-f", FORMAT[0], "-"], stdout=PIPE) | |
while True: | |
data = process.stdout.read(1024) | |
if not data: | |
break | |
yield data | |
@app.route("/") | |
@app.route("/favicon.ico") | |
def nothing(): | |
return "", 404 | |
@app.route("/<path:path>") | |
def play(path): | |
path = os.path.normpath("/" + path).lstrip("/") | |
return Response(ffmpeg_generator(os.path.join(PREFIX, path)), mimetype=FORMAT[1]) | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment