Coding with FFMPEG in Android

2019-04-02 18:46发布

问题:

I succesfully compiled ffmpeg for Android, but I don't know how I can programatically convert flv or mp4 files to mp3, but I need it. Can anybody help me with example or tutorial? Thank you.

回答1:

A good example on how to use an FFmpeg binary compiled for Android via CLI, using ProcessBuilder, is available here:

https://github.com/guardianproject/android-ffmpeg-java/blob/master/src/org/ffmpeg/android/FfmpegController.java

Note the method:

private int execProcess(String cmd, ShellCallback sc, File fileExec) throws IOException, InterruptedException { 

    ProcessBuilder pb = new ProcessBuilder(cmd);
    pb.directory(fileExec);

    Log.d(TAG,cmd);

    // pb.redirectErrorStream(true);
    Process process = pb.start();


    // any error message?
    StreamGobbler errorGobbler = new
    StreamGobbler(process.getErrorStream(), "ERROR", sc);

    // any output?
    StreamGobbler outputGobbler = new
    StreamGobbler(process.getInputStream(), "OUTPUT", sc);

    // kick them off
    errorGobbler.start();
    outputGobbler.start();

    int exitVal = process.waitFor();

    sc.processComplete(exitVal);

    return exitVal;
}