FFMPEG 视频图像解封装解码

FFMPEG4.0 音频解码解封装
FFMPEG 音频封装编码

下面的函数方法基于最新的FFMPEG 4.0(4.X):
本文讲是如何从一个视频文件中提取出其中的图像数据,并将图像数据保存到文件中。

解码解封装的过程与音频差不多,具体如下:
1.读取视频文件的格式信息

    fmt_ctx = avformat_alloc_context();avformat_open_input(&fmt_ctx,input,NULL,NULL);avformat_find_stream_info(fmt_ctx,NULL);

2.获取视频流

    int st_index = av_find_best_stream(fmt_ctx,AVMEDIA_TYPE_VIDEO,-1,-1,NULL,0);LOGV("st_index = %d\n",st_index);AVStream *st = fmt_ctx->streams[st_index];

3.准备×××与解码context

    AVCodec *codec = avcodec_find_decoder(st->codecpar->codec_id);AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);

4.拷贝视频流中的参数到×××context中并打开×××

    avcodec_parameters_to_context(codec_ctx,st->codecpar);avcodec_open2(codec_ctx,codec,NULL);

5.读取视频的格式、宽高信息

    int width = codec_ctx->width;int height = codec_ctx->height;enum AVPixelFormat pixel_fmt = codec_ctx->pix_fmt;

6.申请图像存储空间

    uint8_t *dst_buf[4] = {0};int      dst_linesize[4];int size = av_image_alloc(dst_buf,dst_linesize,width,height,pixel_fmt,1);

7.申明存储原始数据与解码后数据的packet与frame

    AVFrame *frame = av_frame_alloc();AVPacket *packet = av_packet_alloc();

8.读取数据,只取用视频数据

int ret = av_read_frame(fmt_ctx,packet);
//读取到的packet不仅仅是图像数据,还有音频、字幕等数据。
if(packet->stream_index != st_index)
{continue;
}

9.发送数据进行解码
ret = avcodec_send_packet(codec_ctx,packet);
10.接收解码后的原始数据,这是个反复的过程,一个packet可能解码出好几个frame

        ret = avcodec_receive_frame(codec_ctx,frame);if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) //packet解码完了,需要sentbreak;if(ret < 0) {return 1;}

注意:收到的frame可能存在宽高或者fmt格式变化这种情况,后面的流程代码没有考虑这种情况(这种奇葩视频应该不会遇到)

            if(frame->width != width || frame->height != height || frame->format != pixel_fmt){LOGV("eeeeeeeeeeeee");}

11.把frame中的数据拷贝到事先准备的dst_buf中。二维指针数组看作一位数组。
av_image_copy(dst_buf,dst_linesize,frame-&gt;data,frame-&gt;linesize,pixel_fmt,width,height);
12.把数据写入文件。
fwrite(dst_buf[0],1,size,out_file);

下面贴一段完整的示例代码,代码没有考虑失败的情况,结尾没有搞释放,也没有flush×××,示例只是为了掌握整个核心解码流程。

