如何使用libavcodec / ffmpeg来查找视频文件的持续时间 [英] how to use libavcodec/ffmpeg to find duration of video file

查看:231
本文介绍了如何使用libavcodec / ffmpeg来查找视频文件的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个库来执行基本功能,例如视频文件的长度,大小等(我通过元数据或标签猜测),所以我选择了 ffmpeg 。有效的视频格式主要是在电影文件中流行的视频格式, wmv,wmvhd,avi,mpeg,mpeg-4 等。如果可以,请帮助我使用方法来了解视频文件的持续时间。我在Linux平台上。

I needed a library to perform basic functions such as length, size, etc of a video file (i'm guessing through the metadata or tags) so I chose ffmpeg. Valid video formats are primarily those prevalent in movie files viz. wmv, wmvhd, avi, mpeg, mpeg-4, etc. If you can, please help me with the method(s) to be used for knowing the duration the video file. I'm on a Linux platform.

推荐答案

libavcodec很难编程,也很难找到文档,所以我感受到你的痛苦。 本教程是一个好的开始。 这里是主要的API文档。

libavcodec is pretty hard to program against, and it's also hard to find documentation, so I feel your pain. This tutorial is a good start. Here is the main API docs.

查询视频文件的主要数据结构是 AVFormatContext 。在教程中,这是你打开的第一件事,使用 av_open_input_file - 文档的说,它已被弃用,你应该使用 avformat_open_input

The main data structure for querying video files is AVFormatContext. In the tutorial, it's the first thing you open, using av_open_input_file -- the docs for that say it's deprecated and you should use avformat_open_input instead.

在这里,您可以读取属性的AVFormatContext: duration 在一些分数秒(见docs), file_size 以字节为单位, bit_rate 等。

From there, you can read properties out of the AVFormatContext: duration in some fractions of a second (see the docs), file_size in bytes, bit_rate, etc.

所以把它放在一起应该像:

So putting it together should look something like:

AVFormatContext* pFormatCtx = avformat_alloc_context();
avformat_open_input(&pFormatCtx, filename, NULL, NULL);
int64_t duration = pFormatCtx->duration;
// etc
avformat_close_input(&pFormatCtx);
avformat_free_context(pFormatCtx);

编辑:在代码示例中添加了pFormatCtx的分配和取消分配。

Edit: Added allocation and de-allocation of pFormatCtx in the code example.

这篇关于如何使用libavcodec / ffmpeg来查找视频文件的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