多媒体科技FFmpegAndroid FFMPEG

Android 史诗级短视频开源SDK

2019-12-25  本文已影响0人  wanglinajie

trinity

开源地址

https://github.com/wlanjie/trinity

trinity是一个开源的拍摄和短视频处理工具,用kotlin和c++编写,实现了大部分短视频编辑软件热门功能。

apk下载

QQ交流群


125218305

代码规范

系统版本

支持Android 4.3及以上版本

开发环境

使用的开源库

功能

功能说明 是否支持
多段录制
自定义时长
自定义相机配置
摄像头切换
闪光灯
焦距调节
手动对焦
静音
美颜 x
磨皮 x
自定义分辨率及码率
录制背景音
录制变速
软硬编码
多段编辑
替换片段
设置片段时间
背景音乐
硬解码
滤镜
闪白
两分屏
三分屏
四分屏
六分屏
九分屏
模糊分屏
高斯模糊
灵魂出窍
抖动
毛刺
70s
缩放

特效调试

项目中使用xcode调试特效效果, 使用前需要安装glfw


brew install glfw

然后使用xcode打开library/src/main/cpp/opengl.xcodeproj即可

切换效果调用代码


image_process.OnAction("param/blurScreen", 0);

自动化测试

使用方式如下:


cd trinity

python trinity.py

然后使用


adb devices

在终端输入设备名即可

使用

<font color=red>注意: SDK中不做权限判断,使用时需要由调用方申请好权限, SDK中涉及到的时间均为毫秒</font>

权限要求


<uses-permission android:name="android.permission.CAMERA" />

<uses-permission android:name="android.permission.RECORD_AUDIO" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

录制

配置参数


val preview = findViewById<TrinityPreviewView>(R.id.preview)


mRecord = TrinityRecord(preview)


mRecord.release()

回调设置


mRecord.setOnRenderListener(this)


mRecord.setOnRecordingListener(this)


mRecord.setCameraCallback(this)

开启预览


mRecord.startPreview()


mRecord.stopPreview()


// 设置显示类型

// 包含裁剪显示, 原比例上下留黑显示

mRecord.setFrame(mFrame)

录制控制/管理


mRecord.switchCamera()


// 返回当前摄像头id

val facing = mRecord.getCameraFacing()


mRecord.flash(mFlash)


// 设置焦距缩放, 0-100 100为最大缩放

mRecord.setZoom(0)


// 设置相机曝光度, 100为最大曝光

mRecord.setExposureCompensation(0)


// 设置手动对焦, 参数为x和y

mRecord.focus(mPointF)


/**

* @param rotation 旋转角度包含 0 90 180 270

*/

mRecord.setRecordRotation(0)


mRecord.setMute(false)


/**

* @param speed 速度包含 0.25 0.5 1.0 2.0 4.0倍速

*/

mRecord.setSpeed(mSpeed)

开始录制


/**

* 开始录制一段视频

* @param path 录制的视频保存的地址

* @param width 录制视频的宽, SDK中会做16倍整数的运算, 可能最终输出视频的宽和设置进去的不一致

* @param height 录制视频的高, SDK中会做16倍整数的运算, 可能最终输出视频的宽和设置进去的不一致

* @param videoBitRate 视频输出的码率, 如果设置的是2000, 则为2M, 最终输出的和设置的可能有差别

* @param frameRate 视频输出的帧率

* @param useHardWareEncode 是否使用硬编码, 如果设置为true, 而硬编码不支持,则自动切换到软编码

* @param audioSampleRate 音频的采样率

* @param audioChannel 音频的声道数

* @param audioBitRate 音频的码率

* @param duration 需要录制多少时间

* @return Int ErrorCode.SUCCESS 为成功,其它为失败

* @throws InitRecorderFailException

*/

mRecord.startRecording("/sdcard/a.mp4",

                      720,

                      1280,

                      2000, // 2M码率

                      30,

                      false,

                      44100,

                      1, // 单声道

                      128, // 128K 码率

                      Int.MAX_VALUE)


mRecord.stopRecording()

视频编辑

初始化


mVideoEditor = TrinityCore.createEditor(this)


val surfaceView = findViewById<SurfaceView>(R.id.surface_view)

mVideoEditor.setSurfaceView(surfaceView)

导入视频


val clip = MediaClip(file.absolutePath)

mVideoEditor.insertClip(clip)


val clip = MediaClip(file.absolutePath)

mVideoEditor.insertClip(0, clip)


/**

* 根据下标删除一个片段

*/

mVideoEditor.removeClip(index)


val count = mVideoEditor.getClipsCount()


/**

* 如果片段不存在, 返回一个null

*/

val clip = mVideoEditor.getClip(index)


mVideoEditor.replaceClip(index, clip)


/**

* 返回所有片段的集合

*/

val clips = mVideoEditor.getVideoClips()


