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

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

    我这里是用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,如需转载请自行联系原作者




相关文章
|
27天前
|
算法 Shell Linux
【Shell 命令集合 备份压缩 】Linux 处理lha格式 lha命令 使用指南
【Shell 命令集合 备份压缩 】Linux 处理lha格式 lha命令 使用指南
36 0
|
27天前
|
安全 Shell Linux
【Shell 命令集合 备份压缩 】Linux将可执行文件压缩成gzip格式 gzexe命令 使用指南
【Shell 命令集合 备份压缩 】Linux将可执行文件压缩成gzip格式 gzexe命令 使用指南
35 0
|
7月前
|
编解码 Linux
Linux MIPI DSI驱动调试笔记-设备树DCS格式序列之配置LCD初始化代码(二)
Linux MIPI DSI驱动调试笔记-设备树DCS格式序列之配置LCD初始化代码(二)
565 0
|
Linux
Linux制作deb格式安装包教程
Linux制作deb格式安装包教程
542 0
|
6月前
|
Linux
Linux的命令基本格式
因为对服务器来讲,图形界面会占用更多的系统资源,而且会安装更多的服务、开放更多的端口,这对服务器的稳定性和安全性都有负面影响。其实,服务器是一个连显示器都没有的家伙,要图形界面干十么?说到这里,有很多人会很崩溃。 笔者就经常听到抱怨 Linux 是落后于时代的老古董,就像笔者的白头发一样!但是,大家要理解,对服务器来讲,稳定性、可靠性、安全性才是最主要的。而简单易用不是服务器需要考虑的事情,所以学习 Linux,这些枯燥的命令是必须学习和记忆的内容。 命令提示符 登录系统后,第一眼看到的内容是: [root@localhost ~]# 这就是 Linux 系统的命令提示符。那么,这个
114 0
|
27天前
|
算法 Shell Linux
【Shell 命令集合 备份压缩 】Linux 解压缩ARJ格式 unarj命令 使用指南
【Shell 命令集合 备份压缩 】Linux 解压缩ARJ格式 unarj命令 使用指南
27 0
|
28天前
|
Shell Linux
【Shell 命令集合 文件管理】Linux 以不同的进制格式显示文件的内容 od 命令使用教程
【Shell 命令集合 文件管理】Linux 以不同的进制格式显示文件的内容 od 命令使用教程
31 0
|
29天前
|
网络协议 Linux C++
Linux C/C++ 网络编程中地址格式转换(inet_pton和inet_ntop函数)
Linux C/C++ 网络编程中地址格式转换(inet_pton和inet_ntop函数)
22 0
|
7月前
|
机器学习/深度学习 Linux C语言
Linux基础操作3(命令格式,命令查询帮助)
Linux基础操作3(命令格式,命令查询帮助)
46 0
|
4月前
|
Linux
Linux【工具 01】rarlinux工具下载安装处理.rar格式文件实例
Linux【工具 01】rarlinux工具下载安装处理.rar格式文件实例
56 0