ffmpeg音频编码

来源:互联网 发布:mysql创建普通索引 编辑:程序博客网 时间:2024/06/06 08:39
 

ffmpeg音频编码

分类: audio/video 1595人阅读 评论(2) 收藏 举报

以mp3编码为例,编解码库中提供了两种音频编码api,下面分别给出两个api的用法。好啦,废话不多说,贴出代码供参考。

[cpp] view plaincopyprint?
  1. void audio_encode(const char * inputfilename,const char *outputfilename) {  
  2.     AVCodec *codec;  
  3.     AVCodecContext *c = NULL;  
  4.     int frame_size, out_size, outbuf_size;  
  5.     FILE * fin, *fout;  
  6.     short *samples;  
  7.     uint8_t *outbuf;  
  8.     int numberframe = 0;  
  9.     int size = 0;  
  10.     int FRAME_READ = 0;  
  11.          
  12.     printf("Audio encoding\n");  
  13.         av_register_all();  
  14.     /* find the MP3 encoder */  
  15.     codec = avcodec_find_encoder(AV_CODEC_ID_MP3);  
  16.     if (!codec) {  
  17.         fprintf(stderr, "codec not found\n");  
  18.         exit(1);  
  19.     }  
  20.   
  21.     c = avcodec_alloc_context();  
  22.   
  23.     /* put sample parameters */  
  24.     c->bit_rate = 64000;  
  25.       c->sample_rate = 44100;  
  26.     c->channels = 2;  
  27.     c->sample_fmt = AV_SAMPLE_FMT_S16;  
  28.   
  29.       
  30.   
  31.     /* open it */  
  32.     if (avcodec_open(c, codec) < 0) {  
  33.         fprintf(stderr, "could not open codec\n");  
  34.         exit(1);  
  35.     }  
  36.   
  37.     /* the codec gives us the frame size, in samples */  
  38.     frame_size = c->frame_size;  
  39.     samples = malloc(frame_size * 2 * c->channels);   
  40.   
  41.     FRAME_READ = frame_size * 2 * c->channels;  
  42.   
  43.     outbuf_size = 10000;  
  44.     outbuf = malloc(outbuf_size);  
  45.   
  46.     fin = fopen(inputfilename, "rb+");  
  47.     if (!fin) {  
  48.         fprintf(stderr, "could not open %s\n", inputfilename);  
  49.         exit(1);  
  50.     }  
  51.   
  52.     fout = fopen(outputfilename, "wb");  
  53.     if (!fout) {  
  54.         fprintf(stderr, "could not open %s\n", outputfilename);  
  55.         exit(1);  
  56.     }  
  57.     for (;;) {  
  58.         size = fread(samples, 1, FRAME_READ, fin);  
  59.         if (size == 0) {  
  60.             break;  
  61.         }  
  62.         /* encode the samples */  
  63.         out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);  
  64.         fwrite(outbuf, 1, out_size, fout);  
  65.         numberframe++;  
  66.         printf("save frame %d\n", numberframe);  
  67.   
  68.     }  
  69.     fclose(fout);  
  70.     free(outbuf);  
  71.     free(samples);  
  72.   
  73.     avcodec_close(c);  
  74.     av_free(c);  
  75.     printf("audio encode finish...");  
  76. }  


上面程序中用到的音频编码api已经过时了,官方文档中不建议使用,下面看看新的编码api。