val duration = mVideoEditor.getVideoDuration()


val current = mVideoEditor.getCurrentPosition()


val timeRange = mVideoEditor.getClipTimeRange(index)


val index = mVideoEditor.getClipIndex(time)

背景音乐


/**

* @param config 背景音乐json内容

* 具体json内容如下:

* {

*    "path": "/sdcard/trinity.mp3",

*    "startTime": 0,

*    "endTime": 2000

* }

* json 参数解释:

* path: 音乐的本地地址

* startTime: 这个音乐的开始时间

* endTime: 这个音乐的结束时间 2000代表这个音乐只播放2秒钟

*/

val actionId = mVideoEditor.addMusic(config)


/**

* @param config 背景音乐json内容

* 具体json内容如下:

* {

*    "path": "/sdcard/trinity.mp3",

*    "startTime": 2000,

*    "endTime": 4000

* }

* json 参数解释:

* path: 音乐的本地地址

* startTime: 这个音乐的开始时间

* endTime: 这个音乐的结束时间 4000代表这个音乐从开始时间到结束时间播放2秒钟

*/

val actionId = mVideoEditor.addMusic(config)


/**

* 删除背景音乐

* @param actionId 必须为添加背景音乐时返回的actionId

*/

mVideoEditor.deleteMusic(actionId)

添加特效


/**

* 添加滤镜

* 如: content.json的绝对路径为 /sdcard/Android/com.trinity.sample/cache/filters/config.json

* 传入的路径只需要 /sdcard/Android/com.trinity.sample/cache/filters 即可

* 如果当前路径不包含config.json则添加失败

* 具体json内容如下:

* {

*  "type": 0,

*  "intensity": 1.0,

*  "lut": "lut_124/lut_124.png"

* }

*

* json 参数解释:

* type: 保留字段, 目前暂无作用

* intensity: 滤镜透明度, 0.0时和摄像头采集的无差别

* lut: 滤镜颜色表的地址, 必须为本地地址, 而且为相对路径

*      sdk内部会进行路径拼接

* @param configPath 滤镜config.json的父目录

* @return 返回当前滤镜的唯一id

*/

val actionId = mVideoEditor.addFilter(config)


/**

* 更新滤镜

* @param configPath config.json的路径, 目前对称addFilter说明

* @param startTime 滤镜的开始时间

* @param endTime 滤镜的结束时间

* @param actionId 需要更新哪个滤镜, 必须为addFilter返回的actionId

*/

mVideoEditor.updateFilter(config, 0, 2000, actionId)


/**

  * 删除滤镜

  * @param actionId 需要删除哪个滤镜, 必须为addFilter时返回的actionId

  */

mVideoEditor.deleteFilter(actionId) 


/**

* 添加特效

* 如: content.json的绝对路径为 /sdcard/Android/com.trinity.sample/cache/effects/config.json

* 传入的路径只需要 /sdcard/Android/com.trinity.sample/cache/effects 即可

* 如果当前路径不包含config.json则添加失败

* @param configPath 滤镜config.json的父目录

* @return 返回当前特效的唯一id

*/

val actionId = mVideoEditor.addAction(configPath)


/**

* 更新指定特效

* @param startTime 特效的开始时间

* @param endTime 特效的结束时间

* @param actionId 需要更新哪个特效, 必须为addAction返回的actionId

*/

mVideoEditor.updateAction(0, 2000, actionId)


/**

* 删除一个特效

* @param actionId 需要删除哪个特效, 必须为addAction返回的actionId

*/

mVideoEditor.deleteAction(actionId)

开始预览


/**

* @param repeat 是否循环播放

*/

mVideoEditor.play(repeat)


mVideoEditor.pause()


mVideoEditor.resume()


mVideoEditor.stop()

释放资源


mVideoEditor.destroy()

导出视频


val export = TrinityCore.createExport(this)


/**

  * 开始导出

  * @param path 录制的视频保存的地址

  * @param width 录制视频的宽, SDK中会做16倍整数的运算, 可能最终输出视频的宽和设置进去的不一致

  * @param height 录制视频的高, SDK中会做16倍整数的运算, 可能最终输出视频的宽和设置进去的不一致

  * @param videoBitRate 视频输出的码率, 如果设置的是2000, 则为2M, 最终输出的和设置的可能有差别

  * @param frameRate 视频输出的帧率

  * @param sampleRate 音频的采样率

  * @param channelCount 音频的声道数

  * @param audioBitRate 音频的码率

  * @param l 导出回调 包含成功 失败 和进度回调

  * @return Int ErrorCode.SUCCESS 为成功,其它为失败

  */

export.export("/sdcard/export.mp4",

              544,

              960,

              25,

              3000,

              44100,

              1,

              128,

              this)


export.cancel()


export.release()


Copyright 2019 Trinity, Inc.

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

上一篇 下一篇

猜你喜欢

热点阅读