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
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.
来源:https://stackoverflow.com/questions/48022490/convert-webm-as-mp4-on-the-fly