CmdCameraUtil.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.ozs.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.stereotype.Component;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. @Slf4j
  9. @Component
  10. public class CmdCameraUtil {
  11. public void cmd(String cmd) {
  12. long l = System.currentTimeMillis();
  13. Process exec = null;
  14. try {
  15. exec = Runtime.getRuntime().exec(new String[]{"sh", "-c", cmd});
  16. long l1 = System.currentTimeMillis();
  17. log.info("cmd-time:{}", l1 - l);
  18. } catch (IOException e) {
  19. log.error(e.getMessage());
  20. e.printStackTrace();
  21. }
  22. try {
  23. printProcessMsg(exec);
  24. } catch (IOException e) {
  25. log.error(e.getMessage());
  26. e.printStackTrace();
  27. }
  28. try {
  29. int i = exec.waitFor();
  30. log.info("------"+i);
  31. } catch (InterruptedException e) {
  32. log.error(e.getMessage());
  33. e.printStackTrace();
  34. }
  35. }
  36. /**
  37. * 处理process输出流和错误流,防止进程阻塞,在process.waitFor();前调用
  38. *
  39. * @param exec
  40. * @throws IOException
  41. */
  42. private void printProcessMsg(Process exec) throws IOException {
  43. //防止ffmpeg进程塞满缓存造成死锁
  44. InputStream error = exec.getErrorStream();
  45. InputStream is = exec.getInputStream();
  46. StringBuffer result = new StringBuffer();
  47. String line = null;
  48. try {
  49. BufferedReader br = new BufferedReader(new InputStreamReader(error, "GBK"));
  50. BufferedReader br2 = new BufferedReader(new InputStreamReader(is, "GBK"));
  51. while ((line = br.readLine()) != null) {
  52. result.append(line + "\n");
  53. }
  54. log.info("FFMPEG视频合成进程错误信息:" + result.toString());
  55. result = new StringBuffer();
  56. line = null;
  57. while ((line = br2.readLine()) != null) {
  58. result.append(line + "\n");
  59. }
  60. log.info("FFMPEG视频合成进程输出内容为:" + result.toString());
  61. } catch (IOException e2) {
  62. e2.printStackTrace();
  63. } finally {
  64. error.close();
  65. is.close();
  66. }
  67. }
  68. }