VideoUtils.java 12.8 KB
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
package com.jeesite.common.media;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.image.ImageUtils;
import com.jeesite.common.io.FileUtils;
import com.jeesite.common.io.PropertiesUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.lang.TimeUtils;

/**
 * 视频工具类
 * @author ThinkGem
 * @version 2015-5-13
 */
public class VideoUtils {

	private static final Logger log = LoggerFactory.getLogger(VideoUtils.class);
	private static String ffmpegFile; 		// ffmpeg.exe所放的路径
	private static String mencoderFile;		//  mencoder.exe所放的路径
	private static String qtFaststartFile;	//  qt-faststart.exe所放的路径
	
	private String inputFile = "";				// 需转换的原始文件名称
	private String inputFileExtension = "";		// 当前文件的文件后缀
	
	private String outputFile = "";				// 输出文件名称
	private String outputFileExtension = "mp4";	// 最终转换的文件格式 mp4 flv
	
	private String imgFile = "";				// 生成缩略图文件名
	private String imgFileExtension = "jpg";	// 生成视频图片截图后缀 jpg gif
	
	private String width = null;//"800";		// 视频默认宽高
	private String height = null;//"600";		// 视频默认宽高
	
	private boolean status = false; 	// 是否正常状态

	/**
	 * 构造函数
	 * @param inputFile 需要转换视频文件的绝对路径和名称
	 */
	public VideoUtils(String inputFile) {
		this(inputFile, null, null);
	}
	
	/**
	 * 构造函数
	 * @param inputFile 需要转换视频文件的绝对路径和名称
	 * @param outputFile 视频文件转换后的输出文件路径和名称
	 * @param imgFile 视频文件截图的图片路径和名称
	 */
	public VideoUtils(String inputFile, String outputFile, String imgFile) {
		this.inputFile = FileUtils.path(inputFile);
		this.inputFileExtension = FileUtils.getFileExtension(inputFile);
		this.outputFile = outputFile != null ? FileUtils.path(outputFile) : inputFile + "." + outputFileExtension;
		this.imgFile = imgFile != null ? imgFile : inputFile + "." + imgFileExtension;
		this.status = checkfile(inputFile);
	}

	/**
	 * 构造函数
	 * @param inputFile 需要转换视频文件的绝对路径和名称
	 * @param outputFile 视频文件转换后的输出文件路径和名称
	 * @param imgFile 视频文件截图的图片路径和名称
	 * @param width 转换后视频和图片的宽度
	 * @param height 转换后视频和图片的高度
	 */
	public VideoUtils(String inputFile, String outputFile, String imgFile, String width, String height){
		this(inputFile, outputFile, imgFile);
		this.width = width;
		this.height = height;
	}

	/**
	 * 检查文件格式。根据文件格式 分类解析
	 * @return int 0:ffmpag;1:mencoder;0:不支持的格式
	 */
	private int checkContentType() {
		// ffmpeg 能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv,rm,rmvb等)
		if (StringUtils.inString(inputFileExtension, "avi", "mpg", "wmv", "3gp", "mov",
				"mp4", "asf", "asx", "flv", "rm", "rmvb")) {
			return 0;
		}
		// 对ffmpeg无法解析的文件格式(wmv9,等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
		else if (StringUtils.inString(inputFileExtension, "wmv9")) {
			return 1;
		}
		return 9;
	}

	/**
	 * 截取图片
	 * @return boolean  
	 */
	public boolean cutPic() {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		boolean statusTemp = status;
		if (statusTemp) {
			statusTemp = processFfmpegCutpic(inputFile, outputFile);
			try {
				File imgfile = new File(imgFile);
				if (imgfile.exists()){
					ImageUtils.thumbnails(imgfile, 800, 600, null);
				}else{
					statusTemp = false;
				}
			} catch (Exception e) {
				statusTemp = false;
				log.error("视频剪切图片失败", e);
			}
		}
		log.debug("视频剪切图片" + (statusTemp ? "成功" : "失败") + ",用时:" + TimeUtils.formatDateAgo(System.currentTimeMillis() - startTime));
		return statusTemp;
	}

