ffmpeg中G726解码器用法 编码器用法
来源:互联网 发布:甲骨文 ibm做啥软件 编辑:程序博客网 时间:2024/05/29 11:44
文章来源:http://blog.csdn.net/chinabinlang/article/details/27513981?locationNum=15&fps=1
//解码:
本代码实现将cap1.g726文件中的g726编码帧数据进行解码,然后保存到cap1文件中
1、ffmpeg版本 :
0.10.2
2、源码:
extern "C"{ #include "./h/libavcodec/avcodec.h"};
#pragma comment(lib, "avutil.lib") //ffmpeg#pragma comment(lib, "avformat.lib")#pragma comment(lib, "avcodec.lib")
AVCodec *codec;AVCodecContext *c= NULL;AVPacket avpkt;AVFrame *decoded_frame = NULL;
//编码后一帧大小为 84字节#define framesize 84
int main(int argc, char* argv[]){ printf("Hello World!\n"); unsigned char inbuffer[framesize] = {0}; /* register all the codecs */ avcodec_register_all(); av_init_packet(&avpkt); /* find the mpeg audio decoder */ codec = avcodec_find_decoder(CODEC_ID_ADPCM_G726); if (!codec) { fprintf(stderr, "codec not found\n"); return 0; } c = avcodec_alloc_context3(codec);
//g726压缩比为 8:1 ?
//没有如下两句 avcodec_open会返回 -22 错误
//采样率 = 8000 每个采样用的bit数 = 16 通道数 = 1 c->bits_per_coded_sample = 2; //16位采样,即2Bytes
c->channels = 1; /* open it */ int iRet = avcodec_open(c, codec); if ( iRet < 0 ) { fprintf(stderr, "could not open codec\n"); return 0; } FILE *f, *outfile;
//打开存放g726音频帧的文件
f = fopen( "cap1.g726", "rb" ); if (!f) { return 0; }
//打开要存放解码后音频帧的文件 outfile = fopen("cap1", "wb"); if (!outfile)
{ av_free(c); return 0; } avpkt.data = inbuffer; while ( (avpkt.size = fread(inbuffer, 1, framesize, f) ) > 0 ) { int got_frame = 0; if (!decoded_frame) { if (!(decoded_frame = avcodec_alloc_frame())) { return 0; } } else { avcodec_get_frame_defaults(decoded_frame); } int len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt); if (len < 0) { return 0; } if (got_frame) { /* if a frame has been decoded, output it */ int data_size = av_samples_get_buffer_size(NULL, c->channels, decoded_frame->nb_samples, c->sample_fmt, 1); fwrite(decoded_frame->data[0], 1, data_size, outfile); } } fclose(outfile); fclose(f); avcodec_close(c); av_free(c); av_free(decoded_frame); return 0;}
//编码
ffmpeg g726 编码
2012-10-11 10:19:21| 分类: ffmpeg|举报|字号 订阅
一、将文件source中原始音频数据进行编码,并保存到dest中采样率 = 8000 通道数 = 1 采样位数 = 16 波特率 = 16000二、源码 extern "C"{ #include "./h/libavcodec/avcodec.h"};//视频相关库#pragma comment(lib, "avutil.lib") //ffmpeg#pragma comment(lib, "avformat.lib")#pragma comment(lib, "avcodec.lib")AVCodec *codec;AVCodecContext *c= NULL;AVPacket avpkt;AVFrame *decoded_frame = NULL;
#define framesize 80#define buffersize 640
int main(int argc, char* argv[]){
/* register all the codecs */ avcodec_register_all(); av_init_packet(&avpkt);
//如下两句在编码中是必须的,要不avcodec_encode_audio2会失败
avpkt.data = NULL;
avpkt.size = 0;
/* find the mpeg audio decoder */ codec = avcodec_find_encoder(CODEC_ID_ADPCM_G726); if (!codec) { fprintf(stderr, "codec not found\n"); return 0; } c = avcodec_alloc_context3(codec);
//put sample parameters c->bits_per_coded_sample = 2; c->bit_rate = 16000; // c->sample_rate = 8000; //采样率 c->channels = 1; //通道数 c->sample_fmt = AV_SAMPLE_FMT_S16; //应该是采样位数
/* open it */ int iRet = avcodec_open(c, codec); if ( iRet < 0 ) { fprintf(stderr, "could not open codec\n"); return 0; }
int buf_size = buffersize; char szBuffer[buffersize] = {0}; int ret = 0; int got_packet = 0; FILE *f; FILE *output;
//打开存放编码前音频文件
f = fopen("source", "rb");
//打开存放编码后音频文件
output = fopen( "dest", "wb" );
while( fread( szBuffer, 1, buffersize, f ) ) { if (!decoded_frame) { if (!(decoded_frame = avcodec_alloc_frame())) { return 0; } } else { avcodec_get_frame_defaults(decoded_frame); } decoded_frame->nb_samples = buf_size/(c->channels*av_get_bytes_per_sample(c->sample_fmt)); ret = avcodec_fill_audio_frame(decoded_frame, c->channels, c->sample_fmt, (unsigned char *)szBuffer,
buf_size, 1); if (ret < 0) { //av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n"); return 0; } if (avcodec_encode_audio2(c, &avpkt, decoded_frame, &got_packet) < 0) {
return 0; } if (got_packet) { int iiiii = fwrite( avpkt.data, 1, avpkt.size, output ); } } fclose(f); fclose(output);
}
阅读全文0 0
- ffmpeg中G726解码器用法 编码器用法
- ffmpeg中G726解码器用法 编码器用法
- ffmpeg中调用解码器过程
- ffmpeg中sws_scale()用法实例
- ffmpeg中sws_scale()用法实例
- ffmpeg 中 swscale 的用法
- ffmpeg中swscale 的用法
- ffmpeg 中 swscale 的用法
- ffmpeg 中 swscale 的用法
- ffmpeg中swscale 的用法
- ffmpeg中swscale 的用法
- ffmpeg用法
- JPEG编码器和解码器
- 从ffmpeg中提取wma pro解码器
- c语言define中##用法---ffmpeg
- ffmpeg中sws_scale()用法实例---part of FFmpeg
- ffmpeg中如何加入trueHD/mlp编码器
- 创建MP3解码器 - 采用Symbian中MAD(MPEG音频解码库)用法
- CodeForces 832B Petya and Exam
- 意义的感知和语法的形式,UML类图
- 模拟用户登录功能
- 雨后闲侃儿
- EffectiveC++学习笔记-条款36|37
- ffmpeg中G726解码器用法 编码器用法
- Hongcow Solves A Puzzle CodeForces
- maven3 archetype 汇总
- 02-SpringBoot——Spring基础-Java配置
- 【正一专栏】内马尔留不留下都已经是伤痕累累
- 十四、断路器-Hystrix 对 Feign 的支持
- Bellman-Ford Algorithm
- Oil Deposits ( 简单搜索,连通性)
- 文件上传显示缩略图
原创粉丝点击热门IT博客热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 飞会 飞友航班动态实时查询 庄域飞 飞友网图片 叶飞叶神医 南飞叶 飞叶 杨飞叶映雪 夏飞叶梦如 飞叶商贸 八月飞叶作品 南飞叶泡茶喝有什么功效 海贼之金色暗影 川飞叶 呗飞岛 飞哥英雄 飞哥与小佛 飞哥 飞哥换电 大飞哥 飞哥大英雄2之飞哥战队 飞哥换电app 大英雄飞哥 飞哥大英雄2 飞哥和小福 飞哥大英雄演员表 飞哥战队演员表 飞哥大英雄全集 飞哥与小佛的时空大冒险 飞哥战队50集免费观看 飞哥大英雄剧情介绍 飞哥大英雄免费观看 飞哥大英雄1一40集剧情 飞哥与小佛第一季 电视连续剧飞哥大英雄 飞哥大英雄 全40集 飞哥战队50集 怪物猎人世界飞雷龙 全飞半飞哪个对眼睛伤害小 买飞机票哪个网站便宜 雪中飞和波司登哪个好 gw250和飞致250哪耐用