解码mp4文件分别存储为pcm,yuv文件

简介: 使用FFmpeg库在C++中解码MP4文件,并将音频数据存储为PCM格式,视频数据存储为YUV格式。
// 解码分别写入对应文件
#include "myLog.h"
#include <iostream>

extern "C"
{
#include <libavformat\avformat.h>
#include <libavutil\avutil.h>
#include <libavcodec\avcodec.h>
#include <libavutil\imgutils.h>
#include <libavutil\samplefmt.h>
}
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avcodec.lib")

class Decoder
{
public:
    Decoder(std::string src_filename, std::string dst_audio_filename, std::string dst_video_filename);
    ~Decoder();

    // 解封装
    int demuxer();

    // 解码
    int decodec();

    // 打印音视频播放格式
    void print_audio_play_str();
private:
    // 打开解码器
    int open_decodec();

    // 解码pkt
    int decode_pkt(AVCodecContext* codec_ctx, AVPacket* pkt);

    // 输出视频帧
    int output_video_frame(AVFrame* frame);

    // 输出音频帧
    int output_audio_frame(AVFrame* frame);

    // 获取音频格式
    int get_format_from_sample_fmt(std::string& fmtStr, enum AVSampleFormat sample_fmt);
private:
    AVFormatContext* ifmt_ctx_;                    // 格式上下文
    AVCodecContext*  video_decode_ctx_;            // 视频解码器上下文
    AVCodecContext*  audio_decode_ctx_;            // 音频解码器上下文

    int video_idx_;                                // 视频索引                        
    AVStream* video_stream_;                    // 视频流

    int audio_idx_;                                // 音频索引
    AVStream* audio_stream_;                    // 音频流

    std::string src_filename_;                    // 输入文件路径
    std::string dst_audio_filename_;            // 输出音频pcm文件路径
    std::string dst_video_filename_;            // 输出视频yuv路径                            
    FILE* dst_video_fp_;
    FILE* dst_audio_fp_;

    AVFrame* frame_;
    uint8_t* video_dst_data_[4];                // 记录video一行数据
    int video_dst_buf_size_;                    // 分配的真实buf大小 
    int linesize_[4];                            // 每行数据大小(YUV RGB 第四个为保留字段)
};

Decoder::Decoder(std::string src_filename, std::string dst_audio_filename, std::string dst_video_filename)
: ifmt_ctx_(nullptr)
, video_decode_ctx_(nullptr)
, audio_decode_ctx_(nullptr)
, video_stream_(nullptr)
, audio_stream_(nullptr)
, dst_video_fp_(nullptr)
, dst_audio_fp_(nullptr)
, frame_(nullptr)
, src_filename_(src_filename)
, dst_audio_filename_(dst_audio_filename)
, dst_video_filename_(dst_video_filename)
{
    for (int i = 0; i < 4; i++)
    {
        video_dst_data_[i] = NULL;
    }
}

Decoder::~Decoder()
{
    // 释放相关资源
    if (ifmt_ctx_)
    {
        avformat_close_input(&ifmt_ctx_);
    }
    if (audio_decode_ctx_)
    {
        avcodec_free_context(&audio_decode_ctx_);
    }
    if (video_decode_ctx_)
    {
        avcodec_free_context(&video_decode_ctx_);
    }
    if (dst_audio_fp_)
    {
        fclose(dst_audio_fp_);
    }    
    if (dst_video_fp_)
    {
        fclose(dst_video_fp_);
    }
    if (frame_)
    {
        av_frame_free(&frame_);
    }
    if (video_dst_data_)
    {
        av_free(video_dst_data_[0]);    // 释放数据指针(其它三个指针无需释放) data[0]指向缓冲区开始位置,data[1] - data[3]也是指向data[0]的数据指针
    }
}

