java获取视频文件时长和尺寸
时间: 2023-11-27 17:54:44 浏览: 52
获取视频文件时长和尺寸可以使用Java开源库FFmpeg,以下是示例代码:
```java
import org.bytedeco.javacv.FFmpegFrameGrabber;
public class VideoInfo {
public static void main(String[] args) {
String videoPath = "your_video_path";
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoPath);
try {
grabber.start();
int width = grabber.getImageWidth();
int height = grabber.getImageHeight();
double duration = grabber.getLengthInTime() / 1000000.0;
System.out.println("Duration: " + duration + " seconds");
System.out.println("Resolution: " + width + "x" + height);
grabber.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
在上述代码中,我们通过创建一个`FFmpegFrameGrabber`对象,指定视频文件路径,然后通过`start()`方法启动抓取器获取视频信息。通过`getImageWidth()`和`getImageHeight()`方法获取视频的宽度和高度,`getLengthInTime()`方法获取视频时长,最后通过`stop()`方法关闭抓取器。需要注意的是,`getLengthInTime()`方法返回的是微秒数,我们需要将其除以1000000.0得到秒数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)