avformat_open_input() 函数是用于打开媒体文件并获取媒体文件信息的函数,该函数定义在题】_avformat_open_input url格式-程序员宅基地

技术标签: 数据挖掘  

avformat_open_input() 函数是用于打开媒体文件并获取媒体文件信息的函数,该函数定义在libavformat/avformat.h中。

函数原型为:

 

int avformat_open_input(

AVFormatContext **ps, 

const char *url, 

AVInputFormat *fmt, 

AVDictionary **options);

 

函数的参数含义如下:

ps: AVFormatContext结构体的指针。该参数用于存储打开的媒体文件的信息。

当该函数成功返回时,AVFormatContext结构体中将存储媒体文件的相关信息。

 

url:要打开的媒体文件的URL。可以是本地文件路径,也可以是HTTP URL或其他协议的URL。

 

fmt:AVInputFormat结构体的指针,用于指定媒体文件的格式。如果该参数为NULL,

则根据文件扩展名自动选择输入格式。

 

options:AVDictionary结构体的指针,用于传递打开媒体文件时的选项。

-------------------------------------------------------

 

比较重要结构体介绍

 

AVFormatContext 结构体。该参数用于存储打开的媒体文件的信息。当该函数成功返回时,AVFormatContext结构体中将存储媒体文件的相关信息。是FFmpeg 解封装(flv,mp4,rmvb,avi)功能的结构体。

 

typedef struct AVFormatContext{

    struct AVInputFormat *iformat; //输入数据的封装格式

    AVIOContext *pb; //输入数据的缓存

    unsigned int nb_streams; //视音频流的个数

    AVStream **streams; //视音频流

    char filename[1024]; //文件名

    int64_t duration; //时长(单位:微秒ms,转换为秒需要除以1_000_000) 

    int bit_rate; //比特率(单位bps,转换为kbps需要除以1_000) 

    AVDictionary *metadata; //元数据

}AVFormatContext

-----------------------------------------------------------

 

AVCodecContext是包含变量较多的结构体,AVCodecContext中很多的参数是编码的时候使用的,而不是解码的时候使用的。

 

typedef struct AVCodecContext{

  enum AVMediaType codec_type:编解码器的类型(视频,音频...)

  struct AVCodec *codec:采用的解码器AVCodec(H.264,MPEG2...)

  int bit_rate:平均比特率

  uint8_t *extradata; int extradata_size:针对特定编码器包含的附加信息(例如对于H.264解码器来 

  说,存储SPS,PPS等)

  AVRational time_base:根据该参数,可以把PTS转化为实际的时间(单位为秒s)

  int width, height:如果是视频的话,代表宽和高

  int refs:运动估计参考帧的个数(H.264的话会有多帧,MPEG2这类的一般就没有了)

  int sample_rate:采样率(音频)

  int channels:声道数(音频)

  enum AVSampleFormat sample_fmt:采样格式

  int profile:型(H.264里面就有,其他编码标准应该也有)

  int level:级(和profile差不太多)

}AVCodecContext

----------------------------------------------------

 

AVCodec结构体是FFmpeg比较重要的结构体之一,主要用于存储编解码器的信息。

 

typedef struct AVCodec

{

   const char *name:编解码器的名字,比较短

   const char *long_name:编解码器的名字,全称,比较长

   enum AVMediaType type:指明了类型,是视频,音频,还是字幕

   enum AVCodecID id:ID,不重复

   const AVRational *supported_framerates:支持的帧率(仅视频)

   const enum AVPixelFormat *pix_fmts:支持的像素格式(仅视频)

   const int *supported_samplerates:支持的采样率(仅音频)

   const enum AVSampleFormat *sample_fmts:支持的采样格式(仅音频)

   const uint64_t *channel_layouts:支持的声道数(仅音频)

   int priv_data_size:私有数据的大小

}

--------------------------------------------------

 

AVMediaType编解码器类型:

 

enum AVMediaType {

    AVMEDIA_TYPE_UNKNOWN = -1, ///< Usually treated as AVMEDIA_TYPE_DATA

    AVMEDIA_TYPE_VIDEO,

    AVMEDIA_TYPE_AUDIO,

    AVMEDIA_TYPE_DATA, ///< Opaque data information usually continuous

    AVMEDIA_TYPE_SUBTITLE,

    AVMEDIA_TYPE_ATTACHMENT, ///< Opaque data information usually sparse

    AVMEDIA_TYPE_NB

};

====================================

 FFmpeg对H246进行编解码的实现

 

#include "libavutil/adler32.h"

#include "libavcodec/avcodec.h"

#include "libavformat/avformat.h"

#include "libavutil/imgutils.h"

#include "libavutil/timestamp.h"

 

static int video_decode_example(const char *input_filename)

