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




相关文章
|
4月前
|
弹性计算 安全 Linux
阿里云服务器ECS安装宝塔Linux面板、安装网站(新手图文教程)
本教程详解如何在阿里云服务器上安装宝塔Linux面板,涵盖ECS服务器手动安装步骤,包括系统准备、远程连接、安装命令执行、端口开放及LNMP环境部署,手把手引导用户快速搭建网站环境。
|
6月前
|
Linux 网络安全 数据安全/隐私保护
使用Linux系统的mount命令挂载远程服务器的文件夹。
如此一来,你就完成了一次从你的Linux发车站到远程服务器文件夹的有趣旅行。在这个技术之旅中,你既探索了新地方,也学到了如何桥接不同系统之间的距离。
1187 21
|
5月前
|
Java Linux 网络安全
Linux云端服务器上部署Spring Boot应用的教程。
此流程涉及Linux命令行操作、系统服务管理及网络安全知识,需要管理员权限以进行配置和服务管理。务必在一个测试环境中验证所有步骤,确保一切配置正确无误后,再将应用部署到生产环境中。也可以使用如Ansible、Chef等配置管理工具来自动化部署过程,提升效率和可靠性。
603 13
|
5月前
|
监控 Linux 网络安全
FinalShell SSH工具下载,服务器管理,远程桌面加速软件,支持Windows,macOS,Linux
FinalShell是一款国人开发的多平台SSH客户端工具,支持Windows、Mac OS X和Linux系统。它提供一体化服务器管理功能,支持shell和sftp同屏显示,命令自动提示,操作便捷。软件还具备加速功能,提升访问服务器速度,适合普通用户和专业人士使用。
1642 0
|
5月前
|
存储 安全 Linux
Linux服务器上安装配置GitLab的步骤。
按照以上步骤,一个基础的GitLab服务应该运行并可以使用。记得定期检查GitLab官方文档,因为GitLab的安装和配置步骤可能随着新版本而变化。
479 0
|
7月前
|
Ubuntu Linux 网络安全
在Linux云服务器上限制特定IP进行SSH远程连接的设置
温馨提示,修改iptables规则时要格外小心,否则可能导致无法远程访问你的服务器。最好在掌握足够技术知识和理解清楚操作含义之后再进行。另外,在已经配置了防火墙的情况下,例如ufw(Ubuntu Firewall)或firewalld,需要按照相应的防火墙的规则来设置。
402 24
|
7月前
|
存储 安全 Ubuntu
从Linux到Windows:阿里云服务器系统镜像适配场景与选择参考
阿里云为用户提供了丰富多样的服务器操作系统选择,以满足不同场景下的应用需求。目前,云服务器的操作系统镜像主要分为公共镜像、自定义镜像、共享镜像、镜像市场和社区镜像五大类。以下是对这些镜像类型的详细介绍及选择云服务器系统时需要考虑的因素,以供参考。
|
6月前
|
Linux
Linux下版本控制器(SVN) -服务器端环境搭建步骤
Linux下版本控制器(SVN) -服务器端环境搭建步骤
290 0
Linux下版本控制器(SVN) -服务器端环境搭建步骤
|
8月前
|
安全 Linux
阿里云linux服务器使用脚本通过安全组屏蔽异常海外访问ip
公网网站可能会遭受黑客攻击导致访问异常,使用此脚本可以屏蔽掉异常IP 恢复访问。也可自行设置定时任务定期检测屏蔽。
658 28
|
7月前
|
数据挖掘 Linux 数据库
服务器数据恢复—Linux系统服务器数据恢复案例
服务器数据恢复环境: linux操作系统服务器中有一组由4块SAS接口硬盘组建的raid5阵列。 服务器故障: 服务器工作过程中突然崩溃。管理员将服务器操作系统进行了重装。 用户方需要恢复服务器中的数据库、办公文档、代码文件等。