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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
| #import "avformat.h" #import "timestamp.h" #import "avcodec.h" #import "opt.h" #import "samplefmt.h" #import "swscale.h" #import "avutil.h"
static int select_best_sample_rate(const AVCodec *codec){ const int *p; int best_samplerate = 0;
if(!codec->supported_samplerates){ return 44100; } p = codec->supported_samplerates; while(*p){ if(!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate)){ best_samplerate = *p; } p++; } return best_samplerate; }
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt){ const enum AVSampleFormat *p = codec->sample_fmts;
while(*p != AV_SAMPLE_FMT_NONE){ if( *p == sample_fmt) { return 1; } p++; } return 0; }
static int select_channel_layout(const AVCodec *codec) { const uint64_t *p; uint64_t best_ch_layout = 0; int best_nb_channels = 0;
if (!codec->channel_layouts) return AV_CH_LAYOUT_STEREO;
p = codec->channel_layouts; while (*p) { int nb_channels = av_get_channel_layout_nb_channels(*p);
if (nb_channels > best_nb_channels) { best_ch_layout = *p; best_nb_channels = nb_channels; } p++; } return best_ch_layout; }
- (IBAction)encodeAudioAction:(id)sender { NSString * dstpath = [NSString stringWithFormat:@"%@/Library/Caches/1-3.aac",NSHomeDirectory()]; char dstChar [ 1024 ]; strcpy (dstChar ,( char *)[dstpath UTF8String]);
NSString * type = @"libfdk_aac"; char typeChar [ 1024 ]; strcpy (typeChar ,( char *)[type UTF8String]); char * argv[2]; argv[0] = dstChar; argv[1] = typeChar; NSLog(@"存储路径:%@", dstpath); encodeAudio(2, argv); }
#pragma mark 音频编码 void encodeAudio(int argc, char* argv[]) { int ret = -1;
FILE *f = NULL;
char *dst = NULL; char *codecName = NULL;
const AVCodec *codec = NULL; AVCodecContext *ctx = NULL;
AVFrame *frame = NULL; AVPacket *pkt = NULL;
uint16_t *samples = NULL;
av_log_set_level(AV_LOG_DEBUG);
if(argc < 2){ av_log(NULL, AV_LOG_ERROR, "arguments must be more than 2\n"); goto _ERROR; }
dst = argv[0]; codecName = argv[1];
codec = avcodec_find_encoder_by_name(codecName); if(!codec){ av_log(NULL, AV_LOG_ERROR, "don't find Codec: %s", codecName); goto _ERROR; }
ctx = avcodec_alloc_context3(codec); if(!ctx){ av_log(NULL, AV_LOG_ERROR, "NO MEMRORY\n"); goto _ERROR; }
ctx->bit_rate = 64000; ctx->sample_fmt = AV_SAMPLE_FMT_S16; if(!check_sample_fmt(codec, ctx->sample_fmt)){ av_log(NULL, AV_LOG_ERROR, "Encoder does not support sample format!\n"); goto _ERROR; }
ctx->sample_rate = select_best_sample_rate(codec);
ctx->channel_layout = select_channel_layout(codec); ctx->channels = av_get_channel_layout_nb_channels(ctx->channel_layout); ret = avcodec_open2(ctx, codec , NULL); if(ret < 0) { av_log(ctx, AV_LOG_ERROR, "Don't open codec: %s \n", av_err2str(ret)); goto _ERROR; }
f = fopen(dst, "wb"); if(!f){ av_log(NULL, AV_LOG_ERROR, "Don't open file:%s", dst); goto _ERROR; }
frame = av_frame_alloc(); if(!frame){ av_log(NULL, AV_LOG_ERROR, "NO MEMORY!\n"); goto _ERROR; }
frame->nb_samples = ctx->frame_size; frame->format = AV_SAMPLE_FMT_S16;
frame->channel_layout = ctx->channel_layout; frame->sample_rate = ctx->sample_rate; ret = av_frame_get_buffer(frame, 0); if(ret < 0) { av_log(NULL, AV_LOG_ERROR, "Could not allocate the video frame \n"); goto _ERROR; }
pkt = av_packet_alloc(); if(!pkt){ av_log(NULL, AV_LOG_ERROR, "NO MEMORY!\n"); goto _ERROR; }
float t = 0; float tincr = 4*M_PI*440/ctx->sample_rate;
for(int i=0; i < 200; i++){ ret = av_frame_make_writable(frame); if(ret < 0) { av_log(NULL, AV_LOG_ERROR, "Could not allocate space!\n"); goto _ERROR; }
samples = (uint16_t*)frame->data[0]; for(int j=0; j < ctx->frame_size; j++){ samples[2*j] = (int)(sin(t)*10000); for(int k=1; k < ctx->channels; k++){ samples[2*j + k] = samples[2*j]; } t += tincr; } encode(ctx, frame, pkt, f); } encode(ctx, NULL, pkt, f); _ERROR: if(ctx){ avcodec_free_context(&ctx); }
if(frame){ av_frame_free(&frame); }
if(pkt){ av_packet_free(&pkt); }
if(f){ fclose(f); } }
|