ffmpeg 实现音频aac编码
1、编译ffmepg
./configure --disable-yasm --enable-nonfree --enable-libfaac --prefix=/home/ffmpeg/1_ffmpeg-2.1.1/install
2、编译audio_enc.c
makefile:
1
2
3
4
5
6
7
8
|
#!/bin/sh
INCLUDE = .. /include
LIB_DIR = .. /lib
LDFLAGS = -lfaac -lavcodec -lavformat -lavdevice -lavfilter -lavutil -lswresample -pthread -ldl -lswscale -lasound -lz -lm -lbz2
SRC=audio_enc.c
all:$(SRC)
gcc -g -Wall $(SRC) -o target -I $(INCLUDE) -L $(LIB_DIR) $(LDFLAGS)
|
注意:在这里编译的时候需要加上aac库,可能会找不到库函数undefined reference to `faacEncEncode'等
程序运行时,需要提供一个和程序参数一致的wav音频文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*
* Copyright(C), 2013, Ubuntu Inc.
* File name: audio_enc.c
* Author: xubinbin 徐彬彬 (Beijing China)
* Version: 1.0
* Date: 2013.12.23
* Description: Use ffmpeg achieve aac audio coding.
* Function List:
* Email: xubbwd@gmail.com
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavformat/avformat.h>
FILE * fp_in = NULL;
FILE * fp_out = NULL;
static int frame_count;
int main( int argc, char **argv)
{
int ret;
AVCodec *audio_codec;
AVCodecContext *c;
AVFrame *frame;
AVPacket pkt;
int got_output;
/* Initialize libavcodec, and register all codecs and formats. */
av_register_all();
avcodec_register_all();
//avdevice_register_all();
audio_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
c = avcodec_alloc_context3(audio_codec);
c->codec_id = AV_CODEC_ID_AAC;
c->sample_fmt = AV_SAMPLE_FMT_S16;
c->sample_rate = 44100;
c->channels = 2;
c->channel_layout = AV_CH_LAYOUT_STEREO;
c->bit_rate = 64000;
/* open the codec */
ret = avcodec_open2(c, audio_codec, NULL);
if (ret < 0) {
fprintf (stderr, "Could not open video codec: %s\n" , av_err2str(ret));
exit (1);
}
/* allocate and init a re-usable frame */
frame = avcodec_alloc_frame();
if (!frame) {
fprintf (stderr, "Could not allocate video frame\n" );
exit (1);
}
frame->nb_samples = c->frame_size;
frame->format = c->sample_fmt;
frame->channels = c->channels;
frame->channel_layout = c->channel_layout;
frame->linesize[0] = 4096;
frame->extended_data = frame->data[0] = av_malloc(( size_t )frame->linesize[0]);
av_init_packet(&pkt);
fp_in = fopen ( "in.wav" , "rb" );
fp_out= fopen ( "out.aac" , "wb" );
//printf("frame->nb_samples = %d\n",frame->nb_samples);
while (1)
{
frame_count++;
bzero(frame->data[0],frame->linesize[0]);
ret = fread (frame->data[0],frame->linesize[0],1,fp_in);
if (ret <= 0)
{
printf ( "read over !\n" );
break ;
}
ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
if (ret < 0) {
fprintf (stderr, "Error encoding audio frame: %s\n" , av_err2str(ret));
exit (1);
}
if (got_output > 0)
{
//printf("pkt.size = %d\n",pkt.size);
fwrite (pkt.data,pkt.size,1,fp_out);
av_free_packet(&pkt);
}
#if 0
if (frame_count > 10)
{
printf ( "break @@@@@@@@@@@@\n" );
break ;
}
#endif
}
avcodec_close(c);
av_free(c);
avcodec_free_frame(&frame);
fclose (fp_in);
fclose (fp_out);
return 0;
}
|
转载自:http://hi.baidu.com/285988185/item/6864b8b0c9640445ba0e12a6
ffmpeg 实现音频aac编码相关推荐
- iOS - 音频AAC编码
开发中通常会遇到的音频流的处理,我们知道从苹果设备麦克风上面获取的声音是驳杂的,我们利用原生框架<AVFoundation/AVFoundation.h>可以把音频源经过PCM编码后得到音 ...
- FFmpeg源码编译出支持音频AAC编码以及H264,H265编码的库
先决条件: 需要安装msys2(是一个在windows中模拟linux操作系统的软件) 由于在linux中编译ffmpeg比较简单, 所以利用一下msys2, 直接去官网下载安装即可 需要安装visu ...
- 音频AAC编码的RTMP直播
RTMP直播,音频编码采用AAC时,需要把帧头的数据去掉. 第一个数据包,发送4个字节,前面两个是0xAF.0x00,我看有文章写的是这个0xAF的A代表的是AAC,说明如下: 0 = Linear ...
- 树莓派上使用QT+ffmpeg进行音频编码+部署自启动+双击不启动问题
目录 1. 安装完QT后,我们自己的声卡驱动有可能无法采集声音,这里有个妙招: 2.安装fdk-aac: 3.安装ffmpeg: 4.编程注意事项 4.1 QT没有多媒体库,QAudioDeviceI ...
- ios - 音频AAC解码
上一回说到音频AAC编码,这回我们把编码后的文件解码,解码的文件就是上一章录制的音频. 在iOS下进行音频解码及播放的大体流程如下: 1.打开 AAC 文件. 2.获取音频格式信息.如通道数,采样率等 ...
- 基于FFmpeg的音频编码(PCM数据编码成AAC android)
概述 在Android上实现录音,并利用 FFmpeg将PCM数据编码成AAC. 详细 代码下载:http://www.demodashi.com/demo/10512.html 之前做的一个demo ...
- 最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
本文介绍一个最简单的基于FFMPEG的音频编码器.该编码器实现了PCM音频采样数据编码为AAC的压缩编码数据.编码器代码十分简单,但是每一行代码都很重要.通过看本编码器的源代码,可以了解FFMPEG音 ...
- 【Android RTMP】音频数据采集编码 ( 音频数据采集编码 | AAC 高级音频编码 | FAAC 编码器 | Ubuntu 交叉编译 FAAC 编码器 )
文章目录 安卓直播推流专栏博客总结 一. 音频数据采集.编码 二. AAC 高级音频编码 三. FAAC 编码器 四. Ubuntu 18.04.4 交叉编译 FAAC 编码器 安卓直播推流专栏博客总 ...
- 基于 FFMPEG 的音频编解码(三):音频编码
音频编码 基于 FFMPEG 的音频编解码(一):Hello FFMPEG,安装与编译 基于 FFMPEG 的音频编解码(二):音频解码 基于 FFMPEG 的音频编解码(三):音频编码 在前面文章中 ...
最新文章
- 连接SQLite 创建ADO.net实体类
- C# DataGridView 的UserDeletingRow事件,删除
- 中国官方唯一无人车路测报告一出炉,百度股价又涨了
- jlink烧写Nor Flash时出错正确解决方法汇总:PC of target system has unexpected value after programming (2011
- Geoserver怎样设置地图shp文件为相对路径,可轻松复制移植
- Vue项目端口号占用
- 逐浪CMS4.6精彩预告之三:全新会员中心炫目登场
- 一起谈.NET技术,NHibernate3.0剖析:Query篇之NHibernate.Linq标准查询
- 如何用C#在Excel中生成图表?
- Singleton(单件)--对象创建模式
- PMP读书笔记(第1章)
- 你的GitHub,怎么和我用的不太一样?
- 鸟哥:程序员应该不断提升自身的不可替代性
- Linux tcpdump
- (WCF)阅读WCF分布式开发步步为赢(2)自定义托管宿主WCF解决方案开发配置过程详解的一点心得...
- Web模式使用RabbitMQ
- Tom猫喝牛奶(计时器、帧动画)
- raid5换硬盘显示ready_[原创]戴尔服务器raid5更换硬盘状态foreign怎么改成ready
- WinMerge:一个免费开源的文件对比神器
- 负载均衡负载场景和解决方案
热门文章
- 跟我学Kafka:Kafka消费组运维详解
- MyBatis----回顾mybatis自定义和环境搭建+完善自定义Mybatis的注解开发
- 软工网络15团队作业7——Alpha冲刺之事后诸葛亮
- 中国工程科学院计算机院士,重磅!中国科学院和中国工程院2019新院士名单公布!50所高校新晋院士77人...
- Panda3D 初学者教程(一)
- 自动备份android,Android自动备份错误
- 康威生命游戏简易版python_turtle实现
- Python语言程序设计 - 北京理工大学 网课所有资料(源码,pdf,ppt课件,视频等)
- ClickHouse中文官方文档
- 即席和即兴_即席和即兴_新员工参加会议,即兴发言赢得好感