视频需要上传?推荐uni-cdn,帮你节省至少30%的 CDN 费用!详情

# uni.chooseVideo(OBJECT)

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:

  • sourceType 值在 H5 平台根据浏览器的不同而表现不同,一般不可限制仅使用相册,部分浏览器也无法限制是否使用相机。
  • app安卓端选择的视频最大只支持180MB,如需突破该限制请使用原生插件https://ext.dcloud.net.cn/search?q=%E6%96%87%E4%BB%B6%E9%80%89%E6%8B%A9
  • 文件的临时路径,在应用本次启动期间可以正常使用,如需持久保存,需在主动调用 uni.saveFile,在应用下次启动时才能访问得到。
  • camera is not available for some Android phones because the system ROM doesn't support it, which can be switched after opening the shooting interface.
  • The user authorization API can be used to determine whether the user authorizes the application the access to the photo album or camera https://uniapp.dcloud.io/api/other/authorize
  • App下如需进一步压缩视频大小,可以在插件市场搜索视频压缩插件
  • 如需上传到cdn,可使用uniCloud.uploadFile API,uniCloud提供了免费cdn给开发者使用,详见https://doc.dcloud.net.cn/uniCloud/storage?id=uploadfile
  • Most of the videos are selected for uploading. uni ui encapsulates a more complete uni-file-picker component. Files are selected and uploaded to uniCloud's free storage and cdn for one-stop integration. Highly recommended.
  • 部分浏览器中无法获取视频信息。
  • 微信小程序在2023年10月17日之后,使用API需要配置隐私协议

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;
				}
			});
		}
	}
}

# uni.chooseMedia(OBJECT)

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

  • 如需上传到cdn,可使用uniCloud.uploadFile API,uniCloud提供了免费cdn给开发者使用,详见https://doc.dcloud.net.cn/uniCloud/storage?id=uploadfile
  • Most of the selected files are for uploading, uni ui encapsulates a more complete [uni-file-picker component] (https://ext.dcloud.net.cn/plugin?id=4079), file selection, uploading to uniCloud is free One-stop integration in storage and cdn. Highly recommended.
  • 经开发者提醒,微信小程序ios真机可以选择的文件个数不能大于9,详见帖子https://ask.dcloud.net.cn/question/115561
  • 微信小程序在2023年10月17日之后,使用API需要配置隐私协议

# uni.saveVideoToPhotosAlbum(OBJECT)

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

  • 微信小程序在2023年10月17日之后,使用API需要配置隐私协议

# uni.getVideoInfo(OBJECT)

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.

# uni.compressVideo(OBJECT)

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

# uni.openVideoEditor(OBJECT)

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