2012-03-15 156 views
0

我成功編譯了libavcodecspeex已啓用。 我修改了FFMPEG文檔中的示例,將示例音頻編碼爲Speex。 但結果文件不能用VLC播放器(它有Speex解碼器)播放。使用libavcodec編碼Speex(FFMpeg)?

任何提示?

static void audio_encode_example(const char *filename) 
{ 
    AVCodec *codec; 
    AVCodecContext *c= NULL; 
    int frame_size, i, j, out_size, outbuf_size; 
    FILE *f; 
    short *samples; 
    float t, tincr; 
    uint8_t *outbuf; 

    printf("Audio encoding\n"); 

    /* find the MP2 encoder */ 
    codec = avcodec_find_encoder(CODEC_ID_SPEEX); 
    if (!codec) { 
     fprintf(stderr, "codec not found\n"); 
     exit(1); 
    } 

    c= avcodec_alloc_context(); 

    /* put sample parameters */ 
    c->bit_rate = 64000; 
    c->sample_rate = 32000; 
    c->channels = 2; 
    c->sample_fmt=AV_SAMPLE_FMT_S16; 

    /* open it */ 
    if (avcodec_open(c, codec) < 0) { 
     fprintf(stderr, "could not open codec\n"); 
     exit(1); 
    } 

    /* the codec gives us the frame size, in samples */ 
    frame_size = c->frame_size; 
    printf("frame size %d\n",frame_size); 
    samples =(short*) malloc(frame_size * 2 * c->channels); 
    outbuf_size = 10000; 
    outbuf =(uint8_t*) malloc(outbuf_size); 

    f = fopen(filename, "wb"); 
    if (!f) { 
     fprintf(stderr, "could not open %s\n", filename); 
     exit(1); 
    } 

    /* encode a single tone sound */ 
    t = 0; 
    tincr = 2 * M_PI * 440.0/c->sample_rate; 
    for(i=0;i<200;i++) { 
     for(j=0;j<frame_size;j++) { 
      samples[2*j] = (int)(sin(t) * 10000); 
      samples[2*j+1] = samples[2*j]; 
      t += tincr; 
     } 
     /* encode the samples */ 
     out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples); 
     fwrite(outbuf, 1, out_size, f); 
    } 
    fclose(f); 
    free(outbuf); 
    free(samples); 
    avcodec_close(c); 
    av_free(c); 
} 

int main(int argc, char **argv) 
{ 

    avcodec_register_all(); 

    audio_encode_example(argv[1]); 

    return 0; 
} 

回答

1

不Speex的(我不知道)偶然需要到這些幀放置,以某種頭的容器格式?你只需要輸出一個編碼器並將其轉儲到一個文件中,而不需要通過任何格式化(libavformat)。

嘗試使用ffmpeg命令行實用程序將相同的數據編碼到Speex中,並查看結果文件是否播放。

我正在看一些信息www.speex.orgspeex似乎.ogg文件。您正在使用的播放器可能無法識別原始Speex數據,但只能在.ogg中打包。

雖然不是100%確定的答案,但我希望這有些幫助!

+0

我想知道,我使用了CODEC_ID_MP2,結果文件不需要容器。我會嘗試使用OGG容器。 – 2012-03-15 08:44:58