Using FFmpeg without NDK in android

时光毁灭记忆、已成空白 提交于 2019-12-04 14:53:26

问题


I go though many site and search regarding "FFMPEG" implementation for android project.

Most solution founded are using NDK.

but i want to use FFmpeg without using NDK as i found in This Link


回答1:


I have used this project https://github.com/guardianproject/android-ffmpeg-java

It has already compiled for android version of FFMPEG library and this file will be in res/raw folder (you can update this file if you need newer version). You need to add this project as a library to your's. And after thst you can write your own function in java for example like this:

public Clip convert (Clip mediaIn, String outPath, ShellCallback sc) throws Exception
{
    ArrayList<String> cmd = new ArrayList<String>();

    cmd.add(mFfmpegBin);
    cmd.add("-y");
    cmd.add("-i");
    cmd.add(new File(mediaIn.path).getCanonicalPath());

    if (mediaIn.startTime != null)
    {
        cmd.add("-ss");
        cmd.add(mediaIn.startTime);
    }
    if (mediaIn.duration != -1)
    {
        cmd.add("-t");
        cmd.add(String.format(Locale.US,"%f",mediaIn.duration));

    }
    Clip mediaOut = new Clip();
    File fileOut = new File(outPath);
    mediaOut.path = fileOut.getCanonicalPath();
    cmd.add(mediaOut.path);
    execFFMPEG(cmd, sc);
    return mediaOut;
}

and execute it using FfmpegController Object. Please notice me if you have any questions or if this is what you want.

EDIT: I hope you connect this github code as a library for your project. There is FfmpegController.java class in src folder. It's a wrapper for using command line ffmpeg exe file. If you want for example execute command like this one

ffmpeg -i source.wav -b:a 128k output.mp3

you need to add function to FfmpegController.java class. Something like this:

    public Clip convert(Clip mediaIn, String outPath, ShellCallback sc) throws Exception
    {
    ArrayList<String> cmd = new ArrayList<String>();

    Clip mediaOut = new Clip();

    String mediaPath = mediaIn.path;

    cmd = new ArrayList<String>();

    cmd.add(mFfmpegBin);
    cmd.add("-i");
    cmd.add(mediaPath);

    cmd.add("-b:a");
    cmd.add("128k");

    mediaOut.path = outPath;

    cmd.add(mediaOut.path);

    execFFMPEG(cmd, sc);

    return mediaOut; // this is not importatnt because file will be put in outPath
    }

Now in your project initialize FfmpegController object and run your function.




回答2:


I have used this FFmpeg sample which is library that used without NDK

First of download sample example FFmpeg Sample

Download FFmpeg library FFmpeg Library

Extract both in one folder and import project from Android Studio

Now, Calling FFmpeg command

This command is for rotate (/sdcard/videokit/in.mp4) video in 90 Angle and generate out.mp4 in specific location in SD card

ffmpeg -y -i /sdcard/videokit/in.mp4 -strict experimental -vf transpose=1 -s 160x120 -r 30 -aspect 4:3 -ab 48000 -ac 2 -ar 22050 -b 2097k /sdcard/videokit/out.mp4

Now run this command with predefined method in library and add listeners of GeneralUtils

GeneralUtils.copyLicenseFromAssetsToSDIfNeeded(this, workFolder);
      GeneralUtils.copyDemoVideoFromAssetsToSDIfNeeded(this, demoVideoFolder);
   //demoVideoFolder where your Input file path
   //workFolder Absolute path
   // workFolder = getApplicationContext().getFilesDir().getAbsolutePath() + "/";



            LoadJNI vk = new LoadJNI();
        try {

            vk.run(GeneralUtils.utilConvertToComplex(commandStr), workFolder, getApplicationContext());

            // copying vk.log (internal native log) to the videokit folder
            GeneralUtils.copyFileToFolder(vkLogPath, demoVideoFolder);

        } catch (Throwable e) {
            Log.e(Prefs.TAG, "vk run exeption.", e);
        }

Run this and check in File Manager for Output . I hope it works Good :) Enjoy



来源:https://stackoverflow.com/questions/29166919/using-ffmpeg-without-ndk-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!