【问题标题】:Encoding Speex with libavcodec (FFMpeg)?用 libavcodec (FFMpeg) 编码 Speex?
【发布时间】:2012-03-31 17:57:51
【问题描述】:

我在启用 speex 的情况下成功编译了 libavcodec。 我修改了 FFMPEG 文档中的示例,将示例音频编码为 Speex。 但是结果文件不能用 VLC Player(有 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;
}

【问题讨论】:

    标签: encoding ffmpeg libavcodec speex libavformat


    【解决方案1】:

    Speex(我不知道)是否偶然需要一种容器格式来放置这些帧,并带有某种标题?您只是获取编码器的输出并转储到文件中,而不经过任何格式化 (libavformat)。

    尝试使用ffmpeg 命令行实用程序将相同的数据编码到 Speex 中,并查看生成的文件是否可以播放。

    我正在查看www.speex.org 的一些信息,似乎speex 数据已放入.ogg 文件中。您使用的播放器可能无法识别原始 Speex 数据,但前提是它包含在 .ogg 中。

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

    【讨论】:

    • 我在想,我用的是CODEC_ID_MP2,结果文件不需要容器。我会尝试使用OGG容器。
    猜你喜欢
    • 2014-05-21
    • 2021-03-01
    • 1970-01-01
    • 2019-04-07
    • 2011-04-22
    • 2017-05-26
    • 2020-12-11
    • 2019-04-19
    • 2012-01-26
    相关资源
    最近更新 更多