• FFmpeg


    1. 注册所有解码器
     av_register_all();


    2. Codec & CodecContext

       
        AVCodec* codec = avcodec_find_decoder(CODEC_ID_AAC);
        if (!codec)
        {
            fprintf(stderr, "codec not found ");
            exit(1);
        }

     
        AVCodecContext *codec_ctx= avcodec_alloc_context();

       
        if (avcodec_open(codec_ctx, codec) < 0)
        {
            fprintf(stderr, "could not open codec ");
            exit(1);
        }

    3. 准备好AVPacket
     AVPacket avpkt;
     av_init_packet(&avpkt);
        avpkt.size = pesdec.m_nEsLength;
        avpkt.data = pesdec.m_pEs;
        avpkt.pts = pesdec.m_nPts;

    4. 准备好一个足够大的output buffer,用于存储音频解码得到的数据
    奇怪的是长度要为AVCODEC_MAX_AUDIO_FRAME_SIZE *2,少了还不行(aac音频时,会crash)
     int16_t * outbuf = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE * 2];

    5. 解码
     while(1)
     {
      // 读取一个完整的PES

      // 解码
      while (avpkt.size > 0)
      {
       int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; // 对于AAC解码器来说,长度不能小于这个
       int len = avcodec_decode_audio3(codec_ctx, (short *)outbuf, &out_size, &avpkt);
       if (len <= 0)
       {
        fprintf(stderr, "Error while decoding ");
        break;
       }

       if (out_size > 0)
       {
        double pts = (double) avpkt.pts / 45000;
        printf("[time: %.3f] sound sample ", pts);

        
        // fwrite(outbuf, 1, out_size, outfile);
        // printf("get %d bytes. ", out_size);
       }

       avpkt.size -= len;
       avpkt.data += len;
      }
     }

    6. 释放资源
       delete [] outbuf;

        avcodec_close(codec_ctx);
        av_free(codec_ctx);

  • 相关阅读:
    Rsync+inotify自动同步数据
    join和 Daemon守护线程
    多线程的简单演示
    DB2用一张表更新其他表的数据
    WebService到底是什?
    JqueryUI学习笔记-自动完成autocomplete
    DB2 SQLCODE 大全
    eclipse调试java程序的九个技巧
    Class和ClassLoader的getResourceAsStream区别
    linux下vi命令大全
  • 原文地址:https://www.cnblogs.com/lidabo/p/4582393.html
Copyright © 2020-2023  润新知