Chinaunix首页 | 论坛 | 博客
  • 博客访问: 404686
  • 博文数量: 22
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1712
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-09 10:51
文章分类

全部博文(22)

文章存档

2016年(3)

2015年(6)

2014年(1)

2013年(12)

我的朋友

分类: Java

2013-09-09 17:12:38

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

新建一个类StreamGobble类:

 


[java] view plaincopy
  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执行转码

 


[java] view plaincopy
  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;   


阅读(4733) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~