织梦CMS - 轻松建站从此开始!

罗索

linux下的fms2流媒体服务器搭建六部曲之四-格式转换篇

落鹤生 发布于 2011-05-09 22:32 点击:次 
用fms2做流媒体服务器,就需要把所有用户上传的各种视频转换成flv格式,这种格式的文件容量很小,比较适合远程播放。用ffmpeg和mencoder已经足以把绝大部分视频转换成flv了。
TAG:

用fms2做流媒体服务器,就需要把所有用户上传的各种视频转换成flv格式,这种格式的文件容量很小,比较适合远程播放。用ffmpeg和mencoder已经足以把绝大部分视频转换成flv了。

我这里是用perl调用ffmpeg和mecoder命令,java操作数据库和控制线程调用perl进行转换的。说也说不清,我还是直接拿源码来说吧:

1、covert.pl

 #!/usr/bin/perl

  1. my $encoder = $ARGV[0];//java传过来的参数,编码命令,如:/usr/local/ffmepg 
  2. my $fileIn = $ARGV[1];//java传过来的参数,要转换的源文件路径 
  3. my $fileOut = $ARGV[2];//java传过来的参数,转换后的文件路径 
  4. my $logPath = $ARGV[3];//java传过来的参数,日志路径 
  5. my $localTime = localtime;//当前时间 
  6. my $cmd; 
  7.  
  8. my $coder = substr($encoder,rindex($encoder,"/")+1); 
  9. if($coder eq "ffmpeg"){ 
  10.  $cmd = $encoder." -i ".$fileIn." -ab 64 -acodec mp3 -ac 1 -ar 22050 -b 230 -r 29.97  -y "
  11. .$fileOut;//如果转换命令是ffmpeg,调用该命令转换 
  12. else{//否则调用该命令用ffmpeg转换 
  13.  $cmd = $encoder." -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames
  14.  -ovc lavc -lavcopts vcodec=flv:vbitrate=500 -srate 22050 -oac lavc -lavcopts
  15.  acodec=mp3:abitrate=56 -ffourcc FLV1 -oac mp3lame ".$fileIn." -o ".$fileOut; 
  16.  
  17. `echo $localTime: $cmd >> $logPath`;//把命令内容写到日志 
  18. `$cmd`;//执行转换命令 

2、CovertVideo.java

ffmpeg转换flv:

  1. public synchronized static boolean ffmpegToFlv(String fileIn, String fileOut) { 
  2.    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
  3.   String cmdFile = Configuration.getInstance()//从配置文件获取perl文件路径 
  4.     .getConfigValue("path.perl"
  5.     + "covert.pl"
  6.   String encoder = Configuration.getInstance().getConfigValue(//从配置文件获取命令行路径 
  7.     "commend.ffmpeg"); 
  8.   String logPath = Configuration.getInstance().getConfigValue(//日志路径 
  9.     "stdout.path"
  10.     + format.format(new Date()) + ".txt"
  11.   StringBuffer cmd = new StringBuffer("/usr/bin/perl ").append(cmdFile) 
  12.     .append(" ").append(encoder).append(" ").append(fileIn).append( 
  13.       " ").append(fileOut).append(" ").append(logPath); 
  14.   System.out.print(cmd.toString()); 
  15.   String line = null
  16.   InputStream stderr = null
  17.   InputStreamReader isr = null
  18.   BufferedReader br = null
  19.   Runtime rt = null
  20.   boolean success = false
  21.   try { 
  22.    rt = Runtime.getRuntime(); 
  23.    Process proc = rt.exec(cmd.toString());//执行命令,调用perl进行转换 
  24.    stderr = proc.getErrorStream(); 
  25.    isr = new InputStreamReader(stderr); 
  26.    br = new BufferedReader(isr); 
  27.    System.out.println("<ffmpegToFlv>"); 
  28.    while ((line = br.readLine()) != null
  29.     System.out.println(line); 
  30.    System.out.println("</ffmpegToFlv>"); 
  31.    int exitVal = proc.waitFor(); 
  32.    System.out.println("Process exitValue: " + exitVal); 
  33.    File filePath = new File(fileOut); 
  34.    // 如果文件存在,并且长度不为0,则表示转换成功. 
  35.    success = filePath.exists() && filePath.length() > 0
  36.   } catch (Throwable t) { 
  37.    t.printStackTrace(); 
  38.   } finally { 
  39.    try { 
  40.     stderr.close(); 
  41.     isr.close(); 
  42.     br.close(); 
  43.    } catch (Exception e) { 
  44.     e.printStackTrace(); 
  45.     rt.exit(1); 
  46.    } 
  47.   } 
  48.   return success; 
  49.  } 

mencoder转换flv跟上面几乎一样,不写注释了:

  1. public synchronized static boolean mencoderToFlv(String fileIn, 
  2.    String fileOut) { 
  3.   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
  4.   String cmdFile = Configuration.getInstance() 
  5.     .getConfigValue("path.perl"
  6.     + "covert.pl"
  7.   String encoder = Configuration.getInstance().getConfigValue( 
  8.     "commend.mencoder"); 
  9.   String logPath = Configuration.getInstance().getConfigValue( 
  10.     "stdout.path"
  11.     + format.format(new Date()) + ".txt"
  12.   StringBuffer cmd = new StringBuffer("/usr/bin/perl ").append(cmdFile) 
  13.     .append(" ").append(encoder).append(" ").append(fileIn).append( 
  14.       " ").append(fileOut).append(" ").append(logPath); 
  15.   System.out.print(cmd.toString()); 
  16.   String line = null
  17.   InputStream stderr = null
  18.   InputStreamReader isr = null
  19.   BufferedReader br = null
  20.   Runtime rt = null
  21.   boolean success = false
  22.   try { 
  23.    rt = Runtime.getRuntime(); 
  24.    Process proc = rt.exec(cmd.toString()); 
  25.    stderr = proc.getErrorStream(); 
  26.    isr = new InputStreamReader(stderr); 
  27.    br = new BufferedReader(isr); 
  28.    System.out.println("<mencoderToFlv>"); 
  29.    while ((line = br.readLine()) != null
  30.     System.out.println(line); 
  31.    System.out.println("</mencoderToFlv>"); 
  32.    int exitVal = proc.waitFor(); 
  33.    System.out.println("Process exitValue: " + exitVal); 
  34.    File filePath = new File(fileOut); 
  35.    // 如果文件存在,并且长度不为0,则表示转换成功. 
  36.    success = filePath.exists() && filePath.length() > 0
  37.   } catch (Throwable t) { 
  38.    t.printStackTrace(); 
  39.   } finally { 
  40.    try { 
  41.     stderr.close(); 
  42.     isr.close(); 
  43.     br.close(); 
  44.    } catch (Exception e) { 
  45.     e.printStackTrace(); 
  46.     rt.exit(1); 
  47.    } 
  48.   } 
  49.   return success; 
  50.  } 

程序已经编译通过的,配置文件config.properties需要放到web服务的WEB-INF/classes目录下,只需按实际情况把配置文件里面的路径修改成你自己的就可以用了。

(xiayuxiao78)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201105/11548.html]
本文出处:xiayuxiao78.blog.163.com 作者:xiayuxiao78
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容