Cordova 拍视频和本地压缩

简介: 拍视频cordova plugin add cordova-plugin-media-capturejs调用片段function videoCapture() { //开始录像(最长录制时间:15秒) navigator.device.capture.captureVideo(onSuccess, onError, {duration: 15}); //录制成功 function onSuccess(mediaFiles) {

拍视频


cordova plugin add cordova-plugin-media-capture


js调用片段

function videoCapture() {
               //开始录像(最长录制时间:15秒)
               navigator.device.capture.captureVideo(onSuccess, onError, {duration: 15});
               //录制成功
               function onSuccess(mediaFiles) {
                  var i, path, len;
                  //遍历获取录制的文件(iOS只支持一次录制一个视频或音频)
                  for (i = 0, len = mediaFiles.length; i < len; i += 1) {
                    console.log(mediaFiles);
                     path = mediaFiles[i].fullPath;
                     alert("录制成功!\n\n"
                          + "文件名:" + mediaFiles[i].name + "\n"
                          + "大小:" + mediaFiles[i].size + "\n\n"
                          + "localURL地址:" + mediaFiles[i].localURL + "\n\n"
                          + "fullPath地址:" + path);
                  }
               }
               //录制失败
               function onError(error) {
                  alert('录制失败!错误码:' + error.code);
               }
          }


captureVideo参数说明

captureVideo(
            onSuccess: (mediaFiles: MediaFile[]) => void,
            onError: (error: CaptureError) => void,
            options?: VideoOptions): void ;
---------------------------
/** Encapsulates properties of a media capture file. */
interface MediaFile {
    /** The name of the file, without path information. */
    name: string;
    /** The full path of the file, including the name. */
    fullPath: string;
    /** The file's mime type */
    type: string;
    /** The date and time when the file was last modified. */
    lastModifiedDate: Date;
    /** The size of the file, in bytes. */
    size: number;
    /**
     * Retrieves format information about the media capture file.
     * @param successCallback Invoked with a MediaFileData object when successful.
     * @param errorCallback   Invoked if the attempt fails, this function. 
     */
    getFormatData(
        successCallback: (data: MediaFileData) => void,
        errorCallback?: () => void): void;
}
---------------------------
/** Encapsulates video capture configuration options. */
interface VideoOptions {
    /**
     * The maximum number of video clips the device's user can capture in a single
     * capture operation. The value must be greater than or equal to 1.
     */
    limit?: number;
    /** The maximum duration of a video clip, in seconds. */
    duration?: number;
}


navigator.device.capture.captureVideo 输出的log

fullPath: "file:///storage/emulated/0/DCIM/Camera/VID_20190601_120049.mp4"
lastModified: null
lastModifiedDate: 1559361649000
localURL: "cdvfile://localhost/sdcard/DCIM/Camera/VID_20190601_120049.mp4"
name: "VID_20190601_120049.mp4"
size: 2092530
start: 0
type: "video/mp4"


本地压缩


Installation 安装

cordova plugin add cordova-plugin-video-editor


this example uses the cordova media capture plugin

navigator.device.capture.captureVideo(
    videoCaptureSuccess,
    videoCaptureError,
    {
        limit: 1,
        duration: 20
    }
);
function videoCaptureSuccess(mediaFiles) {
    // Wrap this below in a ~100 ms timeout on Android if
    // you just recorded the video using the capture plugin.
    // For some reason it is not available immediately in the file system.
    var file = mediaFiles[0];
    var videoFileName = 'video-name-here'; // I suggest a uuid
    VideoEditor.transcodeVideo(
        videoTranscodeSuccess,
        videoTranscodeError,
        {
            fileUri: file.fullPath,
            outputFileName: videoFileName,
            outputFileType: VideoEditorOptions.OutputFileType.MPEG4,
            optimizeForNetworkUse: VideoEditorOptions.OptimizeForNetworkUse.YES,
            saveToLibrary: true,
            maintainAspectRatio: true,
            width: 640,
            height: 640,
            videoBitrate: 1000000, // 1 megabit
            audioChannels: 2,
            audioSampleRate: 44100,
            audioBitrate: 128000, // 128 kilobits
            progress: function(info) {
                console.log('transcodeVideo progress callback, info: ' + info);
            }
        }
    );
}
function videoTranscodeSuccess(result) {
    // result is the path to the transcoded video on the device
    console.log('videoTranscodeSuccess, result: ' + result);
}
function videoTranscodeError(err) {
    console.log('videoTranscodeError, err: ' + err);
}


cordova-plugin-video-editor源码

npm i cordova-plugin-video-editor

我建议将 maintainAspectRatio 设置为 true。 如果此选项为真,则可以提供任何宽度 / 高度,并且提供的高度将用于计算输出视频的新宽度。 如果您将 maintainAspectRatio 设置为 false,那么您很有可能会得到拉伸和 / 或扭曲的视频。 当 maintainAspectRatio 为 true-时,下面是 iOS 上使用的简化公式

aspectRatio = videoWidth / videoHeight;
outputWidth = height * aspectRatio;
outputHeight = outputWidth / aspectRatio;


默认输出路径为: /storage/emulated/0/Movies/HelloCordova



目录
相关文章
|
5月前
|
编解码 文字识别
搬运5款小体积,无广告,超实用的软件
大家好,我又来啦,今天给大家带来的5款软件,共同特点都是体积小、无广告、超实用,大家观看完可以自行搜索下载哦。
140 1
|
5月前
分享一下基于autojs4的片多多源码
分享一下基于autojs4的片多多源码
68 1
|
前端开发 定位技术 API
不用钱!纯前端打包下载离线瓦片地图
简直无语,瓦片地图明明是开放的,不用钱的,竟然有网站和程序要收费,本人绝不当冤大头,自己动手丰衣足食! 其实也有某些免费下载离线地图的良心程序,但因为下载瓦片的请求太频繁了,搞得打开该地图的时候卡死,被人家服务器记住了!
不用钱!纯前端打包下载离线瓦片地图
|
iOS开发
iOS App廋身体积压缩之字体图标使用
iOS App廋身体积压缩之字体图标使用
iOS App廋身体积压缩之字体图标使用
|
Java Android开发
ImagePicker在安卓上对图片的应用一例
偷得浮生半日闲,早上在逛StackOverflow的时候发现有人求助,提到了一个不怎么火的ImagePicker库:
137 0
|
开发工具 iOS开发 内存技术
iOS开发之多媒体播放
iOS开发之多媒体播放
148 0
youtube下载视屏和字幕办法
youtube下载视屏和字幕办法
186 0
|
编解码 iOS开发
iOS拍摄视频,压缩并上传服务器
iOS拍摄视频,压缩并上传服务器
459 0
|
存储 Python
如何无损压缩 bugly 符号表 40% 体积
如何无损压缩 bugly 符号表 40% 体积
111 0