49ac500e4bf0f4ee3afa2115e8944394a234be07.svn-base 4.68 KB
package com.phxl.common.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class VideoThumbTaker {
	
	static Log log = LogFactory.getLog(VideoThumbTaker.class);
	
	protected String ffmpegApp;

	public VideoThumbTaker(String ffmpegApp) {
		this.ffmpegApp = ffmpegApp;
	}

	@SuppressWarnings("unused")
	/****
	 * 获取指定时间内的图片
	 * 
	 * @param videoFilename:视频路径
	 * @param thumbFilename:图片保存路径
	 * @param width:图片长
	 * @param height:图片宽
	 * @param hour:指定时
	 * @param min:指定分
	 * @param sec:指定秒
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public void getThumb(String videoFilename, String thumbFilename, int width, int height, int hour, int min,
			float sec) throws IOException, InterruptedException {
		ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y", "-i", videoFilename, "-vframes", "1", "-ss",
				hour + ":" + min + ":" + sec, "-f", "mjpeg", "-s", width + "*" + height, "-an", thumbFilename);

		Process process = processBuilder.start();

		InputStream stderr = process.getErrorStream();
		InputStreamReader isr = new InputStreamReader(stderr);
		BufferedReader br = new BufferedReader(isr);
		String line;
		while ((line = br.readLine()) != null)
			;
		process.waitFor();

		if (br != null)
			br.close();
		if (isr != null)
			isr.close();
		if (stderr != null)
			stderr.close();
	}

	/****
	 * 获取指定时间内的图片
	 * 
	 * @param ffmpeg:ffmpeg插件路径
	 * @param localMp4:本地mp4保存路径
	 * @param localMp4Img:本地mp4图片保存路径
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void getVidioImg(String ffmpeg,String localMp4,String localMp4Img) throws Exception{
		VideoThumbTaker videoThumbTaker = new VideoThumbTaker(ffmpeg);
	    videoThumbTaker.getThumb(localMp4, localMp4Img, 800,600, 0, 0, 0.5f);
		
	}
	
	
	/** 
     * 获取视频总时间 
     * @param viedo_path    视频路径 
     * @param ffmpeg_path   ffmpeg路径 
     * @return 
     */  
    public static int getVideoTime(String video_path, String ffmpeg_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));  
                //log.info(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");  
                return time;  
            }  
        } catch (Exception e) {  
        	log.error("获取音频时长失败:"+e);
        }  
          
        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;  
    }  
	
	//测试方法
	public static void main(String[] args) {
		String ffmpegPath = "D:\\software\\ffmpeg\\bin\\ffmpeg.exe";
		//VideoThumbTaker videoThumbTaker = new VideoThumbTaker("D:\\software\\ffmpeg\\bin\\ffmpeg.exe");
		try {
			/*videoThumbTaker.getThumb("D:\\videoMp4sLoc\\114265ef-beb6-437f-9015-2e9af8fa2b9a.mp4", "D:\\thumbTest222.png", 800,600, 0, 0, 1);
			System.out.println("over");*/
			
			System.out.println(getVideoTime("D:\\jpg\\22.mp3",ffmpegPath));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}