hisi3559A平台VENC获取H264裸流封装成mp4

这篇博客介绍了如何在海思3559A平台上,利用FFmpeg库将通过VENC获取的H264裸流数据封装成MP4文件。文章详细阐述了关键函数的作用,包括av_register_all(), avformat_alloc_output_context2()等,并给出了从获取H264流到写入MP4文件的完整流程。" 129771579,13124950,突发急性传染病多尺度智能监测预警系统设计与应用,"['大数据分析', 'GIS应用', '自然语言处理', '机器学习', '疫情监控']
摘要由CSDN通过智能技术生成

主要函数说明:

av_register_all();
avformat_alloc_output_context2():初始化输出文件。
avio_open():打开输出文件。
avformat_write_header():写入文件头。
avformat_new_stream() 向媒体文件添加新流。
av_interleaved_write_frame():写入一个AVPacket到输出文件。
av_write_trailer():写入文件尾。

实现代码参考:

参考雷神的 最简单的基于FFmpeg的封装格式处理

ffmpeg h264文件和裸流 封装mp4

实现代码如下:

头文件和全局定义:


    #include "libavformat/avformat.h"
    
    static AVFormatContext* g_OutFmt_Ctx;
    static int vi = -1;
    static int STREAM_FRAME_RATE = 30;
    static HI_BOOL b_First_IDR_Find = HI_FALSE;
    static int ptsInc = 0;




添加新流函数

    /* Add an output stream */
    static int HI_PDT_Add_Stream(AVFormatContext *poutFmtCtx)
    {
   
        AVOutputFormat *pOutFmt = NULL;
        AVCodecContext *PAVCodecCtx = NULL;
        AVStream *pAVStream = NULL;
    	AVCodec *pcodec = NULL;
    
    	pOutFmt = poutFmtCtx->oformat;
    	
        /* find the encoder */
        pcodec = avcodec_find_encoder(pOutFmt->video_codec);
        if (NULL == pcodec)
        {
   
            printf("could not find encoder for '%s' \n", avcodec_get_name(pOutFmt->video_codec));
            return -1;
        }
    	
        pAVStream = avformat_new_stream(poutFmtCtx, pcodec);
        if (NULL == pAVStream)
        {
   
           printf("could not allocate stream \n");
           return -1;
        }
    	
        pAVStream->id = poutFmtCtx->nb_streams-1;
        PAVCodecCtx = pAVStream->codec;
        vi = pAVStream->index;
    
    	switch ((pcodec)->type)
        {
   
            case AVMEDIA_TYPE_AUDIO:
                printf("AVMEDIA_TYPE_AUDIO\n");
                PAVCodecCtx->sample_fmt = (pcodec)->sample_fmts ? (pcodec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
                PAVCodecCtx->bit_rate = 64000;
                PAVCodecCtx->sample_rate = 44100;
                PAVCodecCtx->channels = 2;
                break;
    			
            case AVMEDIA_TYPE_VIDEO:
                printf(
  • 6
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值