FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件)。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多codec都是从头开发的。FFmpeg在Linux平台下开发,但它同样也可以在其它操作系统环境中编译运行。
mencoder 是一款命令行方式的视频处理软件,是Mplayer自带的编码工具(Mplayer是Linux下的播放器,开源,支持几乎所有视频格式的播放,现在有windows和Mac版本)。
这种方式来实现在web页面播放视频的思路如下:可以将各种视频格式转为flv的格式,然后在页面上只需要有一个flash播放器即可播放flv文件了,这样的话就不用考虑使用不同的播放器来播放的情况,而且页面代码简洁。
以下为转换代码:
(1)将视频转换过程做成一个帮助类:
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.List;
- public class ConvertSingleVideo {
-
- private static String mencoder_home = "D:\\javaserve\\mencoder\\mencoder.exe";//mencoder.exe所放的路径
- private static String ffmpeg_home = "D:\\javaserve\\ffmpeg\\ffmpeg.exe";//ffmpeg.exe所放的路径
-
- public static String inputFile_home = "F:\\java\\work\\jingpinkecheng\\WebRoot\\upload\\input\\";//需转换的文件的位置
- public static String outputFile_home = "D:\\javaserve\\tomcat\\apache-tomcat-7.0.32\\webapps\\jingpinkecheng\\upload\\output\\";//转换后的flv文件所放的文件夹位置
- private String tempFile_home;//存放rm,rmvb等无法使用ffmpeg直接转换为flv文件先转成的avi文件
-
- public ConvertSingleVideo(String tempFilePath){
- this.tempFile_home = tempFilePath;
- }
-
- /**
- * 功能函数
- * @param inputFile 待处理视频,需带路径
- * @param outputFile 处理后视频,需带路径
- * @return
- */
- public boolean convert(String inputFile, String outputFile)
- {
- if (!checkfile(inputFile)) {
- System.out.println(inputFile + " is not file");
- return false;
- }
- if (process(inputFile,outputFile)) {
- System.out.println("ok");
- return true;
- }
- return false;
- }
- //检查文件是否存在
- private boolean checkfile(String path) {
- File file = new File(path);
- if (!file.isFile()) {
- return false;
- }
- return true;
- }
- /**
- * 转换过程 :先检查文件类型,在决定调用 processFlv还是processAVI
- * @param inputFile
- * @param outputFile
- * @return
- */
- private boolean process(String inputFile,String outputFile) {
- int type = checkContentType( inputFile);
- boolean status = false;
- if (type == 0) {
- status = processFLV(inputFile,outputFile);// 直接将文件转为flv文件
- } else if (type == 1) {
- String avifilepath = processAVI(type,inputFile);
- if (avifilepath == null)
- return false;// avi文件没有得到
- status = processFLV(avifilepath,outputFile);// 将avi转为flv
- }
- return status;
- }
- /**
- * 检查视频类型
- * @param inputFile
- * @return ffmpeg 能解析返回0,不能解析返回1
- */
- private int checkContentType(String inputFile) {
- String type = inputFile.substring(inputFile.lastIndexOf(".") + 1,inputFile.length()).toLowerCase();
- // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- if (type.equals("avi")) {
- return 0;
- } else if (type.equals("mpg")) {
- return 0;
- } else if (type.equals("wmv")) {
- return 0;
- } else if (type.equals("3gp")) {
- return 0;
- } else if (type.equals("mov")) {
- return 0;
- } else if (type.equals("mp4")) {
- return 0;
- } else if (type.equals("asf")) {
- return 0;
- } else if (type.equals("asx")) {
- return 0;
- } else if (type.equals("flv")) {
- return 0;
- }
- // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
- // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
- else if (type.equals("wmv9")) {
- return 1;
- } else if (type.equals("rm")) {
- return 1;
- } else if (type.equals("rmvb")) {
- return 1;
- }
- return 9;
- }
- /**
- * ffmepg: 能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- * @param inputFile
- * @param outputFile
- * @return
- */
- private boolean processFLV(String inputFile,String outputFile) {
- if (!checkfile(inputFile)) {
- System.out.println(inputFile + " is not file");
- return false;
- }
- File file = new File(outputFile);
- if(file.exists()){
- System.out.println("flv文件已经存在!无需转换");
- return true;
- } else {
- System.out.println("正在转换成flv文件……");
-
- List<String> commend = new java.util.ArrayList<String>();
- //低精度
- commend.add(ffmpeg_home);
- commend.add("-i");
- commend.add(inputFile);
- commend.add("-ab");
- commend.add("128");
- commend.add("-acodec");
- commend.add("libmp3lame");
- commend.add("-ac");
- commend.add("1");
- commend.add("-ar");
- commend.add("22050");
- commend.add("-r");
- commend.add("29.97");
- // 清晰度 -qscale 4 为最好但文件大, -qscale 6就可以了
- commend.add("-qscale");
- commend.add("4");
- commend.add("-y");
- commend.add(outputFile);
- StringBuffer test=new StringBuffer();
- for(int i=0;i<commend.size();i++)
- test.append(commend.get(i)+" ");
- System.out.println(test);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- builder.start();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
-
- }
-
- }
- /**
- * Mencoder:
- * 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
- * @param type
- * @param inputFile
- * @return
- */
- private String processAVI(int type,String inputFile) {
- File file =new File(tempFile_home);
- if(file.exists()){
- System.out.println("avi文件已经存在!无需转换");
- return tempFile_home;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(mencoder_home);
- commend.add(inputFile);
- commend.add("-oac");
- commend.add("mp3lame");
- commend.add("-lameopts");
- commend.add("preset=64");
- commend.add("-ovc");
- commend.add("xvid");
- commend.add("-xvidencopts");
- commend.add("bitrate=600");
- commend.add("-of");
- commend.add("avi");
- commend.add("-o");
- commend.add(tempFile_home);
- StringBuffer test=new StringBuffer();
- for(int i=0;i<commend.size();i++)
- test.append(commend.get(i)+" ");
- System.out.println(test);
- try
- {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process p=builder.start();
- /**
- * 清空Mencoder进程 的输出流和错误流
- * 因为有些本机平台仅针对标准输入和输出流提供有限的缓冲区大小,
- * 如果读写子进程的输出流或输入流迅速出现失败,则可能导致子进程阻塞,甚至产生死锁。
- */
- final InputStream is1 = p.getInputStream();
- final InputStream is2 = p.getErrorStream();
- new Thread() {
- public void run() {
- BufferedReader br = new BufferedReader(new InputStreamReader(is1));
- try {
- String lineB = null;
- while ((lineB = br.readLine()) != null ){
- if(lineB != null)System.out.println(lineB);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }.start();
- new Thread() {
- public void run() {
- BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
- try {
- String lineC = null;
- while ( (lineC = br2.readLine()) != null){
- if(lineC != null)System.out.println(lineC);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }.start();
-
- //等Mencoder进程转换结束,再调用ffmpeg进程
- p.waitFor();
- System.out.println("who cares");
- return tempFile_home;
- }catch (Exception e){
- System.err.println(e);
- return null;
- }
- }
- }
(2)action中调用该类的方法:
- public String showResourceJiaoxueshipin() throws IOException{
- resourcejiaoxueshipin = itemService.findById(resourcejiaoxueshipinid);
-
- String fileName = resourcejiaoxueshipin.getFirst_img();
- ConvertSingleVideo conver = new ConvertSingleVideo("F:\\java\\work\\jingpinkecheng\\WebRoot\\upload\\temp\\" + fileName.substring(0,fileName.lastIndexOf("."))+".avi");
-
- conver.convert(ConvertSingleVideo.inputFile_home + fileName, ConvertSingleVideo.outputFile_home + fileName.substring(0,fileName.lastIndexOf("."))+".flv");
-
- HttpSession session = request.getSession();
- fileName = new String(fileName.getBytes("UTF-8"),"GBK");//存到session后在jsp页面取出的值是GBK("乱码"),所以这里先变成乱码,传输过去之后即可消除
- session.setAttribute("jiaoxueshipinName",fileName.substring(0,fileName.lastIndexOf("."))+".flv" );
- System.out.println("我是测试:"+session.getAttribute("jiaoxueshipinName"));
- return "success";
-
-
-
- }
(3)jsp代码:
只需要在页面嵌入一个flash播放器,我这里使用vcastr22.swf,并且将flv文件的路径填写正确即可。嵌入代码如下:
- <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" height="120" width="190">
- <param name="movie" value="../upload/vcastr22.swf?vcastr_file=../upload/output/${sessionScope.jiaoxueshipinName }">
- <param name="quality" value="high">
- <param name="allowFullScreen" value="true" />
- <!-- src里就是播放器的路径以及需要显示的flv文件的路径,路径一定要正确! -->
- <embed
- src="../upload/vcastr22.swf?vcastr_file=../upload/output/${sessionScope.jiaoxueshipinName }"
- quality="high"
- pluginspage="http://www.macromedia.com/go/getflashplayer"
- type="application/x-shockwave-flash" width="500" height="350">
- </embed>
- </object>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。