我正在使用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它抛出一个错误为:error=2, No such file or directory的IOException。我尝试在终端上运行该命令,它工作起来没有任何问题。如果重要的话,我在一台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。
发布于 2011-08-03 22:54:56
在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"https://stackoverflow.com/questions/6927104
复制相似问题