zoukankan      html  css  js  c++  java
  • Java操作ffmpeg遇到卡死问题

    问题描述

    使用Procees+Runtime操作ffmpeg时,执行比较大的文件操作会卡死,但cmd打开ffmpeg能正常执行

    问题原因

    因为waitfor()操作时wait但没有指定时间,导致jvm缓冲流满了,卡死

    处理

      /**
         * 处理process输出流和错误流,防止进程阻塞
         * 在process.waitFor();前调用
         * @param process
         */
        private static void dealStream(Process process) {
            if (process == null) {
                return;
            }
    
            // 处理InputStream的线程
            new Thread() {
                @Override
                public void run() {
                    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String line = null;
                    try {
                        while ((line = in.readLine()) != null) {
                            // logger.info("output: " + line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            // 处理ErrorStream的线程
            new Thread() {
                @Override
                public void run() {
                    BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                    String line = null;
                    try {
                        while ((line = err.readLine()) != null) {
                            //  logger.info("err: " + line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            err.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
    
  • 相关阅读:
    java注解
    SpringBoot 整合 MybatisPlus 3.0
    第三章 列表简介
    第二章 变量及简单数据类型
    收藏通俗知识
    2017.8.13 列标,元组,字符串
    Python入门--4--分之和循环
    Python入门---易错已错易混淆----知识点
    Perl语言入门--4--函数
    perl学习之精髓中的精髓
  • 原文地址:https://www.cnblogs.com/psyduck/p/14085657.html
Copyright © 2011-2022 走看看