Convert WebM as MP4 on the fly

2019-03-31 05:27发布

问题:

I am trying to convert a remote WebM file on the fly to MP4. This should happen without writing anything to disk. Furthermore it would be great to be able to stream out results as soon as possible.

This is my flask function without the actual conversion, so you get a idea of the streaming.

@app.route("/stream/mp4")
def as_mp4():
    url = "http://video.webmfiles.org/big-buck-bunny_trailer.webm"
    r = requests.get(url, stream=True)

    def stream():
        # convert it here
        for chunk in r.iter_content(chunk_size=1024):
            yield chunk
        # end for
    # end def
    return Response(stream(), mimetype="video/mp4")
# end def

回答1:

You are not going to get the results you expect. MP4 uses an “index” (called the moov box) that is used to parse the raw/chunked elementary streams (in the mdat box). Because this index contains the duration and size of each frame, the index is not available until the last frame is processed. So, even if you send the data to the client, the client can’t play the video until the entire thing is received.



标签: python mp4 webm