解码.wav文件并使用ffmpeg将其写入另一个文件

时间:2017-02-16 08:21:34

标签: objective-c c audio ffmpeg

如何解码.wav文件并使用ffmpeg将其写入另一个文件?

我通过这段代码获得了解码数据:

-(void)audioDecode:(const char *)outfilename inFileName:(const char *)filename

{

const char* input_filename=filename;

    //avcodec_register_all();
    av_register_all();
    avcodec_register_all();
   // av_register_all();
    //av_ini

//    AVFormatContext* container=avformat_alloc_context();
//    if(avformat_open_input(&container,filename,NULL,NULL)<0){
//        NSLog(@"Could not open file");
//    }

    AVCodec *codec;
    AVCodecContext *c= NULL;
    int len;
    FILE *f, *outfile;
    uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    AVPacket avpkt;
    AVFrame *decoded_frame = NULL;

    av_init_packet(&avpkt);

    printf("Decode audio file %s to %s\n", filename, outfilename);
    avcodec_register_all();

    AVFrame* frame = av_frame_alloc();
    if (!frame)
    {
        fprintf(stderr, "Could not allocate audio frame\n");
                        exit(1);
    }

    AVFormatContext* formatContext = NULL;
    /* Opening the file, and check if it has opened */
    if (avformat_open_input(&formatContext, filename, NULL, NULL) != 0)
    {
        av_frame_free(&frame);
        NSLog(@"Could not open file");
    }

    if (avformat_find_stream_info(formatContext, NULL) < 0)
    {
        av_frame_free(&frame);
        avformat_close_input(&formatContext);

        NSLog(@"Error finding the stream info");
    }

        outfile = fopen(outfilename, "wb");
        if (!outfile) {
            av_free(c);
            exit(1);
        }

    /* Find the audio Stream, if no audio stream are found, clean and exit */
    AVCodec* cdc = NULL;
    int streamIndex = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &cdc, 0);
    if (streamIndex < 0)
    {
        av_frame_free(&frame);
        avformat_close_input(&formatContext);
        NSLog(@"Could not find any audio stream in the file");
        exit(1);
    }

    /* Open the audio stream to read data  in audioStream */
    AVStream* audioStream = formatContext->streams[streamIndex];

    /* Initialize the codec context */
    AVCodecContext* codecContext = audioStream->codec;
    codecContext->codec = cdc;
    /* Open the codec, and verify if it has opened */
    if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0)
    {
        av_frame_free(&frame);
        avformat_close_input(&formatContext);
        NSLog(@"Couldn't open the context with the decoder");
        exit(1);
    }

    /* Initialize buffer to store compressed packets */
    AVPacket readingPacket;
    av_init_packet(&readingPacket);
    int lenght = 1;
    long long int chunk = ((formatContext->bit_rate)*lenght/8);
    int j=0;
    int count = 0;
    //audioChunk output;


    while(av_read_frame(formatContext, &readingPacket)==0){
        //if((count+readingPacket.size)>start){
            if(readingPacket.stream_index == audioStream->index){

                AVPacket decodingPacket = readingPacket;

                // Audio packets can have multiple audio frames in a single packet
                while (decodingPacket.size > 0){
                    // Try to decode the packet into a frame
                    // Some frames rely on multiple packets, so we have to make sure the frame is finished before
                    // we can use it
                    int gotFrame = 0;
                    int result = avcodec_decode_audio4(codecContext, frame, &gotFrame, &decodingPacket);

                    count += result;

                    if (result >= 0 && gotFrame)
                    {
                        decodingPacket.size -= result;
                        decodingPacket.data += result;
                        int a;

                        for(int i=0;i<result-1;i++){
                            fwrite(frame->data[0], 1, decodingPacket.size, outfile);
//                            *(output.data+j)=frame->data[0][i];
//
                            j++;
                            if(j>=chunk) break;
                        }

                        // We now have a fully decoded audio frame
                    }
                    else
                    {
                        decodingPacket.size = 0;
                        decodingPacket.data = NULL;
                    }
                   // if(j>=chunk) break;
                }
            }
//        }else count+=readingPacket.size;
//        
//        // To prevent memory leak, must free packet.
//        av_free_packet(&readingPacket);
//        if(j>=chunk) break;
    }
    fclose(outfile);

但是文件是用零字节创建的。我不知道这段代码有什么问题。我又得到了一个错误:&#34;格式化的adp仅在低分25分的情况下被检测到,错误检测可能!&#34;

0 个答案:

没有答案