	/**
	 * 转换视频
	 * @return boolean  
	 */
	public boolean convert() {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		boolean statusTemp = status;
		int type = checkContentType();
		String tempFile = outputFile + ".tmp";
		if (statusTemp && type == 0) {
			log.debug("使用ffmpage进行视频转换");
			statusTemp = processFfmpeg(inputFile, tempFile);
		} else if (statusTemp && type == 1) {
			log.debug("使用mencoder进行视频转换");
			statusTemp = processMencoder(inputFile, tempFile);
		}
		if (statusTemp){
			log.debug("将mp4视频的元数据信息转到视频第一帧");
			statusTemp = processQtFaststart(tempFile, outputFile);
		}
		log.debug("删除临时文件");
		FileUtils.deleteFile(tempFile);
		log.debug("视频转换" + (statusTemp ? "成功" : "失败") + ",用时:" + TimeUtils.formatDateAgo(System.currentTimeMillis() - startTime));
		return statusTemp;
	}

	/**
	 * 检查文件是否存在
	 * @param inputFile
	 * @return boolean
	 */
	public boolean checkfile(String inputFile) {
		File file = new File(inputFile);
		if (!file.isFile() || !file.exists()) {
			log.warn("文件不存在!");
			return false;
		}
		return true;
	}

	/**
	 * ffmpeg 截取缩略图
	 * @param inputFile
	 * @return boolean  
	 */
	public boolean processFfmpegCutpic(String inputFile, String outputFile) {
		List<String> command = new java.util.ArrayList<String>();
		command.add(getFfmpegFile());
		command.add("-i");
		command.add(inputFile);
		if ((imgFileExtension.toLowerCase()).equals("gif")) {
			command.add("-vframes");
			command.add("30");
			command.add("-f");
			command.add("gif");
		} else {
			command.add("-ss");
			command.add("4");
			command.add("-t");
			command.add("0.001");
			command.add("-f");
			command.add("image2");
		}
//		if (StringUtils.isNotBlank(width) && StringUtils.isNotBlank(height)){
//			command.add("-s");
//			command.add((width + "x" + height));
//		}
		command.add("-y");
		command.add(imgFile);
		return process(command);
	}
	
	/**
	 * ffmpeg能解析转换视频
	 * @param inputFile (asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
	 * @return boolean  
	 */
	private boolean processFfmpeg(String inputFile, String outputFile) {
		List<String> command = new java.util.ArrayList<String>();
		command.add(getFfmpegFile());
		command.add("-i");
		command.add(inputFile);
		command.add("-f");
		command.add(outputFileExtension);
		command.add("-c:v");
		command.add("libx264");
		command.add("-b:v");
		command.add("600k");
		command.add("-g");
		command.add("300");
		command.add("-bf");
		command.add("2");
		command.add("-c:a");
		command.add("aac");
		command.add("-strict");
		command.add("experimental");
		command.add("-ac");
		command.add("1");
		command.add("-ar");
		command.add("44100"); // 11025 22050 32000 44100
		command.add("-r");
		command.add("29.97");
		command.add("-qscale");
		command.add("6");
		if (StringUtils.isNotBlank(width) && StringUtils.isNotBlank(height)){
			command.add("-s");
			command.add((width + "x" + height));
		}
		command.add("-y");
		command.add(outputFile);
		return process(command);
	}

	/**
	 * 直接转换不需要转成avi在转换
	 * @return boolean  
	 */
	private boolean processMencoder(String inputFile, String outputFile) {
		List<String> command = new ArrayList<String>();
		command.add(getMencoderFile());
		command.add(inputFile);
		command.add("-oac");
		command.add("mp3lame");
		command.add("-lameopts");
		command.add("aq=7:vbr=2:q=6");
		command.add("-srate");
		command.add("44100");
		if (StringUtils.isNotBlank(width) && StringUtils.isNotBlank(height)){
			command.add("-vf");
			command.add(("scale=" + width + ":" + height + ",harddup"));
		}
		command.add("-ovc");
		command.add("xvid");
		command.add("-xvidencopts");
		command.add("fixed_quant=8");
		command.add("-of");
		command.add("lavf");
		command.add("-o");
		command.add(outputFile);
		return process(command);
	}

	/**
	 * 将mp4视频的元数据信息转到视频第一帧
	 * @return boolean  
	 */
	private boolean processQtFaststart(String inputFile, String outputFile) {
		List<String> command = new ArrayList<String>();
		command.add(getQtFaststartFile());
		command.add(inputFile);
		command.add(outputFile);
		return process(command);
	}

