Skip to content

Instantly share code, notes, and snippets.

@blha303
Created August 20, 2019 07:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blha303/e7f03977439c24213589fe7d85e54a08 to your computer and use it in GitHub Desktop.
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
#!/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