网站首页 > 博客文章 正文
今天开始就讲解工具类的开发,AudioExtractor 音频的提取类,一个音视频包含了音频和视频,我们在拿到一个mp4文件过后,如果自己写一个播放器就需要解析里面的音频和视频。
这个工具类就是来解析音频里面的音频提取,需要用到的知识点有文件,文件异常IOException,FFmpeg 也是一个开源库,就是一个指令来提取视频里面的音频:
val cmd = arrayOf("-i", video!!.path, "-vn", "-ar", "44100", "-ac", "2", "-ab", "192", "-f", "mp3", outputLocation.path)
有了这个工具类就很方便的把视频里面的音频给提取出来了
class AudioExtractor private constructor(private val context: Context) {
private var video: File? = null
private var callback: FFMpegCallback? = null
private var outputPath = ""
private var outputFileName = ""
fun setFile(originalFiles: File): AudioExtractor {
this.video = originalFiles
return this
}
fun setCallback(callback: FFMpegCallback): AudioExtractor {
this.callback = callback
return this
}
fun setOutputPath(output: String): AudioExtractor {
this.outputPath = output
return this
}
fun setOutputFileName(output: String): AudioExtractor {
this.outputFileName = output
return this
}
fun extract() {
video?.let {
if (!video!!.exists()) {
callback!!.onFailure(IOException("File not exists"))
return
}
if (!video!!.canRead()) {
callback!!.onFailure(IOException("Can't read the file. Missing permission?"))
return
}
val outputLocation = Utils.getConvertedFile(outputPath, outputFileName)
//Create Audio File with 192Kbps
//Select .mp3 format
val cmd = arrayOf("-i", video!!.path, "-vn", "-ar", "44100", "-ac", "2", "-ab", "192", "-f", "mp3", outputLocation.path)
try {
FFmpeg.getInstance(context).execute(cmd, object : ExecuteBinaryResponseHandler() {
override fun onStart() {}
override fun onProgress(message: String?) {
callback!!.onProgress(message!!)
}
override fun onSuccess(message: String?) {
Utils.refreshGallery(outputLocation.path, context)
callback!!.onSuccess(outputLocation, OutputType.TYPE_AUDIO)
}
override fun onFailure(message: String?) {
if (outputLocation.exists()) {
outputLocation.delete()
}
callback!!.onFailure(IOException(message))
}
override fun onFinish() {
callback!!.onFinish()
}
})
} catch (e: Exception) {
callback!!.onFailure(e)
} catch (e2: FFmpegCommandAlreadyRunningException) {
callback!!.onNotAvailable(e2)
}
}
}
companion object {
val TAG = "AudioExtractor"
fun with(context: Context): AudioExtractor {
return AudioExtractor(context)
}
}
}
上面就完成了音频的提取了完成了。
我是一个北上广回回重庆的程序员坚持在写代码,记得点赞,关注,转发,谢谢了。
猜你喜欢
- 2024-09-29 直击灵魂一问:协程到底是怎么切换线程的?
- 2024-09-29 八月冲刺别毁在传统一问一答模式,这份面试准备助你事半功倍
- 2024-09-29 Kotlin遇见数据结构丨说说顺序存储的二叉树如何创建遍历
- 2024-09-29 Kotlin实战—使用Room封装本地数据层
- 2024-09-29 知了堂|MybatisPlus—kotlin代码生成
- 2024-09-29 竟然有5种微服务框架可以替代Spring Boot
- 2024-09-29 ViewBinding 与 Kotlin 委托双剑合璧
- 2024-09-29 Kotlin编写一个GIFDialog 在线音视频解析三
- 2024-09-29 Kotlin+SpringBoot+Redis+Lua实现限流访问控制详解
- 2024-09-29 字节谷歌Kotlin学习王炸!入门到精通+高级强化实战-源码
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)