2014-01-23 24 views
0

在這裏,我想編碼直播RTMP流使用的avconv工具Libav從Flash Media Server未來和廣播低比特率流後停止編碼。 Libav安裝在Ubuntu操作系統上。編碼流僅爲8分鐘。由於avconv工具是通過使用java運行時環境啓動的。 Java代碼在下面給出 -avconv工具8分鐘

public class RunnableStream implements Runnable 
    { 
     String inStream,outStream,width,height,bitRate,frameRate,fname,line,ar,audioBitRate,audioChannel; 
     public RunnableStream(String fname,String inStream,String outStream,String ar,String audioBitRate,String audioChannel,String width,String height,String bitRate,String frameRate) 
     { 
      this.fname=fname; 
      this.inStream=inStream; 
      this.outStream=outStream; 
      this.width=width; 
      this.height=height; 
      this.bitRate=bitRate; 
      this.frameRate=frameRate; 
      this.ar=ar; 
      this.audioBitRate=audioBitRate; 
      this.audioChannel=audioChannel; 

     } 

     public void run() { 
      Process pr; 
      try { 
       pr = Runtime.getRuntime().exec("avconv -async 15 -i "+inStream+" -shortest -s "+width +"*"+height +" -r " +frameRate+" -b:v "+bitRate+" -ab "+audioBitRate+" -ac "+audioChannel+" -ar "+ar+" -f flv "+outStream); 

       InputStream in1 = pr.getInputStream(); 
       InputStream in = pr.getErrorStream(); 
    int c1; 
    while ((c1 = in1.read()) != -1) 
    { 
     System.out.print((char)c1); 
    } 

    int c; 
    while ((c = in.read()) != -1) 
    { 
     System.out.print((char)c); 
    } 
    pr.waitFor(); 
    in.close(); 
    in1.close(); 

      }catch(Exception e){e.printStackTrace();} 
     } 
    } 

但是,當相同的編碼方案或命令被直接施加到命令提示那麼它可以爲atleast1小時運行。下面給出命令行語句 -

avconv -async 15 -i rtmp://IP/live/streamname -shortest -s 176*144 -r 10 -b:v 56k -ab 12k -ac 1 -ar 22050 -f flv rtmp://IP/live/streamname2 
+0

我明白爲時已晚,但也許這個項目將是對您有用:https://github.com/vbauer/avconv4java –

回答

1

我假定這段代碼是爲了從工藝/標準錯誤排水標準輸出:

int c1; 
while ((c1 = in1.read()) != -1) 
{ 
    System.out.print((char)c1); 
} 

int c; 
while ((c = in.read()) != -1) 
{ 
    System.out.print((char)c); 
} 

不幸的是,它實際上只從in1(標準輸出)讀取,直到過程已完成,,然後它將從in(stderr)中讀取。這意味着,如果進程將更多數據寫入標準錯誤緩衝區而不是緩衝區可容納的數據,它將阻止 - 正在展示您所看到的行爲。那不是肯定是的原因,但似乎對我很有幫助。

你應該從不同的線程讀取這些流,基本上 - 這樣你就可以從這兩個流中讀取流而不必等待流程完成。

+0

感謝您Sir.On假設的基礎上,我讀兩個兩個流不同的線程導致下面的答案。一定建議是否有缺陷被覆蓋。 –

+0

@YuvrajKakkar:只是我不確定這是值得發佈的答案,沒有任何解釋性文字。 (你的例外「處理」在一般情況下並不理想,但這是另一回事。)然而,重要的是它是否能解決問題 - 是嗎? –

+0

先生,我要測試它至少2小時。然後我會讓你知道,但直到30分鐘視頻纔可以接受 –

0
public class RunnableStream implements Runnable 
    { 
     String inStream,outStream,width,height,bitRate,frameRate,fname,line,ar,audioBitRate,audioChannel; 
     public RunnableStream(String fname,String inStream,String outStream,String ar,String audioBitRate,String audioChannel,String width,String height,String bitRate,String frameRate) 
     { 
      this.fname=fname; 
      this.inStream=inStream; 
      this.outStream=outStream; 
      this.width=width; 
      this.height=height; 
      this.bitRate=bitRate; 
      this.frameRate=frameRate; 
      this.ar=ar; 
      this.audioBitRate=audioBitRate; 
      this.audioChannel=audioChannel; 

     } 

     public void run() { 
      Process pr; 
      try { 
       pr = Runtime.getRuntime().exec("avconv -async 15 -i "+inStream+" -shortest -s "+width +"*"+height +" -r " +frameRate+" -b:v "+bitRate+" -ab "+audioBitRate+" -ac "+audioChannel+" -ar "+ar+" -f flv "+outStream); 

      StreamGobbler errorGobbler = new StreamGobbler(pr.getErrorStream(), "ERROR");    

       StreamGobbler outputGobbler = new StreamGobbler(pr.getInputStream(), "OUTPUT"); 

       errorGobbler.start(); 
       outputGobbler.start(); 

       int exitVal = pr.waitFor(); 
       System.out.println("ExitValue: " + exitVal); 

      }catch(Exception e){e.printStackTrace();} 
     } 


    } 


    class StreamGobbler extends Thread { 
     InputStream is; 
     String type; 

     StreamGobbler(InputStream is, String type) { 
      this.is = is; 
      this.type = type; 
     } 

     public void run() { 
      try { 
       InputStreamReader isr = new InputStreamReader(is); 
       BufferedReader br = new BufferedReader(isr); 
       String line=null; 
       while ((line = br.readLine()) != null) 
        System.out.println(type + ">" + line);  
       } catch (IOException ioe) 
        { 
        ioe.printStackTrace(); 
        } 
     } 
    } 
}