Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  145] [ 3]  / answers: 1 / hits: 26686  / 8 Years ago, fri, december 16, 2016, 12:00:00

Let's say I have a video element on my website:


<video src="/video.mp4" controls="" id="video"></video>

How do I go about protecting the original source file (/video.mp4) by converting it to a single-session Blob URL?


I have seen a few posts stating that it needs to be done with JavaScript, though none of them actually expand on the necessary details explaining how to do it (or where you can find out how).


So, what is the best approach for something like this?


More From » html

 Answers
16

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.




// Request the video using a new XMLHttpRequest() with the 
// responseType set to blob.
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';

xhr.onload = function(){
var reader = new FileReader();

reader.onloadend = function(){
// Pass this string to atob to decode the base-64 encoded string to a string
// representing each byte of binary data.
var byteCharacters = atob(reader.result.slice(reader.result.indexOf(',') + 1));

// Now you can create an array of byte values using charCodeAt and looping
// over the byte string.
var byteNumbers = new Array(byteCharacters.length);
for(var i = 0; i < byteCharacters.length; i++){
byteNumbers[i] = byteCharacters.charCodeAt(i);
}

// Pass the resulting array to Uint8Array to create a typed array of
// 8-bit, unsigned integers.
var byteArray = new Uint8Array(byteNumbers);

// This can then can be passed to the Blob constructor.
var blob = new Blob([byteArray], {type: 'video/ogg'});

// Now that you have a blob, you can pass it to the createObjectURL method.
var url = URL.createObjectURL(blob);

// The resulting URL can be attached to the src attribute of your video.
document.getElementById('video').src = url;
}

// Pass the response to the FileReader using readAsDataURL.
// This will give you a base-64 encoded string representation of the file.
reader.readAsDataURL(xhr.response);
};

xhr.open('GET', 'https://upload.wikimedia.org/wikipedia/commons/c/c0/Roaring_Burps.ogg');
xhr.send();

<video controls= id=video></video>




[#59681] Wednesday, December 14, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daquanmilesw

Total Points: 57
Total Questions: 102
Total Answers: 110

Location: Wallis and Futuna
Member since Sat, Aug 6, 2022
2 Years ago
daquanmilesw questions
;