在java(windows)中创建fifo管道,将一些数据写入其中,让其他进程读取管道

时间:2017-11-09 14:47:15

标签: java ffmpeg named-pipes mkfifo

我的目标是在windows(java)中创建一个命名管道(fifo),将一些数据(来自摄像头)写入其中并调用ffmpeg命令从该数据生成mp4。但我怀疑它不是打开一个fifo管道,而是打开一个文件。如何让它打开一个fifo管? 这是代码:

public void run () {
String mp4Folder = "C://Users/user_2/Desktop/streamDestination";            // Where the MP4 is to be kept
VFrame frame = null;
int actualPipeValLen = 0;
FileOutputStream requestStream = null;
long lastProcessedTS = 0;
final String FIFOPATH = "D://FIFO//";
final String PIPE_NAME = FIFOPATH + "myfifo";
final String MKFIFOCOMMAND = "mkfifo -m=rw " + PIPE_NAME;
final String DELFIFOCOMMAND = "rm -r " + PIPE_NAME;
String mp4FileName = mp4Folder + File.separator + "1.mp4";
mp4FileName = mp4FileName.replace("\\", "/");
long firstTimeStamp = 0;
try {
    Runtime.getRuntime().exec(MKFIFOCOMMAND);
} catch (IOException e1) {
    e1.printStackTrace();
}
if(firstTimeStamp == 0) {
    firstTimeStamp = frame.getTimestamp();
}
if((frame.getTimestamp() - firstTimeStamp) > (15 * 1000)) {
    if(requestStream != null) {
        requestStream.close();
        requestStream = null;
    }
    if(actualPipeValLen > 0) {
        String[] ffmpeg = new String[] {"ffmpeg", "-i", PIPE_NAME , "-vcodec", "copy", mp4FileName };
        Process ffmpegProcess = Runtime.getRuntime().exec(ffmpeg);
        actualPipeValLen = 0;
        firstTimeStamp = lastProcessedTS;
        Thread.sleep(2 * 1000);
        try {
            Runtime.getRuntime().exec(DELFIFOCOMMAND);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        System.exit(0);
    }
} else {
    System.out.println("Writing into pipe : " + actualPipeValLen);
    if(requestStream == null) {
        requestStream = new  FileOutputStream(PIPE_NAME);
    }
    requestStream.write(frame.getFrame());
    actualPipeValLen += frame.getFrame().length;
    lastProcessedTS = frame.getTimestamp();
}

}

0 个答案:

没有答案