Android: ffmpeg with filenames containing spaces

2019-03-03 21:56发布

问题:

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,

回答1:

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



回答2:

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