Question

edit: I'm aware of the possible duplicate, but the answer it's not directly applicable. I'm using ProcessBuilder and not Runtime.getRuntime().exec:

public int execProcess(List<String> cmds, ShellUtils.ShellCallback sc) {    
    StringBuilder cmdlog = new StringBuilder();
    for (String cmd : cmds) {
         cmdlog.append(' ');
    }
    Utils.logger("v", cmdlog.toString(), DEBUG_TAG);

    ProcessBuilder pb = new ProcessBuilder();
    pb.directory(mBinFileDir);
    pb.command(cmds);

    Process process = null;
    int exitVal = 1; // Default error
    try {
        process = pb.start();

        StreamGobbler errorGobbler = new
        StreamGobbler(process.getErrorStream(), "ERROR", sc);

        StreamGobbler outputGobbler = new
        StreamGobbler(process.getInputStream(), "OUTPUT", sc);

        errorGobbler.start();
        outputGobbler.start();

        exitVal = process.waitFor();

        sc.processComplete(exitVal);

     } catch (Exception e) {
         Log.e(DEBUG_TAG, "Error executing ffmpeg command!", e);
     } finally {
         if (process != null) {
             Utils.logger("w", "destroyng process", DEBUG_TAG);
             process.destroy();
         }
    }
    return exitVal;
}

I have two versions on FFmpeg compiled for android: with and without liblame enabled.

When I load in /data/data/<<my-package-name>>/app_bin/ the FFmpeg binary compiled with NO lame support, it extracts audio from video without an issue. But when I use the one with lame support enabled, I get the error below into the log.

Required libs, also compiled for android, are correctly loaded when shipped into the libs project folder with System.loadLibrary("lame").

I was wondering if there is something else to do in order to properly make the FFmpeg binary find the libs. Those are the same libs given to the NDK to build the FFmpeg binary.

D/dalvikvm(13741): Trying to load lib /data/app-lib/<<my-package-name>>/liblame.so 0x40ffed08
D/dalvikvm(13741): Added shared lib /data/app-lib/<<my-package-name>>/liblame.so 0x40ffed08
D/dalvikvm(13741): No JNI_OnLoad found in /data/app-lib/<<my-package-name>>/liblame.so 0x40ffed08, skipping init
D/FfmpegController(13741): Trying to chmod '/data/data/<<my-package-name>>/app_bin/ffmpeg' to: 755
V/FfmpegController(13741): /data/data/<<my-package-name>>/app_bin/ffmpeg -y -i /storage/sdcard0/Download/video.webm -vn -acodec copy /storage/sdcard0/Download/audio.ogg 
>>>>>> ---------- <<<<<<
D/DownloadsService(13741): soinfo_link_image(linker.cpp:1673): could not load library "liblame.so" needed by "/data/data/<<my-package-name>>/app_bin/ffmpeg"; caused by load_library(linker.cpp:771): library "liblame.so" not foundCANNOT LINK EXECUTABLE
>>>>>> ---------- <<<<<<
I/DownloadsService(13741): FFmpeg process exit value: 255

I'm not attaching code because it's fairly straightforward:

  • download the FFmpeg binary (it's not shipped with the app);
  • copy the binary from the download folder to /data/data/<<my-package-name>>/app_bin/;
  • chmod 755;
  • execute from there and read the output.

Please comment if something else is required.

Was it helpful?

Solution

It's funny I'm finding all my answers lately... anyway, since it can be useful to others out there, if you want to:

  • add the path of a shared library like liblame.so
  • loaded with System.loadLibrary("lame")from libs folder
  • and you want to use ProcessBuilder

do:

1)

ProcessBuilder pb = new ProcessBuilder("liblame.so");
Map<String, String> envMap = pb.environment();
envMap.put("LD_LIBRARY_PATH", "/data/app-lib/<<my-pkg-name>>/");

or, better, to avoid handling the "-1" or "-2" suffix after the package name (that changes):

2)

ProcessBuilder pb = new ProcessBuilder();
Map<String, String> envMap = pb.environment();
envMap.put("LD_LIBRARY_PATH", "/data/data/<<my-pkg-name>>/lib");

and to not hard-code /data/data/<<my-pkg-name>>/lib use: Context.getApplicationInfo().nativeLibraryDir

With this solution 2) I found also that System.loadLibrary("lame") it's not needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top