博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ffmpeg提取音频存为PCM
阅读量:2194 次
发布时间:2019-05-02

本文共 3285 字,大约阅读时间需要 10 分钟。

       写的一个小程序,用来测试文件音频,并存储为PCM.

       PCM可用专门软件来读取. 功能很简单,不值一提

      

/*************************************************************************	> File Name: audio_test.c	> Author: niwenxian	> Mail: niwenxianq@qq.com 	> Created Time: 2013年10月13日 星期日 21时27分50秒 ************************************************************************/#include    
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char * argv[]){const char *input;const char *outfilename = "test.pcm";const char *filename = "test.mp3";input = argv[1];av_register_all();avcodec_register_all();AVFormatContext *iFormatCtx = NULL;AVCodecContext *iaCodecCtx = NULL;AVCodec *iaCodec = NULL;AVFrame *frame = NULL;AVPacket ipacket;av_init_packet(&ipacket);AVDictionary *optionsDict = NULL;struct SwsContext *sws_ctx = NULL;int AudioStreamIndex;int frameFinished;int numBytes;int len;int i;FILE *f, *outfile;uint8_t *outbuf;uint8_t *outbuf3;uint8_t *outbuf2;uint8_t inbuf[1024+ FF_INPUT_BUFFER_PADDING_SIZE];iFormatCtx = avformat_alloc_context();if(avformat_open_input(&iFormatCtx, input, NULL, NULL)!=0){ printf("could not open input_file\n"); return -1;}printf("The url: %s\n", input);if(avformat_find_stream_info(iFormatCtx,NULL) < 0)//获取文件内音视频流的信息 { printf("find stream info failed\n"); return -1 ; } av_dump_format(iFormatCtx, 0, input, 0);AudioStreamIndex = -1;for(i=0; i
nb_streams; i++){ if(iFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO && AudioStreamIndex <0 ) { AudioStreamIndex = i; break; }}if(AudioStreamIndex== -1){ printf("Cannot find VideoStream\n"); return -1;}iaCodecCtx = iFormatCtx->streams[AudioStreamIndex]->codec;iaCodec = avcodec_find_decoder(iaCodecCtx->codec_id);if(iaCodec == NULL){ printf("unsupported codec!\n"); return -1;}if(iaCodec->capabilities & CODEC_CAP_TRUNCATED) iaCodecCtx->flags |= CODEC_CAP_TRUNCATED;if(avcodec_open2(iaCodecCtx, iaCodec, &optionsDict)<0){ printf("could not open the codec\n"); return -1;}outbuf = (uint8_t *)malloc(1024);f = fopen(filename, "rb"); if (f) { printf("could not open %s\n", filename); return -1; }outfile = fopen(outfilename, "wb");if (!outfile) { printf("open outfile failed\n"); av_free(iaCodecCtx); return -1 ; }frame = avcodec_alloc_frame();if (!frame) { fprintf(stderr, "Could not allocate audio frame\n"); exit(1); } //frame->nb_samples = iaCodecCtx->frame_size; //frame->format = iaCodecCtx->sample_fmt; //frame->channel_layout = iaCodecCtx->channel_layout;int pktsize = 0;uint8_t *pktdata;while(av_read_frame(iFormatCtx, &ipacket)>=0) { if(ipacket.stream_index == AudioStreamIndex) { while(ipacket.size>0) { int out_size; len = avcodec_decode_audio4(iaCodecCtx, frame, &out_size, &ipacket); if (len < 0) { pktsize = 0; printf("Error while decoding\n"); continue; } if (out_size ) { int data_size1 = av_samples_get_buffer_size(NULL, iaCodecCtx->channels, frame->nb_samples, iaCodecCtx->sample_fmt, 1); //fwrite(frame->data[0], 1, data_size1, outfile); fwrite(frame->data[0], 1, frame->linesize[0], outfile); fflush(outfile); } ipacket.size -=len; ipacket.data +=len; } }}return 0;}

转载于:https://my.oschina.net/vintnee/blog/640477

你可能感兴趣的文章
搞懂分布式技术20:消息队列因何而生
查看>>
搞懂分布式技术21:浅谈分布式消息技术 Kafka
查看>>
后端技术杂谈1:搜索引擎基础倒排索引
查看>>
后端技术杂谈2:搜索引擎工作原理
查看>>
后端技术杂谈3:Lucene基础原理与实践
查看>>
后端技术杂谈4:Elasticsearch与solr入门实践
查看>>
后端技术杂谈5:云计算的前世今生
查看>>
后端技术杂谈6:白话虚拟化技术
查看>>
后端技术杂谈7:OpenStack的基石KVM
查看>>
后端技术杂谈8:OpenStack架构设计
查看>>
后端技术杂谈9:先搞懂Docker核心概念吧
查看>>
后端技术杂谈10:Docker 核心技术与实现原理
查看>>
夯实Java基础系列2:Java自动拆装箱里隐藏的秘密
查看>>
夯实Java基础系列1:Java面向对象三大特性(基础篇)
查看>>
夯实Java基础系列3:一文搞懂String常见面试题,从基础到实战,更有原理分析和源码解析!
查看>>
夯实Java基础系列4:一文了解final关键字的特性、使用方法,以及实现原理
查看>>
Java 未来行情到底如何,来看看各界人士是怎么说的
查看>>
IntelliJ 平台 2020 年路线图
查看>>
走进JavaWeb技术世界8:浅析Tomcat9请求处理流程与启动部署过程
查看>>
微软宣布加入 OpenJDK,打不过就改变 Java 未来!
查看>>