ffmpeg提取音频数据
学习就是要学习基础,学习原理。
所以大家想学习下面的内容,最好看下下面几篇我写的文章,然后再看下面的内容就会so easy.
https://blog.csdn.net/qq_15255121/article/details/115168456
https://blog.csdn.net/qq_15255121/article/details/115301516
https://blog.csdn.net/qq_15255121/article/details/115348454
抽取音频数据
音频最重要的三个元素 采样率 采样大小(位深) 通道数
AVPacket
AVPacket使用完要进行释放
涉及到四个函数
av_init_packet(<AVPacket *pkt)
av_packet_unref(AVPacket *pkt)
av_packet_alloc() 先分配空间,再进行初始化
av_packet_free(AVPacket **pkt) 内部实现先av_packet_unref,再释放空间
av_init_packet和av_packet_unref是一对
av_packet_alloc和av_packet_free是一对。
要成对出现,不然会内存泄漏
查找流
av_find_best_stream
av_read_frame 读取数据到AVPacket
接下来我们实现一个功能从mp4当中提取出aac数据
我们简单说明下怎样提取出aac数据。
- avformat_open_input获取到AVFormatContext
- 我们从要提取音频数据的多媒体文件中,找到音频流的索引,通过av_find_best_stream
- 音频数据最重要的就是采样率,位深,通道,编码格式。我们通过音频流的编码结构体AVCodecParameters进行获取
- 通过av_read_frame获取数据存到AVPacket当中
- 如果当前的AVPacket是索引是音频数据的索引那么我们继续下面的步骤,如果不是回到4
- 我们知道aac数据分为header和data两部分,header分为ADTS和ADIF.具体解析查看我前面的讲解https://blog.csdn.net/qq_15255121/article/details/115348454。接下来我们根据3部获取到的信息构建header我们这里构建ADTS头,构建成功后,写入aac文件
- 将AVPacket当中的data数据写入aac文件
- 写完后要将AVPacket释放
- 现在回到4继续读取数据,如果读不到数据,执行10
- 释放资源
大体流程就是这样,接下来我们讲解如何获取音频的重要参数采样率,位深,通道,编码格式,并构建ADTS头
获取参数:
当找到音频流索引后,我们通过下面方式进行获取
int aac_type = fmt_ctx->streams[audio_stream_index]->codecpar->profile;
int channels = fmt_ctx->streams[audio_stream_index]->codecpar->channels;
int sample_rate = fmt_ctx->streams[audio_stream_index]->codecpar->sample_rate;
我们知道AAC的头的格式
Structure
AAAAAAAA AAAABCCD EEFFFFGH HHIJKLMM MMMMMMMM MMMOOOOO OOOOOOPP (QQQQQQQQ QQQQQQQQ)
Header consists of 7 or 9 bytes (without or with CRC).
Letter Length (bits) Description
A 12 syncword 0xFFF, all bits must be 1
B 1 MPEG Version: 0 for MPEG-4, 1 for MPEG-2
C 2 Layer: always 0
D 1 protection absent, Warning, set to 1 if there is no CRC and 0 if there is CRC
E 2 profile, the MPEG-4 Audio Object Type minus 1
F 4 MPEG-4 Sampling Frequency Index (15 is forbidden)
G 1 private stream, set to 0 when encoding, ignore when decoding
H 3 MPEG-4 Channel Configuration (in the case of 0, the channel configuration is sent via an inband PCE)
I 1 originality, set to 0 when encoding, ignore when decoding
J 1 home, set to 0 when encoding, ignore when decoding
K 1 copyrighted stream, set to 0 when encoding, ignore when decoding
L 1 copyright start, set to 0 when encoding, ignore when decoding
M 13 frame length, this value must include 7 or 9 bytes of header length: FrameLength = (ProtectionAbsent == 1 ? 7 : 9) + size(AACFrame)
O 11 Buffer fullness
P 2 Number of AAC frames (RDBs) in ADTS frame minus 1, for maximum compatibility always use 1 AAC frame per ADTS frame
Q 16 CRC if protection absent is 0
接下来我们进行按照上面的说明进行构建
我们要知道ffmpeg当中对aacprofile的定义
官方文档对audio_obj_type的定义
所以也就有了下面profile到audio_obj_type的转化
static int get_audio_obj_type(int aactype)
{//AAC HE V2 = AAC LC + SBR + PS//AAV HE = AAC LC + SBR//所以无论是 AAC_HEv2 还是 AAC_HE 都是 AAC_LCswitch (aactype){case 0:case 2:case 3:return aactype + 1;case 1:case 4:case 28:return 2;default:return 2;}
}static int get_sample_rate_index(int freq, int aactype)
{int i = 0;int freq_arr[13] = {96000, 88200, 64000, 48000, 44100, 32000,24000, 22050, 16000, 12000, 11025, 8000, 7350};//如果是 AAC HEv2 或 AAC HE, 则频率减半if (aactype == 28 || aactype == 4){freq /= 2;}for (i = 0; i < 13; i++){if (freq == freq_arr[i]){return i;}}return 4; //默认是44100
}static int get_channel_config(int channels, int aactype)
{//如果是 AAC HEv2 通道数减半if (aactype == 28){return (channels / 2);}return channels;
}
static void adts_header(char *szAdtsHeader, int dataLen, int aactype, int frequency, int channels)
{int audio_object_type = get_audio_obj_type(aactype);int sampling_frequency_index = get_sample_rate_index(frequency, aactype);int channel_config = get_channel_config(channels, aactype);printf("aot=%d, freq_index=%d, channel=%d\n", audio_object_type, sampling_frequency_index, channel_config);int adtsLen = dataLen + 7;szAdtsHeader[0] = 0xff; //syncword:0xfff 高8bitsszAdtsHeader[1] = 0xf0; //syncword:0xfff 低4bitsszAdtsHeader[1] |= (0 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bitszAdtsHeader[1] |= (0 << 1); //Layer:0 2bitsszAdtsHeader[1] |= 1; //protection absent:1 1bitszAdtsHeader[2] = (audio_object_type - 1) << 6; //profile:audio_object_type - 1 2bitsszAdtsHeader[2] |= (sampling_frequency_index & 0x0f) << 2; //sampling frequency index:sampling_frequency_index 4bitsszAdtsHeader[2] |= (0 << 1); //private bit:0 1bitszAdtsHeader[2] |= (channel_config & 0x04) >> 2; //channel configuration:channel_config 高1bitszAdtsHeader[3] = (channel_config & 0x03) << 6; //channel configuration:channel_config 低2bitsszAdtsHeader[3] |= (0 << 5); //original:0 1bitszAdtsHeader[3] |= (0 << 4); //home:0 1bitszAdtsHeader[3] |= (0 << 3); //copyright id bit:0 1bitszAdtsHeader[3] |= (0 << 2); //copyright id start:0 1bitszAdtsHeader[3] |= ((adtsLen & 0x1800) >> 11); //frame length:value 高2bitsszAdtsHeader[4] = (uint8_t)((adtsLen & 0x7f8) >> 3); //frame length:value 中间8bitsszAdtsHeader[5] = (uint8_t)((adtsLen & 0x7) << 5); //frame length:value 低3bitsszAdtsHeader[5] |= 0x1f; //buffer fullness:0x7ff 高5bitsszAdtsHeader[6] = 0xfc;
}
下面是完整代码,来自李超老师提供的demo。
/*** extract audio from media file** copyright lichao 2020.4.10*/#include <stdio.h>
#include <libavutil/log.h>
#include <libavformat/avio.h>
#include <libavformat/avformat.h>#define ADTS_HEADER_LEN 7;static int get_audio_obj_type(int aactype)
{//AAC HE V2 = AAC LC + SBR + PS//AAV HE = AAC LC + SBR//所以无论是 AAC_HEv2 还是 AAC_HE 都是 AAC_LCswitch (aactype){case 0:case 2:case 3:return aactype + 1;case 1:case 4:case 28:return 2;default:return 2;}
}static int get_sample_rate_index(int freq, int aactype)
{int i = 0;int freq_arr[13] = {96000, 88200, 64000, 48000, 44100, 32000,24000, 22050, 16000, 12000, 11025, 8000, 7350};//如果是 AAC HEv2 或 AAC HE, 则频率减半if (aactype == 28 || aactype == 4){freq /= 2;}for (i = 0; i < 13; i++){if (freq == freq_arr[i]){return i;}}return 4; //默认是44100
}static int get_channel_config(int channels, int aactype)
{//如果是 AAC HEv2 通道数减半if (aactype == 28){return (channels / 2);}return channels;
}static void adts_header(char *szAdtsHeader, int dataLen, int aactype, int frequency, int channels)
{int audio_object_type = get_audio_obj_type(aactype);int sampling_frequency_index = get_sample_rate_index(frequency, aactype);int channel_config = get_channel_config(channels, aactype);printf("aot=%d, freq_index=%d, channel=%d\n", audio_object_type, sampling_frequency_index, channel_config);int adtsLen = dataLen + 7;szAdtsHeader[0] = 0xff; //syncword:0xfff 高8bitsszAdtsHeader[1] = 0xf0; //syncword:0xfff 低4bitsszAdtsHeader[1] |= (0 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bitszAdtsHeader[1] |= (0 << 1); //Layer:0 2bitsszAdtsHeader[1] |= 1; //protection absent:1 1bitszAdtsHeader[2] = (audio_object_type - 1) << 6; //profile:audio_object_type - 1 2bitsszAdtsHeader[2] |= (sampling_frequency_index & 0x0f) << 2; //sampling frequency index:sampling_frequency_index 4bitsszAdtsHeader[2] |= (0 << 1); //private bit:0 1bitszAdtsHeader[2] |= (channel_config & 0x04) >> 2; //channel configuration:channel_config 高1bitszAdtsHeader[3] = (channel_config & 0x03) << 6; //channel configuration:channel_config 低2bitsszAdtsHeader[3] |= (0 << 5); //original:0 1bitszAdtsHeader[3] |= (0 << 4); //home:0 1bitszAdtsHeader[3] |= (0 << 3); //copyright id bit:0 1bitszAdtsHeader[3] |= (0 << 2); //copyright id start:0 1bitszAdtsHeader[3] |= ((adtsLen & 0x1800) >> 11); //frame length:value 高2bitsszAdtsHeader[4] = (uint8_t)((adtsLen & 0x7f8) >> 3); //frame length:value 中间8bitsszAdtsHeader[5] = (uint8_t)((adtsLen & 0x7) << 5); //frame length:value 低3bitsszAdtsHeader[5] |= 0x1f; //buffer fullness:0x7ff 高5bitsszAdtsHeader[6] = 0xfc;
}int main(int argc, char *argv[])
{int err_code;char errors[1024];char *src_filename = NULL;char *dst_filename = NULL;FILE *dst_fd = NULL;int audio_stream_index = -1;int len;AVFormatContext *ofmt_ctx = NULL;AVOutputFormat *output_fmt = NULL;AVStream *out_stream = NULL;AVFormatContext *fmt_ctx = NULL;AVFrame *frame = NULL;AVPacket pkt;av_log_set_level(AV_LOG_DEBUG);// if (argc < 3)// {// av_log(NULL, AV_LOG_DEBUG, "the count of parameters should be more than three!\n");// return -1;// }// src_filename = argv[1];// dst_filename = argv[2];src_filename = "/Users/yuanxuzhen/study/mac/ffmpeg_demo/output/sync_714251_3900875.mp4";dst_filename = "/Users/yuanxuzhen/study/mac/ffmpeg_demo/output/test.aac";if (src_filename == NULL || dst_filename == NULL){av_log(NULL, AV_LOG_DEBUG, "src or dts file is null, plz check them!\n");return -1;}dst_fd = fopen(dst_filename, "wb");if (!dst_fd){av_log(NULL, AV_LOG_DEBUG, "Could not open destination file %s\n", dst_filename);return -1;}/*open input media file, and allocate format context*/if ((err_code = avformat_open_input(&fmt_ctx, src_filename, NULL, NULL)) < 0){av_strerror(err_code, errors, 1024);av_log(NULL, AV_LOG_DEBUG, "Could not open source file: %s, %d(%s)\n",src_filename,err_code,errors);return -1;}/*retrieve audio stream*/if ((err_code = avformat_find_stream_info(fmt_ctx, NULL)) < 0){av_strerror(err_code, errors, 1024);av_log(NULL, AV_LOG_DEBUG, "failed to find stream information: %s, %d(%s)\n",src_filename,err_code,errors);return -1;}/*dump input information*/av_dump_format(fmt_ctx, 0, src_filename, 0);frame = av_frame_alloc();if (!frame){av_log(NULL, AV_LOG_DEBUG, "Could not allocate frame\n");return AVERROR(ENOMEM);}/*initialize packet*/av_init_packet(&pkt);pkt.data = NULL;pkt.size = 0;/*find best audio stream*/audio_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);if (audio_stream_index < 0){av_log(NULL, AV_LOG_DEBUG, "Could not find %s stream in input file %s\n",av_get_media_type_string(AVMEDIA_TYPE_AUDIO),src_filename);return AVERROR(EINVAL);}/*#define FF_PROFILE_AAC_MAIN 0#define FF_PROFILE_AAC_LOW 1#define FF_PROFILE_AAC_SSR 2#define FF_PROFILE_AAC_LTP 3#define FF_PROFILE_AAC_HE 4#define FF_PROFILE_AAC_HE_V2 28#define FF_PROFILE_AAC_LD 22#define FF_PROFILE_AAC_ELD 38#define FF_PROFILE_MPEG2_AAC_LOW 128#define FF_PROFILE_MPEG2_AAC_HE 131*/int aac_type = fmt_ctx->streams[audio_stream_index]->codecpar->profile;int channels = fmt_ctx->streams[1]->codecpar->channels;int sample_rate = fmt_ctx->streams[1]->codecpar->sample_rate;if (fmt_ctx->streams[1]->codecpar->codec_id != AV_CODEC_ID_AAC){av_log(NULL, AV_LOG_ERROR, "the audio type is not AAC!\n");goto __ERROR;}else{av_log(NULL, AV_LOG_INFO, "the audio type is AAC!\n");}/*read frames from media file*/while (av_read_frame(fmt_ctx, &pkt) >= 0){if (pkt.stream_index == audio_stream_index){char adts_header_buf[7];adts_header(adts_header_buf, pkt.size, aac_type, sample_rate, channels);fwrite(adts_header_buf, 1, 7, dst_fd);len = fwrite(pkt.data, 1, pkt.size, dst_fd);if (len != pkt.size){av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",len,pkt.size);}}av_packet_unref(&pkt);}__ERROR:/*close input media file*/avformat_close_input(&fmt_ctx);if (dst_fd){fclose(dst_fd);}return 0;
}
提取出数据后,我们使用ffplay xxxx.aac进行播放。
提取完aac数据后,接下来几篇。
我们谈下如何从aac到pcm?如何将pcm转成aac?如何采集音频数据再转成pcm再转成aac?
不停的复习,不停地举一反三,不停的复盘总结才能真正学习会一门新技能。
用慢对抗快;
用体系对抗碎片;
用原理对抗招式。
一起共勉!!!!!加油!!!!!!!
ffmpeg提取音频数据相关推荐
- ffmpeg提取音频播放器总结
ffmpeg提取音频播放器总结: 一:简介 从编写音频播放器代码到完成播放器编写,测试,整整5天的时间,这时间还不算之前对 ffmpeg熟悉的时间,可以说是历经千辛万苦,终于搞出来了,虽然最终效果 ...
- wav文件提取音频数据_python
wav文件提取音频数据_python wav文件格式 首先需要知道wav文件格式:(10条消息) WAV文件格式详解_imxiangzi的专栏-CSDN博客_wav文件格式 简单来说wav文件分为三个 ...
- ffmpeg提取音频并对获取到的音频进行截取
1.下载ffmpeg Download FFmpeghttp://ffmpeg.org/download.html#build-windows 2.对下载包进行解压就能直接使用(为了方便实用将执行程序 ...
- python音频 降噪_从视频中提取音频数据,然后应用傅里叶对音频降噪(python)...
视频准备 QQ有热键 然后随便打开一个视频网站进行录屏 我选择B站 从视频中提取音频 需要安装包moviepy pip install moviepy 提取代码 from moviepy.editor ...
- ffmpeg提取音频文件命令并转化成pcm格式
本文转载自:https://blog.csdn.net/u014552102/article/details/82734278 一.PCM文件的定义 PCM文件:模拟音频信号经模数转换(A/D变 ...
- 从视频中提取音频数据,然后应用傅里叶对音频降噪(python)
视频准备 QQ有热键 然后随便打开一个视频网站进行录屏 我选择B站 从视频中提取音频 需要安装包moviepy pip install moviepy 提取代码 from moviepy.editor ...
- ffmpeg 提取音频,音频转换,添加字幕
刚好有做一个视频处理类的程序,于是了解到了有ffmpeg这样强大的工具. 本篇主要是说明一些相关 ffmpeg 命令行语句的使用. 音频操作 如果不转换,直接输出aac ffmpeg -i input ...
- 记录一次C# 使用FFmpeg提取音频文件
使用的库 using System; using System.IO; using System.Runtime.InteropServices; using AudioRecordService.C ...
- FFmpeg提取视频(mp4)中的音频(m4a)
目的 方法 1. 下载 FFmpeg 下载地址 下载后解压缩,我解压缩到的位置是:D:\Program Files 并将文件夹名称改为ffmpeg 自己可以根据情况,随便解压缩到什么位置都可以 2. ...
- 海康威视摄像机SDK二次开发--提取音频保存至文件
由于最近在开发海康威视摄像头,特此记录一下如何提取音频数据,这里主要依靠语音对讲返回的音频数据,通过回调函数写入文件中,加个WAV头即可播放,编码格式可以自己设置在代码中有注释 文件结构 其中Came ...
最新文章
- java.lang.IllegalStateException: Cannot modify managed objects outside of a write transaction. in /U
- 实验1 查看CPU和内存、用机器指令和汇编指令编程
- C++开发人脸性别识别教程(7)——搭建MFC框架之界面绘制
- 富士施乐打印机-查看端口号
- 新手前端练手网站_编程到底难不难学?新手入门选择哪种语言好?
- matlab knn实现,Matlab之KNN实现
- 8 仓储单位类型E1对于物料xxxxxx没有维护
- php 添加失败是什么原因,安装zblog提示“创建c_option.php失败”的原因和解决办法...
- hibernate一对多自关联的记录(以树形菜单为例)
- 多个同名进程linux获取对应pid,Linux Shell根据进程名获取PID
- 非线性光纤光学_《Nature》子刊:解决大纵横比光纤中传质不匀的难题!
- 【最全】PS各个版本下载安装及小试牛刀教程(PhotoShop CS3 ~~ PhotoShop 2022)
- 怎么看计算机版本号,如何查看电脑windows版本号(查看电脑版本信息的11种方法)...
- Habor数据迁移方式有多少,skopeo效率最好
- bios中 启动首选项 找不到固态硬盘
- WPF 如何在静态资源定义字体大小
- Java生成二维码海报
- 刀片服务器改台式电脑_详解刀片服务器如何走向融合
- python图片拼接成特定图案_Python 把几张图片拼接成一张图片,并且写上文字
- 赤兔android手机视频恢复软件,赤兔安卓手机视频恢复软件
热门文章
- 利用pdf-poppler和printJS完成pdf批量打印
- 给定一个球体的半径,计算其体积。其中球体体积公式为 V = 4/3*πr3,其中 π= 3.1415926。
- win7怎么跳过硬盘自检_从零开始修电脑第二期__电脑自检慢的维修
- MySql存储含有特殊符号的微信昵称
- 各种加密模式在TLS协议中的运用 2 (AEAD:CCM模式)
- zkteco iface702 中控考勤机java开发步骤一---连接考勤机
- 百度移动搜索优化指南2.0
- htmla标签下划线去除_div css网页开发布局时a标签去掉下划线
- libx264开发笔记(一):libx264介绍、海思平台移植编译
- 超简单使用阿里云注册商标