Sending FFmpeg output to HTTP, via Python's Flash

May 28, 2018 ยท View on GitHub

""" Streaming FFmpeg to HTTP, via Python's Flask. This is an incredibly simple example, which will yield issues due to inconsistant input and output rates. If you're going to use this, VLC works okay as a client. You really need to move FFmpeg into a separate thread, which should help stream audio more consistantly to the HTTP client.

Example by Anthony Eden (https://mediarealm.com.au) """

from flask import Flask from flask import stream_with_context, request, Response import subprocess import time

app = Flask(name)

@app.route("/") def hello(): def generate(): startTime = time.time() buffer = [] sentBurst = False

    ffmpeg_command = ["ffmpeg", "-f", "avfoundation", "-i", ":2", "-acodec", "libmp3lame", "-ab", "32k", "-ac", "1", "-f", "mpeg", "pipe:stdout"]
    process = subprocess.Popen(ffmpeg_command, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, bufsize = -1)

    while True:
        # Get some data from ffmpeg
        line = process.stdout.read(1024)

        # We buffer everything before outputting it
        buffer.append(line)

        # Minimum buffer time, 3 seconds
        if sentBurst is False and time.time() > startTime + 3 and len(buffer) > 0:
            sentBurst = True

            for i in range(0, len(buffer) - 2):
                print "Send initial burst #", i
                yield buffer.pop(0)

        elif time.time() > startTime + 3 and len(buffer) > 0:
            yield buffer.pop(0)

        process.poll()
        if isinstance(process.returncode, int):
            if process.returncode > 0:
                print 'FFmpeg Error', p.returncode
            break

return Response(stream_with_context(generate()), mimetype = "audio/mpeg")    

if name == "main": app.run()