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

查看:149
本文介绍了如何使用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中:持续时间在几秒钟内(见文档), 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);

如果您的文件格式没有标题,如MPEG,则可能需要添加此行 avformat_open_input 从数据包中读取信息(可能较慢):

If you have a file format with no headers, like MPEG, you may need to add this line after avformat_open_input to read information from the packets (which might be slower):

avformat_find_stream_info(pFormatCtx, NULL);

修改


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

  • 添加 avformat_find_stream_info(pFormatCtx,NULL)可以使用没有标题的视频类型,例如MPEG

  • Added allocation and de-allocation of pFormatCtx in the code example.
  • Added avformat_find_stream_info(pFormatCtx, NULL) to work with video types that have no headers such as MPEG

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

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