`

java Runtime Process调用可执行文件,子进程阻塞

阅读更多

今天调用mencoder时遇到 Cannot run program " mencoder": CreateProcess error=2, ?????????  查了查,这个可以解决,就拿过来了

我用java调用mencoder实施转码,但是转码过程中出现子进程阻塞,而且还是看了API才知道这个问题的。因为mencoder 的控制台输出信息很多,把缓存区所有的空间占满了,所以程序不能执行后面的程序,mencoder就只能转码28秒的视频,所以需要建立线程及时清空缓存 区。

新建一个类StreamGobble类:

  1. package  thss.jmencoder;   
  2. import  java.io.BufferedReader;   
  3. import  java.io.IOException;   
  4. import  java.io.InputStream;   
  5. import  java.io.InputStreamReader;   
  6. public   class  StreamGobble  extends  Thread {   
  7.     InputStream is;   
  8.     String type;   
  9.   
  10.     StreamGobble(InputStream is, String type) {   
  11.         this .is = is;   
  12.         this .type = type;   
  13.     }   
  14.   
  15.     public   void  run() {   
  16.         try  {   
  17.             InputStreamReader isr = new  InputStreamReader(is);  
  18.             BufferedReader br = new  BufferedReader(isr);   
  19.             String line = null ;   
  20.             while  ((line = br.readLine()) !=  null )   
  21.                 System.out.println(type + ">"  + line);   
  22.         } catch  (IOException ioe) {   
  23.             ioe.printStackTrace();   
  24.         }   
  25.     }   
  26. }   

在main函数中调用Runtime/Process执行转码

  1. String line = value.toString();   
  2. String[] str = line.split(" " );   
  3. String fOutput = null ;   
  4. fOutput = str[3 ] +  ".flv" ;   
  5. Process process = null ;   
  6. try  {   
  7.     Runtime runtime = Runtime.getRuntime();   
  8.     process = runtime .exec("mencoder -ofps 30000/1001 -vf harddup"    
  9.         + " /user/student/data/" +str[ 0 ]   
  10.         + " -ss " +str[ 1 ]+ " -endpos " +str[ 2 ]+ " -sws 2 -of lavf -ovc lavc -lavcopts "    
  11.         + "vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 "    
  12.         + "-nosound -srate 22050 -o /user/student/data/" +fOutput);   
  13.     new  StreamGobble(process.getInputStream(),  "INFO" ).start();   
  14.     new  StreamGobble(process.getErrorStream(),  "ERROR" ).start();   
  15.     int  status = process.waitFor();   
  16.     System.out.println("Process exitValue: "  + status);   
  17.     } catch  (Throwable t) {   
  18.         t.getStackTrace();   
  19.     } finally  {   
  20.         if  (process ==  null )   
  21.             process.destroy();   
  22.         process = null ;   
  23.     } 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics