Blame view

src/main/java/com/phxl/common/util/VideoThumbTaker.java 4.68 KB
Quilan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
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();
		}
	}
}