ffmpeg解码音频保存为PCM

[TOC]

简介

一个简单保存PCM的demo,注意保存那里对双声道的处理,因为pcm对双声道是交替存储的

代码

/*
*一个简单保存PCM的demo,注意保存那里对双声道的处理,因为pcm对双声道是交替存储的
*缪国凯
*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, "avfilter.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>

AVFormatContext *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)  
    {  
        printf("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;  
}  

下载地址

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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dancing_night

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值