【问题标题】:RPi Camera and Java: how to check whether video recording has been completed?RPi Camera and Java:如何检查视频录制是否完成?
【发布时间】:2017-10-05 08:37:04
【问题描述】:

我有一个“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录像还在另一个线程忙。

我该如何解决这个问题?如何让我的代码知道命令行工具的视频录制何时准备就绪?

【问题讨论】:

  • 一种解决方案是使用另一个命令行来确定相机是否仍然“忙碌”。但我找不到任何命令。有人有想法吗?

标签: java command raspberry-pi3


【解决方案1】:

好的,经过一些研究和反复试验,我最终创建了这段代码来检查 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 进程是否仍然存在。如果是,该命令将抛出一行响应文本。如果该过程已完成,则不会有响应文本。通过解释这个响应,这段代码可以返回一个假或真语句。

欢迎对此解决方案提供任何反馈!

【讨论】:

    猜你喜欢
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多