首页 > 其他分享 >simple_ffmpeg_decoder(ffmpeg的解码器)

simple_ffmpeg_decoder(ffmpeg的解码器)

时间:2024-04-14 10:00:26浏览次数:24  
标签:pCodecCtx ffmpeg AVFormatContext simple 音视频 avformat 解码器 NULL pFormatCtx

代码

#include <stdio.h>

#define __STDC_CONSTANT_MACROS
//因为ffmpeg是C语言编写的所以要继承C语言的语法格式
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
};
int main(int argc,char* argv)
{
  AVFormatContext	*pFormatCtx;//音频格式相关信息
  AVCodecContext	*pCodecCtx;//编解码信息
  AVCodec			*pCodec;//编解码器 如H.264编码器和解码器
  AVFrame	*pFrame,*pFrameYUV;//用于存放解码后的音视频帧的数据结构;pFrame用于存储从视频流中解码得到的原始帧数据  pFrameYUV则用于存储将原始帧数据转换为YUV格式后的数据
  AVPacket *packet; //AVPacket结构体用于表示音视频数据包。它包含了编码后的音视频数据以及相关的元数据信息,如数据包大小、时间戳、持续时间等。
  int i, videoindex;
  int y_size;
  int ret, got_picture;
  struct SwsContext *img_convert_ctx;
  //输入文件路径
  char filepath[]="xxxx.ts";
  int frame_cnt ;//frame_cnt 是一个整数变量,用于统计已经解码的视频帧的数量。
  av_register_all();//旧版本中还有这个方法4.x 新版本中弃用了这个
  avformat_network_init();//初始化网络模块 初始化win socket和openssl
  pFormatCtx = avformat_alloc_context();//初始化音视频信息的上下文 返回值为AVFormatContext
	pFormatCtx = avformat_alloc_context();//初始化音视频信息的上下文 返回值为AVFormatContext
//	int avformat_open_input(AVFormatContext * *ps, const char* url, AVInputFormat * fmt, AVDictionary * *options);
	//avformat_open_input函数用于打开指定路径的音频文件,并将文件的信息填充到之前的AVFormatContext结构体中
	//ps是一个指向AVFormatContext* 类型的指针的指针,用于传递要填充的AVFormatContext结构体指针的地址
	//url是要打开的音视频文件的路径
	//fmt是要使用的输入格式,通常为:NULL,表示自动检测输入格式
	//options:是一个指向选项字典的指针
  int avformat_open_input(&pFormatCtx,filepath,NULL,NULL!=2)
  {
    printf("Couldn't open input steram\r\n");
    return  -1;
  }
 	//函数用于获取音视频流的详细信息,比如流的类型、编解码器信息、时长等。它会解析音视频文件中的每个流,并将相关信息填充到AVFormatContext
	// ic是指向已打开 AVFormatContext结构体的指针,其中包含了音视频文件的格式信息。
	//options 是一个指向选项字典的指针,用于设置查找流信息时的选项。这里传入了NULL,表示不设置任何选项。
	if(avformat_find_stream_info(pFormatCtx,NULL)<0)
    {
		printf("Couldn't find stream information.\n");
		return -1;
	}
    videoindex=-1;//videoindex = -1;//初始化videoindex变量为-1这是一个标记,用于记录找到的视频流的索引
    //循环遍历pFormatCtx->nb_streams数组,该数组中存储了音视频文件中的所有流信息
	for(i=0; i<pFormatCtx->nb_streams; i++) 
		if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
        {
			videoindex=i;
			break;
		}
	if(videoindex==-1)
        {
		printf("Didn't find a video stream.\n");//在循环中,判断当前流是否为视频流。如果是视频流,则将视频流,程序输出错误信息并返回-1
		return -1;
	}

	pCodecCtx=pFormatCtx->streams[videoindex]->codec;
	pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
	if(pCodec==NULL){
		printf("Codec not found.\n");
		return -1;
	}
	if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
		printf("Could not open codec.\n");
		return -1;
	}

  /*
	 * 在此处添加输出视频信息的代码
	 * 取自于pFormatCtx,使用fprintf()
	 */
	pFrame=av_frame_alloc();
	pFrameYUV=av_frame_alloc();
	out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
	avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
	packet=(AVPacket *)av_malloc(sizeof(AVPacket));
	//Output Info-----------------------------
	printf("--------------- File Information ----------------\n");
	av_dump_format(pFormatCtx,0,filepath,0);
	printf("-------------------------------------------------\n");
	img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, 
		pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); 

	frame_cnt=0;
	while(av_read_frame(pFormatCtx, packet)>=0){
		if(packet->stream_index==videoindex){
				/*
				 * 在此处添加输出H264码流的代码
				 * 取自于packet,使用fwrite()
				 */
			ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
			if(ret < 0){
				printf("Decode Error.\n");
				return -1;
			}
			if(got_picture){
				sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, 
					pFrameYUV->data, pFrameYUV->linesize);
				printf("Decoded frame index: %d\n",frame_cnt);

				/*
				 * 在此处添加输出YUV的代码
				 * 取自于pFrameYUV,使用fwrite()
				 */

				frame_cnt++;

			}
		}
		av_free_packet(packet);
	}

	sws_freeContext(img_convert_ctx);

	av_frame_free(&pFrameYUV);
	av_frame_free(&pFrame);
	avcodec_close(pCodecCtx);
	avformat_close_input(&pFormatCtx);



  



}

