Code Monkey home page Code Monkey logo

videokit-ffmpeg-android's Introduction

Video processing library based on FFmpeg

Main purpose

Main purpose of this library is to allow developers to embed FFmpeg into their application with literally one line of code in gradle.build. Using native code in Java in general and in Android environment especially always related to some problems. Hopefully VideoKit will resolve some of them.

Setup

Basic setup just add following line of code to your gradle.build file:
compile 'com.infullmobile.android:videokit-release:1.1.2'
Also, if you haven't use inFullMobile public maven in your project before, don't forget to add following line to your repositories:
maven { url 'https://maven.infullmobile.com/public' }
That's pretty much all.

Example usage

Module sample is showing usage of VideoKit. Please note, that VideoKit is basically invoking FFmpeg main() with CLI arguments so you will use commands almost like you use standard FFmpeg on Linux or Windows or MacOS.
Well, let me show you some code:

        final VideoKit videoKit = new VideoKit();
        final Command command = videoKit.createCommand()
                .overwriteOutput()
                .inputPath(path)
                .outputPath(path + POSTFIX)
                .customCommand("-ss 1 -t 3")
                .copyVideoCodec()
                .experimentalFlag()
                .build();

Command is basically set of instructions for FFmpeg. Please note, that order in which you adding instructions in CommandBuilder is quite important. Sometimes, when you mess up order, FFmpeg will not recognize command and end up with error. After you've built a command you should execute it: either with execute() or you can pass it to AsyncCommandExecutor.
Please note, that:
command.execute();
will be executed on thread on which it was called, while:
new AsyncCommandExecutor(command, this).execute();
will be executed in background thread and you have to provide implementation of ProcessingListener to get result of processing either in onSuccess (you will get path to processed file here) or onFailure (you will get error code here).

Other functions

VideoKit is also providing logging mechanism for FFmpeg. You can set logging level by calling function:
videoKit.setLogLevel(level)
where level is enum which consists of:

NO_LOG
ERRORS_ONLY
FULL

Specific behavior of flags

