0

Whenever I issue the following command no "intermediate.ts" output file is created:

getRuntime.exec("ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts")

Even when using a String[] like this:

String[] cmd = {
"sh",
"-c",
"./ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts"
}

But when I open terminal emulator and write the same thing I get the correct output I'm looking for. Through Java I can get output from:

ffmpeg -help 

But cannot create files using FFMpeg and Java's getRuntime.exec(). What am I doing wrong?

MarcusC
  • 15
  • 4

1 Answers1

1

When you run ffmpeg in the context of an Android app, its working directory is the root of the file system, and it is mounted read-only. You can specify full path to the output file, or use exec(cmd, null, dir). The output file must go to a directory to which your app has write permissions, e.g. myActivity.getFilesDir() or myPackageInfo.applicationInfo.dataDir.

You don't need to run ffmpeg through sh, but you must give full path to the executable, or specify PATH through the second argument of Runtime.exec()

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • I've been providing the full path for both ffmpeg and the output file using Runtime.exec(string). The output directory I'm using is getFilesDir(). I tried using Runtime.exec(cmd, null, wrkdir) where cmd is the full path to ffmpeg and wrkdir is getFilesDir() but just get an io exception. I'm not exactly sure what I'm doing wrong. Any suggestions? – MarcusC Mar 20 '17 at 19:31
  • Let us try some other command line instead of ffmpeg, e.g. `/system/sh -c echo abc >def` – Alex Cohn Mar 20 '17 at 20:26
  • `/system/bin/echo abc >def` works just fine. I can also create an empty file using `/system/bin/touch ../../storage/emulated/0/android/data/com.my_package/files/sampleJava.txt` without any problem. However, using the aforementioned path when invoking ffmpeg fails except when using the shell terminal emulator on my phone. My ffmpeg binary is located in `data/data/com.my_package` if that makes any difference. – MarcusC Mar 21 '17 at 09:56
  • You wrote that you do receive output from `ffmpeg --help`. But maybe you did not test this in the context of your app. I don't know how you got **ffmpeg** binary in `/data/data/com.my_package` but make sure that the permissions are correct: you need read and execute by the user that is assigned by Android system to your app. The easiest way to have it all right and automated is to [take advantage of the standard JNI library packaging](http://stackoverflow.com/a/17384650/192373). – Alex Cohn Mar 21 '17 at 10:17