Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play WEBM files individually which are created by MediaRecorder

For recording audio and video, I am creating webm files under the ondataavailable of MediaRecorder API. I have to play each created webm file individually.

Mediarecorder api inserts header information into first chunk (webm file) only, so rest of the chunks do not play individually without the header information.

As suggested link 1 and link 2, I have extracted the header information from first chunk,

// for the most regular webm files, the header information exists
// between 0 to 189 Uint8 array elements

const headerIinformation = arrayBufferFirstChunk.slice(0, 189); 

and perpended this header information into second chunk, still the second chunk could not play, but this time the browser is showing poster (single frame) of video and duration of sum of two chunks, eg:10 seconds; duration of each chunk is 5 second.

The same header-information thing I have done with the hex editor. I opened the webm file in editor and copied the first 190 elements from first webm file and put this into second file, something like below image, even this time, the second webm file could not play and the result was same as in previous example.

Red color is showing the header information:

webm hex

This time I copied the header and cluster information from first webm file placed this into second file, something like below image, but did not get success,

webm hex

Questions

What I am doing wrong here ?

Is there any way that we can play the webm files/chunks individually ?

Note: I can't use the MediaSource to play those chunks.

Edit 1

As @Brad suggested, I want to insert all the content before the first cluster to a later a cluster. I have few webm files that each has duration of 5 seconds. After digging into the files, I came to know, almost every alternate file hasn't cluster point (no 0x1F43B675).

Here I am confused that I'll have to insert header information (initialization data) at the beginning of every file or beginning of every first cluster? If I choose a later option, then how's going to play the webm file that doesn't have any cluster ?

Or, first I need to make each webm file in a way that it has cluster at very beginning, so I can prepend the header information before cluster in those files?

Edit 2

After some digging and reading this , I came up with the conculsion that each webm file needs header info, cluster and actual data.

like image 237
Suman Bogati Avatar asked Jun 06 '20 19:06

Suman Bogati


People also ask

How do I watch WebM media files?

How to Open a WebM File in Windows. To open stand-alone WebM files on desktop and laptop computers running Windows, you can use one of the browsers that support this format. These include Google Chrome, Microsoft Edge, Mozilla Firefox, and Opera.

How do I play a video with WebM extension?

How to Play WEBM Files. You can open a WEBM video with most modern web browsers, including Chrome, Opera, Firefox, and Edge. If you want to play one in the Safari browser on a Mac, you can do so through VLC with the VLC for Mac OS X plug-in. If your browser won't play the video, make sure it's fully updated.

What creates WebM?

WebM files consist of video streams compressed with the VP8 or VP9 video codecs and audio streams compressed with the Vorbis or Opus audio codecs. The WebM file structure is based on the Matroska container.


Video Answer


1 Answers

// for the most regular webm files, the header information exists

// between 0 to 189 Uint8 array elements

Without seeing the actual file data it's hard to say, but this is possibly wrong. The "header information" needs to be everything up to the first Cluster element. That is, you want to keep all data from the start of the file up to before you see 0x1F43B675 and treat it as initialization data. This can/will vary from file to file. In my test file, this occurs a little after 1 KB in.

and perpended this header information into second chunk, still the second chunk could not play, but this time the browser is showing poster (single frame) of video and duration of sum of two chunks, eg:10 seconds; duration of each chunk is 5 second.

The chunks output from the MediaRecorder aren't relevant for segmentation, and can occur at various times. You would actually want to split on the Cluster element. That means you need to parse this WebM file, at least to the point of splitting out Clusters when their identifier 0x1F43B675 comes by.

Is there any way that we can play the webm files/chunks individually ?

You're on the right path, just prepend everything before the first Cluster to a later Cluster.

Once you've got that working, the next problem you'll likely hit is that you won't be able to do this with just any cluster. The first Cluster must begin with a keyframe or the browser won't decode it. Chrome will skip over to the next cluster, to a point, but it isn't reliable. Unfortunately, there's no way to configure keyframe placement with MediaRecorder. If you're lucky enough to be able to process this video server-side, here's how to do it with FFmpeg: https://stackoverflow.com/a/45172617/362536

like image 64
Brad Avatar answered Oct 27 '22 13:10

Brad