我这里是用perl调用ffmpeg和mecoder命令,java操作数据库和控制线程调用perl进行转换的。说也说不清,我还是直接拿源码来说吧:
1、covert.pl
#!/usr/bin/perl
my $encoder = $ARGV[0];//java传过来的参数,编码命令,如:/usr/local/ffmepg
my $fileIn = $ARGV[1];//java传过来的参数,要转换的源文件路径
my $fileOut = $ARGV[2];//java传过来的参数,转换后的文件路径
my $logPath = $ARGV[3];//java传过来的参数,日志路径
my $localTime = localtime;//当前时间
my $cmd;
my $coder = substr($encoder,rindex($encoder,"/")+1);
if($coder eq "ffmpeg"){
$cmd = $encoder." -i ".$fileIn." -ab 64 -acodec mp3 -ac 1 -ar 22050 -b 230 -r 29.97 -y ".$fileOut;//如果转换命令是ffmpeg,调用该命令转换
}
else{//否则调用该命令用ffmpeg转换
$cmd = $encoder." -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -ovc lavc -lavcopts vcodec=flv:vbitrate=500 -srate 22050 -oac lavc -lavcopts acodec=mp3:abitrate=56 -ffourcc FLV1 -oac mp3lame ".$fileIn." -o ".$fileOut;
}
`echo $localTime: $cmd >> $logPath`;//把命令内容写到日志
`$cmd`;//执行转换命令
2、CovertVideo.java
ffmpeg转换flv:
public synchronized static boolean ffmpegToFlv(String fileIn, String fileOut) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String cmdFile = Configuration.getInstance()//从配置文件获取perl文件路径
.getConfigValue("path.perl")
+ "covert.pl";
String encoder = Configuration.getInstance().getConfigValue(//从配置文件获取命令行路径
"commend.ffmpeg");
String logPath = Configuration.getInstance().getConfigValue(//日志路径
"stdout.path")
+ format.format(new Date()) + ".txt";
StringBuffer cmd = new StringBuffer("/usr/bin/perl ").append(cmdFile)
.append(" ").append(encoder).append(" ").append(fileIn).append(
" ").append(fileOut).append(" ").append(logPath);
System.out.print(cmd.toString());
String line = null;
InputStream stderr = null;
InputStreamReader isr = null;
BufferedReader br = null;
Runtime rt = null;
boolean success = false;
try {
rt = Runtime.getRuntime();
Process proc = rt.exec(cmd.toString());//执行命令,调用perl进行转换
stderr = proc.getErrorStream();
isr = new InputStreamReader(stderr);
br = new BufferedReader(isr);
System.out.println("<ffmpegToFlv>");
while ((line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ffmpegToFlv>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
File filePath = new File(fileOut);
// 如果文件存在,并且长度不为0,则表示转换成功.
success = filePath.exists() && filePath.length() > 0;
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
stderr.close();
isr.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
rt.exit(1);
}
}
return success;
}
mencoder转换flv跟上面几乎一样,不写注释了:
public synchronized static boolean mencoderToFlv(String fileIn,
String fileOut) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String cmdFile = Configuration.getInstance()
.getConfigValue("path.perl")
+ "covert.pl";
String encoder = Configuration.getInstance().getConfigValue(
"commend.mencoder");
String logPath = Configuration.getInstance().getConfigValue(
"stdout.path")
+ format.format(new Date()) + ".txt";
StringBuffer cmd = new StringBuffer("/usr/bin/perl ").append(cmdFile)
.append(" ").append(encoder).append(" ").append(fileIn).append(
" ").append(fileOut).append(" ").append(logPath);
System.out.print(cmd.toString());
String line = null;
InputStream stderr = null;
InputStreamReader isr = null;
BufferedReader br = null;
Runtime rt = null;
boolean success = false;
try {
rt = Runtime.getRuntime();
Process proc = rt.exec(cmd.toString());
stderr = proc.getErrorStream();
isr = new InputStreamReader(stderr);
br = new BufferedReader(isr);
System.out.println("<mencoderToFlv>");
while ((line = br.readLine()) != null)
System.out.println(line);
System.out.println("</mencoderToFlv>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
File filePath = new File(fileOut);
// 如果文件存在,并且长度不为0,则表示转换成功.
success = filePath.exists() && filePath.length() > 0;
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
stderr.close();
isr.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
rt.exit(1);
}
}
return success;
}
程序已经编译通过的,配置文件config.properties需要放到web服务的WEB-INF/classes目录下,只需按实际情况把配置文件里面的路径修改成你自己的就可以用了。
本文转自 OldHawk 博客园博客,原文链接:http://www.cnblogs.com/taobataoma/archive/2007/10/12/921990.html,如需转载请自行联系原作者