当前位置: 首页 > news >正文

ffmpeg视频转码相关

ffmpeg视频转码相关

    • 获取视频时长
    • 视频转码

获取视频时长

//    Duration: 00:00:30.03, start: 0.000000, bitrate: 1191 kb/s
    public String getVideoDuration(String inputFilePath) {
        Process process = null;
        try {
            // 定义远程视频的URL
            // 构建FFmpeg命令
            ProcessBuilder processBuilder = new ProcessBuilder(ffmpegPath, "-i",inputFilePath);
            // 读取FFmpeg的输出信息
            // 创建ProcessBuilder并执行命令
            processBuilder.redirectErrorStream(true);
            process = processBuilder.start();
            // 读取FFmpeg命令输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains("Duration:")) {
                    // 获取包含视频时长信息的行
                    String durationLine = line.split("Duration:")[1].split(",")[0].split("\\.")[0].trim();// 00:00:30
//                    String durationLine = line.split("Duration:")[1].split(",")[0].trim();// 00:00:30.03
//                    String[] durationParts = durationLine.split(":");
//                    int hours = Integer.parseInt(durationParts[0].trim());
//                    int minutes = Integer.parseInt(durationParts[1].trim());
//                    double seconds = Double.parseDouble(durationParts[2].trim());
//                    // 计算总秒数
//                    double totalSeconds = hours * 3600 + minutes * 60 + seconds;
                    System.out.println("视频时长:" + durationLine);
                    return durationLine;
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
            if (process != null) {
                process.destroyForcibly();  // 如果发生异常,强制终止进程
            }
        } finally {
            if (process != null) {
                process.destroyForcibly();  // 强制终止进程
            }
        }
        System.out.println("无法获取视频时长。");
        return null;
    }

	public Integer getVideoDurationSecond(String inputFilePath) {
        String videoDuration = getVideoDuration(inputFilePath);
        if (videoDuration == null) {
            return 0;
        }
        String[] durationParts = videoDuration.split(":");
        int hours = Integer.parseInt(durationParts[0].trim());
        int minutes = Integer.parseInt(durationParts[1].trim());
        int seconds = Integer.parseInt(durationParts[2].trim());
        // 计算总秒数
        return hours * 3600 + minutes * 60 + seconds;
    }

视频转码

	public void transcodeVideo(String inputFilePath, String outputFilePath) {
        ProcessBuilder processBuilder = new ProcessBuilder(ffmpegPath, "-i",inputFilePath, "-c:v", "libx264", "-c:a", "aac", outputFilePath);
        try {
            processBuilder.inheritIO(); // 将FFmpeg的输出信息打印到控制台
//            processBuilder.redirectErrorStream(true); // 合并错误输出流
            Process process = processBuilder.start();

            int exitCode = process.waitFor();
            if (exitCode == 0) {
                log.info("视频转码成功!");
            } else {
                log.info("视频转码失败!");
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

参数后续……

http://www.dtcms.com/a/108748.html

相关文章:

  • 统计项目代码行数工具—cloc
  • 001英超:切尔西VS热刺, 伦敦德比“大”战可期
  • 查看进程,认识fork
  • Python 设计模式:外观模式
  • 汽车 HMI 设计的发展趋势与设计要点
  • 《MyBatis CRUD实战与核心配置详解:从基础操作到高级应用》
  • Python入门(8):文件
  • SQL Server Integration Services (SSIS) 服务无法启动
  • I2C 读写 AT24C02
  • Genspark:重新定义搜索体验的AI智能体引擎
  • 循环结构- P1217-回文质数-第三十四天
  • 理解 Cookies:工作原理、类型与隐私安全指南
  • Spring 核心技术解析【纯干货版】- XIX:Spring 日志模块 Spring-Jcl 模块精讲
  • SQL Server:Log Shipping 说明
  • 位运算与集合
  • easyPan技术回顾day4
  • 【蓝桥杯刷题实战】路径之谜
  • APang网联科技项目报告【服务器篇】
  • Shell脚本中的日期变量详解
  • 理解Kotlin高阶函数:传递函数,而不是直接执行
  • 【C++11】异步编程
  • AI agent推理自私属性是否成为社会演化中的生存优势
  • 前端基础之《Vue(1)—简介》
  • 安装 AWS CLI
  • 在汇编层面理解MESI
  • win32汇编环境,网络编程入门之十八
  • 基于CNN-LSTM的深度Q网络(Deep Q-Network,DQN)求解移动机器人路径规划,MATLAB代码
  • RT-Thread 和 FreeRTOS 嵌入式实时操作系统对比
  • 嵌入式学习笔记——ARM
  • 科普:One-Class SVM和SVDD