简体   繁体   English

如何使用jquery和phonegap将视频转换为base64

[英]How to convert video to base64 using jquery and phonegap

Here I have written the code in JavaScript using phonegap but unable to get video data in base64 在这里,我已经使用phonegap用JavaScript编写了代码,但是无法在base64中获取视频数据

function captureVideo() 
{

    navigator.device.capture.captureVideo(captureSuccess, captureError, {limit: 1});
}

function captureSuccess(mediaFiles)
{

    console.log(JSON.stringify(mediaFiles));
    console.log("HERE I WANT BASE64 CODE");

}

To get a data-uri from the video frame you will have to draw it onto a canvas element first (as well as making sure that CORS requirements are fulfilled). 要从视频帧中获取数据uri,您必须先将其绘制到canvas元素上(以及确保满足CORS要求)。

I am not that familiar with Cordova but hopefully this will get you in the right direction: 我对Cordova不太熟悉,但希望它能使您朝正确的方向前进:

// create a canvas element
var canvas = document.createElement('canvas'),     // canvas
    ctx = canvas.getContext('2d'),                 // context
    video = document.getElementById('myVideoId');  // get video element somehow

// setup canvas dimension
canvas.width = video.videoWidth || video.width;
canvas.height = video.videoHeight || video.height;

// draw in current video frame
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);

// extract data-uri
var dataUri = canvas.toDataURL();  // PNG is default, use image/jpeg for JPEG

If you need to do this many times then only set up canvas once and use the drawImage / toDataURL calls when you need a frame. 如果需要多次执行此操作,则只需设置一次画布,并在需要框架时使用drawImage / toDataURL调用。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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