video.md 12.3 KB
Newer Older
M
mehaotian 已提交
1 2 3 4 5
### uni.chooseVideo(OBJECT)
拍摄视频或从手机相册中选视频,返回视频的临时文件路径。另外选择和上传非图像、视频文件参考:[https://ask.dcloud.net.cn/article/35547](https://ask.dcloud.net.cn/article/35547)

**平台差异说明**

6
|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序|
W
wanganxp 已提交
7 8
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|√|√|√|√|√|√|√|
M
mehaotian 已提交
9 10 11 12 13 14

**OBJECT 参数说明**

|参数名|类型|必填|说明|平台差异说明|
|:-|:-|:-|:-|:-|
|sourceType|Array<String>|否|album 从相册选视频,camera 使用相机拍摄,默认为:['album', 'camera']||
15
|compressed|Boolean|否|是否压缩所选的视频源文件,默认值为 true,需要压缩。|微信小程序、百度小程序、字节跳动小程序|
M
mehaotian 已提交
16 17 18 19 20 21 22 23
|maxDuration|Number|否|拍摄视频最长拍摄时间,单位秒。最长支持 60 秒。|APP平台 1.9.7+(iOS支持,Android取决于ROM的拍照组件是否实现此功能,如果没实现此功能则忽略此属性。) 微信小程序、百度小程序|
|camera|String|否|'front'、'back',默认'back'|APP、微信小程序|
|success|Function|否|接口调用成功,返回视频文件的临时文件路径,详见返回参数说明。||
|fail|Function|否|接口调用失败的回调函数||
|complete|Function|否|接口调用结束的回调函数(调用成功、失败都会执行)| |

**success 返回参数说明**

Q
qiang 已提交
24 25 26
|参数|类型|说明|平台差异说明说明|
|:-|:-|:-|:-|
|tempFilePath|String|选定视频的临时文件路径||
Q
qiang 已提交
27
|tempFile|File|选定的视频文件|仅H5(2.6.15+)支持|
Q
qiang 已提交
28 29 30 31 32
|duration|Number|选定视频的时间长度,单位为 s|APP 2.1.0+、H5、微信小程序|
|size|Number|选定视频的数据量大小|APP 2.1.0+、H5、微信小程序|
|height|Number|返回选定视频的高|APP 2.1.0+、H5、微信小程序|
|width|Number|返回选定视频的宽|APP 2.1.0+、H5、微信小程序|
|name|String|包含扩展名的文件名称|仅H5支持|
M
mehaotian 已提交
33 34 35 36

**注意:**
* 文件的临时路径,在应用本次启动期间可以正常使用,如需持久保存,需在主动调用 [uni.saveFile](api/file/file?id=savefile),在应用下次启动时才能访问得到。
* camera 部分 Android 手机下由于系统 ROM 不支持无法生效,打开拍摄界面后可操作切换
W
wanganxp 已提交
37
* 可以通过用户授权API来判断用户是否给应用授予相册或摄像头的访问权限[https://uniapp.dcloud.io/api/other/authorize](https://uniapp.dcloud.io/api/other/authorize)
M
mehaotian 已提交
38
* App下如需进一步压缩视频大小,可以在插件市场搜索[视频压缩](http://ext.dcloud.net.cn/search?q=%E8%A7%86%E9%A2%91%E5%8E%8B%E7%BC%A9)插件
W
wanganxp 已提交
39
* 如需上传到cdn,可使用uniCloud.uploadFile API,uniCloud提供了免费cdn给开发者使用,详见[https://uniapp.dcloud.io/uniCloud/storage?id=uploadfile](https://uniapp.dcloud.io/uniCloud/storage?id=uploadfile)
M
mehaotian 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53

**示例**

```html
<template>
	<view>
		<text>hello</text>
		<button @tap="test">click me</button>
		<video :src="src"></video>
	</view>
</template>
```
```javascript
export default {
雪洛's avatar
雪洛 已提交
54 55 56 57
	data() {
		return {
			src: ''
		}
M
mehaotian 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	},
	methods: {
		test: function () {
			var self = this;
			uni.chooseVideo({
				count: 1,
				sourceType: ['camera', 'album'],
				success: function (res) {
					self.src = res.tempFilePath;
				}
			});
		}
	}
}
```
Q
qiang 已提交
73 74 75


### uni.chooseMedia(OBJECT)
76 77 78 79 80 81
拍摄或从手机相册中选择图片或视频。

**平台差异说明**

|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
Q
qiang 已提交
82 83 84 85
|x|x|2.10.0+|x|x|x|x|

**OBJECT 参数说明**

86 87 88 89 90 91 92 93 94 95 96
|参数名|类型|默认值|必填|说明|
|:-|:-|:-|:-|:-|
|count|Number|9|否|最多可以选择的文件个数|
|mediaType|Array.&lt;string&gt;|['image', 'video']|否|文件类型|
|sourceType|Array.&lt;string&gt;|['album', 'camera']|否|图片和视频选择的来源|
|maxDuration|Number|10|否|拍摄视频最长拍摄时间,单位秒。时间范围为 3s 至 30s 之间|
|sizeType|Array.&lt;string&gt;|['original', 'compressed']|否|仅对 mediaType 为 image 时有效,是否压缩所选文件|
|camera|String|'back'|否|仅在 sourceType 为 camera 时生效,使用前置或后置摄像头|
|success|function||否|接口调用成功的回调函数|
|fail|function||否|接口调用失败的回调函数|
|complete|function||否|接口调用结束的回调函数(调用成功、失败都会执行)|
Q
qiang 已提交
97 98 99

**OBJECT.mediaType 合法值**

100
|值|说明|
Q
qiang 已提交
101 102 103 104 105 106
|:-|:-|
|image|只能拍摄图片或从相册选择图片|
|video|只能拍摄视频或从相册选择视频	|

**OBJECT.sourceType 合法值**

107
|值|说明|
Q
qiang 已提交
108 109 110 111 112 113
|:-|:-|
|album|从相册选择|
|camera|使用相机拍摄	|

**OBJECT.camera 合法值**

114
|值|说明|
Q
qiang 已提交
115 116 117 118
|:-|:-|
|back|使用后置摄像头|
|front|使用前置摄像头	|

119 120 121 122 123 124 125 126 127 128 129
**success 返回参数说明**

|参数名|类型|说明|
|:-|:-|:-|
|tempFiles|Array.&lt;string&gt;|本地临时文件列表|
|type|String|文件类型,有效值有 image 、video|

**res.tempFiles 的结构**

|参数名						|类型		|说明												|
|:-								|:-			|:-													|
Q
qiang 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|tempFilePath			|String	|本地临时文件路径 (本地路径)|
|size							|Number	|本地临时文件大小,单位 B		|
|duration					|Number	|视频的时间长度							|
|height						|Number	|视频的高度									|
|width						|Number	|视频的宽度									|
|thumbTempFilePath|String	|视频缩略图临时文件路径			|


**示例**

```javascript
uni.chooseMedia({
  count: 9,
  mediaType: ['image','video'],
  sourceType: ['album', 'camera'],
  maxDuration: 30,
  camera: 'back',
  success(res) {
    console.log(res.tempFilest)
  }
})

```
M
mehaotian 已提交
153

W
wanganxp 已提交
154 155 156 157 158
**Tips**

* 如需上传到cdn,可使用uniCloud.uploadFile API,uniCloud提供了免费cdn给开发者使用,详见[https://uniapp.dcloud.io/uniCloud/storage?id=uploadfile](https://uniapp.dcloud.io/uniCloud/storage?id=uploadfile)


M
mehaotian 已提交
159 160 161 162 163
### uni.saveVideoToPhotosAlbum(OBJECT)
保存视频到系统相册。

**平台差异说明**

164
|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序|
W
wanganxp 已提交
165
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
雪洛's avatar
雪洛 已提交
166
|√|x|√|√|√|√|√|
M
mehaotian 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

**OBJECT 参数说明**

|参数名|类型|必填|说明|
|:-|:-|:-|:-|
|filePath|String|是|视频文件路径,可以是临时文件路径也可以是永久文件路径|
|success|Function|否|接口调用成功的回调函数|
|fail|Function|否|接口调用失败的回调函数|
|complete|Function|否|接口调用结束的回调函数(调用成功、失败都会执行)|

**success 返回参数说明**

|参数名|类型|说明|
|:-|:-|:-|
|errMsg|String|调用结果|

W
wanganxp 已提交
183 184 185 186
**注意**

- 可以通过用户授权API来判断用户是否给应用授予相册写入权限[https://uniapp.dcloud.io/api/other/authorize](https://uniapp.dcloud.io/api/other/authorize)

M
mehaotian 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199
**示例**

```html
<template>
	<view>
		<text>hello</text>
		<button @tap="test">click me</button>
		<video :src="src"></video>
	</view>
</template>
```
```javascript
export default {
雪洛's avatar
雪洛 已提交
200 201 202 203
	data() {
		return {
			src: ''
		}
M
mehaotian 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	},
	methods: {
		test: function () {
			var self = this;
			uni.chooseVideo({
				count: 1,
				sourceType: ['camera'],
				success: function (res) {
					self.src = res.tempFilePath;
					
					uni.saveVideoToPhotosAlbum({
						filePath: res.tempFilePath,
						success: function () {
							console.log('save success');
						}
					});
				}
			});
		}
	}
}
W
wanganxp 已提交
225
```
雪洛's avatar
雪洛 已提交
226

W
wanganxp 已提交
227 228 229
### uni.getVideoInfo(OBJECT)

获取视频详细信息
雪洛's avatar
雪洛 已提交
230 231 232 233 234

**平台差异说明**

|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
W
wanganxp 已提交
235
|x|x|2.11.0+|x|x|x|x|
雪洛's avatar
雪洛 已提交
236 237 238

**OBJECT 参数说明**

W
wanganxp 已提交
239 240 241 242 243
|属性			|类型			|默认值	|必填	|说明																									|
|:-:			|:-:			|:-:		|:-:	|:-:																									|
|src			|string		|-			|是		|视频文件路径,可以是临时文件路径也可以是永久文件路径	|
|success	|function	|-			|否		|接口调用成功的回调函数																|
|fail			|function	|-			|否		|接口调用失败的回调函数																|
雪洛's avatar
雪洛 已提交
244 245 246 247 248
|complete	|function	|-			|否		|接口调用结束的回调函数(调用成功、失败都会执行)			|

**success 返回参数说明**

|参数名			|类型		|说明								|
W
wanganxp 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|:-					|:-			|:-									|
|orientation|string	|画面方向						|
|type				|string	|视频格式						|
|duration		|number	|视频长度						|
|size				|number	|视频大小,单位 kB	|
|height			|number	|视频的长,单位 px	|
|width			|number	|视频的宽,单位 px	|
|fps				|number	|视频帧率						|
|bitrate		|number	|视频码率,单位 kbps|

**res.orientation参数说明**

|值							|说明									|
|:-							|:-										|
|up							|默认									|
|down						|180度旋转						|
|left						|逆时针旋转90度				|
|right					|顺时针旋转90度				|
|up-mirrored		|同up,但水平翻转			|
|down-mirrored	|同down,但水平翻转		|
|left-mirrored	|同left,但垂直翻转		|
|right-mirrored	|同right,但垂直翻转	|

### uni.compressVideo(OBJECT)

压缩视频接口。开发者可指定压缩质量 quality 进行压缩。当需要更精细的控制时,可指定 bitrate、fps、和 resolution,当 quality 传入时,这三个参数将被忽略。原视频的相关信息可通过 getVideoInfo 获取。
雪洛's avatar
雪洛 已提交
275 276 277 278 279

**平台差异说明**

|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
W
wanganxp 已提交
280 281 282
|x|x|2.11.0+|x|x|x|x|

App端有很多插件支持视频压缩,详见[插件市场](https://ext.dcloud.net.cn/search?q=%E8%A7%86%E9%A2%91%E5%8E%8B%E7%BC%A9)
雪洛's avatar
雪洛 已提交
283

W
wanganxp 已提交
284 285 286
压缩完毕后如需上传到cdn,可使用uniCloud.uploadFile API,uniCloud提供了免费cdn给开发者使用,详见[https://uniapp.dcloud.io/uniCloud/storage?id=uploadfile](https://uniapp.dcloud.io/uniCloud/storage?id=uploadfile)


雪洛's avatar
雪洛 已提交
287 288
**OBJECT 参数说明**

W
wanganxp 已提交
289 290 291 292 293 294 295 296 297
|属性				|类型			|默认值	|必填	|说明																									|
|:-:				|:-:			|:-:		|:-:	|:-:																									|
|src				|string		|				|是		|视频文件路径,可以是临时文件路径也可以是永久文件路径	|
|quality		|string		|				|是		|压缩质量																							|
|bitrate		|number		|				|是		|码率,单位 kbps																			|
|fps				|number		|				|是		|帧率																									|
|resolution	|number		|				|是		|相对于原视频的分辨率比例,取值范围(0, 1]							|
|success		|function	|				|否		|接口调用成功的回调函数																|
|fail				|function	|				|否		|接口调用失败的回调函数																|
雪洛's avatar
雪洛 已提交
298
|complete		|function	|				|否		|接口调用结束的回调函数(调用成功、失败都会执行)			|
W
wanganxp 已提交
299 300 301 302 303 304 305 306

**quality可取值**

|值			|说明	|
|:-			|:-		|
|low		|低		|
|medium	|中		|
|high		|高		|
雪洛's avatar
雪洛 已提交
307 308 309 310

**success 返回参数说明**

|参数名				|类型		|说明									|
W
wanganxp 已提交
311 312 313 314 315
|:-						|:-			|:-										|
|tempFilePath	|string	|压缩后的临时文件地址	|
|size					|string	|压缩后的大小,单位 kB|

### uni.openVideoEditor(OBJECT)
雪洛's avatar
雪洛 已提交
316

W
wanganxp 已提交
317
打开视频编辑器
雪洛's avatar
雪洛 已提交
318 319 320 321 322

**平台差异说明**

|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
W
wanganxp 已提交
323
|x|x|2.12.0+|x|x|x|x|
雪洛's avatar
雪洛 已提交
324 325 326

**OBJECT 参数说明**

W
wanganxp 已提交
327 328 329 330 331
|属性			|类型			|默认值	|必填	|说明																							|
|:-:			|:-:			|:-:		|:-:	|:-:																							|
|filePath	|string		|-			|是		|视频源的路径,只支持本地路径											|
|success	|function	|-			|否		|接口调用成功的回调函数														|
|fail			|function	|-			|否		|接口调用失败的回调函数														|
雪洛's avatar
雪洛 已提交
332 333 334 335 336
|complete	|function	|-			|否		|接口调用结束的回调函数(调用成功、失败都会执行)	|

**success 返回参数说明**

|参数名				|类型		|说明																					|
W
wanganxp 已提交
337 338 339 340 341
|:-						|:-			|:-																						|
|duration			|number	|剪辑后生成的视频文件的时长,单位毫秒(ms)		|
|size					|number	|剪辑后生成的视频文件大小,单位字节数(byte)	|
|tempFilePath	|string	|编辑后生成的视频文件的临时路径								|
|tempThumbPath|string	|编辑后生成的缩略图文件的临时路径							|