[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]->
; 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->STREAMS[G_AUDIOSTREAMINDEX]->CODEC->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 (&IFMT_CTX);
printf ("Enter any key to stop\n");
GetChar ();
return 0; }
Download Address
http://download.csdn.net/detail/dancing_night/9676984