/** demuxing_decode_video.c**  Created on: 2019年1月8日*      Author: deanliu*/#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>static char log_buf[1024*8];
#define LOGV(...) av_log(NULL,AV_LOG_VERBOSE,__VA_ARGS__)void ffmpeg_log_callback(void* ptr, int level, const char* fmt, va_list vl)
{static int print_prefix = 1;av_log_format_line(ptr,level,fmt,vl,log_buf,sizeof(log_buf),&print_prefix);fprintf(stderr,"%s",log_buf);
}int main()
{av_log_set_callback(ffmpeg_log_callback);char *input = "E:/测试音视频/12种格式视频/test.avi";char *output = "d:/video.v";FILE *out_file = fopen(output,"wb");AVFormatContext *fmt_ctx;fmt_ctx = avformat_alloc_context();avformat_open_input(&fmt_ctx,input,NULL,NULL);avformat_find_stream_info(fmt_ctx,NULL);int st_index = av_find_best_stream(fmt_ctx,AVMEDIA_TYPE_VIDEO,-1,-1,NULL,0);LOGV("st_index = %d\n",st_index);AVStream *st = fmt_ctx->streams[st_index];AVCodec *codec = avcodec_find_decoder(st->codecpar->codec_id);AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);avcodec_parameters_to_context(codec_ctx,st->codecpar);avcodec_open2(codec_ctx,codec,NULL);int width = codec_ctx->width;int height = codec_ctx->height;enum AVPixelFormat pixel_fmt = codec_ctx->pix_fmt;uint8_t *dst_buf[4] = {0};int      dst_linesize[4];int size = av_image_alloc(dst_buf,dst_linesize,width,height,pixel_fmt,1);AVFrame *frame = av_frame_alloc();AVPacket *packet = av_packet_alloc();while(1){LOGV("READ\n");int ret = av_read_frame(fmt_ctx,packet);if(ret < 0){LOGV("ret = %d\n",ret);break;}if(packet->stream_index != st_index){continue;}LOGV("SENT\n");ret = avcodec_send_packet(codec_ctx,packet);if(ret < 0){return 1;}while(ret >= 0){LOGV("receiver\n");ret = avcodec_receive_frame(codec_ctx,frame);if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)break;if(ret < 0) {return 1;}if(frame->width != width || frame->height != height || frame->format != pixel_fmt){LOGV("eeeeeeeeeeeee");}av_image_copy(dst_buf,dst_linesize,frame->data,frame->linesize,pixel_fmt,width,height);LOGV("dst_buf = %d,%d,%d,%d\n",dst_buf[2][0],dst_buf[1][1],dst_buf[0][2],dst_buf[0][3]);fwrite(dst_buf[0],1,size,out_file);}}LOGV("dst_linesize = %d,%d,%d,%d\n",dst_linesize[0],dst_linesize[1],dst_linesize[2],size);printf("Play the output video file with the command:\n""ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",av_get_pix_fmt_name(pixel_fmt), width, height,output);LOGV("END!!");fclose(out_file);return 0;
}

转载于:https://blog.51cto.com/4095821/2402711

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/275048.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

对数据可视化的理解_使数据可视化更容易理解

对数据可视化的理解Data is weaving its way into almost all aspects of our lives since the past decade. Our ability to store more information in smaller and smaller spaces has encouraged us to make sure we leave no information out. The ease of collecting inf…

面试官:项目中常用的 .env 文件原理是什么?如何实现?

1. 前言大家好&#xff0c;我是若川。持续组织了5个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。本文仓库 h…

语言分类,我接触和我想学习的

本文信息和数据出自hyperpolyglot&#xff0c;将当前主流编程语言分为11个大类&#xff0c;分别为&#xff1a;解释型(PHP,Perl,Python,Ruby,Tcl,Lua,JavaScript,Io)、操作系统自动化型(POSIX Shell,AppleScript,PowerShell)、C风格(C,Objective C,Java,C#)、Pascal风格(Pascal…

梯度下降法和随机梯度下降法

1. 梯度 在微积分里面&#xff0c;对多元函数的参数求∂偏导数&#xff0c;把求得的各个参数的偏导数以向量的形式写出来&#xff0c;就是梯度。比如函数f(x,y), 分别对x,y求偏导数&#xff0c;求得的梯度向量就是(∂f/∂x, ∂f/∂y)T,简称grad f(x,y)或者▽f(x,y)。对于在点(x…

一张图看程序媛阿源的2021个人年度流水账

大家好&#xff0c;我是若川。持续组织了5个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。本文来自读者阿源小…

案例研究:设计与方法_如何进行1小时的重新设计(案例研究)

案例研究:设计与方法速度设计简介 (Intro to Speed Designing) I’ve been an advocate of speed redesigning technique for a while. The idea is simple — decrease the hand-eye lag and make super quick decisions, seemingly without thinking. The logic behind it is…

图文并茂重新认识下递归

大家好&#xff0c;我是若川。持续组织了5个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。对于大部分前端(包…

《C和指针》读书笔记

看过了经典的K&R C&#xff0c;又看了这本Pointers on C&#xff0c;温习了C语言的基本语法。 在重温过程中&#xff0c;感觉需要重点把握的知识是指针、结构和动态内存分配。 这对今后的算法和操作系统方面的研究学习很有帮助。 3.2.3 声明指针int* b, c, d;本以为这条语句…

