uniapp的移动端录制视频,压缩,并保存本地

录视频和压缩

videoRecording() {
				let that = this;
				uni.showLoading({
					title: '正在加载...'
				})
				uni.chooseVideo({
					count: 1,
					sourceType: ['camera'],
					maxDuration: 30,
					mediaType: ['video'],
					compressed: false,
					success: res => {
						console.log('chooseVideo.size1', res.size / 1048576)
						//视频在60-70-80M之间,压缩后约为6M
						uni.compressVideo({
							src: res.tempFilePath,
							quality: 'high',//视频质量高低,基本上高质量时60m视频压缩后为6做左右,中质量60m压缩为3m左右,低质量60m压缩为2m左右(看不清人了)
							bitrate: 2000,
							fps: 30,
							resolution: 1,
							success: (result) => {
								that.videoRecordingPath = result.tempFilePath;
								console.log('compressVideo.size2', result.size / 1048576)
								that.saveFile(result.tempFilePath);
							},
							fail: () => {
								uni.hideLoading();
								uni.showToast({
									title: '相机调用失败!',
									icon: 'none',
									duration: 1500
								});
							}
						})

					},
					fail: (err) => {
						uni.showToast({
							title: '视频录制失败!',
							duration: 2000
						});
					}
				})

			},

 保存本地

saveFile(filePath) {
				uni.saveFile({
					tempFilePath: filePath,
					success: (res2) => {
						uni.hideLoading();
						this.getSavedFileInfo(res2.savedFilePath)
					},
					fail: () => {
						uni.hideLoading();
						uni.showToast({
							title: '视频本地保存失败!',
							icon: 'none',
							duration: 1500
						});
					}
				})

			},

  

 读取本地视频

//读取视频
			getSavedFileInfo(filePath) {
				let that = this;
				uni.getSavedFileInfo({
					filePath: filePath,
					success: (res) => {
						uni.getVideoInfo({
							src: filePath,
							success: (res) => {
								// that.videoImgShow = true;
								that.videoList.push(filePath) 
							},
							fail: (err) => {
								uni.hideLoading();
								uni.showToast({
									title: '视频获取失败!',
									icon: 'none',
									duration: 1500
								});
							}
						})
					}
				});
			},

  删除视频

closeHandler() {
				// this.addIconShow = false;
				let that=this;
				uni.removeSavedFile({
					filePath: that.videoList[0],
					success: (res) => {
						that.videoList=[]
					},
					fail: () => {
						uni.hideLoading();
						uni.showToast({
							title: '视频删除失败!',
							icon: 'none',
							duration: 1500
						});
					}
				})
			}

  

posted @ 2022-05-24 21:08  大笛子  阅读(2944)  评论(0编辑  收藏  举报