使用ffmpeg编码aac,使用libfdk-aac。

#import "FFMpegAACEncoder.h"
#ifdef __cplusplus
extern "C" {
#endif#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>#ifdef __cplusplus
};
#endif#define ENCODE_SIZE 1024@interface FFMpegAACEncoder(){AVCodecContext                      *_pCodecCtx;AVCodec                             *_pCodec;AVPacket                             _pkt;AVFrame                             *_pFrame;uint8_t                             *_frameBuf;long                                _length;long                                _oneSecBytes;
}@end@implementation FFMpegAACEncoder- (int)initEncoder:(int)bitrate samplerate:(int)samplerate channel:(int)channel{_oneSecBytes = samplerate*channel*2;avcodec_register_all();enum AVCodecID codec_id = AV_CODEC_ID_AAC;//_pCodec = avcodec_find_encoder(codec_id);NSString* codecName = @"libfdk_aac";_pCodec = avcodec_find_encoder_by_name([codecName UTF8String]);if (!_pCodec) {printf("Codec not found\n");return -1;}_pCodecCtx = avcodec_alloc_context3(_pCodec);if (!_pCodecCtx) {printf("Could not allocate video codec context\n");return -1;}_pCodecCtx->codec_id = codec_id;_pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;_pCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16;_pCodecCtx->sample_rate= samplerate;_pCodecCtx->channel_layout = (channel == 2 ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO);_pCodecCtx->channels = av_get_channel_layout_nb_channels(_pCodecCtx->channel_layout);_pCodecCtx->bit_rate = bitrate;_pCodecCtx->frame_size = ENCODE_SIZE;int ret = 0;if ((ret = avcodec_open2(_pCodecCtx, _pCodec, NULL) < 0)) {fprintf(stderr, "Could not open codec\n");return -1;}_pFrame = av_frame_alloc();_pFrame->format = _pCodecCtx->sample_fmt;_pFrame->nb_samples = ENCODE_SIZE;int size = av_samples_get_buffer_size(NULL, _pCodecCtx->channels,ENCODE_SIZE,_pCodecCtx->sample_fmt, 1);_frameBuf = (uint8_t*)av_malloc(size);avcodec_fill_audio_frame(_pFrame, _pCodecCtx->channels, _pCodecCtx->sample_fmt, _frameBuf, size, 1);return 0;
}- (int)encodeToAAC:(char*)pcm len:(int)len{memcpy(_frameBuf, pcm, len);_pFrame->data[0] = _frameBuf;//_pFrame->linesize[0] = len;_pFrame->pts = _length/_oneSecBytes;int got_frame = 0;//Encodeint ret = avcodec_encode_audio2(_pCodecCtx, &_pkt,_pFrame, &got_frame);if(ret < 0){printf("Failed to encode!\n");return -1;}if (got_frame==1){printf("Succeed to encode 1 frame! \tsize:%5d\n",_pkt.size);if (_delegate && [_delegate respondsToSelector:@selector(onAudioEncBuf:len:)]) {[_delegate onAudioEncBuf:_pkt.data len:_pkt.size];}av_free_packet(&_pkt);}return 0;
}- (void)free{int ret = [self flushEncoder];if (ret < 0) {printf("Flushing encoder failed\n");}avcodec_close(_pCodecCtx);av_free(_pCodecCtx);av_freep(&_pFrame->data[0]);av_frame_free(&_pFrame);
}-(int)flushEncoder{int ret;int got_frame;AVPacket enc_pkt;while (1) {enc_pkt.data = NULL;enc_pkt.size = 0;av_init_packet(&enc_pkt);ret = avcodec_encode_audio2 (_pCodecCtx, &enc_pkt,NULL, &got_frame);av_frame_free(NULL);if (ret < 0)break;if (!got_frame){ret=0;break;}printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);//TODO:编码数据回调}return ret;
} @end

下面这样不行,不知道为嘛,有知道怎么弄的求告知。

enum AVCodecID codec_id = AV_CODEC_ID_AAC;

_pCodec = avcodec_find_encoder(codec_id);

