Recording MP4 gets corrupted

447 views
Skip to first unread message

Tomer Setty

unread,
Dec 18, 2018, 4:33:43 PM12/18/18
to kurento
Hi!
I was modifying the files from the Kurento record tutorial and after setting it to record MP4 (in order to support streaming video on IOS)
thing is if a user stream to the Kurento server but doesn't stop the recording properly, the file will get corrupted, this behavior does not happen with WEBM tho...
what can i do to make sure i can get working MP4 files?

im working with latest Kurento version
and this is what i put in my code

var filename = 'vid_' + startTime + '.mp4'
        var elements =
        [
          {type: 'RecorderEndpoint', params: {uri : file_path, mediaProfile: 'MP4'}},
          {type: 'WebRtcEndpoint', params: {}}
        ]

Tomer Setty

unread,
Dec 18, 2018, 4:37:22 PM12/18/18
to kurento
Side note, when saying properly i meant 
                 recorder.stop();
                pipeline.release();

so the flow of corrupted MP4 will usually be a user exit the page without terminating the session with the commands above

Paulo R. Lanzarin

unread,
Dec 18, 2018, 6:15:15 PM12/18/18
to kur...@googlegroups.com
There's not much you can do with MP4 in that case. MP4 is a corruption-prone container for live recordings.
There are tools that might be able to recover corrupted MP4s, but they're both untrustworthy
and hard to use.

What you can do, out of the box, is create a proxy RTP endpoint that forcibly uses VP8 to force transcoding
of the H.264 stream to VP8, and then record it via webm using the proxy as the feed. That's CPU intensive and might degrade quality, though.

What you really wanna do is use another container to record the H.264 stream. MKV is far more reliable container
for recording live H.264 feeds. While it's not part of the Kurento toolkit right now, there are two PRs open that implement MKV support on Kurento (https://github.com/Kurento/kms-core/pull/14 and https://github.com/Kurento/kms-elements/pull/13). You can compile Kurento yourself using those additions
and then use the MKV profile to record your H.264 streams.



--
You received this message because you are subscribed to the Google Groups "kurento" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kurento+u...@googlegroups.com.
To post to this group, send email to kur...@googlegroups.com.
Visit this group at https://groups.google.com/group/kurento.
To view this discussion on the web visit https://groups.google.com/d/msgid/kurento/c3ec494c-83f3-4eb5-a31a-8be4cb2e88b0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Juan Navarro

unread,
Feb 20, 2019, 4:09:04 AM2/20/19
to kurento
Hi Tomer, if you're already around here... in addition to what Paulo already told you, there is another thing you could try:

I've been checking on the possibility to use MP4's ability to write the moov atom at the front of the file (aka. "Fast Start"). This means that headers and other metadata will be written first, instead of last, so an abrupt file ending will mean the file is still playable... contrary to what happens by default.

KMS uses the GStreamer "mp4mux" plugin to create MP4 files, and this plugin supports "fast start" mode:

And interestingly enough, KMS does already enable the fast start mode EXCEPT for file recordings!
I'm not sure about the reasoning for this, as the previous team who wrote this code has already left the project... but you could try enabling fast start also for file recordings and see if it works better.


if ((gst_element_factory_get_element_type (sink_factory) !=
        gst_element_factory_get_element_type (file_sink_factory))) {
  g_object_set (mux, "faststart", TRUE, NULL);
}

to this:

g_object_set (mux, "faststart", TRUE, NULL);

then build your KMS as described in the Developer Guide.

Please get in contact with me if you try this, as I'm trying to decide if adding this change in KMS, but need some feedback about what issued this may cause (for one, it will automatically generate temporary files in the hard disk, according to GStreamer's faststart-file property description)

Carlos

unread,
Mar 6, 2019, 5:22:26 AM3/6/19
to kurento
I was having similar issues even following the "properly stop" due to a race condition because the hello world tutorial code was not waiting for the promises to finish, so headers were wrong when trying to use the recorded file immediately after disconnection. Maybe you can force the user to wait when leaving the session with some JS code?

In case it helps for your case, this is my "disconnect" function now:

   var self = this;

    co(function*(){
        try{
           
            if(krecorder) {
                yield krecorder.stopAndWait(); // Avoids race condition, waiting for emptying buffers and writting headers
                krecorder = null;
            }
            if (webRtcPeer) {
                webRtcPeer.dispose(); // No promise
                webRtcPeer = null;
            }

            if(pipeline){
                yield pipeline.release(); // Promise
                pipeline = null;
            }

            setStatus(IDLE);
        }
        catch(e)
        {
            self.onError.call(self, e);
        }
    })();

Regards

Aashirwad Gupta

unread,
Oct 10, 2019, 3:50:26 AM10/10/19
to kurento
Any idea as to how we can provide the fast start mode via java code (when recording streams as MP4 videos), as I do not have access to change kms files in my current application environments

Juan Navarro

unread,
Oct 10, 2019, 8:13:16 AM10/10/19
to kurento
This is not possible; when this conversation took place I was evaluating the pros and cons of allowing MP4 fast-start in recordings, and turns out that it really has some other drawbacks that don't make it the perfect solution we all wanted. Turns out MP4 is simply a very bad container format for live recordings. You can read more about it here: https://doc-kurento.readthedocs.io/en/latest/knowledge/mp4.html

WEBM works very well and it is resilient to recorder crashes, so with the addition of WEBM support since Safari 12.1, this should become the preferred format for all recordings.

If you have the knowledge and will to make a Pull Request, I would accept one that added MP4 fast-start setting in the Kurento settings file; just let me know.

Regards,
Juan
Reply all
Reply to author
Forward
0 new messages