ffmpeg通过spspps的信息解析视频宽高和yuv格式
Posted 朱韦刚
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ffmpeg通过spspps的信息解析视频宽高和yuv格式相关的知识,希望对你有一定的参考价值。
本文内容是ffmpeg通过spspps的信息解析视频宽高和yuv格式,ffmpeg对这个支持没有对外接口,可以参考下面文章。
http://ffmpeg.org/pipermail/ffmpeg-devel/2015-July/176434.html。
如果想解析请看下面:
/* 通过sps/pps和一个idr帧获取视频宽高以及yuv格式(必须包含一帧IDR帧否则解析不出来,也可以解码一帧数据获取信息);
* 参数:传入的视频,传入的视频长度,视频编码类型(AVCodecID_t),传出的解析后的宽,传出的解析后的高,传出的解析后的yuv格式;
* 返回值:0:失败;1:成功;
*/
int func(const char * videooutdata, const int videooutdatasize, int video_codecID,
int * outwidth, int * outheight, int * outpixelfromat);
int func(const char * videooutdata, const int videooutdatasize, int video_codecID,
int * outwidth, int * outheight, int * outpixelfromat)
int ret = 0;
AVCodecParserContext *parservideo = NULL;
AVCodecContext *cvideo = NULL;
AVCodec *codecvideo = NULL;
char * outvideo = (char *)malloc(sizeof(char)* (videooutdatasize + 64));
memset(outvideo, 0, (videooutdatasize + 64));
AVPacket * outpkt = NULL;
if (videooutdatasize <= 0)
return 0;
outpkt = av_packet_alloc();
if (!outpkt)
return 0;
if (parservideo == NULL)
/* find the H264 or H265 video */
if (video_codecID == AV_CODEC_ID_H264_t)
codecvideo = avcodec_find_decoder(AV_CODEC_ID_H264);
else if (video_codecID == AV_CODEC_ID_H265_t)
codecvideo = avcodec_find_decoder(AV_CODEC_ID_H265);
else
av_packet_free(&outpkt);
return 0;
if (!codecvideo)
printf("parser_videoinfo Codec not found\\n");
return 0;
parservideo = av_parser_init(codecvideo->id);
if (!parservideo)
printf("parser_videoinfo parser not found\\n");
return 0;
cvideo = avcodec_alloc_context3(codecvideo);
if (!cvideo)
printf("parser_videoinfo Could not allocate video codec context\\n");
return 0;
/* open it */
if (avcodec_open2(cvideo, codecvideo, NULL) < 0)
printf("parser_videoinfo Could not open codec\\n");
return 0;
ret = av_parser_parse2(parservideo, cvideo, &outpkt->data, &outpkt->size,
(uint8_t *)videooutdata, videooutdatasize, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (ret > 0)
ret = av_parser_parse2(parservideo, cvideo, &outpkt->data, &outpkt->size,
NULL, 0, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (outpkt->size >0)
*outwidth = parservideo->width;
*outheight = parservideo->height;
*outpixelfromat = parservideo->format;
if (parservideo->width == 0 ||
parservideo->height == 0 ||
parservideo->format == -1)
ret = 0;
ret = 1;
av_parser_close(parservideo);
avcodec_free_context(&cvideo);
av_packet_free(&outpkt);
return ret;
如有错误请指正:
交流请加QQ群:62054820
QQ:379969650.
以上是关于ffmpeg通过spspps的信息解析视频宽高和yuv格式的主要内容,如果未能解决你的问题,请参考以下文章