2017-05-07 24 views
0

我有一個「VidCamera」類,它基本上運行錄製視頻的「raspivid」命令工具。用於從該類記錄視頻的方法,看起來像這樣:RPi相機和Java:如何檢查視頻錄製是否已完成?

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視頻錄製仍然在另一個線程中忙碌。

我該如何解決這個問題?當命令行工具的視頻錄製準備就緒時,如何知道我的代碼?

+0

一個解決方案是找出另一個命令行是否相機仍然「忙」。但我無法找到任何指令。任何人有想法? –

回答

0

好,一些研究和一些試驗和錯誤之後,我結束了創建這段代碼檢查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進程是否仍然存在。如果是,該命令將拋出一行響應文本。如果該過程已完成,則不會有任何回覆文本。通過解釋此響應,此代碼塊可以返回錯誤或真實的語句。

歡迎對此解決方案提供任何反饋!