[转]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,如需转载请自行联系原作者




相关文章
|
14天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
18天前
|
Linux
linux下搭建tftp服务器教程
在Linux中搭建TFTP服务器,需安装`tftp-server`(如`tftpd-hpa`)。步骤包括:更新软件包列表,安装`tftpd-hpa`,启动并设置开机自启,配置服务器(编辑`/etc/default/tftpd-hpa`),添加选项,然后重启服务。完成后,可用`tftp`命令进行文件传输。例如,从IP`192.168.1.100`下载`file.txt`: ``` tftp 192.168.1.100 &lt;&lt;EOF binary put file.txt quit EOF ```
28 4
|
1月前
|
Linux Shell 网络安全
【Shell 命令集合 网络通讯 】Linux 与SMB服务器进行交互 smbclient命令 使用指南
【Shell 命令集合 网络通讯 】Linux 与SMB服务器进行交互 smbclient命令 使用指南
45 1
|
4天前
|
网络协议 安全 Linux
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
|
10天前
|
Linux 数据安全/隐私保护
Linux基础与服务器架构综合小实践
【4月更文挑战第9天】Linux基础与服务器架构综合小实践
1229 8
|
22天前
|
Ubuntu Linux 虚拟化
【Linux】ubuntu安装samba服务器
【Linux】ubuntu安装samba服务器
|
22天前
|
Linux
Linux安装bind9搭建自己的域名服务器
Linux安装bind9搭建自己的域名服务器
11 0
|
25天前
|
网络协议 Linux 网络安全
Linux服务器DNS服务器配置实现bind的正向解释和反向解释
Linux服务器DNS服务器配置实现bind的正向解释和反向解释
17 0
|
1月前
|
网络协议 安全 Shell
【Shell 命令集合 系统设置 】Linux 从远程时间服务器获取当前日期和时间 rdate命令 使用指南
【Shell 命令集合 系统设置 】Linux 从远程时间服务器获取当前日期和时间 rdate命令 使用指南
36 0
|
1月前
|
Shell Linux 网络安全
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
30 0