简体   繁体   中英

FFmpeg Using Java Runtime to Make Video From PNG on Mac with Spaces in Directory Names

I have run into a problem on Mac OSX where I'm trying to make video from PNG's such as:

String command = "ffmpeg -r 30 -i " + outputFileName + fileName + "_%d.png -i "+
    outputFileName + fileName + ".wav -r 30 -pix_fmt yuv420p " +
    outputFileName + videoName;

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);

Unfortunately, I don't have any control over these filenames and they come to me with spaces in them. On Windows, I simply surround the command with quotes and all works as expected. However, on Mac, I tried that and many other things to no avail. Once a space is encountered in the filename, FFmpeg reports that the file cannot be found and truncates the string up to the point of the first space. When I surround the filename with quotes, FFmpeg tells me it can't find the file and shows me the quotes around the file. Such as:

String command = "ffmpeg -r 30 -i \" + outputFileName...\"

Assume the filename is:

/Users/me/Application Folder/...

FFmpeg will report: "/Users/me/Application: No such file or directory.

Without the quotes, it will error with: /Users/me/Application: No such file or directory.

I have tried every escape sequence I can think of. I've also tried putting the escape characters only in the file name where the spaces occur such as:

outputFileName = outputFileName.replace(" ", "\" \"");

In that instance, FFmpeg says: /Users/me/Application": No such file or directory.

Along with escaping with quotes, I tried:

outputFileName = outputFileName.replace(" ", "\\");

I also tried:

outputFileName = outputFileName.replace(" ", "\\ ")

In those cases, I get: /Users/me/Application: No such file or directory.

[Edit: in the preview I see the backslash at the end of application but in the final post it's not there... there's a backslash you should see such as '/.../Application\\']

Can someone tell me how to get FFmpeg to run my command when the directory name has spaces in it? I have no control over the directory names when they come to me. Java is able to find these directories just fine without any sort of escape characters in the directory name. What do I have to do to get FFmpeg to do the same?

Thanks for your help.

Use ProcessBuilder instead. Also, don't use one long command string. Break your command up into individual arguments:

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM