当前位置:   article > 正文

FFmpeg-- mp4文件合成1:aac和h264封装(c++实现)_ffmpeg 编程 h264 acc 封装hls

ffmpeg 编程 h264 acc 封装hls


aac 和 h264 封装为视频流,封装为c++的Muxter类

流程

  • 分配视频文件上下文
    int Init(const char *url);

  • 创建流,赋值给视频的音频流和视频流
    int AddStream(AVCodecContext *codec_ctx);

  • 写视频流的head
    int SendHeader();

  • 写视频流的packet,需要转换packet的pts和dts , 值为 原有pts * 编码时间基/ 视频流的时间基
    int SendPacket(AVPacket *packet)

  • 写视频流的trail
    int SendTrailer();

  • 释放资源
    void DeInit();

api

int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
// 时间基转换函数 , 计算结果为 a * bq / cq

核心代码

muxer.h
#ifndef MUXER_H
#define MUXER_H
#include <iostream>
extern "C"
{
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
}


class Muxer
{
public:
    Muxer();
    ~Muxer();
    // 输出文件 返回<0值异常
    // 初始化
    int Init(const char *url);
    // 资源释放
    void DeInit();
    // 创建流
    int AddStream(AVCodecContext *codec_ctx);

    // 写流
    int SendHeader();
    int SendPacket(AVPacket *packet);
    int SendTrailer();

    int Open(); // avio open
private:
    AVFormatContext *fmt_ctx_ = NULL;
    std::string url_ = "";

    // 编码器上下文
    AVCodecContext *aud_codec_ctx_= NULL;
    AVStream *aud_stream_ = NULL;
    AVCodecContext *vid_codec_ctx_= NULL;
    AVStream *vid_stream_ = NULL;

    int audio_index_ = -1;
    int video_index_ = -1;
};

#endif // MUXER_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
muxer.cpp
 #include "muxer.h"


Muxer::Muxer()
{

}

Muxer::~Muxer()
{

}

int Muxer::Init(const char *url)
{
    int ret = avformat_alloc_output_context2(&fmt_ctx_, NULL, NULL,url);
    if(ret != 0) {
        char errbuf[1024] = {0};
        av_strerror(ret, errbuf, sizeof(errbuf) - 1);
        printf("avformat_alloc_output_context2 failed:%s\n", ret);
        return -1;
    }
    url_ = url;
    return 0;
}

void Muxer::DeInit()
{
    if(fmt_ctx_) {
        avformat_close_input(&fmt_ctx_);
    }
    url_ = "";
    aud_codec_ctx_ = NULL;
    aud_stream_ = NULL;
    audio_index_ = -1;

    vid_codec_ctx_ = NULL;
    vid_stream_ = NULL;
    video_index_ = -1;
}

int Muxer::AddStream(AVCodecContext *codec_ctx)
{
    if(!fmt_ctx_) {
        printf("fmt ctx is NULL\n");
        return -1;
    }
    if(!codec_ctx) {
        printf("codec ctx is NULL\n");
        return -1;
    }
    AVStream *st = avformat_new_stream(fmt_ctx_, NULL);
    if(!st) {
        printf("avformat_new_stream failed\n");
        return -1;
    }
    //    st->codecpar->codec_tag = 0;
    // 从编码器上下文复制, 根据提供的编解码器的值填充参数结构
    avcodec_parameters_from_context(st->codecpar, codec_ctx);
    av_dump_format(fmt_ctx_, 0, url_.c_str(), 1);

    // 判断当前的是视频流还是音频流
    if(codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
        aud_codec_ctx_ = codec_ctx;
        aud_stream_ = st;
        audio_index_ = st->index;
    }  else if(codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
        vid_codec_ctx_ = codec_ctx;
        vid_stream_ = st;
        video_index_ = st->index;
    }
    return 0;
}

int Muxer::SendHeader()
{
    if(!fmt_ctx_) {
        printf("fmt ctx is NULL\n");
        return -1;
    }
    int ret = avformat_write_header(fmt_ctx_, NULL);
    if(ret != 0) {
        char errbuf[1024] = {0};
        av_strerror(ret, errbuf, sizeof(errbuf) - 1);
        printf("avformat_write_header failed:%s\n", ret);
        return -1;
    }
    return 0;
}

int Muxer::SendPacket(AVPacket *packet)
{
    int stream_index = packet->stream_index;

    if(!packet || packet->size <= 0 || !packet->data) {
        printf("packet is null\n");
        if(packet)
            av_packet_free(&packet);

        return -1;
    }

    AVRational src_time_base;   // 编码后的包
    AVRational dst_time_base;   // mp4输出文件对应流的time_base
    if(vid_stream_ && vid_codec_ctx_ && stream_index == video_index_) {

        src_time_base = vid_codec_ctx_->time_base;
        dst_time_base = vid_stream_->time_base;

    } else if(aud_stream_ && aud_codec_ctx_ && stream_index == audio_index_) {

        src_time_base = aud_codec_ctx_->time_base;
        dst_time_base = aud_stream_->time_base;

    }
    // 时间基转换
    // int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const;
    // 时间基转换函数  a * bq / cq
    packet->pts = av_rescale_q(packet->pts, src_time_base, dst_time_base);

    packet->dts = av_rescale_q(packet->dts, src_time_base, dst_time_base);

    packet->duration = av_rescale_q(packet->duration, src_time_base, dst_time_base);

    int ret = 0;

    //写packet
    ret = av_interleaved_write_frame(fmt_ctx_, packet); // 不是立即写入文件,内部缓存,主要是对pts进行排序
    //    ret = av_write_frame(fmt_ctx_, packet);

    av_packet_free(&packet);
    if(ret == 0) {
        return 0;
    }
    else {
        char errbuf[1024] = {0};
        av_strerror(ret, errbuf, sizeof(errbuf) - 1);
        printf("avformat_write_header failed:%s\n", ret);
        return -1;
    }
}

int Muxer::SendTrailer()
{
    if(!fmt_ctx_) {
        printf("fmt ctx is NULL\n");
        return -1;
    }
    int ret = av_write_trailer(fmt_ctx_);
    if(ret != 0) {
        char errbuf[1024] = {0};
        av_strerror(ret, errbuf, sizeof(errbuf) - 1);
        printf("av_write_trailer failed:%s\n", ret);
        return -1;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/491160
推荐阅读
相关标签
  

闽ICP备14008679号