ios ffmpeg(libfdk-aac) aac encode相关推荐

  1. ios端无法播放mp3音频 ffmpeg mp3 转aac

    最近用到ffmpeg,需要把mp3转成aac,调用以下命令参数: 如果不加 -strict experimental ios端可能播放MP3音频失败 ffmpeg -i xxx.mp3 -acodec ...

  2. FFMPEG实现对AAC解码(采用封装格式实现)

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. 原文:https://blog.csdn.net/hiwubihe/article/details/81261022 [音频编解码系列文章] 音 ...

  3. 【ffmpeg】创建AAC编码器

    [ffmpeg]创建AAC编码器 前言 ffmpeg编码过程 创建AAC编码器 创建编码器 创建上下文 设置采样参数 打开编码器 将帧数据提供给编码器 读取编码后的数据 将编码后的数据写入aac文件中 ...

  4. iOS FFmpeg 优秀博客(资源)集锦

    iOS FFmpeg 优秀博客(资源)集锦 这篇博客没有我自己写的内容: 主要是对FFmpeg一些优秀博客的记录 随时更新 1>iOS编译FFmpeg,kxmovie实现视频播放 2>视音 ...

  5. ffmpeg之封装AAC

    AAC是mp4的音频格式,而安防摄像机基本上使用G711等编码,想要封装成mp4供web预览,就需要跨越g711转AAC的这个难关. ffmpeg作为音视频界的泰斗,可以帮助我们实现这一功能. 代码流 ...

  6. ffmpeg pcm编码aac

    本博文参考自雷神博客 https://blog.csdn.net/leixiaohua1020/article/details/25430449 这是ffmpeg加上libfdk-aac 将pcm转a ...

  7. ffmpeg 实现音频aac编码

    1.编译ffmepg ./configure --disable-yasm --enable-nonfree --enable-libfaac --prefix=/home/ffmpeg/1_ffmp ...

  8. 音频压缩编码技术(五)—ffmpeg命令方式生成AAC文件

    命令解析 这个是将xxx.mp4文件当中的音频数据抽取出来存储 ffmpeg -i xxx.mp4 (输入的带有音频的多媒体文件)-vn (video no 过滤视频元素)-c:a libfdk_aa ...

  9. ffmpeg h264和aac封装为mp4文件

    1.I帧/P帧/B帧 I帧:帧内编码图像帧,也叫关键帧,包含一幅完整的图像信息,不含运动矢量,在解码时不需要参考其它帧图像.在闭合式GOP(画面组)中,每个GOP的开始是IDR帧,且当前GOP的数据不 ...

最新文章

  1. Oracle创建触发器的普通应用
  2. java会编译失败吗?_请问为什么这个通用java代码不会编译?
  3. 为jupyter_notebook增加目录
  4. [Windows Phone] 实作不同的地图显示模式
  5. 无法访问你试图使用的功能所在的网络位置_[steam实用工具]解决无法访问商店/社区/好友列表的问题...
  6. 快速学习C语言的途径,让你少走弯路!
  7. linux 那些事儿之我是 u 盘,《Linux那些事儿之我是USB》.PDF
  8. (软件工程复习核心重点)第九章软件项目管理
  9. 删除顺序表中指定值的所有元素(C++,vector)
  10. python函数代码,这个python函数代码如何工作?
  11. 用javascript实现以下功能!_用python80行代码实现一个微信消息撤回捕捉功能
  12. TypeScript笔记(5)—— 基本数据类型
  13. hdu 4723 How Long Do You Have to Draw(贪心)
  14. LaTeX标题、目录及段落的编辑(清晰实例)
  15. 软件项目管理期末复习(看这一篇就够了)
  16. iphone和mac互传文件_华为手机连接苹果电脑文件传输教程:一键文件互传、备份和恢复...
  17. linux 中du和df的使用
  18. 记一次天池比赛 - 性能挑战赛道
  19. 跨域问题No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
  20. win11电脑开移动热点,但是手机连不上

热门文章

  1. CentOS配置snmp
  2. 微设计(www.weidesigner.com)介绍系列文章(三)
  3. andoid-sdk 安装时出现 Stopping ADB server failed(code -1) 错
  4. [...]ubuntu rvm rails 安装完之后消失的解决方法
  5. idea阅读器插件开发实现
  6. XBRL 可扩展商业报告语言
  7. InfoWorld 公布开源软件( 2019 年)
  8. Node.js API 全栈 开发框架
  9. Win8下用DOSBox编写汇编语言
  10. 引用JavaScript的三种方式第二课