使用Java的ProcessBuilder运行SoX

发布于 2024-11-27 17:12:24 字数 1540 浏览 8 评论 0原文

我正在使用 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,没有这样的文件或目录。我尝试在终端上运行该命令,并且没有问题。如果重要的话,我在 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。

I am running SoX from java using a ProcessBuilder to trim a wav file. I am sure I should be able to run SoX, cause in the other JUnit tests, I manage to successfully run the following commands:

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

but when I try to trim a file as in the following:

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

it throws an IOException with the error: error=2, No such file or directory. I tried running the command on a terminal, and it worked without a problem. If it matters, I ran it through a JUnit test from eclipse, on a macbook.

Here's the code I used to build it in 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();

I also tried the same thing, but using an ArrayList.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2024-12-04 17:12:24

在布兰普评论的帮助下,我自己找到了答案。这个问题很容易解决,首先使用字符串列表,然后分隔需要空格的非破折号前缀参数,例如 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"

Found the answer myself, with lots of help from bramp's comment. The problem is easily resolved by first using a List of Strings, and then by separating the non-dash prefixed arguments that require spaces, like sox's effects.

So from something like:

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");

you get:

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");

I think it may have something to do with how Java sends the arguments, or how SoX receives arguments. I was able to replicate my problem in the terminal by using the following command:

sox/sox test/test.wav -b 16 test/newtest.wav "rate 16000" "channels 1"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文