package com.ozs.utils; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @Slf4j @Component public class CmdCameraUtil { public void cmd(String cmd) { long l = System.currentTimeMillis(); Process exec = null; try { exec = Runtime.getRuntime().exec(new String[]{"sh", "-c", cmd}); long l1 = System.currentTimeMillis(); log.info("cmd-time:{}", l1 - l); } catch (IOException e) { log.error(e.getMessage()); e.printStackTrace(); } try { printProcessMsg(exec); } catch (IOException e) { log.error(e.getMessage()); e.printStackTrace(); } try { int i = exec.waitFor(); log.info("------"+i); } catch (InterruptedException e) { log.error(e.getMessage()); e.printStackTrace(); } } /** * 处理process输出流和错误流,防止进程阻塞,在process.waitFor();前调用 * * @param exec * @throws IOException */ private void printProcessMsg(Process exec) throws IOException { //防止ffmpeg进程塞满缓存造成死锁 InputStream error = exec.getErrorStream(); InputStream is = exec.getInputStream(); StringBuffer result = new StringBuffer(); String line = null; try { BufferedReader br = new BufferedReader(new InputStreamReader(error, "GBK")); BufferedReader br2 = new BufferedReader(new InputStreamReader(is, "GBK")); while ((line = br.readLine()) != null) { result.append(line + "\n"); } log.info("FFMPEG视频合成进程错误信息:" + result.toString()); result = new StringBuffer(); line = null; while ((line = br2.readLine()) != null) { result.append(line + "\n"); } log.info("FFMPEG视频合成进程输出内容为:" + result.toString()); } catch (IOException e2) { e2.printStackTrace(); } finally { error.close(); is.close(); } } }