我用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;
阅读(4824) | 评论(0) | 转发(0) |