Java-解析音频文件时长

3 篇文章 0 订阅
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FfmpegCmdDurationTest {

    private final static String DURATION_START = "Duration:";

    private final static String KEY_FOR_HOUR = "hour";
    private final static String KEY_FOR_MINUTE = "minute";
    private final static String KEY_FOR_SECONED = "seconed";
    private final static String KEY_FOR_MILLSECONED = "millseconed";

    //小时 * 60 = 分
    //分 * 60 = 秒
    private final static BigDecimal GAP_60 = new BigDecimal("60");
    //秒* 1000 = 毫秒
    private final static BigDecimal GAP_1000 = new BigDecimal("1000");

    /**
     *  TYPE_0:小时
     *  TYPE_1:分钟
     *  TYPE_2:秒钟
     *  TYPE_3:毫秒
     */
    public final static int TYPE_0 = 0;
    public final static int TYPE_1 = 1;
    public final static int TYPE_2 = 2;
    public final static int TYPE_3 = 3;

    //ffmpeg执行命令 1665557874.18  1665556459.14
    private final static String cmd_4_info = "-i D:/upload/sound//1665557874.18.mp3";

    public static void main(String[] args) {
        try {
            //System.out.println(String.format("获取时长:%s 小时", duration(cmd_4_info,TYPE_0).doubleValue()));
            //System.out.println(String.format("获取时长:%s 分钟", duration(cmd_4_info,TYPE_1).doubleValue()));
            System.out.println(String.format("获取时长:%s 秒钟", duration(cmd_4_info,TYPE_2).setScale(0, BigDecimal.ROUND_FLOOR)));
            System.out.println(duration(cmd_4_info,2).setScale(0, BigDecimal.ROUND_FLOOR).toPlainString());
            //System.out.println(String.format("获取时长:%s 毫秒", duration(cmd_4_info,TYPE_3).doubleValue()));
            //System.out.println("");
            //System.out.println(String.format("获取时长(格式化):%s", durationFormat(cmd_4_info)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     *
     * @Description: (获取格式化的时间duration:such as:00:01:03.32)
     * @param: @param cmd_4_info
     * @param: @return
     * @param: @throws Exception
     * @return: BigDecimal
     * @throws
     */
    public static String durationFormat(final String cmd_4_info) throws Exception {
        String duration = null;
        Map<String,String> map = null;
        try {
            CompletableFuture<String> completableFutureTask = CompletableFuture.supplyAsync(() ->{
                String durationStr = null;
                //执行ffmpeg命令
                StringBuffer sText = getErrorStreamText(cmd_4_info);
                if(null != sText && sText.indexOf(DURATION_START) > -1) {
                    String stextOriginal = sText.toString();
                    //正则解析时间
                    Matcher matcher = patternDuration().matcher(stextOriginal);
                    //正则提取字符串
                    while(matcher.find()){
                        //such as:00:01:03.32
                        durationStr = matcher.group(1);
                        break;
                    }
                }
                return durationStr;
            }, ThreadPoolExecutorUtils.pool);

            duration = completableFutureTask.get();

        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return duration;
    }
    /**
     *
     * @Description: (获取时长)
     * @param: @param cmd_4_info ffmpeg命令,如:-i I:\\荣耀视频测试.mp4
     * @param: @param type 类型:
     *                        TYPE_0:小时
     *                        TYPE_1:分钟
     *                        TYPE_2:秒钟
     *                        TYPE_3:毫秒
     * @param: @return
     * @return: BigDecimal
     * @throws Exception
     * @throws
     */
    public static BigDecimal duration(final String cmd_4_info, int type) throws Exception {
        BigDecimal duration = new BigDecimal("00");
        Map<String,String> map = null;
        try {
            CompletableFuture<Map<String,String>> completableFutureTask = CompletableFuture.supplyAsync(() ->{
                Map<String,String> mapTmp = null;
                //执行ffmpeg命令
                StringBuffer sText = getErrorStreamText(cmd_4_info);
                if(null != sText && sText.indexOf(DURATION_START) > -1) {
                    String stextOriginal = sText.toString();
                    //正则解析时间
                    Matcher matcher = patternDuration().matcher(stextOriginal);
                    //正则提取字符串
                    while(matcher.find()){
                        //such as:00:01:03.32
                        String durationStr = matcher.group(1);
                        mapTmp = getHourMinuteSeconedMillseconed(durationStr);
                        break;
                    }
                }
                return mapTmp;
            }, ThreadPoolExecutorUtils.pool);

            map = completableFutureTask.get();
            if(null != map && map.size() > 0) {
                switch (type) {
                    case TYPE_0:
                        //小时
                        duration = duration.add(new BigDecimal(map.get(KEY_FOR_HOUR)));
                        break;
                    case TYPE_1:
                        //分钟
                        duration = duration.add(new BigDecimal(map.get(KEY_FOR_HOUR)).multiply(GAP_60))
                                .add(new BigDecimal(map.get(KEY_FOR_MINUTE)));
                        break;
                    case TYPE_2:
                        //秒
                        duration = duration.add(new BigDecimal(map.get(KEY_FOR_HOUR)).multiply(GAP_60).multiply(GAP_60))
                                .add(new BigDecimal(map.get(KEY_FOR_MINUTE)).multiply(GAP_60))
                                .add(new BigDecimal(map.get(KEY_FOR_SECONED)));
                        break;
                    case TYPE_3:
                        //毫秒
                        duration = duration.add(new BigDecimal(map.get(KEY_FOR_HOUR)).multiply(GAP_60).multiply(GAP_60).multiply(GAP_1000))
                                .add(new BigDecimal(map.get(KEY_FOR_MINUTE)).multiply(GAP_60).multiply(GAP_1000))
                                .add(new BigDecimal(map.get(KEY_FOR_SECONED)).multiply(GAP_1000))
                                .add(new BigDecimal(map.get(KEY_FOR_MILLSECONED)));
                        break;
                    default:
                        throw new Exception("未知的类型!");
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return duration;
    }

    //模式
    public static Pattern patternDuration() {
        //"(?i)duration:\\s*([0-9\\:\\.]+)"-->匹配到时分秒即可,毫秒不需要
        return Pattern.compile("(?i)duration:\\s*([0-9\\:\\.]+)");
    }
    /**
     *
     * @Description: (获取错误流)
     * @param: @param cmd_4_info
     * @param: @return
     * @return: StringBuffer
     * @throws
     */
    private static StringBuffer getErrorStreamText(String cmdStr) {
        //返回的text
        StringBuffer sText = new StringBuffer();
        FfmpegCmd ffmpegCmd = new FfmpegCmd();
        try {
            ;
            //错误流
            InputStream errorStream = null;
            //destroyOnRuntimeShutdown表示是否立即关闭Runtime
            //如果ffmpeg命令需要长时间执行,destroyOnRuntimeShutdown = false

            //openIOStreams表示是不是需要打开输入输出流:
            //	       inputStream = processWrapper.getInputStream();
            //	       outputStream = processWrapper.getOutputStream();
            //	       errorStream = processWrapper.getErrorStream();
            ffmpegCmd.execute(false, true, cmdStr);
            errorStream = ffmpegCmd.getErrorStream();

            //打印过程
            int len = 0;
            while ((len=errorStream.read())!=-1){
                char t = (char)len;
//		            System.out.print(t);
                sText.append(t);
            }

            //code=0表示正常
            ffmpegCmd.getProcessExitCode();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            ffmpegCmd.close();
        }
        //返回
        return sText;
    }

    /**
     *
     * @Description: (获取duration的时分秒毫秒)
     * @param: durationStr  such as:00:01:03.32
     * @return: Map
     * @throws
     */
    private static Map<String,String> getHourMinuteSeconedMillseconed(String durationStr){
        HashMap<String,String> map = new HashMap<>(4);
        if(null != durationStr && durationStr.length() > 0) {
            String[] durationStrArr = durationStr.split("\\:");
            String hour = durationStrArr[0];
            String minute = durationStrArr[1];
            //特殊
            String seconed = "00";
            String millseconed = "00";
            String seconedTmp = durationStrArr[2];
            if(seconedTmp.contains("\\.")) {
                String[] seconedTmpArr = seconedTmp.split("\\.");
                seconed = seconedTmpArr[0];
                millseconed = seconedTmpArr[1];
            }
            else {
                seconed = seconedTmp;
            }
            map.put(KEY_FOR_HOUR, hour);
            map.put(KEY_FOR_MINUTE, minute);
            map.put(KEY_FOR_SECONED, seconed);
            map.put(KEY_FOR_MILLSECONED, millseconed);
        }
        return map;
    }
}

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

import ws.schild.jave.process.ProcessKiller;
import ws.schild.jave.process.ProcessWrapper;
import ws.schild.jave.process.ffmpeg.DefaultFFMPEGLocator;

public class FfmpegCmd {

    private static final Logger LOG = LoggerFactory.getLogger(ProcessWrapper.class);

    /** The process representing the ffmpeg execution. */
    private Process ffmpeg = null;

    /**
     * A process killer to kill the ffmpeg process with a shutdown hook, useful if the jvm execution
     * is shutted down during an ongoing encoding process.
     */
    private ProcessKiller ffmpegKiller = null;

    /** A stream reading from the ffmpeg process standard output channel. */
    private InputStream inputStream = null;

    /** A stream writing in the ffmpeg process standard input channel. */
    private OutputStream outputStream = null;

    /** A stream reading from the ffmpeg process standard error channel. */
    private InputStream errorStream = null;

    /**
     * Executes the ffmpeg process with the previous given arguments.
     *
     * @param destroyOnRuntimeShutdown destroy process if the runtime VM is shutdown
     * @param openIOStreams Open IO streams for input/output and errorout, should be false when
     *     destroyOnRuntimeShutdown is false too
     * @param ffmpegCmd  windows such as (mp4 transform to mov):
     *     " -i C:\\Users\\hsj\\AppData\\Local\\Temp\\jave\\honer.mp4 -c copy C:\\Users\\hsj\\AppData\\Local\\Temp\\jave\\honer_test.mov "
     * @throws IOException If the process call fails.
     */
    public void execute(boolean destroyOnRuntimeShutdown, boolean openIOStreams, String ffmpegCmd) throws IOException {
        DefaultFFMPEGLocator defaultFFMPEGLocator = new DefaultFFMPEGLocator();

        StringBuffer cmd = new StringBuffer(defaultFFMPEGLocator.getExecutablePath());
        //insert blank for delimiter
        cmd.append(" ");
        cmd.append(ffmpegCmd);
        String cmdStr = String.format("ffmpegCmd final is :%s", cmd.toString());
        System.out.println(cmdStr);
        LOG.info(cmdStr);

        Runtime runtime = Runtime.getRuntime();
        try {
            ffmpeg = runtime.exec(cmd.toString());

            if (destroyOnRuntimeShutdown) {
                ffmpegKiller = new ProcessKiller(ffmpeg);
                runtime.addShutdownHook(ffmpegKiller);
            }

            if (openIOStreams) {
                inputStream = ffmpeg.getInputStream();
                outputStream = ffmpeg.getOutputStream();
                errorStream = ffmpeg.getErrorStream();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns a stream reading from the ffmpeg process standard output channel.
     *
     * @return A stream reading from the ffmpeg process standard output channel.
     */
    public InputStream getInputStream() {
        return inputStream;
    }

    /**
     * Returns a stream writing in the ffmpeg process standard input channel.
     *
     * @return A stream writing in the ffmpeg process standard input channel.
     */
    public OutputStream getOutputStream() {
        return outputStream;
    }

    /**
     * Returns a stream reading from the ffmpeg process standard error channel.
     *
     * @return A stream reading from the ffmpeg process standard error channel.
     */
    public InputStream getErrorStream() {
        return errorStream;
    }

    /** If there's a ffmpeg execution in progress, it kills it. */
    public void destroy() {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Throwable t) {
                LOG.warn("Error closing input stream", t);
            }
            inputStream = null;
        }

        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (Throwable t) {
                LOG.warn("Error closing output stream", t);
            }
            outputStream = null;
        }

        if (errorStream != null) {
            try {
                errorStream.close();
            } catch (Throwable t) {
                LOG.warn("Error closing error stream", t);
            }
            errorStream = null;
        }

        if (ffmpeg != null) {
            ffmpeg.destroy();
            ffmpeg = null;
        }

        if (ffmpegKiller != null) {
            Runtime runtime = Runtime.getRuntime();
            runtime.removeShutdownHook(ffmpegKiller);
            ffmpegKiller = null;
        }
    }

    /**
     * Return the exit code of the ffmpeg process If the process is not yet terminated, it waits for
     * the termination of the process
     *
     * @return process exit code
     */
    public int getProcessExitCode() {
        // Make sure it's terminated
        try {
            ffmpeg.waitFor();
        } catch (InterruptedException ex) {
            LOG.warn("Interrupted during waiting on process, forced shutdown?", ex);
        }
        return ffmpeg.exitValue();
    }

    /**close**/
    public void close() {
        destroy();
    }

}

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorUtils {

    //多线程
    public static int core = Runtime.getRuntime().availableProcessors();
    public static ExecutorService pool = new ThreadPoolExecutor(core,//核心
            core * 2,//最大
            0L,//空闲立即退出
            TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(1024),//无边界阻塞队列
            new ThreadPoolExecutor.AbortPolicy());
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java -jar 配置文件是指使用Java命令来启动Java程序,并通过配置文件来指定程序的一些特定参数和配置信息。通常情况下,Java程序是通过命令行来启动的,而使用Java -jar命令则是一种更加便捷和灵活的方式。 Java -jar命令需要指定程序的jar包路径,通过该jar包中的MANIFEST.MF文件中定义的Main-Class来确定程序的入口。同时,配置文件也可以被嵌入到jar包中,通过使用java -jar命令时加上-cp参数指定相应的配置文件路径来实现读取配置文件。 Java -jar命令的优势在于可以在不额外安装任何其他工具或库的情况下,轻松地启动Java程序,并且保证程序的可移植性和独立性。它适用于各种应用场景,比如打包成可执行的jar包来共享给其他用户,通过Jenkins等CI/CD工具自动化部署和执行等。 需要注意的是,使用Java -jar命令启动Java程序时,程序内部的类路径可能会受到限制。因此,在使用Java -jar命令时,需要注意程序中的路径和资源引用是否正确。此外,配置文件也需要按照一定的格式进行编写,以保证程序的正常启动和运行。 ### 回答2: Java -jar 配置文件是什么意思? Java -jar命令是用于启动Java应用程序的命令,而配置文件则是一种用于指定应用程序设置的文件。因此,Java -jar配置文件其实就是指定Java应用程序的配置文件,以便在运行Java应用程序时加载配置文件中的设置。 Java应用程序的配置文件通常使用XML或属性文件格式,包含有关应用程序的各种设置,如数据库连接字符串、日志级别、缓存大小等。在Java应用程序中,通常使用Java配置库来读取和解析配置文件。 如何使用Java -jar配置文件? 使用Java -jar配置文件的步骤如下: 1. 编写配置文件。创建一个XML或属性文件,设置应用程序需要的各种设置。 2. 修改启动脚本。在启动脚本中添加-javaoption参数,并指定要加载的配置文件路径。例如: java -jar MyApp.jar -javaoption:-Dconfig.file=/path/to/config.xml 3. 在Java应用程序中加载配置。使用Java配置库加载和解析配置文件,以获取应用程序需要的设置。 使用Java -jar配置文件的好处? 使用Java -jar配置文件的好处在于: 1. 管理应用程序设置。通过配置文件,可以方便地管理应用程序的各种设置,而不需要修改Java应用程序的代码。 2. 提高应用程序的灵活性。通过配置文件,可以动态地改变应用程序的设置,而无需重新编译和部署Java应用程序。 3. 便于维护和升级。通过配置文件,可以将应用程序的设置与代码分开,使维护和升级更加方便。 ### 回答3: Java -jar 配置文件是指通过命令行方式启动 Java 应用程序,并通过指定配置文件的方式进行应用程序的配置。一般来说,Java 应用程序需要在启动时加载各种配置信息,例如数据库连接信息、应用程序参数、系统参数等等,因此需要通过配置文件的方式来管理这些信息。 Java -jar 命令是 Java 运行环境提供的一个命令行工具,可以将一个以 jar 文件格式打包的 Java 应用程序直接在命令行中运行。通过 -jar 参数指定要启动的 jar 文件名,就可以启动 Java 应用程序。 而在启动时,若需要加载配置文件,则可以通过在命令行中指定配置文件的路径,并由 Java 应用程序来加载、解析和使用配置文件中的各项配置信息。例如,可以使用 -D 参数指定 Java 系统属性或使用 -cp 参数指定类路径等等。 当然,在实际使用中,不同的 Java 应用程序会有不同的配置文件格式和加在方式,具体操作需要参考该应用程序的文档说明或开发者规范。但总体来说,利用 Java -jar 配置文件可以方便、快捷地启动并配置 Java 应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值