java使用ffmpeg转换视频为flv文件

 
package com.alpha.vedio;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 讲任何视频格式转为flv
 * @author JavaAlpha
 * @date 2011-10-20
 * @version V 1.0
 */

public class TestVedio {
	public static boolean process(String resourcePath) {
		int type = checkContentType(resourcePath);
		boolean status = false;
		if (type == 0) {
			status = processFLV(resourcePath);// 直接将文件转为flv文件
		} else if (type == 1) {
			status = processFLV(resourcePath);// 将rmvb转为flv
		}
		return status;
	}

	private static int checkContentType(String resourcePath) {
		String type = resourcePath.substring(resourcePath.lastIndexOf(".") + 1, resourcePath.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;
		} else if (type.equals("mpeg")) {
			return 0;
		} else if (type.equals("mpe")) {
			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;
	}

	private static boolean checkfile(String path) {
		File file = new File(path);
		if (!file.isFile()) {
			return false;
		}
		return true;
	}

	// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
	@SuppressWarnings("unchecked")
	private static boolean processFLV(String resourcePath) {
		if (!checkfile(resourcePath)) {
			System.out.println(resourcePath + "   is   not   file");
			return false;
		}
		// 文件命名
		Calendar c = Calendar.getInstance();
		String savename = String.valueOf(c.getTimeInMillis()) + Math.round(Math.random() * 100000);
		List commend = new ArrayList();
		commend.add("e:\\ffmpeg");
		commend.add("-i");
		commend.add(resourcePath);
		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("e:\\" + savename + ".flv");
		try {
			Runtime runtime = Runtime.getRuntime();
			Process proce = null;
			String cmd = "";
			String cut = "e://ffmpeg.exe -i" + resourcePath + " -y -f image2   -ss   8   -t   0.001   -s   600x500   e:\\" + savename + ".jpg";
			String cutCmd = cmd + cut;
			proce = runtime.exec(cutCmd);
			ProcessBuilder builder = new ProcessBuilder(commend);
			// builder.command(commend);
			builder.start();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String[] args) {
		if (!checkfile("e:\\fly.avi")) {
			System.out.println("" + "   is   not   file");
			return;
		}
		if (process("e:\\a.mpg")) {
			System.out.println("ok");
		}
	}
}


需要的视频处理软件:ffmpeg.exe 下载地址:

点击打开链接

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值