window系统ffmpeg、mencoder转换flash格式

本文介绍了一种将多种视频格式转换为FLV格式的方法,利用ffmpeg和mencoder工具实现视频格式转换,支持asx、asf、mpg等多种格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做视频格式转换问题,在网上查看了很多资料,网上有很多demo,这里我将我实现的demo拿出来,目的在于方便以后用到的时候,可以直接看了,不用再去找了,使用第三方工具ffmpeg和mencoder软件转换为flash格式,废话不多说,见代码。



package com.sitemaker.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
/**
 * 功能:将任意格式的视频转化为flv格式,有利于在线视频播放
 * 前置条件:C盘下放有 ffmpeg.exe、ffplay.exep、threadGC2.dll(ffmpeg来自 
ffmpeg.rev12665.7z)
 *                 C盘下还需 mencoder.exe、drv43260.dll
 *  注:   ffmpeg 能解析的格式:asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等
 *         mencoder 解析剩下的格式:wmv9,rm,rmvb  
 * 
 */
public class TransferToFlv {
	public static void main(String[] args) {
		ConvertFlv.convert("C:/work/video/test.avi", "C:/work/video/test.swf");
	}
	/**
	 *  功能函数
	 * @param inputFile 待处理视频
	 * @param outputFile 处理后视频
	 * @return
	 */
	public static 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 static boolean checkfile(String path) {
		File file = new File(path);
		if (!file.isFile()) {
			return false;
		}
		return true;
	}
	/**
	 * 转换过程 :先检查文件类型,在决定调用 processFlv还是processAVI
	 * @param inputFile
	 * @param outputFile
	 * @return
	 */
	private static 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 static 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 static boolean processFLV(String inputFile,String outputFile) {
		if (!checkfile(inputFile)) {
			System.out.println(inputFile + " is not file");
			return false;
		}
		List<String> commend = new java.util.ArrayList<String>();
		//低精度
		commend.add("C:/work/soft/ffmpeg/ffmpeg.exe");
		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");
		commend.add("-b");
		commend.add("512");
		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 static String processAVI(int type,String inputFile) {
		File file =new File("c:/work/video/temp.avi");
		if(file.exists())	file.delete();
		List<String> commend = new java.util.ArrayList<String>();
		commend.add("C:/work/soft/mencoder/mencoder.exe");
		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("c:/work/video/temp.avi");
		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进程转换结束,再调用ffmepg进程
			p.waitFor();
			 System.out.println("who cares");
			return "c:/work/video/temp.avi";
		}catch (Exception e){ 
			System.err.println(e); 
			return null;
		} 
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值