javacv ffmpeg使用笔记
- 一、maven依赖
- 二、示例代码
- 1. 获取视频时长
- 三、小技巧
一、maven依赖
使用javacv ffmpeg并指定
classifier
之后,就不需要额外安装ffmpeg软件(jar包中已经内置)了。
- 全量依赖包(不推荐)安装包总大小超过1G
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>${javacv.version}</version>
</dependency>
- 最小化安装
可使用
mvn enforcer:display-info
查看当前操作系统信息
<javacv.version>1.5.9</javacv.version>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>${javacv.version}</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>6.0-${javacv.version}</version>
<classifier>linux-x86_64</classifier>
</dependency>
</dependencies>
<profiles>
<profile>
<id>Windows</id>
<activation>
<os>
<!-- mvn enforcer:display-info -->
<family>windows</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>6.0-${javacv.version}</version>
<classifier>windows-x86_64</classifier>
</dependency>
</dependencies>
</profile>
<profile>
<id>Mac</id>
<activation>
<os>
<!-- mvn enforcer:display-info -->
<family>mac</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>6.0-${javacv.version}</version>
<classifier>macosx-x86_64</classifier>
</dependency>
</dependencies>
</profile>
</profiles>
二、示例代码
1. 获取视频时长
public static void videoDuration(String videoFilePath) {
try (FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(videoFilePath)) {
frameGrabber.start();
int duration = frameGrabber.getLengthInVideoFrames()/ (int) frameGrabber.getFrameRate();
System.out.println(duration);
frameGrabber.stop();
} catch (FrameGrabber.Exception e) {
e.printStackTrace();
}
}
三、小技巧
- 日志级别
static {
avutil.av_log_set_level(avutil.AV_LOG_DEBUG);
FFmpegLogCallback.set();
}