2,554
社区成员
发帖
与我相关
我的任务
分享int main(int argc, char **argv)
{
if(argc < 3)
{
printf("argc number is error\n");
return 0;
}
av_register_all();
AVCodecContext *encodecCtx = NULL;
AVCodec *encodec;
AVPacket enpacket;
AVFrame avframe;
int gotframeptr;
int frame_size = 0;
int *samples = 0;
int SAMPLESIZE = 0;
encodec = avcodec_find_encoder(CODEC_ID_AAC);
if(!encodec)
{
fprintf(stderr, "Unsupported encodec!\n");
return -1;
}
encodecCtx= avcodec_alloc_context3(encodec);
encodecCtx->channels = 2;
encodecCtx->sample_fmt = AV_SAMPLE_FMT_S16;
encodecCtx->sample_rate = 44100;
encodecCtx->bit_rate = 128000;
encodecCtx->profile = FF_PROFILE_AAC_LOW;
encodecCtx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
if((avcodec_open2(encodecCtx, encodec, NULL) < 0))
{
fprintf(stderr, "can't open encodec!\n");
return -1;
}
frame_size = encodecCtx->frame_size;
samples = malloc(frame_size * 2 * encodecCtx->channels);
SAMPLESIZE = frame_size * 2 * encodecCtx->channels;
FILE *fin = NULL;
fin = fopen(argv[1],"rb");
if(!fin)
{
printf("open fin file error\n");
return 0;
}
FILE *fout = NULL;
fout = fopen(argv[2],"a+");
if(!fout)
{
printf("open fout file error\n");
return 0;
}
uint8_t *out_buf = (uint8_t*)malloc(encodecCtx->frame_size*8);
memset(out_buf,0,encodecCtx->frame_size*8);
int size;
int len;
while(1)
{
size = fread(samples,1,SAMPLESIZE,fin);
if(size == 0)
{
printf("read elf\n");
break;
}
avframe.data[0] = (uint8_t*)samples;
avframe.linesize[0] = SAMPLESIZE;
avframe.extended_data = &avframe.data[0];
avframe.nb_samples = encodecCtx->frame_size;
av_init_packet(&enpacket);
enpacket.data = NULL;
enpacket.size = 0;
len = avcodec_encode_audio(encodecCtx,out_buf,FF_MIN_BUFFER_SIZE,(short*)avframe.data[0]);
int num = fwrite(out_buf,1,len,fout);
if(num == 0)
{
printf("write error\n");
sleep(1);
}
memset(out_buf,0,FF_MIN_BUFFER_SIZE);
}
av_free_packet(&enpacket);
fclose(fout);
fclose(fin);
return 0;
}