在js中对Videofile进行Base64编码 [英] Base64-encoding a Videofile in js

查看:185
本文介绍了在js中对Videofile进行Base64编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个视频文件及其路径(例如/outerfolder/innerfolder/video.mp4 ).现在,我想用Base64在JS中编码该视频,以便能够将其存储在数据库中.如果您能帮助我对视频文件进行编码,我将非常感激.预先感谢.

So i've got a video file and the path to it (e.g. /outerfolder/innerfolder/video.mp4). I now want to encode this video with Base64 in JS so that I am able to store it in a database. If you could help me with the encoding of the video file, I would be very thankful. Thanks in advance.

推荐答案

您可以使用以下函数对文件进行编码,以将文件转换为ArrayBuffer:

You could encode the file with the following function to convert your file to an ArrayBuffer:

[更新]

//<input type=file id="encondeMP4">
var encodeMP4 = document.getElementById('encondeMP4');

您现在在文件输入更改时向其中添加事件侦听器

You now add an event listener to when file input changes

window.onload = function () {
    //add eventlisteners

    encodeMP4.addEventListener('change', someFunction);
}

您需要一个函数来处理来自事件监听器的呼叫

you need a function to handle the call from the eventlistener

function someFunction(){
    encode(arrayBufferToString)
}

function encode(callback){
    var file = encodeMP4.files[0];
    var reader = new FileReader();
    reader.onload = function(e){
        var contents = e.target.result;
        var contentBuffer = arrayBufferToString(contents);
        var array = callback(contentBuffer);
    }
    reader.readAsArrayBuffer(file);
}

在var数组中,您现在将MP4二进制编码,在上一个函数中是一个内部变量,因此您需要根据需要调整此代码.也许是一个名为 YourEncodedMP4 = array

in var array you now have the MP4 enconded in binary, in the previous function is an internal variable so you'll need to adapt this code to your needs. Maybe a global container called YourEncodedMP4 = array

function arrayBufferToString(buffer) {
    var binary = '';
    var bytes = new Uint8Array( buffer );
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode( bytes[ i ] );
    }
    return binary;
}

现在您可以调用此函数将MP4加密的字符串转换为Base64了.

you now are ready to call this function to convert the MP4 enconded String to a Base64 one.

由您决定存储var数组的内容的位置,但请记住此调用是异步的.

Is up to you where the contents of var array would be stored, but remembered this call is asynchronous.

现在您可以使用容器"YourEncodedMP4"使用此功能

Now you could use this function using the container "YourEncodedMP4"

function stringToArrayBuffer(YourEncodedMP4) {
    var arrBuff = new ArrayBuffer(YourEncodedMP4.length);
    var writer = new Uint8Array(arrBuff);
    for(var i = 0, len = YourEncodedMP4.length; i < len; i++){
        writer[i] = YourEncodedMP4.charCodeAt(i);
    }

    return writer;
}

您现在拥有了一个返回字节数组的函数,该函数超出了您的使用范围

you now have a function that returns a Byte Array, than you could use

var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(stringToArrayBuffer(YourEncodedMP4))));

您将得到一个StringBase64

you'll end up with a StringBase64

这篇关于在js中对Videofile进行Base64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