用mencoder转换成.flv后用一般的播放器是可以播放的, 可是使用flvplayer无法播放...
问题方向比较明确.是命令的问题...
正确的转换命令如下, 测试--可以用flvplayer播放.
/**
* 使用mencoder转码
* @param videoPath 源路径 -- 要转换的视频文件
* @param targetPath 目标路径 -- 转换后的视频flv
* @return 返回目标路径
*/
public String mencoderTransVideo() {
List<String> commend = new java.util.ArrayList<String>();
commend.add( "d:\\flv\\MediaCoder\\codecs\\mencoder.exe");
commend.add(videoPath);
commend.add( "-of");
commend.add( "lavf");
commend.add( "-oac");
commend.add( "mp3lame");
commend.add( "-lameopts");
commend.add( "abr:br=56");
commend.add( "-ovc");
commend.add( "lavc");
commend.add( "-lavcopts");
commend.add( "vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=4:cmp=6:vb_strategy=1");
commend.add( "-vf");
commend.add( "scale=512:-3");
commend.add( "-ofps");
commend.add( "12");
commend.add( "-srate");
commend.add( "22050");
commend.add( "-o");
commend.add(ChineseSpelling.getInstance().getSelling(targetPath));
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
/**
* 同时清空mencoder的错误流缓冲区和输出缓冲区(防止线程阻塞--停掉tomcat才能转码)
*/
final InputStream is1 = process.getInputStream();
new Thread( new Runnable() {
public void run() {
BufferedReader br = new BufferedReader( new InputStreamReader(is1));
try {
while(br.readLine() != null) ;
} catch (IOException e) {
e.printStackTrace();
}
}
}).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
InputStream is2 = process.getErrorStream();
BufferedReader br2 = new BufferedReader( new InputStreamReader(is2));
StringBuilder buf = new StringBuilder(); // 保存输出结果流
String line = null;
while((line = br2.readLine()) != null) buf.append(line);
return targetPath;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
* 使用mencoder转码
* @param videoPath 源路径 -- 要转换的视频文件
* @param targetPath 目标路径 -- 转换后的视频flv
* @return 返回目标路径
*/
public String mencoderTransVideo() {
List<String> commend = new java.util.ArrayList<String>();
commend.add( "d:\\flv\\MediaCoder\\codecs\\mencoder.exe");
commend.add(videoPath);
commend.add( "-of");
commend.add( "lavf");
commend.add( "-oac");
commend.add( "mp3lame");
commend.add( "-lameopts");
commend.add( "abr:br=56");
commend.add( "-ovc");
commend.add( "lavc");
commend.add( "-lavcopts");
commend.add( "vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=4:cmp=6:vb_strategy=1");
commend.add( "-vf");
commend.add( "scale=512:-3");
commend.add( "-ofps");
commend.add( "12");
commend.add( "-srate");
commend.add( "22050");
commend.add( "-o");
commend.add(ChineseSpelling.getInstance().getSelling(targetPath));
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process process = builder.start();
/**
* 同时清空mencoder的错误流缓冲区和输出缓冲区(防止线程阻塞--停掉tomcat才能转码)
*/
final InputStream is1 = process.getInputStream();
new Thread( new Runnable() {
public void run() {
BufferedReader br = new BufferedReader( new InputStreamReader(is1));
try {
while(br.readLine() != null) ;
} catch (IOException e) {
e.printStackTrace();
}
}
}).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
InputStream is2 = process.getErrorStream();
BufferedReader br2 = new BufferedReader( new InputStreamReader(is2));
StringBuilder buf = new StringBuilder(); // 保存输出结果流
String line = null;
while((line = br2.readLine()) != null) buf.append(line);
return targetPath;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
本文转自chainli 51CTO博客,原文链接:http://blog.51cto.com/lichen/163836,如需转载请自行联系原作者