zoukankan      html  css  js  c++  java
  • FFmpeg简单使用:解封装 ---- 提取aac

    =====================================================

    FFmpeg简单使用:解封装 ---- 基本流程

    FFmpeg简单使用:解封装 ---- 提取aac

    FFmpeg简单使用:音频解码 ---- 提取pcm

    FFmpeg简单使用:视频解码 ---- 提取yuv

    FFmpeg简单使用:音频编码 ---- pcm转aac

    FFmpeg简单使用:视频编码 ---- YUV转H264

    FFmpeg简单使用:过滤器 ---- 视频过滤

    FFmpeg简单使用:过滤器 ---- 视频过滤2

    FFmpeg简单使用:过滤器 ---- h264_mp4toannexb

    FFmpeg简单使用:解封装h264 ---- 提取SPS PPS

    =====================================================

    #include <stdio.h>
    #include "libavutil/log.h"
    #include "libavformat/avio.h"
    #include "libavformat/avformat.h"
    
    //#define AacHeader
    
    const char* fileName = "believe.flv";
    #ifdef AacHeader
    const char* outFileName = "outBelieve.aac";
    #else
    const char* outFileName = "outBelieveNoHeader.aac";
    #endif
    
    const int sampling_frequencies[] = {
        96000,  // 0x0
        88200,  // 0x1
        64000,  // 0x2
        48000,  // 0x3
        44100,  // 0x4
        32000,  // 0x5
        24000,  // 0x6
        22050,  // 0x7
        16000,  // 0x8
        12000,  // 0x9
        11025,  // 0xa
        8000   // 0xb
        // 0xc d e f是保留的
    };
    
    int adts_header(char * const p_adts_header, const int data_length,
                    const int profile, const int samplerate,
                    const int channels)
    {
    
        int sampling_frequency_index = 3; // 默认使用48000hz
        int adtsLen = data_length + 7;
    
        int frequencies_size = sizeof(sampling_frequencies) / sizeof(sampling_frequencies[0]);
        int i = 0;
        for(i = 0; i < frequencies_size; i++)
        {
            if(sampling_frequencies[i] == samplerate)
            {
                sampling_frequency_index = i;
                break;
            }
        }
        if(i >= frequencies_size)
        {
            printf("unsupport samplerate:%d
    ", samplerate);
            return -1;
        }
    
        p_adts_header[0] = 0xff;         //syncword:0xfff                          高8bits
        p_adts_header[1] = 0xf0;         //syncword:0xfff                          低4bits
        p_adts_header[1] |= (0 << 3);    //MPEG Version:0 for MPEG-4,1 for MPEG-2  1bit
        p_adts_header[1] |= (0 << 1);    //Layer:0                                 2bits
        p_adts_header[1] |= 1;           //protection absent:1                     1bit
    
        p_adts_header[2] = (profile)<<6;            //profile:profile               2bits
        p_adts_header[2] |= (sampling_frequency_index & 0x0f)<<2; //sampling frequency index:sampling_frequency_index  4bits
        p_adts_header[2] |= (0 << 1);             //private bit:0                   1bit
        p_adts_header[2] |= (channels & 0x04)>>2; //channel configuration:channels  高1bit
    
        p_adts_header[3] = (channels & 0x03)<<6; //channel configuration:channels 低2bits
        p_adts_header[3] |= (0 << 5);               //original:0                1bit
        p_adts_header[3] |= (0 << 4);               //home:0                    1bit
        p_adts_header[3] |= (0 << 3);               //copyright id bit:0        1bit
        p_adts_header[3] |= (0 << 2);               //copyright id start:0      1bit
        p_adts_header[3] |= ((adtsLen & 0x1800) >> 11);           //frame length:value   高2bits
    
        p_adts_header[4] = (uint8_t)((adtsLen & 0x7f8) >> 3);     //frame length:value    中间8bits
        p_adts_header[5] = (uint8_t)((adtsLen & 0x7) << 5);       //frame length:value    低3bits
        p_adts_header[5] |= 0x1f;                                 //buffer fullness:0x7ff 高5bits
        p_adts_header[6] = 0xfc;      //‭11111100‬       //buffer fullness:0x7ff 低6bits
        // number_of_raw_data_blocks_in_frame:
        //    表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1个AAC原始帧。
        return 0;
    }
    
    int main()
    {
        printf("Hello World!
    ");
    
    
        // 1.打开文件
        AVFormatContext *ctx = NULL;
        int ret = avformat_open_input(&ctx, fileName, NULL, NULL);
        if (ret < 0) {
            char buf[1024] = {0};
            av_strerror(ret, buf, sizeof (buf) - 1);
            printf("avformat_open_input %s failed: %s
    ", fileName, buf);
            return -1;
        }
    
        // 2.获取码流信息
        ret = avformat_find_stream_info(ctx, NULL);
        if (ret < 0) {
            char buf[1024] = {0};
            av_strerror(ret, buf, sizeof (buf) - 1);
            printf("avformat_find_stream_info %s failed: %s
    ", fileName, buf);
            return -1;
        }
    
        // 3.获取音频流
        int audioIndex = av_find_best_stream(ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
        if (audioIndex < 0) {
            printf("av_find_best_stream failed: %s
    ", av_get_media_type_string(AVMEDIA_TYPE_AUDIO));
            return -1;
        }
    
        // 4.检查是否是aac
        if (ctx->streams[audioIndex]->codecpar->codec_id != AV_CODEC_ID_AAC) {
            printf("audio codec %d is not AAC.
    ", ctx->streams[audioIndex]->codecpar->codec_id);
            goto failed;
        }
    
        // 5.打开输出aac文件
         FILE *fd = fopen(outFileName, "wb");
         if (fd == NULL) {
             printf("fopen open %s failed.
    ", outFileName);
             goto failed;
         }
    
        // 6.提取aac数据
         AVPacket pkt;
         av_init_packet(&pkt);
         int len = 0;
         while (av_read_frame(ctx, &pkt) >= 0) {
             if (pkt.stream_index == audioIndex) {
    #ifdef AacHeader  // 写入aac头
                char adts_header_buf[7] = {0};
                adts_header(adts_header_buf, pkt.size,
                            ctx->streams[audioIndex]->codecpar->profile,
                            ctx->streams[audioIndex]->codecpar->sample_rate,
                            ctx->streams[audioIndex]->codecpar->channels);
                fwrite(adts_header_buf, 1, 7, fd);  // 写adts header , ts流不适用,ts流分离出来的packet带了adts header
    #endif
                len = fwrite( pkt.data, 1, pkt.size, fd);   // 写adts data
                if(len != pkt.size)
                {
                    printf("warning, length of writed data isn't equal pkt.size(%d, %d)
    ", len, pkt.size);
                }
             }
             av_packet_unref(&pkt);
         }
    
    failed:
        if (ctx) {
            avformat_close_input(&ctx);
        }
        if (fd) {
            fclose(fd);
        }
    
        return 0;
    }

    这个有个地方要注意,flv提取的aac流没有头,需要自己手动添加:

  • 相关阅读:
    【转】验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。
    【转】在控制台、WinForm项目中的嵌入mdf文件的烦恼
    【转】ADB server didn't ACK
    【转】android中AVD的使用
    【转】你不可以不知道的findViewById
    【转】Android中的view
    【转】c# 如何获取项目的根目录
    TYVJ 1452 最大权闭合图
    NOIP 最后的总结
    POJ 2396 有上下界的可行流
  • 原文地址:https://www.cnblogs.com/vczf/p/13553149.html
Copyright © 2011-2022 走看看