V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wlanjie888
V2EX  ›  Android

发布一个开源 Android 短视频 SDK, 满足 Android 音视频大部分需求 github: https://github.com/wlanjie/trinity

  •  1
     
  •   wlanjie888 · 2020-05-20 11:40:25 +08:00 · 5378 次点击
    这是一个创建于 1435 天前的主题,其中的信息可能已经有所发展或是发生改变。

    trinity

    icon~

    Android Arsenal Build Status Download platform License

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

    apk 下载

    演示

    QQ 交流群

    125218305
    

    联系我

    git commit 规范

    代码规范

    系统版本

    支持 Android 4.3 及以上版本

    开发环境

    • Android Studio 3.5
    • NDK r20
    • kotlin 1.3.41

    使用的开源库

    • fdk-aac
    • ffmpeg 3.4
    • libx264
    • xlogger
    • mnnkit

    功能

    视频拍摄
    功能说明 是否支持
    多段录制
    自定义时长
    自定义相机配置
    摄像头切换
    闪光灯
    焦距调节
    手动对焦
    静音
    美颜 x
    磨皮
    自定义分辨率及码率
    录制背景音
    录制变速
    软硬编码
    视频编辑
    多段编辑
    替换片段
    设置片段时间
    背景音乐
    硬解码
    软解码
    图片视频混合
    特效
    滤镜
    闪白
    两分屏
    三分屏
    四分屏
    六分屏
    九分屏
    模糊分屏
    高斯模糊
    灵魂出窍
    抖动
    毛刺
    70s
    倒计时
    爱心光斑
    人脸识别
    玫瑰眼妆
    王妃
    贴纸妆
    飘落小猪
    猫头

    人脸特效

    玫瑰眼妆

    特效调试

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

    brew install glfw
    

    然后使用 xcode 打开library/src/main/cpp/opengl.xcodeproj即可
    切换效果调用代码

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

    自动化测试

    • 自动化测试使用uiautomator2具体使用请参考文档 使用方式如下:
    cd trinity
    python trinity.py
    

    然后使用

    adb devices
    

    在终端输入设备名即可

    性能

    性能

    使用

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

    添加 jcenter 依赖

    dependencies {
        implementation 'com.github.wlanjie:trinity:0.2.8'
    }
    

    权限要求

    <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" />
    

    录制

    配置参数

    • 创建录制预览 view
    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)
    
    • 设置 zoom
    // 设置焦距缩放, 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 info 导出实体类
      * @param l 导出回调 包含成功 失败 和进度回调
      * @return Int ErrorCode.SUCCESS 为成功,其它为失败
      */
    // 创建实体类, 必须传入视频输出地址  
    val exportVideoInfo = VideoExportInfo("/sdcard/export.mp4")
    // 使用硬解码
    exportVideoInfo.mediaCodecDecode = true
    // 使用硬编码
    exportVideoInfo.mediaCodecEncode = true  
    // 视频宽
    exportVideoInfo.width = 544
    // 视频高
    exportVideoInfo.height = 960
    // 帧率
    exportVideoInfo.frameRate = 25
    // 视频码率 2M
    exportVideoInfo.videoBitRate = 2000
    // 采样率
    exportVideoInfo.sampleRate = 44100
    // 声道数
    exportVideoInfo.channelCount = 1
    // 音频码率 128K
    exportVideoInfo.audioBitRate = 128
    export.export(exportVideoInfo, 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.
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1424 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 17:26 · PVG 01:26 · LAX 10:26 · JFK 13:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.