简体   繁体   中英

Decode a base64 video blob with JavaScript

I've made a video encoder that gives output in base64 encoded video.

A video player needs to play the video in the browser. My current code doesn't works, I tried it also with responseText and not with the response as a blob.

So the server gets the video and processes it as a base64 string. Then the browser needs to decode it and view it as a video, how can I get this done?

My code:

decodeBase64 = function(s) {
    var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
    var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    for(i=0;i<64;i++){e[A.charAt(i)]=i;}
    for(x=0;x<L;x++){
        c=e[s.charAt(x)];b=(b<<6)+c;l+=6;
        while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
    }
    return r;
};
var xhr = new XMLHttpRequest();
xhr.open("GET", "video");
xhr.responseType = "blob";
xhr.onload = response;
xhr.send();

function response(e) {
   var URL = window.URL || window.webkitURL;
   var videoUrl = URL.createObjectURL(decodeBase64(this.response));
   document.querySelector("video").src = videoUrl;
}

Compare output of decodeBase64 with that of (built-in) btoa .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM