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



目录
相关文章
Threejs实现相机视角切换,平滑过渡,点击模型切换到查看模型视角
Threejs实现相机视角切换,平滑过渡,点击模型切换到查看模型视角
2866 0
Threejs实现相机视角切换,平滑过渡,点击模型切换到查看模型视角
|
运维 监控 安全
数字孪生与矿业:提高开采效率
数字孪生技术在矿业领域的应用正逐步改变传统采矿模式,通过创建矿山的虚拟模型并实时集成数据,提高开采效率、优化资源利用、降低安全风险。本文深入探讨其在精准勘探、优化开采计划、实时监控与故障预警等方面的应用,以及带来的优势和典型案例。
|
资源调度 JavaScript 定位技术
Vue2使用百度地图展示或搜索地点(vue-baidu-map)
本文介绍了如何在 Vue 项目中使用 `vue-baidu-map` 插件,包括安装、全局注册及具体应用。首先通过 `yarn add vue-baidu-map` 安装插件,并在 `main.js` 中全局注册。然后展示了如何在地图上显示特定位置的标记,以及如何搜索地点并获取其经纬度和详细地址信息。代码示例提供了详细的实现方法和样式调整。如需使用,请确保已获取百度地图 API 的密钥。
3298 1
|
11月前
|
人工智能 缓存 UED
deepseek-vue3ai流式输出AI对话助手
原创新作vue3.5+deepseek+vite6+vant4仿DeepSeek-R1流式输出ai聊天对话。支持AI流式打字输出效果、浅色/暗黑主题模式、代码高亮、针对移动端+PC端适配处理。
1182 65
|
存储 关系型数据库 Serverless
PostgreSQL计算两个点之间的距离
PostgreSQL计算两个点之间的距离
1256 60
|
前端开发 JavaScript 开发工具
使用jsDelivr和GitHub,上传本地静态资源到免费CDN
本文介绍了一种将本地图片和静态资源(如 js、css、文档等)上传至免费CDN的方法,便于随时调用。具体步骤包括:在GitHub创建仓库`resources`存放资源;通过上传或Git命令同步文件;在仓库中创建新版本并发布。之后即可通过指定格式的URL访问这些CDN资源。此方法简单高效,适合开发者快速部署和共享静态内容。
1338 5
使用jsDelivr和GitHub,上传本地静态资源到免费CDN
|
JavaScript
Vue系列——通过moke进行数据模拟
通过moke进行数据模拟
1149 0
Centos8安装yum源时候出现的异常问题及解决方案(保好使)
Centos8安装yum源时候出现的异常问题及解决方案(保好使)
|
JavaScript 定位技术
vue-baidu-map 百度地图检索、获取坐标
vue-baidu-map 百度地图检索、获取坐标
392 1

热门文章

最新文章