更新记录
1.2.6(2024-10-08)
- 兼容hbx 4.27回调函数
1.2.5(2024-05-20)
- 修复任务进度
1.2.4(2024-04-08)
- 添加getRealPath,解决无法自动创建文件夹问题
平台兼容性
Vue2 | Vue3 |
---|---|
√ | √ |
App | 快应用 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节小程序 | QQ小程序 |
---|---|---|---|---|---|---|
HBuilderX 3.6.8,Android:7.0,iOS:13,HarmonyNext:不确定 | × | × | × | × | × | × |
钉钉小程序 | 快手小程序 | 飞书小程序 | 京东小程序 |
---|---|---|---|
× | × | × | × |
H5-Safari | Android Browser | 微信浏览器(Android) | QQ浏览器(Android) | Chrome | IE | Edge | Firefox | PC-Safari |
---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | × | × | × |
使用文档 (更多插件及问题,请:)
超级福利
说明
此插件为uts api插件,用于ffmpeg命令执行,兼容uni-app官方video,打包不冲突
接口
executeFFmpeg
执行FFmpeg命令
App-Android | App-iOS |
---|---|
√ | √ |
import {
executeFFmpeg
} from '@/uni_modules/sn-uts-ffmpeg'
const vpath = plus.io.convertLocalFileSystemURL('/static/ftest.mp4')
const timestamp = new Date().getTime()
const outFile = plus.io.convertLocalFileSystemURL("_doc/" + timestamp + ".mp4")
// 如果outFile包含子文件夹,需要先创建,ffmpeg并不会帮你创建子文件夹
const cmd = `-i "${vpath}" -vf crop=300:300:10:10 -preset ultrafast -strict -2 "${outFile}"`
executeFFmpeg(cmd, (res) => {
console.log("完成回调:" + JSON.stringify(res));
/**
* 返回字段说明:{"createTime":"2022-07-05 15:35:52","startTime":"2022-07-05 15:35:52","error":null,
* "endTime":"2022-07-05 15:36:02","sessionId":1,"duration":9691,"returnCode":0,"state":3,"command":""}
* sessionId:任务ID
* state:0(创建) 1(执行) 2(失败) 3(完成)
* returnCode: 0(成功) 255(取消) 其它(失败)
* command:执行的命令
* duration:任务执行时长
* createTime:任务创建时间
* startTime:任务开始时间
* endTime:任务结束时间
* */
}, (res) => {
console.log("统计信息回调:" + JSON.stringify(res));
/**
* 返回字段说明:{"videoQuality":0,"speed":4.732010117327661,"videoFps":115.547,"bitrate":723.1251261083256,"time":6041,"videoFrameNumber":147,"sessionId":2,"size":546089}
* videoQuality:视频质量
* speed:速度
* videoFps: 视频fps
* bitrate:比特率
* time:时长,单位ms
* videoFrameNumber:视频帧号
* sessionId:任务ID
* size:大小
**/
// percent:任务进度百分比
// percent=time/1000/duration,其中duration需要通过getMediaInformation获取
}, (res) => {
console.log("开始任务回调:" + JSON.stringify(res));
this.sessionId = res.sessionId;
// * sessionId:任务ID
}, (res) => {
console.log("log回调:" + JSON.stringify(res));
/**
* 返回字段说明:{"sessionId":1,"meessage":"ffmpeg version v4.5-dev-3393-g30322ebe3c","level":32}
* meessage:日志描述,执行错误时返回错误原因,如:-filter_complex: No such file or directory\n
* level:日志等级 -16(标准差) -8(不打印输出) 0(出了点问题,我们现在会崩溃) 8(出了点问题,无法恢复)
* 16(出了点问题,无法无损恢复) 24(有些东西看起来不正确) 32(标准信息) 40(详细资料) 48(仅对 libav* 开发人员有用的东西)
* 56(极其冗长的调试,对 libav* 开发很有用)
* */
})
executeFFprobe
执行FFprobe命令
App-Android | App-iOS |
---|---|
√ | √ |
import {
executeFFprobe
} from '@/uni_modules/sn-uts-ffmpeg'
executeFFprobe('cmd', e => {
// 完成回调
})
getMediaInformation
获取信息
App-Android | App-iOS |
---|---|
√ | √ |
- uni-app
import {
getMediaInformation
} from '@/uni_modules/sn-uts-ffmpeg'
getMediaInformation('/static/ftest.mp4', (res) => {
/**
* allProperties:媒体相关信息
* */
console.log("完成回调:" + JSON.stringify(res));
}, (res) => {
console.log("log回调:" + JSON.stringify(res));
}, 6000)
- uni-app x (uvue)
import {
getMediaInformation
} from '@/uni_modules/sn-uts-ffmpeg'
getMediaInformation('/static/ftest.mp4', (res:UTSJSONObject) => {
/**
* allProperties:媒体相关信息
* */
console.log("完成回调:", res);
const info = res['info'] as UTSJSONObject
const allProperties = info['allProperties'] as org.json.JSONObject
const props = JSON.parse(allProperties.toString()) as UTSJSONObject
const format = props['format'] as UTSJSONObject
const duration = format['duration'] // 视频长度,单位s
}, (res) => {
console.log("log回调:" + JSON.stringify(res));
}, 6000)
cancelAll
取消所有
App-Android | App-iOS |
---|---|
√ | √ |
import {
cancelAll
} from '@/uni_modules/sn-uts-ffmpeg'
cancelAll()
calcel
取消指定
App-Android | App-iOS |
---|---|
√ | √ |
import {
calcel
} from '@/uni_modules/sn-uts-ffmpeg'
const sessionId = 1
calcel(sessionId)
getSessions
获取sessions
App-Android | App-iOS |
---|---|
√ | √ |
import {
getSessions
} from '@/uni_modules/sn-uts-ffmpeg'
getSessions()
getRealPath
获取实际路径
App-Android | App-iOS |
---|---|
√ | √ |
getRealPath('/static/fmp3.mp3')
getRealPath('_doc/fmp3.mp3') // _doc将会自动创建
常见问题
-
MediaCodec configure failed, Generic error in an external library
MediaCodec编码,部分手机兼容问题,暂时无解;可以使用libx264替代
-
4k/8k压缩报错
4k、8k要设置分辨率(比如-s 1920x1080)才行,不能单纯的设置码率
-i <input path> -s 1920x1080 -b:v 8500k <output path>
-
uvue报错 Possible Unhandled Promise Rejection: [java.lang.NullPointerException] {cause: null, message: null}
原因:在非主线程触发了uvue渲染
解决办法:使用主线程执行uvue渲染触发代码,比如
getMediaInformation('/static/test1.mp3', (res:UTSJSONObject) => { UTSAndroid.getDispatcher("main").async(function(_){ const info = res['info'] as UTSJSONObject const allProperties = info['allProperties'] as org.json.JSONObject const props = JSON.parse(allProperties.toString()) as UTSJSONObject console.log('props', allProperties.toString()) const format = props['format'] as UTSJSONObject const streams = props['streams'] as UTSJSONObject[] const data = streams[0] console.log('streams', data, (data['sample_rate'] as string), (data['channels'] as Int)) console.log('info', format['duration']) },null); }, (res:UTSJSONObject) => { }, 4000)