├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── markdown-navigator-enh.xml ├── markdown-navigator.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── ffmpeg_sign.jks ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── imgs │ ├── cmd.jpg │ ├── ffmpeg_version.jpg │ ├── ic_picInPic.jpg │ └── multiple_video.jpg ├── release │ └── app-release.apk └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── mobile │ │ └── ffmpeg │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── mobile │ │ │ └── ffmpeg │ │ │ ├── FFmpegUtil.java │ │ │ ├── FileSizeUtil.java │ │ │ └── MainActivity.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── raw │ │ └── video_demo.mp4 │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── mobile │ └── ffmpeg │ └── ExampleUnitTest.kt ├── build.gradle ├── ffmpeg ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── libs │ ├── arm64-v8a │ │ ├── libavcodec.so │ │ ├── libavdevice.so │ │ ├── libavfilter.so │ │ ├── libavformat.so │ │ ├── libavutil.so │ │ ├── libc++_shared.so │ │ ├── libcpufeatures.so │ │ ├── libmobileffmpeg.so │ │ ├── libmobileffmpeg_abidetect.so │ │ ├── libswresample.so │ │ └── libswscale.so │ └── armeabi-v7a │ │ ├── libavcodec.so │ │ ├── libavcodec_neon.so │ │ ├── libavdevice.so │ │ ├── libavfilter.so │ │ ├── libavfilter_neon.so │ │ ├── libavformat.so │ │ ├── libavutil.so │ │ ├── libc++_shared.so │ │ ├── libcpufeatures.so │ │ ├── libmobileffmpeg.so │ │ ├── libmobileffmpeg_abidetect.so │ │ ├── libmobileffmpeg_armv7a_neon.so │ │ ├── libswresample.so │ │ ├── libswscale.so │ │ └── libswscale_neon.so └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── mobile │ │ └── ffmpeg │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ ├── arthenica │ │ │ └── mobileffmpeg │ │ │ │ ├── AbiDetect.java │ │ │ │ └── Config.java │ │ │ └── mobile │ │ │ └── ffmpeg │ │ │ ├── Abi.java │ │ │ ├── CameraSupport.java │ │ │ ├── FFmpeg.java │ │ │ ├── Level.java │ │ │ ├── LogCallback.java │ │ │ ├── LogMessage.java │ │ │ ├── Packages.java │ │ │ ├── Statistics.java │ │ │ ├── StatisticsCallback.java │ │ │ └── util │ │ │ ├── FFmpegAsyncUtils.java │ │ │ ├── FFmpegAsyncUtils2.java │ │ │ └── FFmpegExecuteCallback.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── mobile │ └── ffmpeg │ └── ExampleUnitTest.kt ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/ 5 | /app/release/output.json 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | .cxx 11 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 9 | 10 | 11 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | xmlns:android 20 | 21 | ^$ 22 | 23 | 24 | 25 |
26 |
27 | 28 | 29 | 30 | xmlns:.* 31 | 32 | ^$ 33 | 34 | 35 | BY_NAME 36 | 37 |
38 |
39 | 40 | 41 | 42 | .*:id 43 | 44 | http://schemas.android.com/apk/res/android 45 | 46 | 47 | 48 |
49 |
50 | 51 | 52 | 53 | .*:name 54 | 55 | http://schemas.android.com/apk/res/android 56 | 57 | 58 | 59 |
60 |
61 | 62 | 63 | 64 | name 65 | 66 | ^$ 67 | 68 | 69 | 70 |
71 |
72 | 73 | 74 | 75 | style 76 | 77 | ^$ 78 | 79 | 80 | 81 |
82 |
83 | 84 | 85 | 86 | .* 87 | 88 | ^$ 89 | 90 | 91 | BY_NAME 92 | 93 |
94 |
95 | 96 | 97 | 98 | .* 99 | 100 | http://schemas.android.com/apk/res/android 101 | 102 | 103 | ANDROID_ATTRIBUTE_ORDER 104 | 105 |
106 |
107 | 108 | 109 | 110 | .* 111 | 112 | .* 113 | 114 | 115 | BY_NAME 116 | 117 |
118 |
119 |
120 |
121 | 122 | 124 |
125 |
-------------------------------------------------------------------------------- /.idea/markdown-navigator-enh.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 29 | 30 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 yangfeng1994 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FFmpeg-Android 2 | [![](https://jitpack.io/v/yangfeng1994/FFmpeg-Android.svg)](https://jitpack.io/#yangfeng1994/FFmpeg-Android) 3 | [![](https://img.shields.io/badge/FFmpeg-4.0-yellow.svg)](http://ffmpeg.org/releases/ffmpeg-4.0.tar.bz2) 4 | 5 | 6 | FFmpeg 在 Android中使用. 7 | 在您的Android项目中轻松执行FFmpeg命令。 8 | 9 | ## 关于 10 | 引入本项目使您的项目尽可能的小,功能尽可能的完善,已使用本项目上线的项目有 [影音坊](http://server.m.pp.cn/download/apk?appId=8061477&custom=0&ch_src=pp_dev&ch=default)。 11 | 12 | 项目支持 androidx 或者 support 13 | 14 | 本项目没有引入任何第三方库,不会对您的项目有任何的代码侵入,可兼容最低api版本为15 15 | 16 | # 项目截图 17 | 18 | 图-1:ffmpeg_version 19 | 图-2:cmd 20 | 图-3:cmd 21 | 图-3:cmd 22 | 23 | ### 体系结构 24 | FFmpeg-Android运行在以下架构上: 25 | - armeabi 26 | - armeabi-v7a 27 | - armv7-neon 28 | - arm64-v8a 29 | 30 | ### 特性 31 | 32 | - 使用最新的 git-2020-01-25-fd11dd500 Copyright (c) 2000-2020 the FFmpeg developers 33 | - 多线程 34 | 35 | ## 使用 36 | 37 | 在项目的 build.gradle 中添加 38 | 39 | 40 | ``` 41 | allprojects{ 42 | 43 | repositories { 44 | ... 45 | maven { url 'https://jitpack.io' } 46 | } 47 | 48 | } 49 | 50 | ``` 51 | 52 | #### 53 | 54 | 1.重新编译了ffmpeg 55 | 56 | 2.支持更多的命令,执行速度更快 57 | 58 | 3.支持在安卓10(aip29)上使用 59 | 60 | 4.支持进度回调,进度为执行文件的进度,如果想计算进度,拿(当前返回进度)除以(文件进度) 61 | 62 | app 的 build.gradle 下添加 63 | 64 | ``` 65 | dependencies { 66 | implementation 'com.github.yangfeng1994:FFmpeg-Android:v2.0.1' 67 | } 68 | ``` 69 | 70 | 71 | 设置支持的so库版本 72 | 73 | ``` 74 | android{ 75 | defaultConfig{ 76 | ndk { 77 | abiFilters 'armeabi-v7a', 'arm64-v8a' //过滤的so库版本 78 | } 79 | } 80 | } 81 | ``` 82 | 83 | #### 友情提示 84 | 85 | - 申请权限(对本地文件处理时,建议您务必申请权限,不然无法对音视频进行操作) 86 | 87 | - 对输入的文件是否存在,进行判断(如您想要输出的文件 已经在手机中存在,将无法对输出文件) 88 | 89 | ### 运行 FFmpeg 90 | 91 | 92 | ##### java 93 | 94 | ``` 95 | // 要执行“ffmpeg -version”命令 96 | 97 | String[] cmd = new String[]{"-version"}; 98 | 99 | FFmpegAsyncUtils asyncTask =new FFmpegAsyncUtils() 100 | 101 | asyncTask.setCallback(new FFmpegExecuteCallback() { 102 | 103 | @Override 104 | public void onFFmpegStart() {} 105 | 106 | @Override 107 | public void onFFmpegSucceed(@Nullable String executeOutput) { 108 | 109 | } 110 | 111 | @Override 112 | public void onFFmpegFailed(@Nullable String executeOutput) { 113 | 114 | } 115 | 116 | @Override 117 | public void onFFmpegProgress(@Nullable Integer progress) { 118 | fload mprogress = progress/执行视频文件或语音文件时长 119 | } 120 | 121 | @Override 122 | public void onFFmpegCancel() { 123 | 124 | } 125 | }) 126 | 127 | asyncTask.execute(cmd); 128 | 129 | 注意,传入的参数是一个lsit 130 | 131 | 如果想要传入string,命令行拼接的时候,需要用空格隔开 使用FFmpegAsyncUtils2就行 132 | 133 | ``` 134 | 135 | ##### kotlin 136 | 137 | 138 | ``` 139 | // 要执行“ffmpeg -version”命令,只需传递“ arrayOf(-version) ”即可 140 | 141 | val asyncTask = FFmpegAsyncUtils() 142 | 143 | asyncTask.setCallback(object :FFmpegExecuteCallback{ 144 | 145 | override fun onFFmpegStart() { 146 | 147 | } 148 | 149 | override fun onFFmpegProgress(progress: Int?) { 150 | 151 | //注意kotlin的除法,建议转为float后,再进行除以 152 | 153 | val mprogress = progress?.div(执行视频文件或语音文件时长) 154 | 155 | } 156 | 157 | override fun onFFmpegCancel() { 158 | 159 | } 160 | 161 | override fun onFFmpegSucceed(executeOutput: String?) { 162 | 163 | } 164 | 165 | override fun onFFmpegFailed(executeOutput: String?) { 166 | 167 | } 168 | 169 | }) 170 | 171 | asyncTask.execute(cmd) 172 | 173 | 注意,传入的参数是一个lsit 174 | 175 | 如果想要传入string,命令行拼接的时候,需要用空格隔开 使用FFmpegAsyncUtils2就行 176 | ``` 177 | 178 | 179 | #### 注意: 180 | 181 | - 所有命令行都不需要以 "ffmpeg"开头,直接命令行就行。 182 | 183 | - 本项目使用的是AsyncTask,需要注意,每个子线程AsyncTask只能执行一次命令,取消后 184 | 应重新new 一个AsyncTask 对象。 185 | 186 | - 混淆在model里面已经添加,无需再次添加 187 | 188 | ### 停止(或退出)FFmpeg进程 189 | 190 | - 如果你想停止运行中的ffmpeg, 只需在调用' asyncTask.onCancel() ' 191 | 192 | #### FFmpegExecuteCallback 接口中方法的介绍 193 | 194 | - onFFmpegStart() 开始执行 195 | 196 | - onFFmpegProgress(progress: Int?) 进度 参数为执行音视频文件的所在的毫秒值 197 | 198 | - onFFmpegCancel() 取消执行 199 | 200 | - onFFmpegSucceed(executeOutput: String?) 执行成功 参数为ffmpeg的执行结果信息 201 | 202 | - onFFmpegFailed(executeOutput: String?) 执行失败 参数为返回为失败原因 203 | 204 | #### 历史版本 205 | 206 | - v1.1.1 优化了代码的逻辑,兼容了低版本的手机,使项目同时兼容androidx与support 207 | 208 | ``` 209 | implementation 'com.github.yangfeng1994:FFmpeg-Android:v1.1.1' 210 | 211 | ``` 212 | 213 | - v1.0.1 移除x86 so包,优化项目大小 214 | 215 | ``` 216 | implementation 'com.github.yangfeng1994:FFmpeg-Android:v1.0.1' 217 | 218 | ``` 219 | 220 | - v1.0.0 新建项目 第一版本 221 | 222 | 223 | 224 | ``` 225 | implementation 'com.github.yangfeng1994:FFmpeg-Android:v1.0.0' 226 | ``` 227 | 228 | # 体验demo 229 | 230 | - [点击下载](https://raw.githubusercontent.com/yangfeng1994/FFmpeg-Android/master/app/release/app-release.apk) 231 | 232 | ## Licensing 233 | - [Library license](https://github.com/yangfeng1994/FFmpeg-Android/blob/master/LICENSE) 234 | - [FFmpeg license](https://www.ffmpeg.org/legal.html) 235 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 29 9 | buildToolsVersion "29.0.3" 10 | defaultConfig { 11 | applicationId "com.mobile.ffmpeg.test" 12 | minSdkVersion 16 13 | targetSdkVersion 29 14 | versionCode 1 15 | versionName "1.0" 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | ndk { 18 | abiFilters 'armeabi-v7a' 19 | } 20 | } 21 | signingConfigs { 22 | config { 23 | keyAlias 'ffmpeg' 24 | keyPassword '123456' 25 | storeFile file('ffmpeg_sign.jks') 26 | storePassword '123456' 27 | } 28 | } 29 | buildTypes { 30 | release { 31 | minifyEnabled true 32 | signingConfig signingConfigs.config 33 | } 34 | debug { 35 | minifyEnabled false 36 | signingConfig signingConfigs.config 37 | } 38 | } 39 | compileOptions { 40 | sourceCompatibility JavaVersion.VERSION_1_8 41 | targetCompatibility JavaVersion.VERSION_1_8 42 | } 43 | } 44 | 45 | dependencies { 46 | implementation fileTree(dir: 'libs', include: ['*.jar']) 47 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 48 | implementation 'androidx.appcompat:appcompat:1.1.0' 49 | implementation 'androidx.core:core-ktx:1.2.0' 50 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 51 | testImplementation 'junit:junit:4.12' 52 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 53 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 54 | // implementation project(path: ':ffmpeg') 55 | implementation 'com.github.yangfeng1994:FFmpeg-Android:v2.0.1' 56 | implementation 'com.google.android.exoplayer:exoplayer-core:2.11.0' 57 | implementation 'com.google.android.exoplayer:exoplayer-ui:2.11.0' 58 | } 59 | -------------------------------------------------------------------------------- /app/ffmpeg_sign.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfeng1994/FFmpeg-Android/bc9aa26e9ab21c7349c81e24c5a60c9b5913b579/app/ffmpeg_sign.jks -------------------------------------------------------------------------------- /app/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfeng1994/FFmpeg-Android/bc9aa26e9ab21c7349c81e24c5a60c9b5913b579/app/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun May 31 18:20:30 CST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip 7 | -------------------------------------------------------------------------------- /app/imgs/cmd.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfeng1994/FFmpeg-Android/bc9aa26e9ab21c7349c81e24c5a60c9b5913b579/app/imgs/cmd.jpg -------------------------------------------------------------------------------- /app/imgs/ffmpeg_version.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfeng1994/FFmpeg-Android/bc9aa26e9ab21c7349c81e24c5a60c9b5913b579/app/imgs/ffmpeg_version.jpg -------------------------------------------------------------------------------- /app/imgs/ic_picInPic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfeng1994/FFmpeg-Android/bc9aa26e9ab21c7349c81e24c5a60c9b5913b579/app/imgs/ic_picInPic.jpg -------------------------------------------------------------------------------- /app/imgs/multiple_video.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfeng1994/FFmpeg-Android/bc9aa26e9ab21c7349c81e24c5a60c9b5913b579/app/imgs/multiple_video.jpg -------------------------------------------------------------------------------- /app/release/app-release.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfeng1994/FFmpeg-Android/bc9aa26e9ab21c7349c81e24c5a60c9b5913b579/app/release/app-release.apk -------------------------------------------------------------------------------- /app/src/androidTest/java/com/mobile/ffmpeg/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.mobile.ffmpeg 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.mobile.ffmpeg", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/mobile/ffmpeg/FFmpegUtil.java: -------------------------------------------------------------------------------- 1 | package com.mobile.ffmpeg; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.util.Log; 5 | 6 | import java.util.Locale; 7 | 8 | /** 9 | * ffmpeg工具:拼接命令行处理音视频 10 | */ 11 | 12 | public class FFmpegUtil { 13 | 14 | //水平拼接 15 | public final static int LAYOUT_HORIZONTAL = 1; 16 | //垂直拼接 17 | public final static int LAYOUT_VERTICAL = 2; 18 | 19 | /** 20 | * 使用ffmpeg命令行进行音频转码 21 | * 22 | * @param srcFile 源文件 23 | * @param targetFile 目标文件(后缀指定转码格式) 24 | * @return 转码后的文件 25 | */ 26 | public static String[] transformAudio(String srcFile, String targetFile) { 27 | String transformAudioCmd = "-i %s %s"; 28 | transformAudioCmd = String.format(transformAudioCmd, srcFile, targetFile); 29 | return transformAudioCmd.split(" ");//以空格分割为字符串数组 30 | } 31 | 32 | /** 33 | * 使用ffmpeg命令行进行音频剪切 34 | * 35 | * @param srcFile 源文件 36 | * @param startTime 剪切的开始时间(单位为秒) 37 | * @param duration 剪切时长(单位为秒) 38 | * @param targetFile 目标文件 39 | * @return 剪切后的文件 40 | */ 41 | @SuppressLint("DefaultLocale") 42 | public static String[] cutAudio(String srcFile, int startTime, int duration, String targetFile) { 43 | String cutAudioCmd = "-i %s -ss %d -t %d %s"; 44 | cutAudioCmd = String.format(cutAudioCmd, srcFile, startTime, duration, targetFile); 45 | return cutAudioCmd.split(" ");//以空格分割为字符串数组 46 | } 47 | 48 | /** 49 | * 使用ffmpeg命令行进行音频合并 50 | * 51 | * @param srcFile 源文件 52 | * @param appendFile 待追加的文件 53 | * @param targetFile 目标文件 54 | * @return 合并后的文件 55 | */ 56 | public static String[] concatAudio(String srcFile, String appendFile, String targetFile) { 57 | String concatAudioCmd = "-i concat:%s|%s -acodec copy %s"; 58 | concatAudioCmd = String.format(concatAudioCmd, srcFile, appendFile, targetFile); 59 | return concatAudioCmd.split(" ");//以空格分割为字符串数组 60 | } 61 | 62 | /** 63 | * 使用ffmpeg命令行进行音频混合 64 | * 65 | * @param srcFile 源文件 66 | * @param mixFile 待混合文件 67 | * @param targetFile 目标文件 68 | * @return 混合后的文件 69 | */ 70 | public static String[] mixAudio(String srcFile, String mixFile, String targetFile) { 71 | String mixAudioCmd = "-i %s -i %s -filter_complex amix=inputs=2:duration=first -strict -2 %s"; 72 | mixAudioCmd = String.format(mixAudioCmd, srcFile, mixFile, targetFile); 73 | return mixAudioCmd.split(" ");//以空格分割为字符串数组 74 | } 75 | //混音公式:value = sample1 + sample2 - (sample1 * sample2 / (pow(2, 16-1) - 1)) 76 | 77 | 78 | /** 79 | * 使用ffmpeg命令行进行音视频合成 80 | * 81 | * @param videoFile 视频文件 82 | * @param audioFile 音频文件 83 | * @param duration 视频时长 84 | * @param muxFile 目标文件 85 | * @return 合成后的命令行 86 | */ 87 | @SuppressLint("DefaultLocale") 88 | public static String[] mediaMux(String videoFile, String audioFile, int duration, String muxFile) { 89 | //-t:时长 如果忽略音视频时长,则把"-t %d"去掉 90 | String mixAudioCmd = "-i %s -i %s -t %d %s"; 91 | mixAudioCmd = String.format(mixAudioCmd, videoFile, audioFile, duration, muxFile); 92 | return mixAudioCmd.split(" ");//以空格分割为字符串数组 93 | } 94 | 95 | /** 96 | * 使用ffmpeg命令行进行抽取音频 97 | * 98 | * @param srcFile 原文件 99 | * @param targetFile 目标文件 100 | * @return 需要执行命令行 101 | */ 102 | public static String[] extractAudio(String srcFile, String targetFile) { 103 | //-vn:video not 104 | String mixAudioCmd = "-i %s -acodec copy -vn %s"; 105 | mixAudioCmd = String.format(mixAudioCmd, srcFile, targetFile); 106 | return mixAudioCmd.split(" ");//以空格分割为字符串数组 107 | } 108 | 109 | /** 110 | * 使用ffmpeg命令行进行抽取视频 111 | * 112 | * @param srcFile 原文件 113 | * @param targetFile 目标文件 114 | * @return 需要执行命令行 115 | */ 116 | public static String[] extractVideo(String srcFile, String targetFile) { 117 | //-an audio not 118 | String mixAudioCmd = "-i %s -vcodec copy -an %s"; 119 | mixAudioCmd = String.format(mixAudioCmd, srcFile, targetFile); 120 | return mixAudioCmd.split(" ");//以空格分割为字符串数组 121 | } 122 | 123 | 124 | /** 125 | * 使用ffmpeg命令行进行视频转码 126 | * 127 | * @param srcFile 源文件 128 | * @param targetFile 目标文件(后缀指定转码格式) 129 | * @return 需要执行命令行 130 | */ 131 | public static String[] transformVideo(String srcFile, String targetFile) { 132 | //指定目标视频的帧率、码率、分辨率 133 | // String transformVideoCmd = "-i %s -r 25 -b 200 -s 1080x720 %s"; 134 | String transformVideoCmd = "-i %s -vcodec copy -acodec copy %s"; 135 | transformVideoCmd = String.format(transformVideoCmd, srcFile, targetFile); 136 | return transformVideoCmd.split(" ");//以空格分割为字符串数组 137 | } 138 | 139 | /** 140 | * 使用ffmpeg命令行进行视频剪切 141 | * 142 | * @param srcFile 源文件 143 | * @param startTime 剪切的开始时间(单位为秒) 144 | * @param endTime 结束时间 145 | * @param targetFile 目标文件 146 | * @return 返回的命令行 147 | */ 148 | @SuppressLint("DefaultLocale") 149 | public static String[] cutVideo(String srcFile, String startTime, String endTime, String targetFile) { 150 | String cutVideoCmd = "-ss %s -t %s -i %s -c:v libx264 -c:a aac -strict experimental -b:a 98k %s"; 151 | cutVideoCmd = String.format(cutVideoCmd, startTime, endTime, srcFile, targetFile); 152 | return cutVideoCmd.split(" ");//以空格分割为字符串数组 153 | } 154 | 155 | /** 156 | * 使用ffmpeg命令行进行视频截图 157 | * 158 | * @param srcFile 源文件 159 | * @param size 图片尺寸大小 160 | * @param targetFile 目标文件 161 | * @return 需要执行命令行 162 | */ 163 | public static String[] screenShot(String srcFile, String size, String targetFile) { 164 | String screenShotCmd = "-i %s -f image2 -t 0.001 -s %s %s"; 165 | screenShotCmd = String.format(screenShotCmd, srcFile, size, targetFile); 166 | return screenShotCmd.split(" ");//以空格分割为字符串数组 167 | } 168 | 169 | /** 170 | * 使用ffmpeg命令行给视频添加水印 171 | * 172 | * @param srcFile 源文件 173 | * @param waterMark 水印文件路径 174 | * @param targetFile 目标文件 175 | * @return 需要执行命令行 176 | */ 177 | public static String[] addWaterMark(String srcFile, String waterMark, String targetFile) { 178 | String waterMarkCmd = "-i %s -i %s -filter_complex overlay=0:0 %s"; 179 | waterMarkCmd = String.format(waterMarkCmd, srcFile, waterMark, targetFile); 180 | return waterMarkCmd.split(" ");//以空格分割为字符串数组 181 | } 182 | 183 | /** 184 | * 使用ffmpeg命令行进行视频转成Gif动图 185 | * 186 | * @param srcFile 源文件 187 | * @param startTime 开始时间 188 | * @param duration 截取时长 189 | * @param targetFile 目标文件 190 | * @return 需要执行命令行 191 | */ 192 | @SuppressLint("DefaultLocale") 193 | public static String[] generateGif(String srcFile, int startTime, int duration, String targetFile) { 194 | //String screenShotCmd = "-i %s -vframes %d -f gif %s"; 195 | String screenShotCmd = "-i %s -ss %d -t %d -s 320x240 -f gif %s"; 196 | screenShotCmd = String.format(screenShotCmd, srcFile, startTime, duration, targetFile); 197 | return screenShotCmd.split(" ");//以空格分割为字符串数组 198 | } 199 | 200 | /** 201 | * 使用ffmpeg命令行进行屏幕录制 202 | * 203 | * @param size 视频尺寸大小 204 | * @param recordTime 录屏时间 205 | * @param targetFile 目标文件 206 | * @return 需要执行命令行 207 | */ 208 | @SuppressLint("DefaultLocale") 209 | public static String[] screenRecord(String size, int recordTime, String targetFile) { 210 | //-vd x11:0,0 指录制所使用的偏移为 x=0 和 y=0 211 | //String screenRecordCmd = "-vcodec mpeg4 -b 1000 -r 10 -g 300 -vd x11:0,0 -s %s %s"; 212 | String screenRecordCmd = "-vcodec mpeg4 -b 1000 -r 10 -g 300 -vd x11:0,0 -s %s -t %d %s"; 213 | screenRecordCmd = String.format(screenRecordCmd, size, recordTime, targetFile); 214 | Log.i("VideoHandleActivity", "screenRecordCmd=" + screenRecordCmd); 215 | return screenRecordCmd.split(" ");//以空格分割为字符串数组 216 | } 217 | 218 | /** 219 | * 使用ffmpeg命令行进行图片合成视频 220 | * 221 | * @param srcFile 源文件 222 | * @param targetFile 目标文件(mpg格式) 223 | * @return 需要执行命令行 224 | */ 225 | @SuppressLint("DefaultLocale") 226 | public static String[] pictureToVideo(String srcFile, String targetFile) { 227 | //-f image2:代表使用image2格式,需要放在输入文件前面 228 | String combineVideo = "-f image2 -r 1 -i %simg#d.jpg -vcodec mpeg4 %s"; 229 | combineVideo = String.format(combineVideo, srcFile, targetFile); 230 | combineVideo = combineVideo.replace("#", "%"); 231 | Log.i("VideoHandleActivity", "combineVideo=" + combineVideo); 232 | return combineVideo.split(" ");//以空格分割为字符串数组 233 | } 234 | 235 | /** 236 | * 音频编码 237 | * 238 | * @param srcFile 源文件pcm裸流 239 | * @param targetFile 编码后目标文件 240 | * @param sampleRate 采样率 241 | * @param channel 声道:单声道为1/立体声道为2 242 | * @return 音频编码的命令行 243 | */ 244 | @SuppressLint("DefaultLocale") 245 | public static String[] encodeAudio(String srcFile, String targetFile, int sampleRate, int channel) { 246 | String combineVideo = "-f s16le -ar %d -ac %d -i %s %s"; 247 | combineVideo = String.format(combineVideo, sampleRate, channel, srcFile, targetFile); 248 | return combineVideo.split(" "); 249 | } 250 | 251 | /** 252 | * 多画面拼接视频 253 | * 254 | * @param input1 输入文件1 255 | * @param input2 输入文件2 256 | * @param videoLayout 视频布局 257 | * @param targetFile 画面拼接文件 258 | * @return 画面拼接的命令行 259 | */ 260 | public static String[] multiVideo(String input1, String input2, String targetFile, int videoLayout) { 261 | // String multiVideo = "-i %s -i %s -i %s -i %s -filter_complex " + 262 | // "\"[0:v]pad=iw*2:ih*2[a];[a][1:v]overlay=w[b];[b][2:v]overlay=0:h[c];[c][3:v]overlay=w:h\" %s"; 263 | String multiVideo = "-i %s -i %s -filter_complex hstack %s";//hstack:水平拼接,默认 264 | if (videoLayout == LAYOUT_VERTICAL) {//vstack:垂直拼接 265 | multiVideo = multiVideo.replace("hstack", "vstack"); 266 | } 267 | multiVideo = String.format(multiVideo, input1, input2, targetFile); 268 | return multiVideo.split(" "); 269 | } 270 | 271 | /** 272 | * 视频反序倒播 273 | * 274 | * @param inputFile 输入文件 275 | * @param targetFile 反序文件 276 | * @return 视频反序的命令行 277 | */ 278 | public static String[] reverseVideo(String inputFile, String targetFile) { 279 | //FIXME 音频也反序 280 | // String reverseVideo = "-i %s -filter_complex [0:v]reverse[v];[0:a]areverse[a] -map [v] -map [a] %s"; 281 | String reverseVideo = "-i %s -filter_complex [0:v]reverse[v] -map [v] %s";//单纯视频反序 282 | reverseVideo = String.format(reverseVideo, inputFile, targetFile); 283 | return reverseVideo.split(" "); 284 | } 285 | 286 | /** 287 | * 视频降噪 288 | * 289 | * @param inputFile 输入文件 290 | * @param targetFile 输出文件 291 | * @return 视频降噪的命令行 292 | */ 293 | public static String[] denoiseVideo(String inputFile, String targetFile) { 294 | String reverseVideo = "-i %s -nr 500 %s"; 295 | reverseVideo = String.format(reverseVideo, inputFile, targetFile); 296 | return reverseVideo.split(" "); 297 | } 298 | 299 | /** 300 | * 视频抽帧转成图片 301 | * 302 | * @param inputFile 输入文件 303 | * @param startTime 开始时间 304 | * @param duration 持续时间 305 | * @param frameRate 帧率 306 | * @param targetFile 输出文件 307 | * @return 视频抽帧的命令行 308 | */ 309 | public static String[] videoToImage(String inputFile, int startTime, int duration, int frameRate, String targetFile) { 310 | //-ss:开始时间,单位为秒 311 | //-t:持续时间,单位为秒 312 | //-r:帧率,每秒抽多少帧 313 | String toImage = "-i %s -ss %s -t %s -r %s %s"; 314 | toImage = String.format(Locale.CHINESE, toImage, inputFile, startTime, duration, frameRate, targetFile); 315 | toImage = toImage + "%3d.jpg"; 316 | return toImage.split(" "); 317 | } 318 | 319 | /** 320 | * 视频叠加成画中画 321 | * 322 | * @param inputFile1 输入文件 323 | * @param inputFile2 输入文件 324 | * @param targetFile 输出文件 325 | * @param x 小视频起点x坐标 326 | * @param y 小视频起点y坐标 327 | * @return 视频画中画的命令行 328 | */ 329 | @SuppressLint("DefaultLocale") 330 | public static String[] picInPicVideo(String inputFile1, String inputFile2, int x, int y, String targetFile) { 331 | String reverseVideo = "-i %s -i %s -filter_complex overlay=%d:%d %s"; 332 | reverseVideo = String.format(reverseVideo, inputFile1, inputFile2, x, y, targetFile); 333 | return reverseVideo.split(" "); 334 | } 335 | 336 | } 337 | -------------------------------------------------------------------------------- /app/src/main/java/com/mobile/ffmpeg/FileSizeUtil.java: -------------------------------------------------------------------------------- 1 | package com.mobile.ffmpeg; 2 | 3 | import android.util.Log; 4 | 5 | import java.io.File; 6 | import java.io.FileInputStream; 7 | import java.text.DecimalFormat; 8 | 9 | public class FileSizeUtil { 10 | 11 | public static final int SIZETYPE_B = 1;//获取文件大小单位为B的double值 12 | public static final int SIZETYPE_KB = 2;//获取文件大小单位为KB的double值 13 | public static final int SIZETYPE_MB = 3;//获取文件大小单位为MB的double值 14 | public static final int SIZETYPE_GB = 4;//获取文件大小单位为GB的double值 15 | 16 | /** 17 | * 获取文件指定文件的指定单位的大小 18 | * 19 | * @param filePath 文件路径 20 | * @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB 21 | * @return double值的大小 22 | */ 23 | public static double getFileOrFilesSize(String filePath, int sizeType) { 24 | File file = new File(filePath); 25 | long blockSize = 0; 26 | try { 27 | if (file.isDirectory()) { 28 | blockSize = getFileSizes(file); 29 | } else { 30 | blockSize = getFileSize(file); 31 | } 32 | } catch (Exception e) { 33 | e.printStackTrace(); 34 | Log.e("获取文件大小", "获取失败!"); 35 | } 36 | return FormetFileSize(blockSize, sizeType); 37 | } 38 | 39 | /** 40 | * 调用此方法自动计算指定文件或指定文件夹的大小 41 | * 42 | * @param filePath 文件路径 43 | * @return 计算好的带B、KB、MB、GB的字符串 44 | */ 45 | public static String getAutoFileOrFilesSize(String filePath) { 46 | File file = new File(filePath); 47 | long blockSize = 0; 48 | try { 49 | if (file.isDirectory()) { 50 | blockSize = getFileSizes(file); 51 | } else { 52 | blockSize = getFileSize(file); 53 | } 54 | } catch (Exception e) { 55 | e.printStackTrace(); 56 | Log.e("获取文件大小", "获取失败!"); 57 | } 58 | return FormetFileSize(blockSize); 59 | } 60 | 61 | /** 62 | * 获取指定文件大小 63 | * 64 | * @param file 65 | * @return 66 | * @throws Exception 67 | */ 68 | private static long getFileSize(File file) throws Exception { 69 | long size = 0; 70 | if (file.exists()) { 71 | FileInputStream fis = null; 72 | fis = new FileInputStream(file); 73 | size = fis.available(); 74 | } else { 75 | file.createNewFile(); 76 | Log.e("获取文件大小", "文件不存在!"); 77 | } 78 | return size; 79 | } 80 | 81 | /** 82 | * 获取指定文件夹 83 | * 84 | * @param f 85 | * @return 86 | * @throws Exception 87 | */ 88 | private static long getFileSizes(File f) throws Exception { 89 | long size = 0; 90 | File flist[] = f.listFiles(); 91 | for (int i = 0; i < flist.length; i++) { 92 | if (flist[i].isDirectory()) { 93 | size = size + getFileSizes(flist[i]); 94 | } else { 95 | size = size + getFileSize(flist[i]); 96 | } 97 | } 98 | return size; 99 | } 100 | 101 | /** 102 | * 转换文件大小 103 | * 104 | * @param fileS 105 | * @return 106 | */ 107 | private static String FormetFileSize(long fileS) { 108 | DecimalFormat df = new DecimalFormat("#.00"); 109 | String fileSizeString = ""; 110 | String wrongSize = "0B"; 111 | if (fileS == 0) { 112 | return wrongSize; 113 | } 114 | if (fileS < 1024) { 115 | fileSizeString = df.format((double) fileS) + "B"; 116 | } else if (fileS < 1048576) { 117 | fileSizeString = df.format((double) fileS / 1024) + "KB"; 118 | } else if (fileS < 1073741824) { 119 | fileSizeString = df.format((double) fileS / 1048576) + "MB"; 120 | } else { 121 | fileSizeString = df.format((double) fileS / 1073741824) + "GB"; 122 | } 123 | return fileSizeString; 124 | } 125 | 126 | /** 127 | * 转换文件大小,指定转换的类型 128 | * 129 | * @param fileS 130 | * @param sizeType 131 | * @return 132 | */ 133 | private static double FormetFileSize(long fileS, int sizeType) { 134 | DecimalFormat df = new DecimalFormat("#.00"); 135 | double fileSizeLong = 0; 136 | switch (sizeType) { 137 | case SIZETYPE_B: 138 | fileSizeLong = Double.valueOf(df.format((double) fileS)); 139 | break; 140 | case SIZETYPE_KB: 141 | fileSizeLong = Double.valueOf(df.format((double) fileS / 1024)); 142 | break; 143 | case SIZETYPE_MB: 144 | fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576)); 145 | break; 146 | case SIZETYPE_GB: 147 | fileSizeLong = Double.valueOf(df.format((double) fileS / 1073741824)); 148 | break; 149 | default: 150 | break; 151 | } 152 | return fileSizeLong; 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /app/src/main/java/com/mobile/ffmpeg/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.mobile.ffmpeg 2 | 3 | import android.app.ProgressDialog 4 | import android.content.Context 5 | import android.media.MediaMetadataRetriever 6 | import android.net.Uri 7 | import android.os.Bundle 8 | import android.util.Log 9 | import android.view.View 10 | import androidx.appcompat.app.AppCompatActivity 11 | import com.google.android.exoplayer2.SimpleExoPlayer 12 | import com.google.android.exoplayer2.source.ProgressiveMediaSource 13 | import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter 14 | import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory 15 | import com.google.android.exoplayer2.util.Util 16 | import com.mobile.ffmpeg.test.R 17 | import com.mobile.ffmpeg.util.FFmpegAsyncUtils 18 | import com.mobile.ffmpeg.util.FFmpegExecuteCallback 19 | import kotlinx.android.synthetic.main.activity_main.* 20 | import java.io.File 21 | import java.io.FileOutputStream 22 | import java.io.IOException 23 | 24 | 25 | class MainActivity : AppCompatActivity(), 26 | FFmpegExecuteCallback { 27 | var asyncTask: FFmpegAsyncUtils? = null 28 | var mProgressDialog: ProgressDialog? = null 29 | private var exoPlayer: SimpleExoPlayer? = null 30 | private var duration: Int = 1 31 | override fun onFFmpegCancel() { 32 | Log.e("yyy", " onFFmpegCancel ") 33 | } 34 | 35 | override fun onFFmpegStart() { 36 | Log.e("yyy", " onFFmpegStart ") 37 | } 38 | 39 | override fun onFFmpegSucceed(executeOutput: String?) { 40 | mProgressDialog?.dismiss() 41 | command_output.text = executeOutput 42 | Log.e("yyy", " onFFmpegSucceed $executeOutput") 43 | setVideo(tempFile) 44 | } 45 | 46 | override fun onFFmpegFailed(executeOutput: String?) { 47 | Log.e("yyyy", "onFFmpegFailed $executeOutput") 48 | command_output.text = executeOutput 49 | } 50 | 51 | override fun onFFmpegProgress(progress: Int?) { 52 | val mprogress = progress?.toFloat()?.div(duration)?.times(100)?.toInt() ?: 0 53 | mProgressDialog?.setMessage("正在执行命令行 \n \n ${mprogress}%") 54 | } 55 | 56 | fun getDuration(videoPath: String?): Int { 57 | return try { 58 | val mmr = MediaMetadataRetriever() 59 | mmr.setDataSource(videoPath) 60 | mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION).toInt() 61 | } catch (e: Exception) { 62 | e.printStackTrace() 63 | 0 64 | } 65 | } 66 | 67 | var tempFile: File? = null 68 | var file1: File? = null 69 | var cmd: Array? = null 70 | 71 | override fun onCreate(savedInstanceState: Bundle?) { 72 | super.onCreate(savedInstanceState) 73 | setContentView(R.layout.activity_main) 74 | initExoVideo() 75 | run_command.setOnClickListener { 76 | runThread() 77 | } 78 | } 79 | 80 | private fun runThread() { 81 | mProgressDialog = ProgressDialog(this) 82 | mProgressDialog?.setMessage("开始执行命令行") 83 | mProgressDialog?.setProgressNumberFormat("") 84 | mProgressDialog?.setButton("取消") { dialog, which -> 85 | asyncTask?.onCancel() 86 | } 87 | mProgressDialog?.show() 88 | 89 | val cache = cacheDir 90 | val time = System.currentTimeMillis() 91 | val file = File(cache, "video_demo$time.mp4") 92 | tempFile = File(cache, "result_video$time.mp4") 93 | try { 94 | file1 = redRawToFile(this, R.raw.video_demo, file) 95 | } catch (e: IOException) { 96 | e.printStackTrace() 97 | } 98 | //多画面拼接视频 99 | cmd = FFmpegUtil.multiVideo( 100 | file1?.getAbsolutePath(), 101 | file1?.getAbsolutePath(), 102 | tempFile?.getAbsolutePath(), 103 | FFmpegUtil.LAYOUT_HORIZONTAL 104 | ) 105 | duration = getDuration(file1?.absolutePath) 106 | asyncTask = FFmpegAsyncUtils() 107 | asyncTask?.setCallback(this) 108 | asyncTask?.execute(cmd) 109 | } 110 | 111 | /** 112 | * 读取流到文件中 113 | * 114 | * @param context 115 | * @param resourceId 116 | * @param file 117 | * @return 118 | * @throws IOException 119 | */ 120 | @Throws(IOException::class) 121 | private fun redRawToFile(context: Context, resourceId: Int, file: File): File { 122 | val inputStream = context.resources.openRawResource(resourceId) 123 | if (file.exists()) { 124 | file.delete() 125 | } 126 | val outputStream = FileOutputStream(file) 127 | try { 128 | val buffer = ByteArray(1024) 129 | var readSize: Int = -1 130 | while ({ readSize = inputStream.read(buffer);readSize }() > 0) { 131 | outputStream.write(buffer, 0, readSize) 132 | } 133 | } catch (e: IOException) { 134 | Log.e("yyyy", "Saving raw resource failed.", e) 135 | return file 136 | } finally { 137 | inputStream.close() 138 | outputStream.flush() 139 | outputStream.close() 140 | return file 141 | } 142 | } 143 | 144 | private fun initExoVideo() { 145 | exoPlayer = SimpleExoPlayer.Builder(this).build() 146 | } 147 | 148 | private fun setVideo(file: File?) { 149 | mPlayerView.visibility = View.VISIBLE 150 | val meter = DefaultBandwidthMeter.Builder(this).build() 151 | val defaultDataSourceFactory = DefaultDataSourceFactory( 152 | this, 153 | Util.getUserAgent(this, getString(R.string.app_name)), 154 | meter 155 | ) 156 | val parse = Uri.fromFile(file) 157 | val mediaSource = 158 | ProgressiveMediaSource.Factory(defaultDataSourceFactory).createMediaSource(parse) 159 | exoPlayer?.prepare(mediaSource) 160 | mPlayerView.player = exoPlayer 161 | exoPlayer?.setPlayWhenReady(true) 162 | } 163 | 164 | } 165 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 |