//
// test.c
// av_recoder
//
// Created by 蔡磊 on 2023/4/25.
//
#include "test.h"
void test(void){
printf("this is a c foundtion test\n");
av_log_set_level(AV_LOG_DEBUG);
av_log(NULL, AV_LOG_DEBUG, "this is av log\n");
}
void changeRecordStatu(int status){
rec_status = status;
}
// 打开编码器
AVCodecContext* openAvCodec(void){
// 找到编码器
// avcodec_find_encoder(AV_CODEC_ID_AAC);
const AVCodec *codec = avcodec_find_encoder_by_name("libfdk-aac");
AVCodecContext * codecContext = avcodec_alloc_context3(codec);
codecContext->sample_fmt = AV_SAMPLE_FMT_FLTP;
codecContext->channel_layout =AV_CH_LAYOUT_STEREO;
codecContext->sample_rate = 96000;
codecContext->codec_type = AVMEDIA_TYPE_AUDIO;
codecContext->ch_layout.nb_channels = 1;
codecContext->bit_rate = 64000; // aac_lc 128k aac_he 64k aac_he_v2 32k
// codecContext->profile = FF_PROFILE_AAC_HE_V2;
// 打开编码器
int ret = avcodec_open2(codecContext, codec, NULL);
if(ret) {
printf("open avcodec failed \n");
char errbuf[128] = { 0 };
const char *errbuf_ptr = errbuf;
if (av_strerror(ret, errbuf, sizeof(errbuf)) < 0)
printf(errbuf);
}
return codecContext;
}
SwrContext* init_swr(void){
// 创建重采样上下文
SwrContext *swr_ctx = NULL;
swr_ctx = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, 44100, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_FLT, 44100, 0, NULL);
if(!swr_ctx) {
}
if(swr_init(swr_ctx)<0) {
}
return swr_ctx;
}
void record(void){
// if(recording){
// return;
// }
char errors[1024];
int ret;
int count = 0;
AVFormatContext *fmt_ctx = avformat_alloc_context();
char *deviceName = ":0";
// 注册设备
avdevice_register_all();
// get format
const AVInputFormat *inputFormat = av_find_input_format("avfoundation");
// 重采样上下文
SwrContext* swr_ctx = init_swr();
// aac 编码器上下文
AVCodecContext *codeContext = openAvCodec();
AVFrame *frame = av_frame_alloc();
frame->format = AV_SAMPLE_FMT_FLTP;
frame->channel_layout = AV_CH_LAYOUT_STEREO;
frame->nb_samples = 512; // 单通道每秒采样数
av_frame_get_buffer(frame, 0);
if(!frame || !frame->buf[0]) {
}
AVPacket *newPkt = av_packet_alloc();
if(!newPkt) {
}
// 打开设备
if((ret = avformat_open_input(&fmt_ctx, deviceName, inputFormat, NULL) < 0)) {
av_strerror(ret, errors, 1024);
fprintf(stderr,"Failed to open audio device,[%d]%s\n",ret,errors);
return;
}
// 读取数据
AVPacket *avPacket = av_packet_alloc();
// 创建文件夹保存数据
char *out = "/Users/cailei/Downloads/audio.aac";
FILE *outfile = fopen(out, "wb+");
rec_status = 1;
uint8_t **src_data = NULL;
int src_linesize = 0;
uint8_t **out_data = NULL;
int out_linesize = 0;
// 创建输入缓冲区
av_samples_alloc_array_and_samples(&src_data, &src_linesize, 1, 512, AV_SAMPLE_FMT_FLT, 0);
// 创建输出缓冲区
av_samples_alloc_array_and_samples(&out_data, &out_linesize, 1, 512, AV_SAMPLE_FMT_S16, 0);
while(((ret = av_read_frame(fmt_ctx, avPacket)) == 0 || ret == -35) && rec_status) {
if(ret == -35) {
usleep(100);
continue;
}
printf("pkt size is %d \n",avPacket->size);
// 进行内存拷贝
memcpy((void*)src_data[0], (void*)avPacket->data, avPacket->size);
count++;
// 重采样开始
swr_convert(swr_ctx, out_data, 512, (const uint8_t **)src_data, 512);
// 将重采样的数据拷贝到avframe中
memcpy((void*)frame->data[0], (void*)out_data[0], out_linesize);
// 将数据送到编码器
ret = avcodec_send_frame(codeContext, frame);
while(ret >= 0) {
// 获取编码后的音频数据
ret = avcodec_receive_packet(codeContext, newPkt);
if(ret < 0) {
if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
// 编码器没有数据,或者编码器数据不够编码的数据了
break;
}else if(ret < 0) {
printf("Error, encoding audio frame\n");
exit(-1);
}
}
fwrite(newPkt,1,newPkt->size, outfile);
fflush(outfile);
}
av_packet_unref(avPacket);
}
av_packet_free(&avPacket);
fclose(outfile);
// 释放输入输出缓冲区
if(src_data) {
av_freep(&src_data[0]);
}
av_freep(src_data);
if(out_data) {
av_freep(&out_data[0]);
}
av_freep(out_data);
// 释放重采样上下文
swr_free(&swr_ctx);
avformat_close_input(&fmt_ctx);
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本文旨在开发一款基于iOS平台的音视频处理应用,主要实现录音和播放功能。为了达到这一目标,我们将使用FFmpeg库,这是一个广泛应用于音视频处理的开源库。通过调用FFmpeg的功能和接口,我们可以实现录音和播放功能,并提供用户友好的界面进行操作。本文将介绍开发环境的搭建、FFmpeg库的集成和使用、录音和播放功能的实现方法,以及应用的测试和优化。最终的目标是开发一个功能完善、稳定可靠的音视频处理应用,为用户提供高质量的录音和播放体验。 关键词:iOS平台,音视频处理,FFmpeg库,录音,播放功能 引言 音视频处理在移动应用开发中具有广泛的应用,其中录音和播放是常见的功能需求。为了实现高质量的录音和播放,我们选择使用FFmpeg库,它是一个功能强大且广泛使用的开源库。通过调用FFmpeg库的接口,我们可以实现音频的录制和播放,并能够对音频进行格式转换、编码解码等操作。 开发环境搭建 在开始开发之前,我们需要搭建适合iOS平台的开发环境。我们将使用Xcode作为主要的集成开发环境,确保Xcode已正确安装和配置。另外,我们还需要下载和编译FFmpeg库,并将其集成到我们的项目中。
资源推荐
资源详情
资源评论
收起资源包目录
IOS 音视频 ffmpeg 录音并播放 (221个子文件)
00db43ec3c8b462759505d635dc5545d4e8e50 185B
0e81f5a610b955c5ebd947329d50762dda5d67 84B
186fe207cd968ba72ee3f412c49823ddcb914f 125B
256fa87c2ac31691805466d0a1fbb49833ef51 133B
2cb5d6d09fe5019d2f930632eaa15b4e51aedc 104B
49a1b0a87a7e175e64c3f3aaa922a1c4c1bd79 60B
4e5d29cf9deab94a006f1eebb18c0f72e3e1fc 3KB
50f40dc8caccacdf8d7d0eef8d6189cabd5e06 141B
65b8ab242800b016f704c1f642c99b6e3449e4 220B
6bd324c26d854187559f27f8194c8afd1795ac 58B
758cf8638e9503a0e1cebb713f75597f937501 94B
8789700816459c1e1480e0b34781d9fb78a1ca 105B
8f0e57e0721334d084b2c3c7e94d36af09d5d4 294B
93e1046ed16e3da821df622e0eb73331ff69b6 264B
95063fdf90625aae1621d4a6cdc59f6f63c742 58B
aff0ce43c2084462a8f12ea6e9c55c87914caf 226B
test.c 4KB
c00596a7fca3f3d4bdd64053b69d86745f9e10 66B
c4446240502937a385eeca652d10c39d522ba1 7KB
COMMIT_EDITMSG 15B
config 137B
description 73B
.DS_Store 8KB
.DS_Store 6KB
.DS_Store 6KB
libavcodec.60.10.100.dylib 12.8MB
libavfilter.9.5.100.dylib 4.38MB
libavformat.60.5.100.dylib 2.16MB
libswscale.7.2.100.dylib 886KB
libavutil.58.6.100.dylib 637KB
libavdevice.60.2.100.dylib 119KB
libswresample.4.11.100.dylib 116KB
libpostproc.57.2.100.dylib 82KB
libswresample.4.dylib 28B
libswresample.dylib 28B
libavformat.60.dylib 26B
libpostproc.57.dylib 26B
libpostproc.dylib 26B
libavcodec.60.dylib 26B
libavdevice.60.dylib 26B
libavdevice.dylib 26B
libavformat.dylib 26B
libavcodec.dylib 26B
libavfilter.dylib 25B
libavfilter.9.dylib 25B
libavutil.58.dylib 24B
libswscale.7.dylib 24B
libavutil.dylib 24B
libswscale.dylib 24B
ef3ae0265b40c475e8ef90e3a311c31786c594 227B
av_recoderDebug.entitlements 393B
av_recoder.entitlements 322B
exclude 40B
f4c56df625ae53e59157ac409a640de447a4c0 57B
avcodec.h 115KB
avformat.h 107KB
avfilter.h 55KB
pixfmt.h 40KB
opt.h 36KB
frame.h 33KB
channel_layout.h 32KB
avio.h 31KB
swresample.h 25KB
hwcontext.h 24KB
packet.h 23KB
mem.h 20KB
intreadwrite.h 18KB
codec_id.h 17KB
common.h 17KB
swscale.h 16KB
pixdesc.h 16KB
fifo.h 15KB
avstring.h 15KB
hdr_dynamic_metadata.h 14KB
imgutils.h 14KB
avdevice.h 13KB
codec.h 13KB
log.h 12KB
buffer.h 12KB
bsf.h 11KB
samplefmt.h 10KB
hdr_dynamic_vivid_metadata.h 10KB
avutil.h 10KB
hwcontext_vulkan.h 9KB
dict.h 9KB
bprint.h 8KB
film_grain_params.h 8KB
hash.h 8KB
mathematics.h 8KB
spherical.h 8KB
parseutils.h 8KB
timecode.h 8KB
dovi_meta.h 7KB
codec_par.h 7KB
encryption_info.h 7KB
buffersink.h 7KB
buffersrc.h 7KB
defs.h 7KB
hwcontext_d3d11va.h 7KB
eval.h 6KB
共 221 条
- 1
- 2
- 3
资源评论
小菜的OnePiece
- 粉丝: 105
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功