4

我在 xcode 项目中添加了 LibFlac。然后我在我的项目中添加了来自 Libflac 的 decode/main.c。我通过了 infile.flac 并运行了项目的可执行文件,但它给出了以下错误

解码:失败状态:FLAC__STREAM_DECODER_END_OF_STREAM 登录

这是main.c

int main(int argc, char *argv[])
{
    FLAC__bool ok = true;
    FLAC__StreamDecoder *decoder = 0;
    FLAC__StreamDecoderInitStatus init_status;
    FILE *fout;

    const char *infile = "infile.flac";
    const char *outfile = "outfile.wav";

    /*
    if(argc != 3) {
        fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]);
        return 1;
    }
    */

    if((fout = fopen("infile.flac", "wb")) == NULL) {
        fprintf(stderr, "ERROR: opening %s for output\n", argv[2]);
        return 1;
    }

    if((decoder = FLAC__stream_decoder_new()) == NULL) {
        fprintf(stderr, "ERROR: allocating decoder\n");
        fclose(fout);
        return 1;
    }

    (void)FLAC__stream_decoder_set_md5_checking(decoder, true);

    init_status = FLAC__stream_decoder_init_file(decoder, infile, write_callback, metadata_callback, error_callback, /*client_data=*/fout);
    if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
        fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
        ok = false;
    }

    if(ok) {
        ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
        fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
        fprintf(stderr, "   state: %s\n", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
    }

    FLAC__stream_decoder_delete(decoder);
    fclose(fout);

    return 0;
}

请帮我。为什么我收到此错误?

4

1 回答 1

3

用“wb”打开你的输入文件会在打开它时截断你的infile。这不可能是你想要的,对吧?我认为你的意思是;

if((fout = fopen(outfile, "wb")) == NULL) {

FLAC 样本的工作方式似乎有些混乱。

FLAC__stream_decoder_init_file

打开您为其指定文件名的文件以进行解码,并为解码设置回调。

FLAC__stream_decoder_process_until_end_of_stream

对文件进行解码,并且对于每个解码的帧,它都会调用在对 FLAC__stream_decoder_init_file 的调用中提供的 write_callback 函数,并将参数作为最后一个参数提供给它。

也就是说,所有写文件的工作都是在write_callback中完成的。这就是为您提供解码数据的地方,您应该逐帧生成和写入输出文件。如果您查看http://flac.cvs.sourceforge.net/viewvc/flac/flac/examples/c/decode/file/main.c?view=markup上的示例,这似乎是您复制到的内容开始,这正是它的作用。

于 2012-01-09T09:12:08.997 回答