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

转:http://blog.csdn.net/canedy/article/details/6005153

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

新建一个类StreamGobble类:

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执行转码

String line = value.toString(); 
String[] str = line.split(" "); 
String fOutput = null; 
fOutput = str[3] + ".flv"; 
Process process = null; 
try { 
    Runtime runtime = Runtime.getRuntime(); 
    process = runtime .exec("mencoder -ofps 30000/1001 -vf harddup" 
        + " /user/student/data/"+str[0] 
        + " -ss "+str[1]+" -endpos "+str[2]+" -sws 2 -of lavf -ovc lavc -lavcopts " 
        + "vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 " 
        + "-nosound -srate 22050 -o /user/student/data/"+fOutput); 
    new StreamGobble(process.getInputStream(), "INFO").start(); 
    new StreamGobble(process.getErrorStream(), "ERROR").start(); 
    int status = process.waitFor(); 
    System.out.println("Process exitValue: " + status); 
    } catch (Throwable t) { 
        t.getStackTrace(); 
    } finally { 
        if (process == null) 
            process.destroy(); 
        process = null; 
    }
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值