简体   繁体   English

在Android中使用没有NDK的FFmpeg

[英]Using FFmpeg without NDK in android

I go though many site and search regarding " FFMPEG " implementation for android project. 我经过许多站点,并搜索有关“ FFMPEG ”实现的android项目。

Most solution founded are using NDK. 建立的大多数解决方案都使用NDK。

but i want to use FFmpeg without using NDK as i found in This Link 但我想使用FFmpeg而不使用 本链接中的 NDK

I have used this project https://github.com/guardianproject/android-ffmpeg-java 我已经使用了这个项目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). 它已经为FFMPEG库的android版本进行了编译,并且该文件位于res / raw文件夹中(如果需要更新的版本,可以更新此文件)。 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: 然后,您可以使用以下示例在Java中编写自己的函数:

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. 并使用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. 编辑:我希望您将此github代码连接为您的项目的库。 There is FfmpegController.java class in src folder. src文件夹中有FfmpegController.java类。 It's a wrapper for using command line ffmpeg exe file. 它是使用命令行ffmpeg exe文件的包装器。 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. 您需要向FfmpegController.java类添加功能。 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. 现在在您的项目中初始化FfmpegController对象并运行您的函数。

I have used this FFmpeg sample which is library that used without NDK 我使用了这个FFmpeg示例,它是没有NDK使用的库

First of download sample example FFmpeg Sample 首先下载示例示例 FFmpeg示例

Download FFmpeg library FFmpeg Library 下载FFmpeg库 FFmpeg库

Extract both in one folder and import project from Android Studio 将其提取到一个文件夹中并从Android Studio导入项目

Now, Calling FFmpeg command 现在,调用FFmpeg命令

This command is for rotate (/sdcard/videokit/in.mp4) video in 90 Angle and generate out.mp4 in specific location in SD card 此命令用于以90度角度旋转(/sdcard/videokit/in.mp4)视频,并在SD卡中的特定位置生成out.mp4

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的侦听器

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 . 运行此程序,然后检入File Manager for Output。 I hope it works Good :) Enjoy 我希望它很好:)享受

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM