/FFmpegCommand

FFmpegCommand适用于Android的FFmpeg命令库,实现了对音视频相关的处理,能够快速的处理音视频,大概功能包括:音视频剪切,音视频转码,音视频解码原始数据,音视频编码,视频转图片或gif,视频添加水印,多画面拼接,音频混音,视频亮度和对比度,音频淡入和淡出效果等

Primary LanguageKotlinApache License 2.0Apache-2.0

FFmpegCommand

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 in 1.2.0 and later versions :

  • Delete the runCmdAsync and runCmdSync methods and change them to runCmd to execute the FFmpeg command
  • Delete multi-command runMoreAsync and runMoreSync methods, runCmd internally realizes automatic synchronization and sequential execution
  • Added error log prompt, use ffmpeg-cmd to filter the error log when an error occurs

We apologize for the inconvenience caused by this modification.

【README-中文】

Summary

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】

The main function

License FFmpeg X264 mp3lame fdk-aac fdk-aac

  • Support all FFmpeg commands
  • Support video format conversion : mp4->flv
  • Support audio codec : mp3->pcm pcm->mp3 pcm->aac
  • Support audio transcoding : mp3->aac mp3->amr
  • Support video codec : mp4->yuv yuv->h264
  • Support video transcoding : mp4->flv mp4->avi
  • Support cutting and splicing of audio and video
  • Support video to picture : mp4->png mp4->gif
  • Support audio sound size control and mixing (such as reading sound plus background music)
  • Support some filters, audio fade in, fade out effects, video brightness and contrast, and add watermark
  • Support for generating silent audio
  • Support for obtaining media file information
  • Support continuous execution of FFmpeg commands
Run FFmpeg Get media information
图-1:命令行展示 图-2:命令行执行

Introduce

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

Use

FFmpegCommand Method

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

runCmd

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:

  • progress:progress, calculated by referring to the first input file (that is the input file after the first -i), and it may be incorrect when there are multiple input files
  • pts:Elapsed time, progress appears incorrectly using the current value for calculation, the calculation method is as follows
var duration :Int? = FFmpegCommand.getMediaInfo(mAudioPath,MediaAttribute.DURATION)
var progress = pts/duration!!

Custom FFmpeg command

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))
}

Multi-process execution

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:

  1. Define other processes different from the main process
<service android:name=".service.FFmpegCommandService" android:process=":ffmpegCommand" />
<service android:name=".service.FFmpegCommandService2" android:process=":ffmpegCommand2" />
  1. Perform push operations in other processes
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)
    }
}

Cancel execution

After executing the following method, the CommonCallBack->onCancel() method will be called back

FFmpegCommand.cancel();

【common problem】

【new version update】

Reference

【KFFmpegCommandActivity-Command reference】 【KFFmpegInfoActivity-Media Information Reference】 【KFFmppegFormatActivity-Support package format】 【KFFmpegCodecActivity-Support codec】

Compatibility

Compatible with Android minSdkVersion >=21

图-7 Demo下载

图-8 Demo下载

图-9 Demo下载

Compile SO

【Compile FFmpeg for use in Android】 【Custom MP3 encoder】

Experiential exchange

Scan code to download|click to download communication WeChat appreciation
图-4 Demo下载 图-4 Demo下载 图-5 赞赏

Star

If you think it is helpful to you, give a star to support it, and welcome a lot of forks!

Confuse

-keep class com.coder.ffmpeg.** {*;}
-dontwarn  com.coder.ffmpeg.**

License

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.