1. <legend id="z1ta0"></legend>

          <label id="z1ta0"></label>

          <label id="z1ta0"></label>

            <big id="z1ta0"></big>

            Cordova 拍視頻和本地壓縮

            小編:管理員 325閱讀 2022.09.13

            拍視頻
            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

            關聯標簽: