ffmpeg提取音频存为PCM
来源:互联网 发布:淘宝联盟的客服电话 编辑:程序博客网 时间:2024/04/30 07:00
写的一个小程序,用来测试文件音频,并存储为PCM.
PCM可用专门软件来读取. 功能很简单,不值一提
/*************************************************************************> File Name: audio_test.c> Author: niwenxian> Mail: niwenxianq@qq.com > Created Time: 2013年10月13日 星期日 21时27分50秒 ************************************************************************/#include <stdlib.h>#include <stdio.h>#include <string.h>#include <math.h>#include <libavutil/opt.h>#include <libavutil/mathematics.h>#include <libavutil/imgutils.h>#include <libavutil/samplefmt.h>#include <libavutil/timestamp.h>#include <libavformat/avformat.h>#include <libavcodec/avcodec.h>#include <libswscale/swscale.h>#include <libavutil/mathematics.h>#include <libswresample/swresample.h>#include <libavutil/channel_layout.h>#include <libavutil/common.h>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<iFormatCtx->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;}
- ffmpeg提取音频存为PCM
- ffmpeg解码音频保存为PCM
- ffmpeg提取音频、转换为mp3
- ffmpeg提取YUV和PCM
- ffmpeg提取音频&拼接音频
- ffmpeg实战教程(三)音频PCM采样为AAC,视频YUV编码为H264/HEVC
- 最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
- 最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
- 最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
- ffmpeg学习十:将pcm格式的音频编码为aac格式
- ffmpeg添加MP4的pcm音频支持
- 【FFmpeg(2016)】SwrContext 转换PCM音频位数
- ffmpeg 提取视频文件中的音频
- 采用FFmpeg从视频中提取音频(声音)保存为mp3文件
- 采用FFmpeg从视频中提取音频(声音)保存为mp3文件
- ffmpeg pcm码流编码为aac
- FFMPEG学习----分离视音频里的PCM数据
- 【FFmpeg杂记】音频解码输出PCM格式数据分析
- THINKPHP日常用到的基础知识
- C#乱序数组的简单方法
- Android应用开发详解【郭宏志】(奋斗之小鸟)_PDF 电子书
- 老员工职场法宝——年轻心态
- linux下静态库和动态库的创建
- ffmpeg提取音频存为PCM
- sample format
- Android 4学习(7):用户界面 - 基础
- Top 5 Questions about C/C++ Pointers 【转】
- HTML中<!DOCTYPE>标签的作用
- 数据流图的画法
- ORA-1536: space quota exceeded for tablespace "<name>"
- java 发邮件
- Android APK破解