HardwareVideoCodec高效的 Android 音視頻編碼庫
HardwareVideoCodec是一個高效的Android音視頻編碼庫,支持軟編和硬編。使用它你可以很容易的實現(xiàn)任何分辨率的視頻編碼,無需關心攝像頭預覽大小。一切都如此簡單。目前已迭代多個穩(wěn)定版本,歡迎查閱學習和使用,如有BUG或建議,歡迎提Issue。
一、簡介
HardwareVideoCodec是個高性能、易用的Android音視頻編碼開源庫,支持多款濾鏡,支持RTMP直播推流,以及軟編和硬編。硬編性能較好,在高通630的中端機子上實測1080p、30fps毫無問題。軟編性能差一點,同樣的機子,軟編只能達到720p、24fps。硬編性能較好,軟編兼容性較好,這個需要根據(jù)的業(yè)務需求進行選擇。
HardwareVideoCodec目前已經(jīng)迭代到了1.6.3版本,更新了新的美顏濾鏡,美顏更出色,更有40+濾鏡庫。支持RTMP推流,實測1080p、30fps局域網(wǎng)推流毫無性能壓力。以下是主要的特性:
支持
高性能的RTMP直播推流。支持在不重啟Camera的基礎上,
熱切換畫面分辨率。支持包括
美顏濾鏡在內的20多款濾鏡。支持視頻
軟編和硬編。支持
錄制視頻保存成mp4。使用
OpenGL進行畫面渲染,更少的CPU和內存占用,高通630的中端機子硬編并開啟RTMP推流實測僅12%的CPU占用。
運行截圖
二、使用
1.把以下代碼加入到Project的build.gradle。
buildscript {
ext.kotlin_version = '1.2.30'//Latest kotlin version
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
2.導入依賴,把以下代碼加入到Module的build.gradle。
dependencies {
implementation 'com.lmy.codec:hardwarevideocodec:1.6.3'
implementation 'com.lmy.codec:rtmp:1.6.3'//如果需要使用RTMP推流功能
}
3.在Activity中使用HardwareVideoCodec。
class MainActivity : AppCompatActivity() {
private lateinit var mRecorder: VideoRecorderImpl
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mTextureView = TextureView(this)
setContentView(mTextureView)
mRecorder = VideoRecorderImpl(this).apply {
reset()
setOutputUri("${Environment.getExternalStorageDirectory().absolutePath}/test.mp4")
//setOutputUri("rtmp://192.168.16.125:1935/live/livestream")//如果需要使用RTMP推流,把路徑改為RTMP推流地址即可
setOutputSize(720, 1280)//Default 720x1280
setFilter(NormalFilter::class.java)//Default NormalFilter
setPreviewDisplay(mTextureView)
}
mRecorder.prepare()
//For recording control
mTextureView.setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
if (mRecorder.prepared())
mRecorder.start()
}
MotionEvent.ACTION_UP -> {
if (mRecorder.started())
mRecorder.pause()
}
}
true
}
}
override fun onDestroy() {
super.onDestroy()
mRecorder.release()
}
}
運行并授予必要權限,不出意外的話,你已經(jīng)可以看到攝像頭畫面了。如果有什么問題,歡迎在評論區(qū)留言或者ISSUE,我會及時解答。
三、開源協(xié)議
HardwareVideoCodec is GPL 2.0.
