zoukankan      html  css  js  c++  java
  • FFMpeg音频混合,背景音(八):将MP3文件解码成PCM

    FFMpeg版本:4.3.1
    #include<iostream> #include<windows.h> using namespace std; //用到的C的头文件 extern "C" { #include<libavcodec/avcodec.h> #include<libavfilter/avfilter.h> #include<libavfilter/buffersink.h> #include<libavfilter/buffersrc.h> #include<libavfilter/avfiltergraph.h> #include<libavformat/avformat.h> #include<libavutil/avutil.h> #include<libavutil/fifo.h> #include<libavutil/audio_fifo.h> #include<libavdevice/avdevice.h> #include <libavutil/frame.h> #include <libavutil/mem.h> #include <stdio.h> #include <stdlib.h> #include <string.h> } //对用到的预编译 #pragma comment(lib, "avcodec.lib") #pragma comment(lib, "avfilter.lib") #pragma comment(lib, "avutil.lib") #pragma comment(lib, "avformat.lib") #pragma comment(lib, "swscale.lib") //第一个音频文件 #define AUDIO_INBUF_SIZE 20480 #define AUDIO_REFILL_THRESH 4096 static void decode(AVCodecContext* dec_ctx, AVPacket* pkt, AVFrame* frame, FILE* outfile) { int i, ch; int ret, data_size; /* send the packet with the compressed data to the decoder */ ret = avcodec_send_packet(dec_ctx, pkt); if (ret < 0) { fprintf(stderr, "Error submitting the packet to the decoder "); exit(1); } /* read all the output frames (in general there may be any number of them */ while (ret >= 0) { ret = avcodec_receive_frame(dec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) return; else if (ret < 0) { fprintf(stderr, "Error during decoding "); exit(1); } data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt); if (data_size < 0) { /* This should not occur, checking just for paranoia */ fprintf(stderr, "Failed to calculate data size "); exit(1); } for (i = 0; i < frame->nb_samples; i++) for (ch = 0; ch < dec_ctx->channels; ch++) fwrite(frame->data[ch] + data_size * i, 1, data_size, outfile); } } int main() { const char* outfilename, * filename; const AVCodec* codec; AVCodecContext* c = NULL; AVCodecParserContext* parser = NULL; int len, ret; FILE* f, * outfile; uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; uint8_t* data; size_t data_size; AVPacket* pkt; AVFrame* decoded_frame = NULL; filename = "D:\FFMpeg\project\pcm_aac\pcm_aac\outdio.mp3"; outfilename = "D:\FFMpeg\project\pcm_aac\pcm_aac\outdio.pcm"; /* register all the codecs */ avcodec_register_all(); pkt = av_packet_alloc(); /* find the MPEG audio decoder */ codec = avcodec_find_decoder(AV_CODEC_ID_MP3); if (!codec) { fprintf(stderr, "Codec not found "); exit(1); } parser = av_parser_init(codec->id); if (!parser) { fprintf(stderr, "Parser not found "); exit(1); } c = avcodec_alloc_context3(codec); if (!c) { fprintf(stderr, "Could not allocate audio codec context "); exit(1); } /* open it */ if (avcodec_open2(c, codec, NULL) < 0) { fprintf(stderr, "Could not open codec "); exit(1); } f = fopen(filename, "rb"); if (!f) { fprintf(stderr, "Could not open %s ", filename); exit(1); } outfile = fopen(outfilename, "wb"); if (!outfile) { av_free(c); exit(1); } /* decode until eof */ data = inbuf; data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); while (data_size > 0) { if (!decoded_frame) { if (!(decoded_frame = av_frame_alloc())) { fprintf(stderr, "Could not allocate audio frame "); exit(1); } } ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size, data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0); if (ret < 0) { fprintf(stderr, "Error while parsing "); exit(1); } data += ret; data_size -= ret; cout << c->sample_rate << endl; cout << c->channels << endl; if (pkt->size) decode(c, pkt, decoded_frame, outfile); if (data_size < AUDIO_REFILL_THRESH) { memmove(inbuf, data, data_size); data = inbuf; len = fread(data + data_size, 1, AUDIO_INBUF_SIZE - data_size, f); if (len > 0) data_size += len; } } /* flush the decoder */ pkt->data = NULL; pkt->size = 0; decode(c, pkt, decoded_frame, outfile); fclose(outfile); fclose(f); avcodec_free_context(&c); av_parser_close(parser); av_frame_free(&decoded_frame); av_packet_free(&pkt); return 0; }
  • 相关阅读:
    图像控件 ImageControl
    日期条控件 DateFieldControl
    日期选择器和日期条控件 DateChooserAndDateFieldControls
    计数器控件实例 NumericStepper
    树结构控件实例 TreeControl
    vue2.0leaflet
    关于工具类中@Autowired注入为NULL的问题记录
    zabbix_agentd重装后启动时IPC和共享内存段问题
    rsync如何不指定密码文件
    MySQL5.7 JSON类型及其相关函数的学习
  • 原文地址:https://www.cnblogs.com/zhangxianrong/p/13890020.html
Copyright © 2011-2022 走看看