ffmpeg 实现音频aac编码

1、编译ffmepg

./configure --disable-yasm --enable-nonfree --enable-libfaac --prefix=/home/ffmpeg/1_ffmpeg-2.1.1/install


2、编译audio_enc.c

makefile:

1
2
3
4
5
6
7
8
#!/bin/sh
INCLUDE = .. /include
LIB_DIR = .. /lib
LDFLAGS =  -lfaac -lavcodec -lavformat -lavdevice -lavfilter -lavutil -lswresample -pthread  -ldl -lswscale -lasound -lz -lm -lbz2
 
SRC=audio_enc.c
all:$(SRC)
     gcc -g -Wall $(SRC) -o target -I $(INCLUDE) -L $(LIB_DIR) $(LDFLAGS)

注意:在这里编译的时候需要加上aac库,可能会找不到库函数undefined reference to `faacEncEncode'等


程序运行时,需要提供一个和程序参数一致的wav音频文件:


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
/*
* Copyright(C), 2013, Ubuntu Inc.
* File name:        audio_enc.c
* Author:           xubinbin 徐彬彬 (Beijing China)
* Version:          1.0
* Date:             2013.12.23
* Description:      Use ffmpeg achieve aac audio coding.
* Function List:   
* Email:            xubbwd@gmail.com
*/
 
#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;
     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->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 */
     frame = avcodec_alloc_frame();
     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;
     frame->linesize[0] = 4096;
     frame->extended_data = frame->data[0] = av_malloc(( size_t )frame->linesize[0]);
 
     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);
     avcodec_free_frame(&frame);
 
     fclose (fp_in);
     fclose (fp_out);
 
     return  0;
}
转载自:http://hi.baidu.com/285988185/item/6864b8b0c9640445ba0e12a6
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值