同步操作将从 AnJoiner/FFmpegCommand 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
To users of
FFmpegCommand
:First of all, thank you all for your support of this library. Thank you for using it so that we have the motivation to continue to open source. Thank you for your questions and make this library more perfect.
Asynchronous processing and multi-code execution were provided before
1.2.0
, but many people reported that it is impossible to perform asynchronous and multi-code is not very useful, so after repeated consideration, the following changes will be made in1.2.0
and later versions :
- Delete the
runCmdAsync
andrunCmdSync
methods and change them torunCmd
to execute theFFmpeg
command- Delete multi-command
runMoreAsync
andrunMoreSync
methods,runCmd
internally realizes automatic synchronization and sequential execution- Added error log prompt, use
ffmpeg-cmd
to filter the error log when an error occursWe apologize for the inconvenience caused by this modification.
In our development, audio and video related content is often used, generally we will choose FFmpeg, but its cross-compilation is a very troublesome thing for us . So here for the convenience of future use, I wrote this FFmpegCommand
, FFmpegCommand
is composed of FFmpeg
core library, and integrates lame
, libx264
, fdk-aac
and libopencore-amr
mainstream audio and video processing Android program
Note: The current library is only available for Android
If you can’t access all the information, please go to【Domestic Mirror】
Run FFmpeg | Get media information |
---|---|
Find build.gradle
in the project root directory and add the following:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then add the import in build.gradle
in app
or other module
directory
Choose only one of the following two introductions, and replace the following according to the latest version ${latestVersion}
,Current latest version
// All codecs-larger size
implementation 'com.github.AnJoiner:FFmpegCommand:1.2.1'
// Some commonly used codecs-smaller in size, about 6M less than the introduction above
implementation 'com.github.AnJoiner:FFmpegCommand:1.2.1-lite'
Change build.gradle under module, the current library only supports armeabi-v7a
and arm64-v8a
, of course you can use only one (usually using armeabi-v7a
for backward compatibility). You can Can refer to 【Android ABI】
android {
defaultConfig {
ndk {
abiFilters "armeabi-v7a",'arm64-v8a'
moduleName "app"
}
}
}
If there is no special codec requirement, it is strongly recommended to use lite
tag
Method | Function |
---|---|
FFmpegCommand->setDebug(debug: Boolean) | Debug mode, printable log |
FFmpegCommand->runCmd(cmd: Array<String?>) | Execute ffmpeg command without callback |
FFmpegCommand->runCmd(cmd: Array<String?> callBack: IFFmpegCallBack?) | Execute ffmpeg command and call back start, complete, cancel, progress, error |
FFmpegCommand->getMediaInfo(path: String?, @MediaAttribute type: Int) | Get media information: video width and height, bit rate... |
FFmpegCommand->getSupportFormat(@FormatAttribute formatType: Int) | Get the encapsulation and decapsulation formats supported by the current library |
FFmpegCommand->getSupportCodec(@CodecAttribute codecType: Int) | Get the codec supported by the current library |
FFmpegCommand->cancel() | Exit FFmpeg command execution |
Use runCmd
to call FFmpeg
to execute FFmpeg commands synchronously. External threads need to be added, otherwise the application will become unresponsive.
Direct call FFmpegCommand.runCmd(cmd: Array<String?> callBack: IFFmpegCallBack?)
method,The first parameter is provided by the FFmpegUtils
tool class, or you can add it yourself
Does not support asynchronous execution of FFmpeg commands, after all, C is a process-oriented language, and resource occupation problems will occur
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.transformAudio(audioPath, targetPath), callback("transcoding complete", targetPath))
}
The second parameter is the callback method
open class CommonCallBack : IFFmpegCallBack {
// Start callback
override fun onStart() {}
// Progress callback
override fun onProgress(progress: Int, pts: Long) {}
// Cancel callback
override fun onCancel() {}
// Complete callback
override fun onComplete() {}
// Error callback
override fun onError(errorCode: Int, errorMsg: String?) {}
}
It should be noted that in the onProgress
method, you can see that the callback returns 2 values:
-i
), and it may be incorrect when there are multiple input filesvar duration :Int? = FFmpegCommand.getMediaInfo(mAudioPath,MediaAttribute.DURATION)
var progress = pts/duration!!
This is just a demonstration of audio cutting, many functions such as the above, please refer to it yourself FFmpegUtils If the requirements are not met, you can add your own FFmpeg command, E.g:
var command = "ffmpeg -y -i %s -vn -acodec copy -ss %d -t %d %s"
command = String.format(command, srcFile, startTime, duration, targetFile)
GlobalScope.launch {
FFmpegCommand.runCmd(command.split(" ").toTypedArray(), callback("Audio cut is complete", targetPath))
}
Since the bottom layer is temporarily unable to implement multithreading (after all, C is a process-oriented language), if you need to push the stream at the same time, it is impossible to execute other commands at the same time. To solve this problem, you can use the following multi-process method:
<service android:name=".service.FFmpegCommandService" android:process=":ffmpegCommand" />
<service android:name=".service.FFmpegCommandService2" android:process=":ffmpegCommand2" />
class FFmpegCommandService : Service() {
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
val videoPath = File(externalCacheDir, "test.mp4").absolutePath
val output = File(externalCacheDir, "output.yuv").absolutePath
val cmd = "ffmpeg -y -i %s -an -c:v rawvideo -pixel_format yuv420p %s"
val result = String.format(Locale.CHINA, cmd, videoPath, output)
val strings: Array<String?> = result.split(" ").toTypedArray()
FFmpegCommand.runCmd(strings)
return super.onStartCommand(intent, flags, startId)
}
}
After executing the following method, the CommonCallBack->onCancel()
method will be called back
FFmpegCommand.cancel();
【KFFmpegCommandActivity-Command reference】 【KFFmpegInfoActivity-Media Information Reference】 【KFFmppegFormatActivity-Support package format】 【KFFmpegCodecActivity-Support codec】
Compatible with Android minSdkVersion >=21
【Compile FFmpeg for use in Android】 【Custom MP3 encoder】
Scan code to download|click to download | communication | WeChat appreciation |
---|---|---|
If you think it is helpful to you, give a star to support it, and welcome a lot of forks!
-keep class com.coder.ffmpeg.** {*;}
-dontwarn com.coder.ffmpeg.**
Copyright 2019 AnJoiner
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.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。