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. }   

package thss.jmencoder; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class StreamGobble extends Thread { InputStream is; String type; StreamGobble(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(); } } }

在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.     } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值