{

    const AVCodec *codec = NULL;

    AVCodecContext *ctx= NULL;

    AVCodecParameters *origin_par = NULL;

    AVFrame *fr = NULL;

    uint8_t *byte_buffer = NULL;

    AVPacket *pkt;

    AVFormatContext *fmt_ctx = NULL;

    int number_of_written_bytes;

    int video_stream;

    int byte_buffer_size;

    int i = 0;

    int result;

 

    result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL);

    if (result < 0) {

        av_log(NULL, AV_LOG_ERROR, "Can't open file\n");

        return result;

    }

 

    result = avformat_find_stream_info(fmt_ctx, NULL);

    if (result < 0) {

        av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n");

        return result;

    }

 

    video_stream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);

    if (video_stream < 0) {

      av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n");

      return -1;

    }

 

    origin_par = fmt_ctx->streams[video_stream]->codecpar;

 

    codec = avcodec_find_decoder(origin_par->codec_id);

    if (!codec) {

        av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");

        return -1;

    }

 

    ctx = avcodec_alloc_context3(codec);

    if (!ctx) {

        av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n");

        return AVERROR(ENOMEM);

    }

 

    result = avcodec_parameters_to_context(ctx, origin_par);

    if (result) {

        av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n");

        return result;

    }

 

    result = avcodec_open2(ctx, codec, NULL);

    if (result < 0) {

        av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");

        return result;

    }

 

    fr = av_frame_alloc();

    if (!fr) {

        av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");

        return AVERROR(ENOMEM);

    }

 

    pkt = av_packet_alloc();

    if (!pkt) {

        av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n");

        return AVERROR(ENOMEM);

    }

 

    byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 16);

    byte_buffer = av_malloc(byte_buffer_size);

    if (!byte_buffer) {

        av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");

        return AVERROR(ENOMEM);

    }

 

    printf("#tb %d: %d/%d\n", video_stream, fmt_ctx->streams[video_stream]->time_base.num, fmt_ctx->streams[video_stream]->time_base.den);

    i = 0;

 

    result = 0;

    while (result >= 0) {

        result = av_read_frame(fmt_ctx, pkt);

        if (result >= 0 && pkt->stream_index != video_stream) {

            av_packet_unref(pkt);

            continue;

        }

 

        if (result < 0)

            result = avcodec_send_packet(ctx, NULL);

        else {

            if (pkt->pts == AV_NOPTS_VALUE)

                pkt->pts = pkt->dts = i;

            result = avcodec_send_packet(ctx, pkt);

        }

        av_packet_unref(pkt);

 

        if (result < 0) {

            av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");

            return result;

        }

 

        while (result >= 0) {

            result = avcodec_receive_frame(ctx, fr);

            if (result == AVERROR_EOF)

                goto finish;

            else if (result == AVERROR(EAGAIN)) {

                result = 0;

                break;

            } else if (result < 0) {

                av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");

                return result;

            }

 

            number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,

                                    (const uint8_t* const *)fr->data, (const int*) fr->linesize,

                                    ctx->pix_fmt, ctx->width, ctx->height, 1);

            if (number_of_written_bytes < 0) {

                av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");

                av_frame_unref(fr);

                return number_of_written_bytes;

            }

            printf("%d, %s, %s, %8"PRId64", %8d, 0x%08"PRIx32"\n", video_stream,

                   av_ts2str(fr->pts), av_ts2str(fr->pkt_dts), fr->duration,

                   number_of_written_bytes, av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes));

 

            av_frame_unref(fr);

        }

        i++;

    }

 

finish:

    av_packet_free(&pkt);

    av_frame_free(&fr);

    avformat_close_input(&fmt_ctx);

    avcodec_free_context(&ctx);

    av_freep(&byte_buffer);

    return 0;

}

 

int main(int argc, char **argv)

{

    if (argc < 2)

    {

        av_log(NULL, AV_LOG_ERROR, "Incorrect input\n");

        return 1;

    }

 

    if (video_decode_example(argv[1]) != 0)

        return 1;

 

    return 0;

}

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_72359435/article/details/136630574

智能推荐

海康威视网络摄像头开发流程(五)------- 直播页面测试_ezuikit 测试的url-程序员宅基地

文章浏览阅读3.8k次。1、将下载好的萤石js插件,添加到SoringBoot项目中。位置可参考下图所示。(容易出错的地方,在将js插件在html页面引入时,发生路径错误的问题)所以如果对页面中引入js的路径不清楚,可参考下图所示存放路径。2、将ezuikit.js引入到demo-live.html中。(可直接将如下代码复制到你创建的html页面中)<!DOCTYPE html><html lan..._ezuikit 测试的url

如何确定组态王与多动能RTU的通信方式_组态王ua-程序员宅基地

文章浏览阅读322次。第二步,在弹出的对话框选择,设备驱动—>PLC—>莫迪康—>ModbusRTU—>COM,根据配置软件选择的协议选期期,这里以此为例,然后点击“下一步”。第四步,把使用虚拟串口打勾(GPRS设备),根据需要选择要生成虚拟口,这里以选择KVCOM1为例,然后点击“下一步”设备ID即Modbus地址(1-255) 使用DTU时,为下485接口上的设备地址。第六步,Modbus的从机地址,与配置软件相同,这里以1为例,点击“下一步“第五步,Modbus的从机地址,与配置软件相同,这里以1为例,点击“下一步“_组态王ua

npm超详细安装(包括配置环境变量)!!!npm安装教程(node.js安装教程)_npm安装配置-程序员宅基地

