我有一个"VidCamera“类,它基本上运行"raspivid”命令工具来录制视频。从该类录制视频的方法如下所示:
public void recordVideo(String filename, int duration) throws IOException {
setFilename(filename);
setDuration(duration);
try {
String cmdline = getCmdLine();
Process sysprocess = Runtime.getRuntime().exec(cmdline);
}
catch (IOException e) {
System.out.println("Exception while recording video.");
e.printStackTrace();
}
}getCmdLine()方法被构造为输出类似于:raspivid -w 640 -h 480 -fps 60 - t 5000 -o testvideo.h264的内容。
视频录制工作如预期,因为它只是使用命令行工具。但是,我的代码不知道视频何时完成录制。
例如,当我在我的主类中测试视频录制时:
try {
camcorder.recordVideo("myVideoTest.h264", 5000);
System.out.println("Video recording completed.");
}
catch (Exception e) {
System.out.println(e);
}然后显示文字“视频录制完成”。在调用recordVideo()方法后立即打印。在后台,5s视频录制仍在另一个线程中忙碌。
我该如何解决这个问题呢?如何让我的代码知道来自命令行工具的视频录制何时准备就绪?
发布于 2017-05-13 20:34:13
好的,经过一些研究和一些尝试和错误,我最终创建了这段代码来检查raspivid进程的状态:
public boolean getRecordingStatus() throws IOException {
// Check from command line tool if process is still running.
boolean response = true;
try {
String cmdResponseLine;
String[] cmd = {"/bin/sh", "-c", "ps -e | grep raspivid"};
// Feed the pipe with a String array, does not work with String only.
Process sysprocess = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(sysprocess.getInputStream()));
if ((cmdResponseLine = in.readLine()) != null)
response = true;
else
response = false;
in.close();
}
catch (Exception e) {
System.out.println("Exception while checking for raspivid process...");
e.printStackTrace();
}
return response;
}使用"ps -e | grep raspivid“命令,代码检查raspivid进程是否仍然有效。如果是,该命令将抛出一行响应文本。如果该过程已完成,则不会有响应文本。通过解释此响应,此代码块可以返回false或true语句。
欢迎对此解决方案的任何反馈!
https://stackoverflow.com/questions/43829062
复制相似问题