Encoding 用libavcodec(FFMpeg)编码Speex?

Encoding 用libavcodec(FFMpeg)编码Speex?,encoding,ffmpeg,libavcodec,speex,libavformat,Encoding,Ffmpeg,Libavcodec,Speex,Libavformat,我成功编译了libavcodec,并启用了speex。 我修改了FFMPEG文档中的示例,将示例音频编码到Speex中。 但是结果文件不能用VLC播放器(带有Speex解码器)播放 有什么建议吗 static void audio_encode_example(const char *filename) { AVCodec *codec; AVCodecContext *c= NULL; int frame_size, i, j, out_size, outbuf_siz

我成功编译了libavcodec,并启用了speex。 我修改了FFMPEG文档中的示例,将示例音频编码到Speex中。 但是结果文件不能用VLC播放器(带有Speex解码器)播放

有什么建议吗

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;
}
static void audio\u encode\u示例(const char*filename)
{
AVCodec*编解码器;
AVCodecContext*c=NULL;
帧内尺寸,i,j,帧外尺寸,帧外尺寸;
文件*f;
短*样本;
浮动t,tincr;
uint8_t*f;
printf(“音频编码”);
/*找到MP2编码器*/
codec=avcodec\u find\u编码器(codec\u ID\u SPEEX);
如果(!编解码器){
fprintf(stderr,“找不到编解码器”);
出口(1);
}
c=avcodec_alloc_context();
/*输入样本参数*/
c->比特率=64000;
c->抽样率=32000;
c->channels=2;
c->sample_fmt=AV_sample_fmt_S16;
/*打开它*/
如果(avcodec_打开(c,codec)<0){
fprintf(stderr,“无法打开编解码器”\n);
出口(1);
}
/*编解码器以示例的形式给出了帧大小*/
框架大小=c->框架大小;
printf(“帧大小%d\n”,帧大小);
samples=(短*)malloc(帧大小*2*c->channels);
突出尺寸=10000;
EXBUFF=(uint8_t*)malloc(EXBUF_尺寸);
f=fopen(文件名,“wb”);
如果(!f){
fprintf(stderr,“无法打开%s\n”,文件名);
出口(1);
}
/*对单音进行编码*/
t=0;
tincr=2*M_PI*440.0/c->采样率;
对于(i=0;i,Speex(我不知道)是否偶然需要一种容器格式来放置这些帧,并带有某种标头?您只需获取编码器的输出并将其转储到文件中,而无需进行任何格式化(
libavformat

尝试使用
ffmpeg
命令行实用程序将相同的数据编码到Speex中,然后查看结果文件是否播放

我在
www.speex.org
上查看一些信息,似乎
speex
数据被放入
.ogg
文件中。您正在使用的播放器可能无法识别原始speex数据,但只有当它被包装在
.ogg
中时


虽然不是100%确定的答案,但我希望这能有所帮助!

我想知道,我使用了CODEC\u ID\u MP2,结果文件不需要容器。我将尝试使用OGG容器。