分享一个常用的ffmpeg工具类

最近一直在处理视频文件,然后经常会要截图、抽帧、对视频文件进行抽取音频,对wav转换成mp3等操作,使用的是ffmpeg的一个工具类,目前只有几个方面的,以后需要新的用途会更新此工具类,希望这个工具类可以帮助到一些需要的小伙伴,可以直接拿去用,代码如下:

public class FfmpegUtil {
	
	//ffmpeg安装目录
	public   static  String FFMPEG_PATH ="D:\\\\ffmpeg.exe";
//	public   static  String FFMPEG_PATH =FfmpegUtil.class.getClassLoader().getResource("com/wal/utils/ffmpeg.exe").getFile();
	
	//设置图片大小
	private final static String IMG_SIZE = "1920x1080";

	/**
	 * 视频截图方法(windows)
	 */
	public static boolean ffmpegToImage(String videoPath,String imagePath,int timePoint){  
	        List<String> commands = new java.util.ArrayList<String>();  
	        FFMPEG_PATH= FFMPEG_PATH.replace("%20", " ");
	        commands.add(FFMPEG_PATH);    
	        commands.add("-ss");    
	        commands.add(timePoint+"");//这个参数是设置截取视频多少秒时的画面    
	        commands.add("-i");    
	        commands.add(videoPath);    
	        commands.add("-y");    
	        commands.add("-f");    
	        commands.add("image2");    
	        commands.add("-t");    
	        commands.add("0.001");    
	        commands.add("-s");    
	        commands.add(IMG_SIZE); //这个参数是设置截取图片的大小   
	        commands.add(imagePath);    
	        try {    
	        ProcessBuilder builder = new ProcessBuilder();    
	        builder.command(commands);    
	        builder.start();    
	        System.out.println("截取成功:"+imagePath);
	        return true;    
	        } catch (Exception e) {    
	        e.printStackTrace();    
	        return false;    
	        }    
	    } 
	
	/**
	 * @Description 文件是否能被ffmpeg解析
	 */
	public static int checkFileType(String fileName) {  
        String type = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length())  
                .toLowerCase();  
        if (type.equals("avi")) {  
            return 0;  
        }  else if (type.equals("mov")) {  
            return 0;  
        } else if (type.equals("mp4")) {  
            return 0;  
        }  else if (type.equals("flv")) {  
            return 0;  
        } 
        else if (type.equals("png")) {  
            return 1;  
        } else if (type.equals("jpg")) {  
            return 1;  
        } else if (type.equals("jpeg")) {  
            return 1;  
        }  
        return 9;  
    }


    /**
	 * @Description 获取视频时长
	 */
	static int getVideoTime(String video_path) {
		List<String> commands = new java.util.ArrayList<String>();
		commands.add(FFMPEG_PATH);
		commands.add("-i");
		commands.add(video_path);
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commands);
			final Process p = builder.start();

			//从输入流中读取视频信息
			BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
			StringBuffer sb = new StringBuffer();
			String line = "";
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			br.close();

			//从视频信息中解析时长
			String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
			Pattern pattern = Pattern.compile(regexDuration);
			Matcher m = pattern.matcher(sb.toString());
			if (m.find()) {
				int time = getTimelen(m.group(1));
				System.out.println(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");
				return time;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return 0;
	}

	//格式:"00:00:10.68"
	private static int getTimelen(String timelen){
		int min=0;
		String strs[] = timelen.split(":");
		if (strs[0].compareTo("0") > 0) {
			min+=Integer.valueOf(strs[0])*60*60;//秒
		}
		if(strs[1].compareTo("0")>0){
			min+=Integer.valueOf(strs[1])*60;
		}
		if(strs[2].compareTo("0")>0){
			min+=Math.round(Float.valueOf(strs[2]));
		}
		return min;
	}

	/**
	 * 秒转化成 hh:mm:ss
	 * @param duration
	 * @return
	 */
	public static String convertInt2Date(long duration){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		return formatter.format(cal.getTimeInMillis() + duration * 1000);
	}

	/**
	 * 视频抽取音频文件
	 * @param videoPath
	 * @param type
	 * @param audioPath
	 * @return
	 */
	public static boolean ffmpegToAudio(String videoPath,String type, String audioPath){
		List<String> commands = new java.util.ArrayList<String>();
		FFMPEG_PATH= FFMPEG_PATH.replace("%20", " ");
		commands.add(FFMPEG_PATH);
		commands.add("-i");
		commands.add(videoPath);
		commands.add("-f");
		commands.add(type);
		commands.add("-vn");
		commands.add("-y");
		commands.add("-acodec");
		if("wav".equals(type)){
			commands.add("pcm_s16le");
		}else if("mp3".equals(type)){
			commands.add("mp3");
		}
		commands.add("-ar");
		commands.add("16000");
		commands.add("-ac");
		commands.add("1");
		commands.add(audioPath);
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commands);
			Process p = builder.start();
			System.out.println("抽离成功:"+audioPath);

			// 1. start
			BufferedReader buf = null; // 保存ffmpeg的输出结果流
			String line = null;

			buf = new BufferedReader(new InputStreamReader(p.getInputStream()));

			StringBuffer sb = new StringBuffer();
			while ((line = buf.readLine()) != null) {
				System.out.println(line);
				sb.append(line);
				continue;
			}
			p.waitFor();// 这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行
			// 1. end
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}


	/**
	 * wav 转 mp3
	 * @param wavPath
	 * @param mp3Path
	 * @return
	 */
	//wav转mp3命令:ffmpeg -i test.wav -f mp3 -acodec libmp3lame -y wav2mp3.mp3
	public static boolean ffmpegOfwavTomp3(String wavPath, String mp3Path){
		List<String> commands = new java.util.ArrayList<String>();
		FFMPEG_PATH= FFMPEG_PATH.replace("%20", " ");
		commands.add(FFMPEG_PATH);
		commands.add("-i");
		commands.add(wavPath);
		commands.add("-f");
		commands.add("mp3");
		commands.add("-acodec");
		commands.add("libmp3lame");
		commands.add("-y");
		commands.add(mp3Path);
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commands);
			Process p = builder.start();
			System.out.println("转换成功:"+mp3Path);

			// 1. start
			BufferedReader buf = null; // 保存ffmpeg的输出结果流
			String line = null;

			buf = new BufferedReader(new InputStreamReader(p.getInputStream()));

			StringBuffer sb = new StringBuffer();
			while ((line = buf.readLine()) != null) {
				System.out.println(line);
				sb.append(line);
				continue;
			}
			p.waitFor();// 这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行
			// 1. end
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static void main(String[] args) {
//        // 视频文件
//        String videoRealPath1 = "D:\\5.MOV";
//        // 截图的路径(输出路径)
//        String imageRealPath1 = "D:\\31.jpg";
//        String imageRealPath2 = "D:\\32.jpg";
//        if(checkFileType(videoRealPath1) ==0){
//        	 ffmpegToImage(videoRealPath1,imageRealPath1,10);
//        	 ffmpegToImage(videoRealPath1,imageRealPath2,11);
//        }
//		String videoRealPath = "D:\\5.MOV";
//		String audioRealPath = "D:\\HHH\\5.wav";
//		String audioType = "wav";
//		ffmpegToAudio(videoRealPath,audioType,audioRealPath);

		String wavPath = "D:\\HHH\\123.wav";
		String mp3Path = "D:\\HHH\\123.mp3";
		ffmpegOfwavTomp3(wavPath,mp3Path);
    }	
}

有不懂的地方可以给我留言,或者进群交流!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wal1314520

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值