我使用ProcessBuilder从java运行SoX来修剪wav文件。我确信我应该能够运行SoX,因为在其他JUnit测试中,我设法成功运行以下命令:
sox/sox --version
sox/sox --i -r test/test.wav
sox/sox --i -D test/test.wav
sox/sox --i -b test/test.wav
sox/sox --i -c test/test.wav
但是当我尝试修剪文件时,如下所示:
sox/sox -V3 "/Users/username/workspace/Thesis Corpus Integrator/test/test.wav" -b 16 "/Users/username/workspace/Thesis Corpus Integrator/test/newWaveFile.wav" channels 1 trim 0:00:00.000 =0:00:30.000
它会引发IOException
错误:error=2, No such file or directory
。我尝试在终端上运行命令,它没有问题。如果重要的话,我在macbook上通过eclipse的JUnit测试运行它。
以下是我在ProcessBuilder中构建它的代码:
StringBuilder command = new StringBuilder(soxCommand) // soxCommand resolves to sox/sox, and is used in all the other tests without any problems
if (WavCutter.getMetadata(srcFile.getAbsolutePath(),
MetadataField.SAMPLE_RATE) != 16000) {
command.append(" -V3");
command.append(" -G");
command.append(" \"" + srcFile.getAbsolutePath() + '\"');
command.append(" -b 16");
command.append(" \"" + destFile.getAbsolutePath() + '\"');
command.append(" channels 1");
command.append(" gain -h");
command.append(" rate 16000");
command.append(" trim");
command.append(" " + startTime.toString());
command.append(" " + '=' + endTime.toString());
Process soxProcess = new ProcessBuilder(command.toString())
.start();
我也尝试了同样的事情,但是使用了ArrayList。
答案 0 :(得分:0)
在bramp评论的帮助下,我自己找到了答案。首先使用字符串列表,然后分离需要空格的非短划线前缀参数,例如sox的effects
,就可以轻松解决问题。
所以来自:
StringBuilder s = new StringBuilder("sox/sox"); // command itself is 'sox'
// everything after this is an argument
s.add(srcFile.getPath());
s.add("-b 16");
s.add(destFile.getPath());
s.add("rate 16000");
s.add("channels 1");
你得到:
ArrayList<String> s = new ArrayList<String>();
s.add("sox/sox"); // command/process
// everything after this is an argument
s.add(srcFile.getPath());
s.add("-b 16");
s.add(destFile.getPath());
s.add("rate"); // notice how I had to split up the rate argument
s.add("16000");
s.add("channels"); // same applies here
s.add("1");
我认为这可能与Java发送参数的方式或SoX如何接收参数有关。我能够使用以下命令在终端中复制我的问题:
sox/sox test/test.wav -b 16 test/newtest.wav "rate 16000" "channels 1"