java processbuilder ffmpeg pipe

时间:2015-05-24 10:44:29

标签: java ffmpeg pipe processbuilder

我尝试运行ffmpeg out java。在这里我的代码:

String[] temp = {"ffmpeg\\ffmpeg.exe","-i","input_track.ac3","-threads","0","-af","volume=volume="0.0"dB","-acodec","pcm_s32le","-ac","6","-ar","48000","-f","wav","-","|","ffmpeg\\fdkaac","--ignorelength","-m","1","-o","ouput_track.aac","-"};

ProcessBuilder pb = new ProcessBuilder(temp);
Process p = pb.start();
int ev = 0;
if (p.waitFor() != 0)
{
   ev = p.exitValue();
}

我在windows cmd尝试了这个命令,这里有一个" |"在ffmpeg命令行。

也许有人说我可以吗?

最好的问候

2 个答案:

答案 0 :(得分:1)

此问题类似于How to make pipes work with Runtime.exec()? ...除了它适用于Windows。

问题基本相同:exec方法不了解shell语法,例如管道,输入或输出方向等。解决方案也基本相同:执行适当的shell并获取它来处理shell语法。

在这种情况下,请尝试以下方法:

String[] temp = new String[] {
    "cmd", "/c",
    "ffmpeg\\ffmpeg.exe -i input_track.ac3 -threads 0 " + 
    "-af volume=volume=\"0.0\"dB -acodec pcm_s32le -ac 6 " +
    "-ar 48000 -f wav - | " +
    "ffmpeg\\fdkaac --ignorelength -m 1 -o ouput_track.aac -"
};

请注意,实际命令是单个字符串。 (0.0周围的引号看起来有点奇怪,但这就是你的问题所在。)

答案 1 :(得分:0)

|是一个shell管道符,在java中你必须要在一个命令中运行这个命令(bash -c"整个命令行|在这里"),否则你必须运行两个进程(前一个进程和后一个进程),其中第一个进程的stdout写入第二个进程的stdin。为此,您通常使用redirectOutput(Redirect.PIPE)redirectInput(Redirect.PIPE)