Cordova 拍视频和本地压缩

2022-05-17 19:56:01 浏览数 (2)

拍视频

代码语言:javascript复制
cordova plugin add cordova-plugin-media-capture

js调用片段

代码语言:javascript复制
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("录制成功!nn"
                            "文件名:"   mediaFiles[i].name   "n"
                            "大小:"   mediaFiles[i].size   "nn"
                            "localURL地址:"   mediaFiles[i].localURL   "nn"
                            "fullPath地址:"   path);
                  }
               }
 
               //录制失败
               function onError(error) {
                  alert('录制失败!错误码:'   error.code);
               }
          }

captureVideo参数说明

代码语言:javascript复制
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

代码语言:javascript复制
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 安装

代码语言:javascript复制
cordova plugin add cordova-plugin-video-editor

this example uses the cordova media capture plugin

代码语言:javascript复制
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源码

代码语言:javascript复制
npm i cordova-plugin-video-editor

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

代码语言:javascript复制
aspectRatio = videoWidth / videoHeight;
outputWidth = height * aspectRatio;
outputHeight = outputWidth / aspectRatio;

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

0 人点赞