文章浏览阅读9.4k次,点赞22次,收藏19次。安装npm相当于安装node.js,Node.js已自带npm,安装Node.js时会一起安装,npm的作用就是对Node.js依赖的包进行管理,也可以理解为用来安装/卸载Node.js需要装的东西_npm安装配置

火车头采集器AI伪原创【php源码】-程序员宅基地

文章浏览阅读748次,点赞21次,收藏26次。大家好,小编来为大家解答以下问题,python基础训练100题,python入门100例题,现在让我们一起来看看吧!宝子们还在新手村练级的时候,不单要吸入基础知识,夯实自己的理论基础,还要去实际操作练练手啊!由于文章篇幅限制,不可能将100道题全部呈现在此除了这些,下面还有我整理好的基础入门学习资料,视频和讲解文案都很齐全,用来入门绝对靠谱,需要的自提。保证100%免费这不,贴心的我爆肝给大家整理了这份今天给大家分享100道Python练习题。大家一定要给我三连啊~

Linux Ubuntu 安装 Sublime Text (无法使用 wget 命令,使用安装包下载)_ubuntu 安装sumlime text打不开-程序员宅基地

文章浏览阅读1k次。 为了在 Linux ( Ubuntu) 上安装sublime,一般大家都会选择常见的教程或是 sublime 官网教程,然而在国内这种方法可能失效。为此,需要用安装包安装。以下就是使用官网安装包安装的教程。打开 sublime 官网后,点击右上角 download, 或是直接访问点击打开链接,即可看到各个平台上的安装包。选择 Linux 64 位版并下载。下载后,打开终端,进入安装..._ubuntu 安装sumlime text打不开

CrossOver for Mac 2024无需安装 Windows 即可以在 Mac 上运行游戏 Mac运行exe程序和游戏 CrossOver虚拟机 crossover运行免安装游戏包-程序员宅基地

文章浏览阅读563次,点赞13次,收藏6次。CrossOver24是一款类虚拟机软件,专为macOS和Linux用户设计。它的核心技术是Wine,这是一种在Linux和macOS等非Windows操作系统上运行Windows应用程序的开源软件。通过CrossOver24,用户可以在不购买Windows授权或使用传统虚拟机的情况下,直接在Mac或Linux系统上运行Windows软件和游戏。该软件还提供了丰富的功能,如自动配置、无缝集成和实时传输等,以实现高效的跨平台操作体验。

随便推点

一个用聊天的方式让ChatGPT写的线程安全的环形List_为什么gpt一写list就卡-程序员宅基地

文章浏览阅读1.7k次。一个用聊天的方式让ChatGPT帮我写的线程安全的环形List_为什么gpt一写list就卡

Tomcat自带的设置编码Filter-程序员宅基地

文章浏览阅读336次。我们在前面的文章里曾写过Web应用中乱码产生的原因和处理方式,旧文回顾:深度揭秘乱码问题背后的原因及解决方式其中我们提到可以通过Filter的方式来设置请求和响应的encoding,来解..._filterconfig selectencoding

javascript中encodeURI和decodeURI方法使用介绍_js encodeur decodeurl-程序员宅基地

文章浏览阅读651次。转自:http://www.jb51.net/article/36480.htmencodeURI和decodeURI是成对来使用的,因为浏览器的地址栏有中文字符的话,可以会出现不可预期的错误,所以可以encodeURI把非英文字符转化为英文编码,decodeURI可以用来把字符还原回来_js encodeur decodeurl

Android开发——打包apk遇到The destination folder does not exist or is not writeable-程序员宅基地

文章浏览阅读1.9w次,点赞6次,收藏3次。前言在日常的Android开发当中,我们肯定要打包apk。但是今天我打包的时候遇到一个很奇怪的问题Android The destination folder does not exist or is not writeable,大意是目标文件夹不存在或不可写。出现问题的原因以及解决办法上面有说报错的中文大意是:目标文件夹不存在或不可写。其实问题就在我们的打包界面当中图中标红的Desti..._the destination folder does not exist or is not writeable

Eclipse配置高大上环境-程序员宅基地

文章浏览阅读94次。一、配置代码编辑区的样式 <1>打开Eclipse,Help —> Install NewSoftware,界面如下: <2>点击add...,按下图所示操作: name:随意填写,Location:http://eclipse-color-th..._ecplise高大上设置

Linux安装MySQL-5.6.24-1.linux_glibc2.5.x86_64.rpm-bundle.tar_linux mysql 安装 mysql-5.6.24-1.linux_glibc2.5.x86_6-程序员宅基地

文章浏览阅读2.8k次。一,下载mysql:http://dev.mysql.com/downloads/mysql/; 打开页面之后,在Select Platform:下选择linux Generic,如果没有出现Linux的选项,请换一个浏览器试试。我用的谷歌版本不可以,换一个别的浏览器就行了,如果还是不行,需要换一个翻墙的浏览器。 二,下载完后解压缩并放到安装文件夹下: 1、MySQL-client-5.6.2_linux mysql 安装 mysql-5.6.24-1.linux_glibc2.5.x86_64.rpm-bundle