2013-12-20 46 views
2

我建設的ffmpeg()一個簡單的用戶界面使用EXEC推出ffmpeg.exe與參數後卡住。它適用於OS X,但在Windows 7,8幾秒鐘後ffmpeg的過程自動暫停,當我殺死父親過程只恢復。 (也ddlhost.exe創建) Ffmpeg.exe從CMD成功轉換相同的視頻。Java運行時的exec()獲得,而

在互聯網上搜索,我發現這個answer但我有運行未使用的輸入和錯誤流一個簡單的測試程序同樣的問題。

這裏是一個有主要的一個同樣的問題:測試程序代碼:}

public class Main { 

static String param_ffmpeg_1 = "./data/ffmpeg.exe"; 
static String param_ffmpeg_2 = "-i"; 

static String in = "./data/source.mov"; 
static String out = "./data/out.flv"; 

static Process p; 

public static void main(String[] args) { 

    /*File f = new File(out); 

    if (f.exists()){ 
     f.delete(); 
    }*/ 


    Runtime rt = Runtime.getRuntime() ; 
    //String cmd1 = param_ffmpeg_1 + param_ffmpeg_2 + in_path + param_ffmpeg_3 + out_path ; 
    System.out.println(in); 
    System.out.println(out); 
    String[] cmd1 = new String[] { param_ffmpeg_1, param_ffmpeg_2, in, "-ar", "44100", "-vb", "2500k", "-s", "882x496", "-f", "flv", out}; 

    try { 
     p = rt.exec(cmd1); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    int r = 123456; 
    try { 
     r = p.waitFor(); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    System.out.println(r); 


} 

回答

2

是否ffmpg寫東西到標準輸出或標準錯誤?如果是,你必須消費。 (在單獨的線程,因爲你需要消耗stderr和並行標準輸出)查看詳情Process.getInputStream()Process.getErrorStream()。當緩衝區已滿時,被調用的程序將停止並掛起。

它在OS/X而不是Windows中工作的事實可能是由不同的緩衝區大小引起的。

3

你應該叫getInputStream()getErrorStream()Runtime.exec和排水,所有的時間返回的Process該進程正在運行。如果你不這樣做,它最終會填滿並阻止並停止運行。

例子可見Java Process with Input/Output Stream

the accepted answer

ProcessBuilder builder = new ProcessBuilder("/bin/bash"); 
builder.redirectErrorStream(true); 
Process process = builder.start(); 
InputStream itsOutput = process.getInputStream(); 
// Wrap the stream in a Reader ... 
while ((line = reader.readLine()) != null) { 
    System.out.println ("Stdout: " + line); 
}