通过java中的管道通过NC发送视频

时间:2013-09-10 12:23:47

标签: java runtime pipe raspberry-pi netcat

我在控制台上有以下工作:

COMPUTER LINUX: nc -l -p 5012 | mplayer -fps 31 -cache 1024 -
RPI:  raspivid -t 2000 -o - | nc 192.168.0.5 5012

但是如果我尝试用Java包装它就行不通了。它没有崩溃,它一直运行到程序结束时没有任何反应:

public void video() {
    try {
        String[] cmds = {"/bin/sh", "-c", "nc -l -u 5012 | mplayer -fps 31 -cache 1024 -"};
        Process videoProcess = Runtime.getRuntime().exec(cmds);
        adder.streamVideo(2000);
     catch (IOException e) {
    System.out.println("IOE");
    e.printStackTrace();
    }
}

其中adder.streamVideo()调用RPI代码。

public boolean streamVideo(int streamDuration) {
    String[] cmd = {"/bin/sh", "-c",
            "raspivid -t " + streamDuration + " -o - | nc 192.168.0.5 5012"};
    try {
        Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("Video streaming failed");
        e.printStackTrace();
    }
    return true;
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

exec()不会召唤一个shell,因此管道|和输出/输入重定向> and <之类的所有外壳都不起作用。但你的问题是可以解决的,参见例如https://stackoverflow.com/a/5928316/2536029