用fms2做流媒体服务器,就需要把所有用户上传的各种视频转换成flv格式,这种格式的文件容量很小,比较适合远程播放。用ffmpeg和mencoder已经足以把绝大部分视频转换成flv了。
我这里是用perl调用ffmpeg和mecoder命令,java操作数据库和控制线程调用perl进行转换的。说也说不清,我还是直接拿源码来说吧:
1、covert.pl
#!/usr/bin/perl
- my $encoder = $ARGV[0];
- my $fileIn = $ARGV[1];
- my $fileOut = $ARGV[2];
- my $logPath = $ARGV[3];
- 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;
- }
- else{
- $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()
- .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());
- 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);
-
- 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);
-
- 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目录下,只需按实际情况把配置文件里面的路径修改成你自己的就可以用了。
(xiayuxiao78) |