Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a video from a Blob Javascript

I would like to display a video from a Javascript Blob/File object in the HTML5 video tag.

This code only works for small videos :

var reader = new FileReader(); reader.onload = function(e) {     document.getElementById("video").src=reader.result;  } reader.readAsDataURL(vid); 

I cannot use this for big videos (> 10MB). Is there a solution to display a big video from a blob object in HTML 5?

like image 331
Antonin M. Avatar asked Jan 14 '13 10:01

Antonin M.


People also ask

How do I watch a Blob video?

Right-click on the webpage and click “Inspect” in the menu. When the DevTools panel opens, click on the three vertical dots in the top-right corner and select “Undock into a separate window.” Press Ctrl + F on Windows or Cmd + F on Mac devices to find the blob URL. Enter “ blob:HTTP ” to find the link for the video.

How do I play Blob files?

Method 1: Use VLC Media Player to Convert and Download Blob Video. VLC Media Player might not be as popular as it once used to be, but the app still has its uses. The media player can convert blob URL videos into downloadable MP4 files and save them to your PC.

What is Blob in video SRC?

Blob URL/Object URL is a pseudo protocol to allow Blob and File objects to be used as URL source for things like images, download links for binary data and so forth. For example, you can not hand an Image object raw byte-data as it would not know what to do with it.


2 Answers

I've found. It was so simple that I didnt even see it...

 /**  * @param {Blob} videoFile  * @param {HTMLVideoElement} videoEl   * @returns {void}  */ function display( videoFile, videoEl ) {      // Preconditions:     if( !( videoFile instanceof Blob ) ) throw new Error( '`videoFile` must be a Blob or File object.' ); // The `File` prototype extends the `Blob` prototype, so `instanceof Blob` works for both.     if( !( videoEl instanceof HTMLVideoElement ) ) throw new Error( '`videoEl` must be a <video> element.' );          //       const newObjectUrl = URL.createObjectURL( videoFile );              // URLs created by `URL.createObjectURL` always use the `blob:` URI scheme: https://w3c.github.io/FileAPI/#dfn-createObjectURL     const oldObjectUrl = videoEl.currentSrc;     if( oldObjectUrl && oldObjectUrl.startsWith('blob:') ) {         // It is very important to revoke the previous ObjectURL to prevent memory leaks. Un-set the `src` first.         // See https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL          videoEl.src = ''; // <-- Un-set the src property *before* revoking the object URL.         URL.revokeObjectURL( oldObjectUrl );     }      // Then set the new URL:     videoEl.src = newObjectUrl;      // And load it:     videoEl.load(); // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/load      } 
like image 179
Antonin M. Avatar answered Sep 27 '22 22:09

Antonin M.


In some case blobObject.data should be provided in createObjectURL() method. See this simple trick:

function playVideo(videoStream){ // as blob    var video = document.querySelector('video');   var videoUrl=window.URL.createObjectURL(videoStream.data);// blob.data gives actual data   video.src = videoUrl; } 
like image 22
susan097 Avatar answered Sep 27 '22 21:09

susan097