package com.aa.iot.mapp.std.videocenter.hk.process;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.aa.iot.mapp.std.videocenter.scheduler.VideoDataSynchronizTask;

/**
 * 调用ffmpeg进行
 * 
 * @author duhai
 * @date 2021年3月8日
 */
@Component
public class FfmpegExecProcess {

	private final static Logger logger = LoggerFactory.getLogger(VideoDataSynchronizTask.class);

	/**
	 * 文件格式转换
	 * 
	 * @param oldFilePath
	 * @param newFilePath
	 * @return
	 */
	public Boolean fileTypeChange(final String oldFilePath, final String newFilePath) {
		if (logger.isInfoEnabled()) {
			logger.info("##### fileTypeChange 开始 oldFilePath:{}, newFilePath:{} #####", oldFilePath, newFilePath);
		}
		try {
			//String command = "ffmpeg -i " + oldFilePath + " -c copy " + newFilePath;
			List<String> command = new ArrayList<String>();
			command.add("ffmpeg");
			command.add("-i");
			command.add(oldFilePath);
			command.add("-c");
			command.add("copy");
			command.add(newFilePath);
	        
			ProcessBuilder builder = new ProcessBuilder(command);
			// 合并输出流和错误流
			builder.redirectErrorStream(true);
			// 启动进程
			Process process = builder.start();
			// 获得输出流
			BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String line = null;
			System.out.println("ffmpeg start <info>");
			while (null != (line = br.readLine())) {
				System.out.println(line);
			}
			System.out.println("</info> ffmpeg end ");
			int exitVal = process.waitFor();
			System.out.println("Process exitValue: " + exitVal);
		} catch (Throwable t) {
			logger.error("##### fileTypeChange 失败 #####", t);
			return false;
		}
		if (logger.isInfoEnabled()) {
			logger.info("##### fileTypeChange 结束 #####");
		}
		return true;
	}

	/**
	 * 文件格式转换(会有进程卡死的问题)
	 * 
	 * @param oldFilePath
	 * @param newFilePath
	 * @return
	 */
	public static Boolean fileTypeChange2(final String oldFilePath, final String newFilePath) {
		if (logger.isInfoEnabled()) {
			logger.info("##### FfmpegExecProcess fileTypeChange 开始 oldFilePath:{}, newFilePath:{} #####", oldFilePath,
					newFilePath);
		}
		try {
			String command = "ffmpeg -i " + oldFilePath + " -c copy " + newFilePath;
			logger.info("command:" + command);
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec(command);
			InputStream stderr = proc.getErrorStream();
			InputStreamReader isr = new InputStreamReader(stderr);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			logger.info("ffmpeg start <info>");
			while ((line = br.readLine()) != null) {
				logger.info(line);
			}
			logger.info("</info> ffmpeg end ");
			int exitVal = proc.waitFor();
			logger.info("Process exitValue: " + exitVal);
		} catch (Throwable t) {
			logger.error("##### FfmpegExecProcess fileTypeChange 失败 #####", t);
			return false;
		}
		if (logger.isInfoEnabled()) {
			logger.info("##### FfmpegExecProcess fileTypeChange 结束 #####");
		}
		return true;
	}
}

 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