标签:pCodecCtx,ffmpeg,AVFormatContext,simple,音视频,avformat,解码器,NULL,pFormatCtx
From: https://www.cnblogs.com/doubleconquer/p/18132326

相关文章

  • FFmpeg开发笔记(十三)Windows环境给FFmpeg集成libopus和libvpx
    ​MP4是最常见的视频封装格式,在《FFmpeg开发实战:从零基础到短视频上线》一书的“1.2.3 自行编译与安装FFmpeg”介绍了如何给FFmpeg集成x264和x265两个库,从而支持H.264和H.265两种标准的编解码。视频的封装格式除了悠久的MP4和ASF之外,还有较新的WebM格式,该格式的音频编码主要采......
  • django simpleui 的list_display添加自定义列、显示图片 及alert弹窗的设置方法
    参考djangosimpleui的list_display添加自定义列、显示图片及alert弹窗的设置方法-CSDN博客环境:python:3.8.xDjango:3.2.xDjango-simpleui:2021.x先定义下模型#models.pyclassDog(models.Model):name=models.CharField(max_length=15,verbose_name='小狗名字')......
  • 视频处理的利器,ffmpeg-python库详解与应用示例
    左手编程,右手年华。大家好,我是一点,关注我,带你走入编程的世界。公众号:一点sir,关注领取python编程资料在数字媒体的时代,视频处理成为了一项重要的技能。无论是剪辑、转码、还是添加特效,都需要强大的工具来处理视频素材。Python作为一门功能强大的编程语言,在视频处理领域也有着广......
  • centos7中ffmpeg的安装方法
    Linux系统安装ffmpeg&升级ffmpeg一、介绍多媒体视频处理工具FFmpeg有非常强大的功能,包括视频采集功能、视频格式转换、视频抓图、给视频加水印等。由于最近要处理音视频格式转换问题,因此需要安装、升级ffmpeg,下面来记录一下踩坑过程。 二、安装ffmpeg1、下载并解压ffmpeg......
  • FFmpeg常用功能
    1.转码视频格式:ffmpeg-iinput.mp4output.avi上述命令将输入的MP4视频文件转换为AVI格式。2.压缩视频文件:ffmpeg-iinput.mp4-vcodeclibx264-crf23output.mp4 该命令使用libx264视频编解码器对输入的MP4文件进行压缩,并将压缩后的视频保存为MP4格式。CRF值(Cons......
  • 基于 FFmpeg 的自定义 Media Extractor(2):自定义 Extractor 的实现方法
    文章目录前言C/NDKAPI简介C++API简介实现自定义Extractor编译自定义Extractor参考资料前言在上一篇文章中,简要介绍了Extractor组件选择及创建过程。本文将继续基于Android11探索自定义Extractor的实现,及其接入到Android多媒体框架中的方法。C/NDKA......
  • ffmpeg对视频进行裁减crop
    ffmpeg-i input.mp4 -r 50 -vf crop=800:900:150:200 output.mp4input.mp4:你需要裁减的视频50:裁减之后的视频的帧率crop=800:900:150:200:150:200表示的是从视频的左上角(150,200)这个位置开始对视频进行裁减。其中800表示裁减后的视频的w是800,900表示h。out......
  • FFmpeg 7.0 “Dijkstra” 发布
    FFmpeg7.0“Dijkstra”发布来源:OSCHINA编辑: 白开水不加糖2024-04-0710:11:00 2国产数据库圈,为啥那么多水货?”FFmpeg7.0“Dijkstra”现已发布。此版本以荷兰计算机科学家EdsgerW.Dijkstra的名字命名,一些值得注意的变化包括原生VVC解码器(目前处于......
  • 常用API(一):Date SimpleDateFormat
       packagecom.itheima.日期;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;publicclassdate{publicstaticvoidmain(String[]args)throwsParseException{Dated=newDate();System.out.pr......
  • FFmpeg开发笔记(十二)Linux环境给FFmpeg集成libopus和libvpx
    ​MP4是最常见的视频封装格式,在《FFmpeg开发实战:从零基础到短视频上线》一书的“1.2.3 自行编译与安装FFmpeg”介绍了如何给FFmpeg集成x264和x265两个库,从而支持H.264和H.265两种标准的编解码。视频的封装格式除了古老的MP4和ASF之外,还有较新的WebM格式,该格式的音频编码主要采......