FPGA设计者的5项基本功

记得《佟林传》里&#xff0c;佟林练的基本功是“绕大树、解皮绳”&#xff0c;然后才练成了什么“鬼影随行、柳叶绵丝掌”。 在我看来&#xff0c;成为一名说得过去的FPGA设计者&#xff0c;需要练好5项基本功&#xff1a;仿真、综合、时序分析、调试、验证。 需要强调的一点是…

unity 全息交互ui_UI向3D投影全息界面的连续发展

unity 全息交互uiThe user interface has been natural in its evolution and strategically heading towards the 3D-projection holographic interface (3D-PHI) era.用户界面在其发展过程中一直很自然&#xff0c;并且在战略上正朝着3D投影全息界面( 3D-PHI )时代迈进。 Si…

开发工具 快捷键整理

快捷键大全 JAVA 开发工具 MyEclipse -------------------------------------MyEclipse 快捷键1(CTRL)-------------------------------------Ctrl1 快速修复CtrlD: 删除当前行 CtrlQ 定位到最后编辑的地方 CtrlL 定位在某行 CtrlO 快速显示 OutLine CtrlT 快速显示当前类…

前端构建新世代,Esbuild 原来还能这么玩!

大家好&#xff0c;我是若川。持续组织了5个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。今天分享一篇esbui…

大三下学期十四周总结

在小组的学习方面&#xff0c;这周主要是对微信小程序的学习。对JSON格式请求在Spring boot与小程序之间的交互有了一些了解。对微信的接口wx.request、wx.uploadFile、wx.chooseImage的接口的使用。微信开发后台传过来的响应数据如果不是标准的json格式&#xff0c;需要在小程…

平面设计师和ui设计师_平面设计师为什么要享受所有乐趣?

平面设计师和ui设计师Graphic designers are pretty cool. We have to admit that. Be it their dressing style, their attitude and most importantly their enviable gadgets. Large Mac monitor, wacom tablet, drawing sets, swatchbooks , iPad pro with pencil, humungo…

转:Xcode下的GDB调试命令

Xcode的调试器为用户提供了一个GDB的图形化界面&#xff0c;GDB是GNU组织的开放源代码调试器。您可以在Xcode的图形界面里做任何事情&#xff1b;但是&#xff0c;如果您需要您可以在命令行里使用GDB的命令&#xff0c;且gdb可以在终端运行&#xff0c;也可以在Xcode下的控制台…

web表单设计:点石成金_设计复杂的用户表单:12个UX最佳实践

web表单设计:点石成金It’s been a few years that I’ve been taking interest in designing complex user forms, where a lot of information is requested from users. Here are a few industries where you regularly find such flows:几年来&#xff0c;我一直对设计复杂…

跨平台开发框架到底哪家强?5款主流框架横向对比!

跨平台开发框架到底哪家强&#xff1f;目前市场上有多个专业做跨平台开发的框架&#xff0c;那么对开发者来说究竟哪一个框架更符合自己的需求呢&#xff1f;笔者特地总结对比了一下不同框架的特性。国内外笔者选择了一共5个主流的测评对象&#xff0c;分别是RN&#xff0c;Flu…

【一句日历】2019年6月

【2019年6月1日儿童节星期六】 人们在协商&#xff0c;解决和处理各种状况时&#xff0c;若要获得圆满的结果&#xff0c;平静的心和自我控制能力必不可少。任何人都明白。如果我们不能很好地控制自我&#xff0c;反而让焦躁和嗔怒干扰了我们&#xff0c;那么我们的工作不再具有…

Android学习摘要一之Android历史

Google与你998年9月7日创立&#xff0c;经过十几年在搜索引擎方面的精耕细作&#xff0c;成为全球互联网巨头&#xff0c;尤其在地图搜索的应用更是引人注目。Google与2007年11月5日宣布基于Linux平台的开源手机操作系统&#xff0c;名称为Android&#xff0c;中文译为“机器人…

c#创建web应用程序_创建Web应用程序图标集的6个步骤

c#创建web应用程序I am not great at creating logos or icons, mainly because of the lack of practice. So when I was tasked to create an unique icon set for our web app, I wasn’t confident that things will turn out right. After researching effective and rele…