ffmpeg提取aac数据
方法1:通过命令提取
ffmpeg -i input.mp4 -vn -acodec aac ../output.aac
方法2:通过代码提取
流程图

main.c
#include "libavutil/log.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
static int get_audio_obj_type(int aac_type) {
//AAC HE V2 = AAC LC + SBR + PS
//AAV HE = AAC LC + SBR
//所以无论是 AAC_HEv2 还是 AAC_HE 都是 AAC_LC
if (aac_type == 0 || aac_type == 2 || aac_type == 3) {
return aac_type + 1;
} else {
return 2;
}
}
static int get_sample_rate_index(int freq, int aac_type) {
int i;
int freq_arr[13] = {
96000, 88200, 64000, 48000, 44100, 32000,
24000, 22050, 16000, 12000, 11025, 8000, 7350
};
//如果是 AAC HEv2 或 AAC HE, 则频率减半
if (aac_type == 28 || aac_type == 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 aac_type) {
//如果是 AAC HEv2 通道数减半
if (aac_type == 28) {
return (channels / 2);
}
return channels;
}
void getADTSHeader(uint8_t *adtsHeader, int packetSize, int profile, int sampleRate, int channels) {
int audio_object_type = get_audio_obj_type(profile);
int sampling_frequency_index = get_sample_rate_index(sampleRate, profile);
int channel_config = get_channel_config(channels, profile);
int adtsLen = packetSize + 7;
adtsHeader[0] = 0xff; //高8bits
adtsHeader[1] = 0xf0; //低4bits
adtsHeader[1] |= (0 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bit
adtsHeader[1] |= (0 << 1); //Layer:0 2bits
adtsHeader[1] |= 1; //protection absent:1 1bit
adtsHeader[2] = (audio_object_type - 1) << 6; //profile:audio_object_type - 1 2bits
adtsHeader[2] |= (sampling_frequency_index & 0x0f)
<< 2; //sampling frequency index:sampling_frequency_index 4bits
adtsHeader[2] |= (0 << 1); //private bit:0 1bit
adtsHeader[2] |=
(channel_config & 0x04) >> 2; //channel configuration:channel_config 高1bit
adtsHeader[3] =
(channel_config & 0x03) << 6; //channel configuration:channel_config 低2bits
adtsHeader[3] |= (0 << 5); //original:0 1bit
adtsHeader[3] |= (0 << 4); //home:0 1bit
adtsHeader[3] |= (0 << 3); //copyright id bit:0 1bit
adtsHeader[3] |= (0 << 2); //copyright id start:0 1bit
adtsHeader[3] |= ((adtsLen & 0x1800) >> 11); //frame length:value 高2bits
adtsHeader[4] = (uint8_t) ((adtsLen & 0x7f8) >> 3); //frame length:value 中间8bits
adtsHeader[5] = (uint8_t) ((adtsLen & 0x7) << 5); //frame length:value 低3bits
adtsHeader[5] |= 0x1f; //buffer fullness:0x7ff 高5bits
adtsHeader[6] = 0xfc;
}
int main(int argc, char **argv) {
av_log_set_level(AV_LOG_DEBUG);
if (argc < 3) {
av_log(NULL, AV_LOG_ERROR, "Usage: %s inputFile outputFile\n", argv[0]);
return -1;
}
const char *inputFile = argv[1];
const char *outputFile = argv[2];
AVFormatContext *fCtx = NULL;
int ret = avformat_open_input(&fCtx, inputFile, NULL, NULL);
if (ret != 0) {
av_log(NULL, AV_LOG_ERROR, "Open input file %s failed: %s\n", inputFile, av_err2str(ret));
return -1;
}
ret = avformat_find_stream_info(fCtx, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Find input file stream info failed: %s\n", av_err2str(ret));
avformat_close_input(&fCtx);
return -1;
}
int audioIndex = av_find_best_stream(fCtx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if (audioIndex < 0) {
av_log(NULL, AV_LOG_ERROR, "Find best stream failed: %d\n", audioIndex);
avformat_close_input(&fCtx);
return -1;
}
av_log(NULL, AV_LOG_INFO, "audioIndex=%d\n", audioIndex);
FILE *dst = fopen(outputFile, "wb");
if (dst == NULL) {
av_log(NULL, AV_LOG_ERROR, "Open output file: %s failed\n", outputFile);
avformat_close_input(&fCtx);
return -1;
}
AVPacket *packet = av_packet_alloc();
packet->data = NULL;
packet->size = 0;
while (av_read_frame(fCtx, packet) == 0) {
if (packet->stream_index == audioIndex) {
uint8_t header[7] = {0};
getADTSHeader(header,
packet->size, fCtx->streams[audioIndex]->codecpar->profile,
fCtx->streams[audioIndex]->codecpar->sample_rate,
fCtx->streams[audioIndex]->codecpar->channels);
size_t size = fwrite(header, sizeof(uint8_t), sizeof(header), dst);
if (size != sizeof(header)) {
av_log(NULL, AV_LOG_ERROR, "Write ADTS header size: %zu, expected size: %lu\n", size, sizeof(header));
fclose(dst);
avformat_close_input(&fCtx);
return -1;
}
size = fwrite(packet->data, sizeof(uint8_t), packet->size, dst);
if (size != packet->size) {
av_log(NULL, AV_LOG_ERROR, "Write size: %zu, packet size: %d\n", size, packet->size);
fclose(dst);
avformat_close_input(&fCtx);
return -1;
}
}
av_packet_unref(packet);
}
if (fCtx != NULL) {
avformat_close_input(&fCtx);
}
fclose(dst);
return 0;
}
Makefile
TARGET=main SRC=main.c CC=gcc CFLAGS=-I /usr/local/ffmpeg/include LDFLAGS=-L /usr/local/ffmpeg/lib LDFLAGS+= -lavutil -lavformat -lavcodec all:$(TARGET) $(TARGET):$(SRC) $(CC) $(SRC) $(CFLAGS) $(LDFLAGS) -o $(TARGET) clean: rm -rf $(TARGET)

浙公网安备 33010602011771号