[cpp] view plaincopyprint?
  1. static void audio_encode_example(const char *output_filename,const char *input_filename) {  
  2.     AVCodec *codec;  
  3.     AVCodecContext *c = NULL;  
  4.     AVFrame *frame;  
  5.     AVPacket pkt;  
  6.     int i,ret, got_output;  
  7.     int buffer_size;  
  8.     FILE *fout, *fin;  
  9.     uint8_t *samples;  
  10.     int numberframe = 0;  
  11.   
  12.     printf("Encode audio file %s\n", output_filename);  
  13.         av_register_all();  
  14.     /* find the MP3 encoder */  
  15.     codec = avcodec_find_encoder(AV_CODEC_ID_MP3);  
  16.     if (!codec) {  
  17.         fprintf(stderr, "Codec not found\n");  
  18.         exit(1);  
  19.     }  
  20.   
  21.     c = avcodec_alloc_context3(codec);  
  22.   
  23.     /* put sample parameters */  
  24.     c->bit_rate = 64000;  
  25.     c->sample_rate = 44100;  
  26.     c->channels = 2;  
  27.   
  28.       
  29.     c->sample_fmt = AV_SAMPLE_FMT_S16;  
  30.       
  31.   
  32.     /* select other audio parameters supported by the encoder */  
  33.   
  34.     c->channel_layout = select_channel_layout(codec);  
  35.       
  36.   
  37.     /* open it */  
  38.     if (avcodec_open2(c, codec, NULL ) < 0) {  
  39.         fprintf(stderr, "Could not open codec\n");  
  40.         exit(1);  
  41.     }  
  42.   
  43.     fout = fopen(output_filename, "wb");  
  44.     if (!fout) {  
  45.         fprintf(stderr, "Could not open %s\n", output_filename);  
  46.         exit(1);  
  47.     }  
  48.   
  49.     fin = fopen(input_filename, "rb");  
  50.     if (!fin) {  
  51.         fprintf(stderr, "Could not open %s\n", input_filename);  
  52.         exit(1);  
  53.     }  
  54.   
  55.     /* frame containing input raw audio */  
  56.     frame = avcodec_alloc_frame();  
  57.     if (!frame) {  
  58.         fprintf(stderr, "Could not allocate audio frame\n");  
  59.         exit(1);  
  60.     }  
  61.   
  62.     frame->nb_samples = c->frame_size;  
  63.     frame->format = c->sample_fmt;  
  64.     frame->channel_layout = c->channel_layout;  
  65.   
  66.     /* the codec gives us the frame size, in samples, 
  67.      * we calculate the size of the samples buffer in bytes */  
  68.     buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,  
  69.             c->sample_fmt, 0);  
  70.     samples = av_malloc(buffer_size);  
  71.     if (!samples) {  
  72.         fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",  
  73.                 buffer_size);  
  74.         exit(1);  
  75.     }  
  76.     /* setup the data pointers in the AVFrame */  
  77.     ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,  
  78.             (const uint8_t*) samples, buffer_size, 0);  
  79.     if (ret < 0) {  
  80.         fprintf(stderr, "Could not setup audio frame\n");  
  81.         exit(1);  
  82.     }  
  83.   
  84.     for (;;) {  
  85.   
  86.         av_init_packet(&pkt);  
  87.         pkt.data = samples;  
  88.         pkt.size = fread(samples, 1, buffer_size, fin);  
  89.         if (pkt.size == 0) {  
  90.             break;  
  91.         }  
  92.   
  93.         ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);  
  94.         if (ret < 0) {  
  95.             fprintf(stderr, "Error encoding audio frame\n");  
  96.             exit(1);  
  97.         }  
  98.         if (got_output) {  
  99.             fwrite(pkt.data, 1, pkt.size, fout);  
  100.             av_free_packet(&pkt);  
  101.             numberframe++;  
  102.             printf("save frame %d\n", numberframe);  
  103.         }  
  104.   
  105.     }  
  106.   
  107.     /* get the delayed frames */  
  108.     for (got_output = 1; got_output; i++) {  
  109.       av_init_packet(&pkt);  
  110.         pkt.size=1024;  
  111.         ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);  
  112.         if (ret < 0) {  
  113.             fprintf(stderr, "Error encoding frame\n");  
  114.             exit(1);  
  115.         }  
  116.   
  117.         if (got_output) {  
  118.             fwrite(pkt.data, 1, pkt.size, fout);  
  119.             av_free_packet(&pkt);  
  120.         }  
  121.     }  
  122.     fclose(fout);  
  123.     fclose(fin);  
  124.     av_freep(&samples);  
  125.     avcodec_free_frame(&frame);  
  126.     avcodec_close(c);  
  127.     av_free(c);  
  128.     printf("audio encode finish...");  
  129. }  




PS:水平有限,如以上内容有误,欢迎指正!

0 0
原创粉丝点击