-->

Java ProcessBuilder: space within quotation marks

2019-07-04 05:35发布

问题:

I am using ProcessBuilder to run FFMPEG to convert and label some of my MP3-Files.

Manually using the following in a .bat file works as expected:

"E:\Dokumente\workspace\MusicBot\ffmpeg\bin\ffmpeg.exe" 
    -i "The Glitch Mob - We Can Make The World Stop.mp4" 
    -metadata author="The Glitch Mob" 
    -metadata title="We Can Make The World Stop" 
    -ab 320k "mob.mp3"

Now what i am trying to achieve using java's ProcessBuilder

ProcessBuilder pr = new ProcessBuilder(FFMPEG_PATH, 
    "-i", target.getAbsolutePath(),
    "-metadata", "title=\"We Can Make The World Stop\"", 
    "-metadata", "author=\"The Glitch Mob\"", 
    "-ab", "320k", 
    tar.getAbsolutePath());

results in a [NULL @ 000000000032f680] Unable to find a suitable output format for 'Can'. Using title and author without spaces in them works, however.

回答1:

The double quotes on the command line are there to tell the shell interpreter not to split your string into multiple parameters. This is to ensure that the application receives title=We Can Make The World Stop as a single argument.

Since ProcessBuilder handles multiple command line arguments explicitly, there's no need for escaping whitespace when calling it.