0

I want to execute ffmpeg from an Android app, very much as described here: Using FFmpeg with Android-NDK.

Executing the following commands work fine:

Process p = Runtime.getRuntime().exec("/data/data/yourpackagename/ffmpeg -i infile.mp4 outfile.mp4");

or

Process p = Runtime.getRuntime().exec(new String[]{"/data/data/yourpackagename/ffmpeg", "-i", "infile.mp4", "outfile.mp4");

But when the input or output filenames contain spaces ffmpeg fails with a "File not found" error:

Process p = Runtime.getRuntime().exec("/data/data/yourpackagename/ffmpeg -i \"in space file.mp4\" outfile.mp4"); // fails
Process p = Runtime.getRuntime().exec("/data/data/yourpackagename/ffmpeg -i 'in space file.mp4' outfile.mp4"); // fails
Process p = Runtime.getRuntime().exec(new String[]{"/data/data/yourpackagename/ffmpeg", "-i", "\"in space file.mp4\"", "outfile.mp4"); //fails

FFMPEG also fails for files without spaces but quoted:

Process p = Runtime.getRuntime().exec("/data/data/yourpackagename/ffmpeg -i \"infile.mp4\" outfile.mp4"); // fails

How can I correctly pass a filename containing spaces to the ffmpeg command?

Regards,

Community
  • 1
  • 1
Hyndrix
  • 4,282
  • 7
  • 41
  • 82
  • Maybe change the spaces by : %20 – An-droid May 16 '14 at 14:14
  • I have extended my question: because ffmpeg also fails if I quote files (even if they do not have spaces) so something seems to go wrong during passing the arguments to ffmpeg from java. – Hyndrix May 16 '14 at 14:20

2 Answers2

0

Wild guess , but you may want the java process to pass "\ " read "backslash space" to the underlying OS when you want ffmpeg to open a file with a name containing a space.

Its not what the java does with the string fileName. Its what the underlying OS and ffmpeg process receives that it will use as 'filename'. There you want "foo bar.input" to be

"foo\ bar.input"

So, the underlying expression "ffmpeg -i foo\ bar.input" should work.

you just need to figure out how to construct your java strings so that the process feeds the above expression to the NDK / process layer running the "ffmpeg" command.

more here

Community
  • 1
  • 1
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43
  • Thanks for the hint. I already tried this, but when I e.g. pass ``in\ space\ file.mp4`` then ffmpeg complains that it cannot open "in\" (it truncates all after this). – Hyndrix May 16 '14 at 15:48
  • are you on a rooted phone? if yes then you can get a shell and directly try out all these permutations until you find one that works. THen, u have to recreate that shell expression in your java/process wrapper... – Robert Rowntree May 16 '14 at 16:12
  • Both commands (quoted and backslashed) work fine from the command line. – Hyndrix May 17 '14 at 05:48
0

I finally got it. I forgot one possibility, and exactly this one is working:

Process p = Runtime.getRuntime().exec(new String[]{"/data/data/yourpackagename/ffmpeg", "-i", "in space file.mp4", "outfile.mp4"); //OK
Hyndrix
  • 4,282
  • 7
  • 41
  • 82
  • 1
    i am trying to convert video ,where user can choose from gallary but m facing issue that uri contain space like "/storage/emulated/0/WhatsApp/Media/WhatsApp Video/VID-20150108-WA0008.mp4" and vk.run(GeneralUtils.utilConvertToComplex(commandStr), workFolder, getApplicationContext()); this command not working can you plz help me out whats issue in uri path and how we can fix it. – Parag Chauhan Jan 27 '15 at 19:38
  • Just a guess, but you can try to urlencode the url before passing it. – Hyndrix Jan 28 '15 at 06:21