4

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
clemens
  • 16,716
  • 11
  • 50
  • 65
luckydonald
  • 5,976
  • 4
  • 38
  • 58
  • You could use ffmpeg, and pipe in the binary stream. – user1767754 Dec 29 '17 at 12:34
  • `ffmpeg` reading directly from the URL and writing to stdout, perhaps, but you might have trouble with MP4 format since it requires seekable output. It should work with Matroska (MKV) format. – mhawke Dec 29 '17 at 12:50
  • @mhawke What about the "progressive download" option some encoders have? stackoverflow.com/a/10330501/3423324 – luckydonald Jul 01 '18 at 00:23
  • Turns out "progressive download" is archived by moving the part at the end of the mp4 (moov atom for metadata that also has to specify the length) to the beginning after fully transcoding. Therefore, you still need to generate the full file before get progressive download. Also see https://stackoverflow.com/questions/13010530/live-streaming-through-mp4 for further ideas. – luckydonald Jul 18 '18 at 12:45

1 Answers1

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.

szatmary
  • 29,969
  • 8
  • 44
  • 57
  • Seem to be possible via "fast start"/"web optimized"/"progressive download" options https://stackoverflow.com/a/10330501/3423324 – luckydonald Jul 01 '18 at 00:18
  • Moving the moov box to the front of the file still requires the entire file be completed first. Then rewritten to move the data to the front. – szatmary Jul 01 '18 at 16:52
  • It is possible via fragmented mp4. But that’s different that that question asked for. – szatmary Jul 01 '18 at 17:11