Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set video objects source file to a blob url

As an example, say I have a video object set on my website with the following attributes:

<video controls="" preload="auto" id="_video"></video>

and the original source being ./video/video.mp4 (for example).

How can I go about protecting the original source files location through converting it to a BLOB url?

I have seen a few posts stating that it needs to be done within through JavaScript, but none of them actually go to the extent of explaining how to do it, or where you can find out.

So, how would you do it; and what is the best way of doing it?

like image 478
GROVER. Avatar asked Dec 16 '16 12:12

GROVER.


People also ask

How do I get a blob URL for a 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.

Can we store video in blob?

Similar to the popular AWS S3 storage, Azure too provides an object storage called the Azure Blob Storage. Users can upload different kinds of files like images, videos, documents, zip files, and more without worrying about the storage space available, which is practically infinite.

How do I create a blob URL?

createObjectURL() static method creates a string containing a URL representing the object given in the parameter. The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object. To release an object URL, call revokeObjectURL() .

What is blob in video URL?

Blob, short for Binary Large Object, is a collection of binary data that usually store images, audio, or other multimedia objects. A Blob URL can serve as a URL source for the data contained in the Blob object. However, the Blob URL is generated internally by the browser only.


1 Answers

Request the video using a new XMLHttpRequest() with the responseType set to blob.

Pass the xhr.result to a new FileReader() using readAsDataURL, which will give you a base-64 encoded string representation of the file.

Pass this string to atob to decode the base-64 encoded string to a string representing each byte of binary data. Now you can create an array of byte values using charCodeAt looping over the byte string.

This array can be passed to new Uint8Array() to create a typed array of 8-bit unsigned ints, which then can be passed to the new Blob() constructor.

Now that you have a blob you can use URL.createObjectURL() and pass the created blob to it. The created object url can be passed to the src attribute of your video DOM element.

Here is a quick and dirty example. Hope it helps. Make sure to go over the docs of all of the methods being used and check their browser support. This will not protect your video from being downloadable though.

var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';

xhr.onload = function() {
  
  var reader = new FileReader();
  
  reader.onloadend = function() {
  
    var byteCharacters = atob(reader.result.slice(reader.result.indexOf(',') + 1));
    
    var byteNumbers = new Array(byteCharacters.length);

    for (var i = 0; i < byteCharacters.length; i++) {
      
      byteNumbers[i] = byteCharacters.charCodeAt(i);
      
    }

    var byteArray = new Uint8Array(byteNumbers);
    var blob = new Blob([byteArray], {type: 'video/ogg'});
    var url = URL.createObjectURL(blob);
    
    document.getElementById('_video').src = url;
    
  }
  
  reader.readAsDataURL(xhr.response);
  
};

xhr.open('GET', 'https://upload.wikimedia.org/wikipedia/commons/c/c0/Roaring_Burps.ogg');
xhr.send();
<video controls="" preload="auto" id="_video"></video>
like image 70
DavidDomain Avatar answered Oct 13 '22 14:10

DavidDomain