Convert WebM as MP4 on the fly
Asked Answered
A

1

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
Amuse answered 29/12, 2017 at 12:15 Comment(4)
You could use ffmpeg, and pipe in the binary stream.Childbearing
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.Either
@Either What about the "progressive download" option some encoders have? https://mcmap.net/q/216786/-html5-how-to-stream-large-mp4-filesAmuse
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://mcmap.net/q/146161/-live-streaming-through-mp4 for further ideas.Amuse
H
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.

Hypochlorite answered 29/12, 2017 at 15:34 Comment(3)
Seem to be possible via "fast start"/"web optimized"/"progressive download" options https://mcmap.net/q/216786/-html5-how-to-stream-large-mp4-filesAmuse
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.Hypochlorite
It is possible via fragmented mp4. But that’s different that that question asked for.Hypochlorite

© 2022 - 2024 — McMap. All rights reserved.