本文主要是介绍牛人写的把其他格式的文件转换成flv格式的文件,java版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在做视频相关的项目中需要把其它格式的文件转换成 flv 格式的文件,因 flv 文件体积小,且可以边下载边播放,很有优势,然后用播放器即可在web页面里播放,这个就是设计 flv 格式文件的生成问题,网上看牛人写的把其他格式的文件转换成flv格式的文件,java版,好奇转载一下作者的回复:
不是播放器问题!你要把你所有的后台视频格式转换成flv格式的。现在基本上大型在线视频网站都是flv格式的。我上面说了用ffmpeg可以转成flv格式的。api或者exe都可以做到。你自己研究下,转成flv之后基本上用一个flash播放器都可以变下载变放,而且随意拖动。
package com.cemso.util;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class VideoConverter {private final static String BASE_PATH = FileOperationTool.DEFAULT_VIDEO_DES_PATH;private final static String FLV_PATH = FileOperationTool.DEFAULT_FLV_PATH;private static String PATH = "";private static String FILE_NAME = ""; private static final Log log = LogFactory.getLog(com.cemso.util.VideoConverter.class);public static boolean convert(String fileName) {PATH = BASE_PATH + fileName;FILE_NAME = fileName;if(log.isDebugEnabled()){log.debug("start to convert video to flv format...");log.debug("the file name is : " + FILE_NAME);log.debug("the file path is : " + PATH);}if (!checkfile(PATH)) {return false;} else {if (process()) {if(log.isDebugEnabled()){log.debug("process() ok");}return true;} else {return false;}}}private static boolean process() {int type = checkContentType();boolean status = false;if (type == 0) {if(log.isDebugEnabled()){log.debug("Start to convert to flv file");}status = processFLV(PATH);// 直接将文件转为flv文件} else if (type == 1) {String avifilepath = processAVI(type);if (avifilepath == null){return false;// avi文件没有得到}status = processFLV(avifilepath);// 将avi转为flv} else if(type == 9){if(log.isDebugEnabled()){log.debug("this file is no need to convert.");}return false;}return status;}private static int checkContentType() {String type = PATH.substring(PATH.lastIndexOf(".") + 1, PATH.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;}// check fileprivate static boolean checkfile(String path) {File file = new File(path);if (!file.isFile()) {return false;}return true;}// 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.private static String processAVI(int type) {List<String> commend = new ArrayList<String>();commend.add(FLV_PATH + "/mencoder");commend.add(PATH);commend.add("-oac");commend.add("lavc");commend.add("-lavcopts");commend.add("acodec=mp3:abitrate=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(FLV_PATH + FILE_NAME.substring(0,FILE_NAME.lastIndexOf(".")) + ".avi");try {ProcessBuilder builder = new ProcessBuilder();builder.command(commend);builder.start();return FLV_PATH + FILE_NAME.substring(0,FILE_NAME.lastIndexOf(".")) + ".avi";} catch (Exception e) {e.printStackTrace();return null;}}// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)private static boolean processFLV(String oldfilepath) {if (!checkfile(PATH)) {return false;}List<String> commend = new ArrayList<String>();commend.add(FLV_PATH + "ffmpeg");commend.add("-i");commend.add(oldfilepath);commend.add("-ab");commend.add("56");commend.add("-ar");commend.add("22050");commend.add("-qscale");commend.add("8");commend.add("-r");commend.add("15");commend.add("-s");commend.add("600x500");commend.add(FLV_PATH + FILE_NAME.substring(0,FILE_NAME.lastIndexOf(".")) + ".flv");try {Runtime runtime = Runtime.getRuntime();String cmd = "";String cut = FLV_PATH + "ffmpeg.exe -i "+ oldfilepath+ " -y -f image2 -ss 8 -t 0.001 -s 600x500 "+ FLV_PATH+ FILE_NAME.substring(0,FILE_NAME.lastIndexOf("."))+ ".jpg";String cutCmd = cmd + cut;runtime.exec(cutCmd);ProcessBuilder builder = new ProcessBuilder(commend);builder.command(commend);Process process = builder.start();int i = doWaitFor(process);if(i == 0){if(log.isDebugEnabled()){log.debug("ffmpeg has finished.");}}return true;} catch (Exception e) {e.printStackTrace();return false;}}public static int doWaitFor(Process p) {int exitValue = -1; // returned to caller when p is finishedtry {InputStream in = p.getInputStream();InputStream err = p.getErrorStream();boolean finished = false; // Set to true when p is finishedwhile(!finished){try {while( in.available() > 0) {// Print the output of our system callCharacter c = new Character( (char) in.read());if(log.isDebugEnabled()){log.debug( c);}}while( err.available() > 0) {// Print the output of our system callCharacter c = new Character( (char) err.read());if(log.isDebugEnabled()){log.debug( c);}}// Ask the process for its exitValue. If the process// is not finished, an IllegalThreadStateException// is thrown. If it is finished, we fall through and// the variable finished is set to true.exitValue = p.exitValue();finished = true;} catch (IllegalThreadStateException e) {Thread.currentThread();// Process is not finished yet;// Sleep a little to save on CPU cyclesThread.sleep(500);}}} catch (Exception e) {// unexpected exception! print it out for debugging...if(log.isErrorEnabled()){log.error("doWaitFor(): unexpected exception - " + e.getMessage());}if(log.isErrorEnabled()){log.error(e.getMessage());}}// return completion status to callerreturn exitValue;}
}
原文参考: http://bbs.csdn.net/topics/390615854 见 5L 的回复
这篇关于牛人写的把其他格式的文件转换成flv格式的文件,java版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!