FFmpeg decoding audio saved as PCM

Source: Internet
Author: User
Tags int size printf

[TOC] Introduction

A simple Save the PCM demo, note to save there for the two-channel processing, because the PCM pair of two channels is alternately stored code

/* * A simple Save the PCM demo, note to save there for the two-channel processing, because the PCM pair of channels are alternately stored * Miu *MK (821486004@qq.com) */#ifdef __cplusplus extern "C" {#endif #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libavdevice/avdevice.h" #include " Libavfilter/avfilter.h "#include" libavfilter/avfiltergraph.h "#include" libavfilter/buffersink.h "#include"

Libavfilter/buffersrc.h "#include" libavutil/audio_fifo.h "#include" libavutil/avutil.h "#include" libavutil/fifo.h " #ifdef __cplusplus} #endif #pragma comment (lib, "Avcodec.lib") #pragma comment (lib, "Avformat.lib") #pragma comment (lib , "Avutil.lib") #pragma comment (lib, "Avdevice.lib") #pragma comment (lib, "Avfilter.lib")//#pragma comment (lib, "avfilt Er.lib ")//#pragma comment (lib," Postproc.lib ")//#pragma comment (lib," Swresample.lib ") #pragma comment (lib," Swscale.lib ") #include <windows.h> #include <conio.h> #include <time.h> #include <tchar.h> avfo  
Rmatcontext *ifmt_ctx = NULL;

int g_audiostreamindex =-1; #incLude <stdio.h> int openinputfile (const char* filename) {int ret = 0; Open the input if (ret = avformat_open_input (&ifmt_ctx, filename, null, NULL)) < 0) {Prin  
        TF ("Can not open input");  
    return ret;   
        } if (ret = Avformat_find_stream_info (Ifmt_ctx, NULL)) {printf ("Can not find input stream info");  
    return ret; }//open the decoder for (int i = 0; i < ifmt_ctx->nb_streams; i++) {if (ifmt_ctx->
            Streams[i]->codec->codec_type = = Avmedia_type_audio) {g_audiostreamindex = i; ret = Avcodec_open2 (Ifmt_ctx->streams[i]->codec, Avcodec_find_decoder (ifmt_ctx->streams[i]-&gt  

            ; codec->codec_id), NULL);  
                if (Ret < 0) {printf ("Can not open decoder");  
            return ret;  
}}} return 0;  }

int _tmain (int argc, _tchar* argv[]) {if (ARGC < 2) {return-1;  
    } avpacket pkt_in, pkt_out;  
    Avframe *frame = NULL;  
    unsigned int stream_index;  

    Av_register_all ();  
        if (Openinputfile (argv[1) < 0) {printf ("Failed to open input file");  
    Goto end;  
    } FILE *p = NULL;
    Char tmpname[100]; sprintf_s (Tmpname, "%S_%D_%DCHANNEL.PCM", argv[1], Ifmt_ctx->streams[g_audiostreamindex]->codec->sample_
    Rate, ifmt_ctx->streams[g_audiostreamindex]->codec->channels);

    p = fopen (Tmpname, "w+b");
    int size = Av_get_bytes_per_sample (IFMT_CTX-&GT;STREAMS[G_AUDIOSTREAMINDEX]-&GT;CODEC-&GT;SAMPLE_FMT);  
        while (1) {if (Av_read_frame (Ifmt_ctx, &pkt_in) < 0) {break;  
        } pkt_out.data = NULL;  
        pkt_out.size = 0;  

        Av_init_packet (&pkt_out); if (G_audiostreamindex = = pkt_in.Stream_index) {stream_index = Pkt_in.stream_index;  
            frame = Av_frame_alloc ();  
            int got_frame =-1;  

            int ret =-1;  

            ret = Avcodec_decode_audio4 (Ifmt_ctx->streams[stream_index]->codec, Frame, &got_frame, &pkt_in);  
                if (Ret < 0) {Av_frame_free (&frame);  
                printf ("Decoding audio stream failed\n");  
            Break
                } if (Got_frame) {if (frame->data[0] && frame->data[1])
                    {for (int i = 0; i < ifmt_ctx->streams[stream_index]->codec->frame_size; i++)
                        {fwrite (frame->data[0] + i * size, 1, size, p);
                    Fwrite (frame->data[1] + i * size, 1, size, p);
             }} else if (Frame->data[0])   {fwrite (frame->data[0], 1, frame->linesize[0], p);  
}}}} fclose (P);  

    End:avformat_close_input (&AMP;IFMT_CTX);
    printf ("Enter any key to stop\n");  
    GetChar ();  
return 0;   }
Download Address

http://download.csdn.net/detail/dancing_night/9676984

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.