uniapp怎么使用plus.zip.compressVideo压缩视频
时间: 2024-04-22 17:24:50 浏览: 377
在uni-app中使用plus.zip.compressVideo
方法压缩视频需要使用HBuilderX进行开发,并在真机环境下运行。
以下是一个示例代码,演示如何在uni-app中使用plus.zip.compressVideo
方法压缩视频:
<template>
<view>
<button @click="compressVideo">压缩视频</button>
</view>
</template>
<script>
export default {
methods: {
compressVideo() {
const self = this;
plus.gallery.pick(function (e) {
plus.zip.compressVideo({
src: e,
dst: '_doc/compressed.mp4',
quality: 0.5,
overwrite: true
}, function (event) {
console.log('视频压缩成功');
console.log('压缩后的视频路径:' + event.target);
self.getCompressedVideoSize(event.target);
}, function (error) {
console.log('视频压缩失败:' + error.message);
});
}, function (error) {
console.log('选择视频失败:' + error.message);
}, {
filter: 'video',
multiple: false
});
},
getCompressedVideoSize(path) {
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
fs.root.getFile(path, { create: false }, function (fileEntry) {
fileEntry.getMetadata(function (metadata) {
// 获取压缩后的视频文件大小
const size = metadata.size;
console.log('压缩后的视频文件大小:' + size + ' bytes');
}, function (error) {
console.log('获取压缩后的视频文件大小失败:' + error.message);
});
}, function (error) {
console.log('获取压缩后的视频文件失败:' + error.message);
});
}, function (error) {
console.log('请求文件系统失败:' + error.message);
});
}
}
}
</script>
在这个示例中,我们通过点击按钮调用compressVideo
方法来选择视频并进行压缩。在视频压缩成功后,我们通过plus.io.requestFileSystem
来获取压缩后的视频文件大小。
需要注意的是,使用plus.zip.compressVideo
方法进行视频压缩需要在真机环境下运行,无法在模拟器中测试。
希望这个示例对你有帮助!如果还有其他问题,请随时提问。
相关推荐


















