English
视频需要上传?推荐
uni-cdn
,帮你节省至少30%的 CDN 费用!详情。
Shoot a video or select one from the mobile photo album, and call back the temporary file path of the video.
If you select and upload non-image and video files, please refer to: https://uniapp.dcloud.io/api/media/file.
Platform difference description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|
√ | √ | √ | √ | √ | √ | √ | √ | √ |
OBJECT parameter description
Parameter name | Type | Required | Instruction | Platform difference description |
---|---|---|---|---|
sourceType | Array<String> | No | album means to select video from album, camera means to use camera to shoot, defaulting to['album', 'camera'] | |
extension | Array<String> | 否 | 根据文件拓展名过滤,每一项都不能是空字符串。默认不过滤。 | H5(HBuilder X 2.9.9+) |
compressed | Boolean | 否 | 是否压缩所选的视频源文件,默认值为 true,需要压缩。 | 微信小程序、百度小程序、抖音小程序、飞书小程序、京东小程序、App(HBuilder X 3.2.7+) |
maxDuration | Number | No | The longest video recording time, in seconds. A maximum of 60 seconds is supported. | APP platform 1.9.7+ (iOS support, Android depends on whether the camera component of ROM implements this function, if this function is not implemented, this property is ignored.) WeChat applet, Baidu applet, Jingdong applet |
camera | String | No | 'front', 'back', default 'back' | APP, WeChat applet, Jingdong applet |
success | Function | No | The interface is successfully called, and the temporary file path of the video file is returned. See the description of return parameters for details. | |
fail | Function | No | Callback function for failed interface calling | |
complete | Function | No | Callback function for closed interface calling (available both for successful and failed calling) |
success return parameter description
Parameters | Type | Description | Platform Difference |
---|---|---|---|
tempFilePath | String | Select temporary file path for video | |
tempFile | File | Selected video file | Only supported by H5 (2.6.15+) |
duration | Number | The duration of the selected video, in s | APP 2.1.0+, H5, WeChat applet, Jingdong applet |
size | Number | Data size of the selected video | APP 2.1.0+, H5, WeChat applet, Jingdong applet |
height | Number | Returns the height of the selected video | APP 2.1.0+, H5, WeChat applet, Jingdong applet |
width | Number | Returns the width of the selected video | APP 2.1.0+, H5, WeChat applet, Jingdong applet |
name | String | File names with extensions | Supported on H5 only |
Notice:
Example
<template>
<view>
<text>hello</text>
<button @tap="test">click me</button>
<video :src="src"></video>
</view>
</template>
export default {
data() {
return {
src: ''
}
},
methods: {
test: function () {
var self = this;
uni.chooseVideo({
sourceType: ['camera', 'album'],
success: function (res) {
self.src = res.tempFilePath;
}
});
}
}
}
Take a photo or select a picture or video from your phone gallery.
If you select and upload non-image and video files, please refer to: https://uniapp.dcloud.io/api/media/file.
Platform difference description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|
x | x | 2.10.0+ | x | x | √ | x | x | √ |
OBJECT parameter description
Parameter Name | Type | Default Value | Required | Description |
---|---|---|---|---|
count | Number | 9 (Note: iOS cannot be greater than 9) | No | Maximum number of files that can be selected |
mediaType | Array.<string> | ['image', 'video'] | No | FileType |
sourceType | Array.<string> | ['album', 'camera'] | No | Source for image and video selection |
maxDuration | Number | 10 | No | The longest video recording time, in seconds. The time range is between 3s and 30s |
sizeType | Array.<string> | ['original', 'compressed'] | No | Valid only when mediaType is image, whether to compress the selected file |
camera | String | 'back' | No | Only valid when sourceType is camera, using front or back camera |
success | function | No | Callback function for successful interface call | |
fail | function | No | Callback function for interface call failure | |
complete | function | No | The callback function for the end of the interface call (the call will be executed if the call succeeds or fails) |
OBJECT.mediaType legal value
value | description |
---|---|
image | Can only take pictures or select pictures from the album |
video | Only shoot video or select video from album |
mix | You can select pictures and videos at the same time |
OBJECT.sourceType legal value
value | description |
---|---|
album | Select from Album |
camera | Shooting with the camera |
OBJECT.camera legal value
value | description |
---|---|
back | Using the rear camera |
front | Using the front camera |
success return parameter description
Parameter name | Type | Instruction |
---|---|---|
tempFiles | Array.<string> | List of local temporary files |
type | String | File type, valid values are image, video, mix |
structure of res.tempFiles
parameter name | type | description |
---|---|---|
tempFilePath | String | Local temp file path (local path) |
size | Number | The size of the local temporary file, unit B |
duration | Number | The duration of the video |
height | Number | The height of the video |
width | Number | The width of the video |
thumbTempFilePath | String | Video thumbnail temp file path |
fileType | String | FileType |
fileType legal value
value | description |
---|---|
image | Image |
video | video |
Example
uni.chooseMedia({
count: 9,
mediaType: ['image','video'],
sourceType: ['album', 'camera'],
maxDuration: 30,
camera: 'back',
success(res) {
console.log(res.tempFiles)
}
})
Tips
Save the video to the system album.
Platform difference description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|
√ | x | √ | √ | √ | √ | √ | √ | x |
OBJECT parameter description
Parameter name | Type | Required | Instruction |
---|---|---|---|
filePath | String | Yes | The video file path can be a temporary file path or a permanent file path. |
success | Function | No | Callback function for successful interface calling |
fail | Function | No | Callback function for failed interface calling |
complete | Function | No | Callback function for closed interface calling (available both for successful and failed calling) |
success return parameter description
Parameter name | Type | Instruction |
---|---|---|
errMsg | String | Call result |
Notice
Example
<template>
<view>
<text>hello</text>
<button @tap="test">click me</button>
<video :src="src"></video>
</view>
</template>
export default {
data() {
return {
src: ''
}
},
methods: {
test: function () {
var self = this;
uni.chooseVideo({
sourceType: ['camera'],
success: function (res) {
self.src = res.tempFilePath;
uni.saveVideoToPhotosAlbum({
filePath: res.tempFilePath,
success: function () {
console.log('save success');
}
});
}
});
}
}
}
Tips
Get video details
Platform difference description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|
3.1.10+ | 3.1.10+ | 2.11.0+ | x | x | x | x | √ | x |
OBJECT parameter description
property | type | default | required | description |
---|---|---|---|---|
src | string | - | Yes | Video file path, can be a temporary file path or a permanent file path (network address is not supported) |
success | function | - | No | Callback function for successful interface call |
fail | function | - | No | Callback function for interface call failure |
complete | function | - | No | The callback function for the end of the interface call (the call will be executed if the call succeeds or fails) |
success return parameter description
Parameter Name | Type | Description | Platform Difference Description |
---|---|---|---|
orientation | string | Screen orientation | WeChat applet, App (3.1.14+) |
type | string | Video format | WeChat applet, App (3.1.14+) |
duration | number | Video length | WeChat applet, App (3.1.10+), H5 |
size | number | Video size, unit kB | WeChat applet, App (3.1.10+), H5 |
height | number | The length of the video, in px | WeChat applet, App (3.1.10+), H5 |
width | number | The width of the video, in px | WeChat applet, App (3.1.10+), H5 |
fps | number | Video frame rate | WeChat applet, App (3.1.14+) |
bitrate | number | Video bit rate, unit kbps | WeChat applet, App (3.1.14+) |
res.orientation parameter description
Value | Instruction |
---|---|
up | Default |
down | 180° rotation |
left | Rotate 90° counterclockwise |
right | Rotate 90° clockwise |
up-mirrored | Same as up, but flipped horizontally |
down-mirrored | Same as down, but flipped horizontally. |
left-mirrored | Same as left, but flipped vertically. |
right-mirrored | Same as right, but flipped vertically. |
Compressed video interface. Developers can specify the compression quality (quality) for compression. When finer control is needed, bitrate, fps and resolution can be specified, which will be ignored when quality is passed in. The related information of the original video can be obtained through getVideoInfo.
Platform difference description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|
3.1.10+ | x | 2.11.0+ | x | x | x | x | x | x |
Many plug-ins are available on the App side that support video compression. See.Plug-in market for details
压缩完毕后如需上传到cdn,可使用uniCloud.uploadFile API,uniCloud提供了免费cdn给开发者使用,详见https://doc.dcloud.net.cn/uniCloud/storage?id=uploadfile
OBJECT parameter description
Attribute | Type | Defaults | Required | Instruction |
---|---|---|---|---|
src | string | Yes | The video file path can be a temporary file path or a permanent file path. | |
quality | string | Yes | Compression quality | |
bitrate | number | Yes | bit rate, in kbps | |
fps | number | Yes | Frame rate | |
resolution | number | Yes | Resolution ratio to the original video, with the value range of (0, 1] | |
success | function | No | Callback function for successful interface calling | |
fail | function | No | Callback function for failed interface calling | |
complete | function | No | Callback function for closed interface calling (available both for successful and failed calling) |
Optional values of quality
Value | Instruction |
---|---|
low | Low |
medium | Middle |
high | High |
success return parameter description
Parameter name | Type | Instruction |
---|---|---|
tempFilePath | string | Compressed temporary file address |
size | string | Compressed size, in kB |
Open video editor
Platform difference description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|
x | x | 2.12.0+ | x | x | x | x | x | x |
OBJECT parameter description
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | string | - | 是 | 视频源的路径,只支持本地路径 |
minDuration | string | - | 是 | 视频裁剪的最小长度(2.16.1) |
maxDuration | string | - | 是 | 视频裁剪的最大长度 (2.16.1) |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | No | The callback function for the end of the interface call (the call will be executed if the call succeeds or fails) |
success return parameter description
parameter name | type | description |
---|---|---|
duration | number | The duration of the video file generated after editing, in milliseconds (ms) |
size | number | The size of the video file generated after editing, in bytes (byte) |
tempFilePath | string | Temporary path of the edited video file |
tempThumbPath | string | Temporary path to the thumbnail file generated after editing |