ffmpeg下载和实战获取音视频时长

目录

一、介绍和下载

(1)介绍

(2)下载

二、实战演示

(1)通过ffmpeg获取音视频的时长

[1. 命令行方式:](#1. 命令行方式:)

[2. java代码实现:](#2. java代码实现:)


一、介绍和下载

(1)介绍

FFmpeg 是一个​​跨平台的开源音视频处理解决方案​​,它包含了庞大的库集合和命令行工具,用于处理多媒体内容(音频、视频、字幕等)。

(2)下载

Download FFmpeghttps://ffmpeg.org/download.html

二、实战演示

(1)通过ffmpeg获取音视频的时长

1. 命令行方式:

复制代码
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 D:\\Desktop\\测试音频\\119.mp3
  • -v error:只显示错误信息,避免多余输出。
  • -show_entries format=duration:仅显示时长信息。
  • -of default=noprint_wrappers=1:nokey=1:简化输出格式,只返回数值(单位:秒)。

2. java代码实现:

复制代码
package com.grmdcxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FFmpegDuration {
    public static void main(String[] args) {
        String mediaPath = "D:\\Desktop\\教程视频\\框架讲解\\旋转骰子效果.mp4";
        double duration = getMediaDuration(mediaPath);
        System.out.println("音视频时长(秒): " + duration);
        System.out.println("音视频时长(HH:MM:SS): " + secondsToTime(duration));
    }

    /**
     * 调用 ffprobe 获取音视频时长(秒)
     */
    public static double getMediaDuration(String filePath) {
        try {
            // 构建 ffprobe 命令
            String[] cmd = {
                "ffprobe",
                "-v", "error",
                "-show_entries", "format=duration",
                "-of", "default=noprint_wrappers=1:nokey=1",
                filePath
            };

            // 执行命令并读取输出
            Process process = Runtime.getRuntime().exec(cmd);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = reader.readLine();

            if (line != null) {
                return Double.parseDouble(line);
            }
        } catch (IOException | NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 将秒数转换为 HH:MM:SS 格式
     */
    public static String secondsToTime(double seconds) {
        int hours = (int) (seconds / 3600);
        int minutes = (int) ((seconds % 3600) / 60);
        int secs = (int) (seconds % 60);
        return String.format("%02d:%02d:%02d", hours, minutes, secs);
    }
}
相关推荐
程序员老陆2 天前
WHIP 与 WHEP:WebRTC 直播的标准化入口与出口
ffmpeg·音视频·webrtc·whip·whep
可乐鸡翅yeah_3 天前
混合内容 Mixed‑Content 安全策略,HLS HTTPS 页面加载 HTTP 资源排错实战
运维·ffmpeg·音视频·媒体·m3u8
刘广睿4 天前
视频导出为什么发灰?色彩空间与色彩范围(BT.601/709/2020)的 ffmpeg 转换实战
ffmpeg·音视频·视频处理·色彩空间·素材管理
yivifu4 天前
精选FFmpeg 实用音频视频转换命令集
ffmpeg·音视频转换
风哥2号7 天前
数据库教程FGMT02‑生产环境Linux+Oracle19c安装配置与项目实战
linux·数据库·ffmpeg
Bruce_Liuxiaowei8 天前
录屏片段无损合并:从 ffmpeg concat 原理到 Python 自动化脚本
python·ffmpeg·自动化
工具派10 天前
口播视频怎么自动剪掉冷场?视频静音删除的阈值实测
ffmpeg·音视频·视频
❀͜͡傀儡师10 天前
移动端 360° 商品展示方案:从 20MB 雪碧图到 800KB 丝滑视频
ffmpeg·webgl·sprite
2501_9304724410 天前
深度复盘|数据库迁移实战(上):腾讯云助手解析慢查询日志,定位索引缺失与语法不兼容
数据库·阿里云·ffmpeg·云计算·腾讯云·aws
我就是DaLing呀!10 天前
flutter + ffmpeg_kit_extended_flutter 大视频合并下载
flutter·ffmpeg·音视频