随笔-118  评论-133  文章-4  trackbacks-0
参考以下帖子:
http://blog.csdn.net/vblittleboy/article/details/20121341

作了一下改动,修正了bug,提高适用性:
#include <stdlib.h>
#include 
<stdio.h>
#include 
<string.h>
#include 
<math.h>
 
#include 
<libavutil/opt.h>
#include 
<libavutil/mathematics.h>
#include 
<libavformat/avformat.h>
 
FILE 
* fp_in = NULL;
FILE 
* fp_out = NULL;
 
static 
int frame_count;
 
int main(int argc, char **argv)
{
    
int ret;
    AVCodec 
*audio_codec;
    AVCodecContext 
*c;
    AVFrame 
*frame;
    AVPacket pkt 
= { 0 }; // data and size must be 0;
    
int got_output;
 
    
/* Initialize libavcodec, and register all codecs and formats. */
    av_register_all();
    avcodec_register_all();
    
//avdevice_register_all();
 
    audio_codec 
= avcodec_find_encoder(AV_CODEC_ID_AAC);
    c 
= avcodec_alloc_context3(audio_codec);
//    c->strict_std_compliance =FF_COMPLIANCE_EXPERIMENTAL;   
    c
->codec_id = AV_CODEC_ID_AAC;
    c
->sample_fmt = AV_SAMPLE_FMT_S16;
    c
->sample_rate = 44100;
    c
->channels = 2;
    c
->channel_layout = AV_CH_LAYOUT_STEREO;
    c
->bit_rate = 64000;
 
    
/* open the codec */
    ret 
= avcodec_open2(c, audio_codec, NULL);
    
if (ret < 0) {
        fprintf(stderr, 
"Could not open video codec: %s\n", av_err2str(ret));
        
exit(1);
    }
 
    
/* allocate and init a re-usable frame */
#
if 0
    frame 
= avcodec_alloc_frame();
#
else
    frame 
= av_frame_alloc();
#endif
    
if (!frame) {
        fprintf(stderr, 
"Could not allocate video frame\n");
        
exit(1);
    }
 
 
    frame
->nb_samples = c->frame_size;
    frame
->format = c->sample_fmt;
    frame
->channels = c->channels;
    frame
->channel_layout = c->channel_layout;
#
if 0
    frame
->linesize[0= 4096;
    frame
->extended_data = frame->data[0= av_malloc((size_t)frame->linesize[0]);
#
else
    ret 
= av_frame_get_buffer(frame, 0);
    
if (ret < 0) {
        fprintf(stderr, 
"Could not allocate an audio frame.\n");
        
exit(1);
    }
    printf(
"----nb_samples= %d, linesize= %d\n", frame->nb_samples, frame->linesize[0]);
#endif

    av_init_packet(
&pkt);

    fp_in 
= fopen("in.wav","rb");
    fp_out
= fopen("out.aac","wb");
 
    
//printf("frame->nb_samples = %d\n",frame->nb_samples);
     
    
while(1)
    {
        frame_count
++;
        bzero(frame
->data[0],frame->linesize[0]);
        ret 
= fread(frame->data[0],frame->linesize[0],1,fp_in);
        
if(ret <= 0)
        {
            printf(
"read over !\n");
            break;
        }
        ret 
= avcodec_encode_audio2(c, &pkt, frame, &got_output);
        
if (ret < 0) {
            fprintf(stderr, 
"Error encoding audio frame: %s\n", av_err2str(ret));
            
exit(1);
        }
     
        
if(got_output > 0)
        {
            
//printf("pkt.size = %d\n",pkt.size);
            fwrite(pkt.data,pkt.size,
1,fp_out);
            av_free_packet(
&pkt);
        }
 
        #
if 0
        
if(frame_count > 10)
        {
            printf(
"break @@@@@@@@@@@@\n");
            break;
        }
        #endif
    }
 
    avcodec_close(c);
    av_free(c);
#
if 0
    avcodec_free_frame(
&frame);
#
else
    av_frame_free(
&frame);
#endif
    fclose(fp_in);
    fclose(fp_out);
 
    return 
0;
}

另外,建议看一下ffmpeg自带的例程,很有参考价值(特别是需要用到重采样功能):
doc/examples/transcode_aac.c

注:
    aac编码用到了libfdk_aac库,详细请参考:
http://trac.ffmpeg.org/wiki/Encode/AAC
posted on 2017-02-10 11:24 lfc 阅读(1976) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。