As I already have mentioned: FFmpeg is quite sensible to the order of flags. To help out with that to user VideoKit have default behavior for particular flags, namely: input paths, output path and experimental flag.
The order in which VideoKit is laying those flags is following:

  1. Input paths are always going first (doesn't matter in which place of chain you have added particular path)
  2. Output path is always going at the end of command
  3. Experimental flag is always just before output path

Error codes

I've tried to make library as debuggable as it even possible, taking in account that we're working with native code. If you getting failures from VideoKit and log in not really helpfull try to look into docs folder in ffmpeg_return_codes file. In this file you not only have list of possible return codes with which FFMpeg can exit, but also file and number of line of code in which exit was called.
Version of FFmpeg is 3.2.4 and changed sources can be found over here:
Development Kit

License

FFmpeg is licensed under LGPL:
License
This software uses libraries from the FFmpeg project under the LGPLv2.1

videokit-ffmpeg-android's People

Contributors

iljakosynkin avatar kolipass avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

videokit-ffmpeg-android's Issues

I am getting no implementation found error.

I am using this for implementing watermark over a video. It works great on Samsung Galaxy S5. However on the later devices it crashes with the following error:

No implementation found for int processing.ffmpeg.videokit.VideoKit.run(int, java.lang.String[]) (tried Java_processing_ffmpeg_videokit_VideoKit_run and Java_processing_ffmpeg_videokit_VideoKit_run__I_3Ljava_lang_String_2)

I am using to following code to do a watermark over a video:

final Command command = videoKit.createCommand()
                .overwriteOutput()
                .inputPath(videoPath)
                .customCommand("-i " + stickerPath)
                .outputPath(newVideoPath)
                .customCommand("-filter_complex overlay=1:1")
                .experimentalFlag()
                .build();
        command.execute();

video compression result only 1 second

Hi @IljaKosynkin and @kolipass , I want to ask how to compress video file properly?
I got inputPath = "/storage/emulated/0/BDRC/BDRC/video.mp4"
and I want to overwrite output, so this is what I did.

val videoKit = VideoKit()
val command = videoKit.createCommand()
        .overwriteOutput()
        .inputPath(inputPath)
        .outputPath(inputPath)
        .copyVideoCodec()
        .experimentalFlag()
        .build()

val result = AsyncCommandExecutor(command, object: ProcessingListener {
    override fun onSuccess(path: String?) {
        Log.e("SUCCESS", "COMPRESSING")
    }

    override fun onFailure(returnCode: Int) {
        Log.e("FAILURE", "COMPRESSING")
    }
}).execute()

but the result always 1 second video even though I record for 10 seconds. Can you explain what is wrong, please? I want to understand how this library works.

Regards,
Elmer

Can not use crop command

  final Command command = videoKit.createCommand()
            .inputPath(path)
            .outputPath(System.currentTimeMillis() + POSTFIX)
            .crop(0,0,300,300)
            .experimentalFlag()
            .build();

It can not use crop command

Log show

03-20 02:23:40.610 27785-28025/ffmpeg.videokit.sample I/Videokit: Option: ffmpeg
03-20 02:23:40.610 27785-28025/ffmpeg.videokit.sample I/Videokit: Option: -i
03-20 02:23:40.610 27785-28025/ffmpeg.videokit.sample I/Videokit: Option: /storage/emulated/0/intro.mp4
03-20 02:23:40.611 27785-28025/ffmpeg.videokit.sample I/Videokit: Option: -vf
03-20 02:23:40.611 27785-28025/ffmpeg.videokit.sample I/Videokit: Option: crop=300:300:0:0
03-20 02:23:40.611 27785-28025/ffmpeg.videokit.sample I/Videokit: Option: -strict
03-20 02:23:40.611 27785-28025/ffmpeg.videokit.sample I/Videokit: Option: -2
03-20 02:23:40.611 27785-28025/ffmpeg.videokit.sample I/Videokit: Option: 1489951420608.mp4

And I want to use custom command and it show error same crop command

this command I want to use

-vf crop=iw:iw,scale=480:ih*480/iw -threads 5 -preset ultrafast -strict -2

White Screen

I'm using Android Studio 3.0.1. The project compiles and runs but (using the emulator), but I just get a white screen for the MainActivity. Duplicated on Samsung Note 5 running Android 7. White Screen.

E/RecyclerView: No adapter attached; skipping layout

FIXED!
added: recyclerView.setHasFixedSize(true); at the end of setupList method.

Use opinionated method names in the builder

We currently have 3 different approaches to the methods in the CommandBuilder class.

  • set ( currently getting phased out I believe)
  • add
  • verb

Most of the methods start with a verb.

CommandBuilder withoutAudio();
CommandBuilder copyVideoCodec();

This makes for a nicer and more understandable API. Add/set methods at some point become noise when searching for an option within a builder class.

I suggest posting a full method list here. Go through some options for a rename, and phase them out in a 1.0.2 or rather 1.1.0

video Maksing & merging

hello ,
any idea about how can we add one black-white Video between 2 Videos . i want to do like add black-white video at last second of 1 video & starting second of 2 video . overlay merge this 3 video( black-white, last second of 1 video & starting second of 2 video). at last concate all this videos & get output.
please provide command for this.

Creating video from images

First off, this is a killer library! Thanks!

I'd really like to use this library to create a video from a set of images. I'm trying to do this, but it's not working:

final VideoKit videoKit = new VideoKit();
final Command command = videoKit.createCommand()
.overwriteOutput()
.inputPath(path + "0001.jpg")
.outputPath(path + "test.mp4")
.customCommand("-f image2 -i " +
path + "%04d.jpg " +
"-vcodec mpeg4 -acodec aac " +
path + "test.mp4")
.build();
command.execute();

I think the issue is that it has to have both an input and output file to execute which is messing up the arguments sent to ffmpeg. Is there any way to make the process method in VideoKit public. This would make this much more flexible for my purposes.

Custom command for watermark

Hi @IljaKosynkin and to everyone else. I tried to put watermark on one video but custom command won't execute and error code is 3037. I run the same code for trimming video and video is trimmed successfully so there is no issues with inputpath or outputpath also I have ic_watermark.png in assets folder. I tried with image from Drawable but same error code.

So here is the command which I tried to run:
executeFFmepg(path, generateFile().getAbsolutePath(), "-i file:///android_asset/ic_watermark.png -filter_complex overlay=main_w-overlay_w-5:main_h-overlay_h-5");

and this is the executeFFmepg method:

 `private void executeFFmepg(String inputPath, String outputPath, String customCommand){
    final Command command = videoKit.createCommand()
            .overwriteOutput()
            .inputPath(inputPath)
            .outputPath(outputPath)
            .customCommand(customCommand)
            .experimentalFlag()
            .build();
     new AsyncCommandExecutor(command, this).execute();  }`

A don't have experience with FFmpeg so any help would be very appreciated. Thanks

Unable to install via Maven

I am getting the following error:

Error:(53, 13) Failed to resolve: com.infullmobile.android:videokit-release:1.1.0
<a href="openFile:/Users/Subash/Developer/UPShow-Android/app/build.gradle">Show in File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>

even though I included the maven repository

repositories {
    maven { url 'https://maven.fabric.io/public' }
    maven { url 'https://maven.infullmobile.com/public' }
}

-crf option not working

I'm am trying the following commands but I get a error 2041 which apparently is related to "invalid sample description";

final Command command = videoKit.createCommand()
                          .inputPath(mVideoUri.toString().replace("file://",""))
                          .inputPath(newBackgroundBitmap.getPath())
                          .outputPath(directoryToStore+"/"+lastSaved+mp)
                          .customCommand("-filter_complex [1:v][0:v]scale2ref=iw:ih[ovr][base];[ovr]colorchannelmixer=aa=0.7[ovrl];[base][ovrl]overlay[v] -map [v]")
                          .customCommand("-crf "+"23")
                          .customCommand("-ss "+valueLeft)
                          .customCommand("-t "+valueDur)

I also tried increasing the bitrate with .limitVideoBitrate("6000k") but I keep getting back low bitrates.

Compile with --enable-libfreetype

Hi @IljaKosynkin, I appreciate all of your work on this library!

I've tried to fork FFmpeg-Development-Kit and recompile the ffmpeg .so files with --enable-libfreetype. I was able to successfully compile freetype2 for android first, but when running your configure command for ffmpeg via build_all.sh I keep getting: Error: freetype2 not found.

I've ran through all possible suggestions on the internet about this error message and I cannot seem to figure it out. I need the drawtext filter in order to complete the project I'm working on. Specifically it seems potentially that I'm either a) not compiling freetype2 correctly or b) ffmpeg cannot find where I compiled freetype2.

Have you attempted to enable any other options for ffmpeg that used another 3rd party library? See an example current configure command I'm trying below:

./configure --prefix=$PREFIX \
    $COMMON \
    $CONFIGURATION \
    --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
    --target-os=linux \
    --arch=arm \
    --enable-cross-compile \
    --sysroot=$SYSROOT \
    --extra-cflags="-Os -marm $ADDI_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS" \
    --pkg-config=/usr/local/bin/pkg-config

also COMMON is now:
export COMMON="--disable-static --disable-programs --disable-doc --enable-shared --enable-protocol=file --enable-pic --enable-small --enable-libfreetype"

Here is my example configure command for building freetype:

./configure \
  --with-pic \
  --with-sysroot="${TOOLCHAIN}/sysroot" \
  --host=arm-linux-androideabi \
  --target=arm-linux-androideabi \
  --enable-static \
  --disable-shared \
  --with-png=no \
  --with-zlib=yes \
  --prefix="$PREFIX"

And here is what my built freetype folder looks like:
screen shot 2017-10-30 at 5 33 13 pm

Any ideas, help, or direction would be immensely appreciated. I've been banging my head on this for several days now ๐Ÿ˜ข

How can i use concat function with this lib

How can i use concat ffmpeg function to merge two videos with this library. When i use ffmpeg as precompiled binary I was able to it with this code:

LinkedList command = new LinkedList<>();
command.add(mFfmpegPath);
command.add("-f");
command.add("concat");
command.add("-i");
command.add(inputFile);
command.add("-c");
command.add("copy");
command.add("-y");
command.add(outputFile);
return executeFFmpegCommand(command);

Now with your library i try with this code:

String customCommand = "-f concat -c copy";
final Command command = mVideoKit.createCommand()
.overwriteOutput()
.inputPath(tempTextFilePath)
.outputPath(outputPath)
.customCommand(customCommand)
.copyVideoCodec()
.experimentalFlag()
.build();
new AsyncCommandExecutor(command, this).execute();

but it not work and i get error code 3085 in onFailure
Please help me, i would appreciate very much

Custom Command Error

Thanks for this nice interface of FFMPEG for android. I am trying to execute custom command i.e. adding an image overlay on a video. It always fails. Here is mt code

final Command command = videoKit.createCommand()
.overwriteOutput()
.addInputPath(path)
.addInputPath("/sdcard/logo.png")// logo.png exists on sdcard.
.addOutputPath(path + POSTFIX)
.copyVideoCodec()
.addExperimentalFlag()
.addCustomCommand("-filter_complex overlay")
.build();

If there is any mistake please let me know. thanks

Command fail with 2041 error code

Hi,

I have modified the code to just send the custom command(in custom command i have send entire command parameter). but i am getting failure , as there is no native code so i am not able to fix it.

Please help

Trying to crop H.264 video getting error.

I did see one of your past issues you mention you didn't have libx264 library included since it was not GPL. I tried to create it following your blog post but no luck.

The problem I am running onto is that the files i am trying to convert is H.264 Codec, so when i crop the output file has pixelation.

Have you thought of providing a build with libx264 included in it?

Thank you so much for the framework, its really fast and easy to use

failvideokit: Code:1002

I get code 1002 error (Main ended with status 1002) when exporting larger (longer in length) videos. Everything works perfectly when exporting shorter videos.

Here is where the error comes from:

@OverRide
public void onFailure(int returnCode) {
Log.d("failvideokit","Code:"+returnCode);
}

This happens when I try to implement trimming with the following;

.customCommand("-ss "+valueLeft)
.customCommand("-to "+valueRight)

Any idea why this error is only showing on longer videos?

Use tags to mark releases.

Remember to tag the releases. Not only this helps with going back to some code, but has one important feature to spread the library. This way we can easily provide it via jitpack.io cleanest and fastest solution compared to say maven central.

PS. and push them with a flag --tags

onProgress call back

HI I am using your library for my application. There are only two call backs success and failure, I need on progress call back to show the progress, so will there be any update from your side please let me know.

Not able to lower bitrate?

Hello - first, thank you so much for this library!

Can you please review my custom command below? I am not able to reduce the bitrate of my video. I am able to successfully change the resolution/size of the video using -filter:v scale command.

final Command command = videoKit.createCommand() .overwriteOutput() .inputPath(path) .outputPath(newPath) .customCommand("-filter:v scale=1080:-1" + " -b:v 512k") .experimentalFlag() .build();

Apk size increase.

Hi, Thank you for making awesome library but my app size increase from 7mb to 34mb after added this library. How can i solve it?

get 3044response code in coverting mp4 to mp3

final Command command = videoKit.createCommand()

            .inputPath("/storage/emulated/0/WhatsApp/Media/WhatsApp Video/VID-20181118-WA0003.mp4")
            .outputPath("/storage/emulated/0/abc.mp3")
            .customCommand("-vn -ar 44100 -ac 2 -ab 320 -f mp3")
            .build();

    new AsyncCommandExecutor(command, this).execute();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.