	/**
	 * 执行命令
	 * @param command
	 * @return boolean  
	 */
	private boolean process(List<String> command) {
		try {
			log.debug(ListUtils.convertToString(command, " "));
//			Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
			Process process = Runtime.getRuntime().exec(command.toArray(new String[command.size()]));
			new PrintErrorReader(process.getErrorStream()).start();
			new PrintInputStream(process.getInputStream()).start();
			process.waitFor();
			return true;
		} catch (Exception e) {
			if (StringUtils.contains(e.getMessage(), "CreateProcess error=2")){
				log.error("缺少视频转换工具,请配置video.ffmpegFile相关参数。" + e.getMessage());
			}else{
				log.error(e.getMessage(), e);
			}
			return false;
		}
	}

	public static String getFfmpegFile() {
		if (ffmpegFile == null){
			ffmpegFile = PropertiesUtils.getInstance().getProperty("video.ffmpegFile");
		}
		return ffmpegFile;
	}

	public static void setFfmpegFile(String ffmpegFile) {
		VideoUtils.ffmpegFile = ffmpegFile;
	}

	public static String getMencoderFile() {
		if (mencoderFile == null){
			mencoderFile = PropertiesUtils.getInstance().getProperty("video.mencoderFile");
		}
		return mencoderFile;
	}

	public static void setMencoderFile(String mencoderFile) {
		VideoUtils.mencoderFile = mencoderFile;
	}

	public static String getQtFaststartFile() {
		if (qtFaststartFile == null){
			qtFaststartFile = PropertiesUtils.getInstance().getProperty("video.qtFaststartFile");
		}
		return qtFaststartFile;
	}

	public static void setQtFaststartFile(String qtFaststartFile) {
		VideoUtils.qtFaststartFile = qtFaststartFile;
	}

	public String getInputFile() {
		return inputFile;
	}

	public void setInputFile(String inputFile) {
		this.inputFile = FileUtils.path(inputFile);
	}

	public String getInputFileExtension() {
		return inputFileExtension;
	}

	public void setInputFileExtension(String inputFileExtension) {
		this.inputFileExtension = inputFileExtension;
	}
	
	public String getOutputFile() {
		return outputFile;
	}

	public void setOutputFile(String outputFile) {
		this.outputFile = FileUtils.path(outputFile);
	}

	public String getOutputFileExtension() {
		return outputFileExtension;
	}

	public void setOutputFileExtension(String outputFileExtension) {
		this.outputFileExtension = outputFileExtension;
	}

	public String getImgFile() {
		return imgFile;
	}

	public void setImgFile(String imgFile) {
		this.imgFile = imgFile;
	}

	public String getImgFileExtension() {
		return imgFileExtension;
	}

	public void setImgFileExtension(String imgFileExtension) {
		this.imgFileExtension = imgFileExtension;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	class PrintInputStream extends Thread {
		java.io.InputStream __is = null;

		public PrintInputStream(java.io.InputStream is) {
			__is = is;
		}

		@Override
		public void run() {
			try {
				BufferedReader br = new BufferedReader(new InputStreamReader(__is));
				String line = null;
				while ((line = br.readLine()) != null) {
					log.debug(line);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	class PrintErrorReader extends Thread {
		java.io.InputStream __is = null;

		public PrintErrorReader(java.io.InputStream is) {
			__is = is;
		}

		@Override
		public void run() {
			try {
				BufferedReader br = new BufferedReader(new InputStreamReader(__is));
				String line = null;
				while ((line = br.readLine()) != null) {
					log.error(line);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

//	public static void main(String[] args) {
////		VideoUtils.setFfmpegFile("d:/tools/video/ffmpeg-4.9/bin/ffmpeg.exe");
//		VideoUtils.setFfmpegFile("d:/tools/video/libav-10.6-win64/bin/avconv.exe");
//		VideoUtils.setMencoderFile("d:/tools/video/mencoder-4.9/mencoder.exe");
//		VideoUtils.setQtFaststartFile("d:/tools/video/qt-faststart/qt-faststart.exe");
//		final VideoUtils v = new VideoUtils("e:/Users/Administrator/Desktop/mp4/20150001.mp4");
////		new Thread(new Runnable() {
////			@Override
////			public void run() {
//				System.out.println("img:转换" + (v.cutPic()?"成功":"失败"));
//				System.out.println("file:转换" + (v.convert()?"成功":"失败"));
////			}
////		}).start();
//		System.out.println("inputFile: " + v.getInputFile());
//		System.out.println("imageFile: " + v.getImgFile());
//		System.out.println("outputFile: " + v.getOutputFile());
//	}

}