Bootstrap

Java使用FFMPEG获取在线URL视频时长

1、首先安装FFMPEG并配置环境变量

下载地址:Download FFmpeg

2、代码实现(通过Java执行cmd命令获取视频时长)

public static void getTimes(List<CmsOutlineListVO> resultList) {

        String videoUrl = "https://xxx.mp4"; // 替换为实际的视频URL

        try {
            ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg", "-i", videoUrl);
            Process process = processBuilder.start();

            // 读取FFmpeg命令的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                // 检查输出中是否包含视频时长信息
                if (line.contains("Duration:")) {
                    String duration = line.split(",")[0].split("Duration: ")[1].trim();
                    System.out.println("视频时长: " + duration);
                    break;
                }
            }
            // 关闭读取器和进程
            reader.close();
            process.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

;