int Decoder::demuxer()
{
    // 1. 打开文件
    int nRet = avformat_open_input(&ifmt_ctx_, src_filename_.c_str(), NULL, NULL);
    if (nRet < 0)
    {
        LOG_WARNING("avformat_open_input error\n");
        return -1;
    }

    // 2. 查找流信息
    nRet = avformat_find_stream_info(ifmt_ctx_, NULL);
    if (nRet < 0)
    {
        avformat_close_input(&ifmt_ctx_);
        LOG_WARNING("avformat_find_stream_info error\n");
        return -2;
    }

    // 3. 获取音视频流索引
    audio_idx_ = av_find_best_stream(ifmt_ctx_, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
    video_idx_ = av_find_best_stream(ifmt_ctx_, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);

    return 0;
}

int Decoder::open_decodec()
{
    // 打开音频解码器
    if (audio_idx_ >= 0)
    {
        audio_stream_ = ifmt_ctx_->streams[audio_idx_];
        // 查找解码器
        AVCodec* audio_decodec = avcodec_find_decoder(audio_stream_->codecpar->codec_id);
        if (!audio_decodec)
        {
            LOG_WARNING("avcodec_find_decoder error\n");
            return AVERROR(ENOMEM);
        }

        // 给解码上下文分配内存
        audio_decode_ctx_ = avcodec_alloc_context3(audio_decodec);
        if (!audio_decode_ctx_)
        {
            LOG_WARNING("avcodec_alloc_context3 error\n");
            return AVERROR(ENOMEM);
        }

        // 拷贝解码参数到解码上下文
        int nRet = avcodec_parameters_to_context(audio_decode_ctx_, audio_stream_->codecpar);
        if (nRet < 0)
        {
            LOG_WARNING("avcodec_parameters_to_context error\n");
            return -1;
        }
        // 打开解码器
        nRet = avcodec_open2(audio_decode_ctx_, audio_decodec, NULL);
        if (nRet < 0)
        {
            LOG_WARNING("avcodec_open2 error\n");
            return -2;
        }

        dst_audio_fp_ = fopen(dst_audio_filename_.c_str(), "wb");
        if (!dst_audio_fp_)
        {
            LOG_WARNING("fopen dst_audio_filename_ error\n");
            return -1;
        }
    }

    // 打开视频解码器
    if (video_idx_ >= 0)
    {
        video_stream_ = ifmt_ctx_->streams[video_idx_];
        // 查找解码器
        AVCodec* video_decodec = avcodec_find_decoder(video_stream_->codecpar->codec_id);
        if (!video_decodec)
        {
            LOG_WARNING("avcodec_find_decoder error\n");
            return AVERROR(ENOMEM);
        }

        // 给解码上下文分配内存
        video_decode_ctx_ = avcodec_alloc_context3(video_decodec);
        if (!video_decode_ctx_)
        {
            LOG_WARNING("avcodec_alloc_context3 error\n");
            return AVERROR(ENOMEM);
        }

        // 拷贝解码参数到解码上下文
        int nRet = avcodec_parameters_to_context(video_decode_ctx_, video_stream_->codecpar);
        if (nRet < 0)
        {
            LOG_WARNING("avcodec_parameters_to_context error\n");
            return -1;
        }
        // 打开解码器
        nRet = avcodec_open2(video_decode_ctx_,video_decodec, NULL);
        if (nRet < 0)
        {
            LOG_WARNING("avcodec_open2 error\n");
            return -2;
        }

        video_dst_buf_size_ = av_image_alloc(video_dst_data_, linesize_,
            video_decode_ctx_->width, video_decode_ctx_->height, video_decode_ctx_->pix_fmt, 1);

        dst_video_fp_ = fopen(dst_video_filename_.c_str(), "wb");
        if (!dst_video_fp_)
        {
            LOG_WARNING("fopen dst_video_filename_ error\n");
            return -1;
        }
    }
}

int Decoder::decodec()
{
    // 打开解码器
    int nRet = open_decodec();
    if (nRet < 0)
    {
        LOG_WARNING("open_decodec error\n");
        return -666;
    }

    AVPacket* pkt = av_packet_alloc();
    av_init_packet(pkt);
    pkt->data = NULL;
    pkt->size = 0;

    while (av_read_frame(ifmt_ctx_, pkt) >= 0)
    {
        if (pkt->stream_index == audio_idx_)    // 音频包
        {
            nRet = decode_pkt(audio_decode_ctx_, pkt);
        }
        else if (pkt->stream_index == video_idx_) // 视频包
        {
            nRet = decode_pkt(video_decode_ctx_, pkt);
        }
        av_packet_unref(pkt);
        if (nRet < 0)
        {
            break;
        }
    }

    // 冲刷解码器
    if (video_decode_ctx_)
    {
        decode_pkt(video_decode_ctx_, NULL);
    }
    if (audio_decode_ctx_)
    {
        decode_pkt(video_decode_ctx_, NULL);
    }
    return 0;
}

int Decoder::decode_pkt(AVCodecContext* codec_ctx, AVPacket* pkt)
{
    int nRet = avcodec_send_packet(codec_ctx, pkt);
    if (nRet < 0)
    {
        LOG_WARNING("avcodec_send_packet fflush codec\n");
        return -1;
    }

    // 一个包可能对应多个frame
    frame_ = av_frame_alloc();
    while (nRet >= 0)
    {
        nRet = avcodec_receive_frame(codec_ctx, frame_);
        if (nRet < 0)
        {
            if (nRet == AVERROR_EOF || nRet == AVERROR(EAGAIN))
                return 0;
            return nRet;
        }
        // 写数据
        if (codec_ctx->codec->type == AVMEDIA_TYPE_AUDIO)
        {
            nRet = output_audio_frame(frame_);
        }
        else if (codec_ctx->codec->type == AVMEDIA_TYPE_VIDEO)
        {
            nRet = output_video_frame(frame_);
        }
        av_frame_unref(frame_);
        if (nRet < 0)
            return nRet;
    }
}

int Decoder::output_video_frame(AVFrame* frame)
{
    if (video_dst_buf_size_ < 0)
    {
        LOG_WARNING("av_image_alloc error\n");
        return -2;
    }
    // 写入帧到文件
    if (frame->width != video_decode_ctx_->width ||
        frame->height != video_decode_ctx_->height ||
        frame->format != video_decode_ctx_->pix_fmt)
    {
        LOG_WARNING("frame and video_decode_ctx_ error\n");
        return -3;
    }
    // 填充数据到 video_dst_data
    av_image_copy(video_dst_data_, linesize_,
        (const uint8_t**)(frame->data), frame->linesize,
        video_decode_ctx_->pix_fmt, video_decode_ctx_->width, video_decode_ctx_->height);
    size_t write_video_size = fwrite(video_dst_data_[0], 1, video_dst_buf_size_, dst_video_fp_);
    fflush(dst_video_fp_);
    return 0;
}

int Decoder::output_audio_frame(AVFrame* frame)
{
    // 计算一帧音频数据的bytes
    size_t a_sample_point_bytes = frame->nb_samples * av_get_bytes_per_sample((AVSampleFormat)frame->format);
    // 写入数据到文件
    fwrite(frame->extended_data[0], 1, a_sample_point_bytes, dst_audio_fp_);
    fflush(dst_audio_fp_);
    return 0;
}

int Decoder::get_format_from_sample_fmt(std::string& fmtStr, enum AVSampleFormat sample_fmt)
{
    struct sample_fmt_entry {
        enum AVSampleFormat sample_fmt; 
        std::string fmt_be;
        std::string fmt_le;
    } sample_fmt_entries[] = {
        { AV_SAMPLE_FMT_U8, "u8", "u8" },
        { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
        { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
        { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
        { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
    };

    for (int i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
        struct sample_fmt_entry *entry = &sample_fmt_entries[i];
        if (sample_fmt == entry->sample_fmt) {
            fmtStr = AV_NE(entry->fmt_be, entry->fmt_le);
            return 0;
        }
    }

    fprintf(stderr, "sample format %s is not supported as output format\n", av_get_sample_fmt_name(sample_fmt));
    return -1;
}

void Decoder::print_audio_play_str()
{
    if (audio_stream_) {
        enum AVSampleFormat sfmt = audio_decode_ctx_->sample_fmt;
        int n_channels = audio_decode_ctx_->channels;
        std::string fmtStr;

        if (av_sample_fmt_is_planar(sfmt))    // 是planar格式
        {
            const char *packed = av_get_sample_fmt_name(sfmt);
            LOG_INFO("Warning: the sample format the decoder produced is planar "
                "(%s). This example will output the first channel only.\n",
                packed ? packed : "?");
            sfmt = av_get_packed_sample_fmt(sfmt);
            n_channels = 1;
        }

        int ret = -1;
        if ((ret = get_format_from_sample_fmt(fmtStr, sfmt)) < 0)
            return;

        LOG_INFO("Play the output audio file with the command:\n"
            "ffplay -f %s -ac %d -ar %d %s\n",
            fmtStr.c_str(), n_channels, audio_decode_ctx_->sample_rate,
            dst_audio_filename_.c_str());

        LOG_INFO("play video yuv:\nffplay -video_size %dx%d -f rawvideo -pixel_format yuv420p %s",
            video_decode_ctx_->width, video_decode_ctx_->height, dst_video_filename_.c_str());
    }
}

int main()
{
    std::string src_filename = "./MediaFile/HTA_13s.mp4";    // input_5s
    std::string dst_audio_filename = "./MediaFile/HTA_13s_decodec_audio.pcm"; // HTA_13s_decodec_audio
    std::string dst_video_filename = "./MediaFile/HTA_13s_decodec_video.yuv"; // HTA_13s_decodec_video

    Decoder decoder(src_filename, dst_audio_filename, dst_video_filename);
    // 解复用
    int nRet = decoder.demuxer();
    if (nRet < 0)
    {
        LOG_WARNING("main demuxer error\n");
        return -666;
    }
    // 解码写入到文件
    nRet = decoder.decodec();
    if (nRet < 0)
    {
        LOG_WARNING("main decodec error\n");
        return -777;
    }

    LOG_INFO("decode finish, write file success\n");
    decoder.print_audio_play_str();

    getchar();
    return 0;
}

注意:使用av_image_alloc为一个char* data[4]的数组分配内存时,其内部只是为data[0]分配内存,其余的data[1] - data[3]指针指向data[0]的某个位置(根据像素格式的不同)

相关文章
pip镜像源大全及配置
在中国使用pip时,可以配置国内镜像源来提高安装速度和稳定性。以下是一些常见的国内镜像源:
21901 0
|
消息中间件 Java Shell
在CentOS7上安装RocketMQ 4.8.0
本文是博主在CentOS7上安装RocketMQ 4.8.0的记录,希望对大家有所帮助。
1272 0
|
程序员 项目管理
程序员如何做好个人职业规划彻底摆脱焦虑?
程序员如何做好个人职业规划彻底摆脱焦虑?
413 0
|
5月前
|
人工智能 自然语言处理 知识图谱
实战指南:基于【两大核心+四轮驱动】理论,制定高效Geo优化策略
随着生成式AI重塑信息获取方式,传统SEO正升级为GEO(生成式引擎优化)。本文解读于磊老师首创的【两大核心+四轮驱动】GEO理论,融合E-E-A-T原则,提出以人性化内容与权威性建设为核心,通过结构化数据、多模态覆盖、意图优化与持续监测,构建AI时代高效获客的内容战略。
648 1
|
7月前
|
机器学习/深度学习 算法 安全
【光伏功率预测】基于EMD-PCA-LSTM的光伏功率预测模型(Matlab代码实现)
【光伏功率预测】基于EMD-PCA-LSTM的光伏功率预测模型(Matlab代码实现)
351 1
|
人工智能 算法 数据安全/隐私保护
基于文档智能和百炼平台的RAG应用-部署实践有感
本文对《文档智能 & RAG让AI大模型更懂业务》解决方案进行了详细测评,涵盖实践原理理解、部署体验、LLM知识库优势及改进空间、适用业务场景等方面。测评指出,该方案在提升AI大模型对特定业务领域的理解和应用能力方面表现突出,但需在技术细节描述、知识库维护、多语言支持、性能优化及数据安全等方面进一步完善。
611 1
|
编解码 语音技术 内存技术
FFmpeg开发笔记(五十八)把32位采样的MP3转换为16位的PCM音频
《FFmpeg开发实战:从零基础到短视频上线》一书中的“5.1.2 把音频流保存为PCM文件”章节介绍了将媒体文件中的音频流转换为原始PCM音频的方法。示例代码直接保存解码后的PCM数据,保留了原始音频的采样频率、声道数量和采样位数。但在实际应用中,有时需要特定规格的PCM音频。例如,某些语音识别引擎仅接受16位PCM数据,而标准MP3音频通常采用32位采样,因此需将32位MP3音频转换为16位PCM音频。
526 0
FFmpeg开发笔记(五十八)把32位采样的MP3转换为16位的PCM音频
|
机器学习/深度学习 人工智能 物联网
微软Phi-4系列开源:多模态与文本处理的创新突破
微软近期推出 Phi-4-multimodal 和 Phi-4-mini,这些模型是 Microsoft Phi 系列小型语言模型 (SLM) 中的最新模型。Phi-4-multimodal 能够同时处理语音、视觉和文本,为创建创新且具有上下文感知能力的应用程序开辟了新的可能性。另一方面,Phi-4-mini 在基于文本的任务方面表现出色,以紧凑的形式提供高精度和可扩展性。
797 4
|
网络协议 Linux
linux学习之套接字通信
Linux中的套接字通信是网络编程的核心,允许多个进程通过网络交换数据。套接字提供跨网络通信能力,涵盖本地进程间通信及远程通信。主要基于TCP和UDP两种模型:TCP面向连接且可靠,适用于文件传输等高可靠性需求;UDP无连接且速度快,适合实时音视频通信等低延迟场景。通过创建、绑定、监听及读写操作,可以在Linux环境下轻松实现这两种通信模型。
429 1
|
安全 前端开发 JavaScript
跨域iframe通信
跨域iframe通信
613 2

热门文章

最新文章