当前位置: 首页 > news >正文

ffmpeg aac s16 encode_audio.c

用ffmpeg库时,用代码对pcm内容采用aac编码进行压缩,出现如下错误。

[aac @ 000002bc5edc6e40] Format aac detected only with low score of 1, misdetection possible!
[aac @ 000002bc5edc8140] Error decoding AAC frame header.
[aac @ 000002bc5edc8140] More than one AAC RDB per ADTS frame is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
[aac @ 000002bc5edc8140] Multiple frames in a packet.
[aac @ 000002bc5edc8140] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 000002bc5edc8140] Too large remapped id is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
[aac @ 000002bc5edc8140] If you want to help, upload a sample of this file to https://streams.videolan.org/upload/ and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)
[aac @ 000002bc5edc8140] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 000002bc5edc8140] channel element 1.3 is not allocated
[aac @ 000002bc5edc8140] SBR was found before the first channel element.
[aac @ 000002bc5edc8140] channel element 3.6 is not allocated
[aac @ 000002bc5edc8140] channel element 2.12 is not allocated
[aac @ 000002bc5edc8140] Assuming an incorrectly encoded 7.1 channel layout instead of a spec-compliant 7.1(wide) layout, use -strict 1 to decode according to the specification instead.
[aac @ 000002bc5edc8140] channel element 3.12 is not allocated
[aac @ 000002bc5edc6e40] Packet corrupt (stream = 0, dts = NOPTS).
[aac @ 000002bc5edc8140] channel element 1.9 is not allocated
[aac @ 000002bc5edc6e40] Estimating duration from bitrate, this may be inaccurate
[aac @ 000002bc5edc6e40] Could not find codec parameters for stream 0 (Audio: aac (LTP), 3.0, fltp, 505 kb/s): unspecified sample rate
Consider increasing the value for the 'analyzeduration' (0) and 'probesize' (5000000) options 

过程分析:

数据源是无压缩的pcm数据(cr:44100, cn=2(stereo) , format:s16 ) 。
同样的内容如果使用命令行进行压缩
ffmpeg -i E:/video/out.pcm -ar 44100 -b 128K -ac 2 -c:a aac_mf E:/video/out1.aac -y
进行压缩,发现能进行播放。
(ffmpeg中的aac不支持s16格式数据,aac_mf才支持,可以用ffmpeg -h encoder=aac 查看)

使用代码进行压缩出现上图右边的错误。测试代码是从ffmpeg/doc/example/encode_audio.c上进行更改的。
没有进入ffmpeg源码进行调试,只能从现有的结果进行分析,发现代码生成的内容大小是没有问题的,但是被ffmpeg错误解析。猜测应该是数据包头解析错误。但是因为之前没有做过aac编码,真是瞎子摸象,一句一句的修改测试,无果。

最后在网上找到一个能运行的demo才解决问题:FFmpeg简单使用:音频编码 ---- pcm转aac_avframe 音频 aac-CSDN博客 

最后发现原因是aac编码后的数据包写入文件时需要额外写入数据包头,而demo中的代码太简单,根本没有这个步骤,下面是代码示例:

static void get_adts_header(AVCodecContext *ctx, uint8_t *adts_header, int aac_length)
{uint8_t freq_idx = 0;    //0: 96000 Hz  3: 48000 Hz 4: 44100 Hzswitch (ctx->sample_rate) {case 96000: freq_idx = 0; break;case 88200: freq_idx = 1; break;case 64000: freq_idx = 2; break;case 48000: freq_idx = 3; break;case 44100: freq_idx = 4; break;case 32000: freq_idx = 5; break;case 24000: freq_idx = 6; break;case 22050: freq_idx = 7; break;case 16000: freq_idx = 8; break;case 12000: freq_idx = 9; break;case 11025: freq_idx = 10; break;case 8000: freq_idx = 11; break;case 7350: freq_idx = 12; break;default: freq_idx = 4; break;}uint8_t chanCfg = ctx->channels;uint32_t frame_length = aac_length + 7;adts_header[0] = 0xFF;adts_header[1] = 0xF1;adts_header[2] = ((ctx->profile) << 6) + (freq_idx << 2) + (chanCfg >> 2);adts_header[3] = (((chanCfg & 3) << 6) + (frame_length  >> 11));adts_header[4] = ((frame_length & 0x7FF) >> 3);adts_header[5] = (((frame_length & 7) << 5) + 0x1F);adts_header[6] = 0xFC;
}static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,FILE *output)
{int ret;/* send the frame for encoding */ret = avcodec_send_frame(ctx, frame);if (ret < 0) {fprintf(stderr, "Error sending the frame to the encoder\n");exit(1);}/* read all the available output packets (in general there may be any* number of them */while (ret >= 0) {ret = avcodec_receive_packet(ctx, pkt);if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)return;else if (ret < 0) {fprintf(stderr, "Error encoding audio frame\n");exit(1);}//额外写入包头uint8_t aac_header[7];get_adts_header(ctx, aac_header, pkt->size);size_t len = 0;len = fwrite(aac_header, 1, 7, output);if(len != 7) {fprintf(stderr, "fwrite aac_header failed\n");return -1;}//        printf("%d:%d\n",count++,pkt->pts);fwrite(pkt->data, 1, pkt->size, output);//av_packet_unref(pkt);}
}int main(int argc, char **argv)
{const char *filename;const AVCodec *codec;AVCodecContext *c= NULL;AVFrame *frame;AVPacket *pkt;int i, j, k, ret;FILE *f;uint16_t *samples;float t, tincr;filename="E:/video/out.aac";/* find the MP2 encoder *///codec = avcodec_find_encoder(AV_CODEC_ID_MP2);//codec = avcodec_find_encoder(AV_CODEC_ID_AAC);//printf("%s\n",avcodec_get_id( AV_CODEC_ID_AAC_LATM));codec = avcodec_find_encoder_by_name("aac_mf");if (!codec) {fprintf(stderr, "Codec not found\n");exit(1);}c = avcodec_alloc_context3(codec);if (!c) {fprintf(stderr, "Could not allocate audio codec context\n");exit(1);}/* put sample parameters */c->bit_rate = 64000;/* check that the encoder supports s16 pcm input */c->sample_fmt = AV_SAMPLE_FMT_S16;if (!check_sample_fmt(codec, c->sample_fmt)) {fprintf(stderr, "Encoder does not support sample format %s",av_get_sample_fmt_name(c->sample_fmt));exit(1);}//c->profile = FF_PROFILE_AAC_LOW;  c->bit_rate为0,c->profile设置才会有效/* select other audio parameters supported by the encoder */c->sample_rate    = select_sample_rate(codec);ret = select_channel_layout(codec, &c->ch_layout);if (ret < 0)exit(1);c->channels = c->ch_layout.nb_channels;//sample_rate(cr)=44100,表示1秒钟有44100个采样点。格式为s16 + stereo表示双通道,每通道16位(short) packed格式,(S16P 表示双通道通道16位planar格式)
//也就是一个采样点需要4个字节来保存。这里的frame_size 表示采样点个数。c->frame_size=512;//手动设置frame_size//	声音的timebase设置为采样率或其倍数,因为PTS是int64的。如果timebase刚好等于sample_rate,
//	那么更具pts计算公式 audio_pts = n*frame_size*timebase/sample_rate = n*frame_size
//	这样对时间的pts设置极为方便。c->time_base=av_make_q(c->sample_rate,1);//手动设置time_base,/* open it */if (avcodec_open2(c, codec, NULL) < 0) {fprintf(stderr, "Could not open codec\n");exit(1);}f = fopen(filename, "wb");if (!f) {fprintf(stderr, "Could not open %s\n", filename);exit(1);}/* packet for holding encoded output */pkt = av_packet_alloc();if (!pkt) {fprintf(stderr, "could not allocate the packet\n");exit(1);}/* frame containing input raw audio */frame = av_frame_alloc();if (!frame) {fprintf(stderr, "Could not allocate audio frame\n");exit(1);}frame->nb_samples     = c->frame_size;frame->format         = c->sample_fmt;ret = av_channel_layout_copy(&frame->ch_layout, &c->ch_layout);if (ret < 0)exit(1);frame->channels = c->channels;frame->pts = 0;   //初始化pts//音频的packed格式的数据,都存在data[0]中,并且是连续的;stereo planer格式才会将左右两个通道分开在data[0]和data[1]中存放,av_frame_get_buffer会为frame按照参数申请缓存。
//packed :LLRRLLRRLLRRLLRR
//planer :LLLLLLLL....RRRRRRRRR..../* allocate the data buffers */ret = av_frame_get_buffer(frame, 0);if (ret < 0) {fprintf(stderr, "Could not allocate audio data buffers\n");exit(1);}/* encode a single tone sound */t = 0;uint8_t *buffer = malloc(frame->nb_samples*frame->ch_layout.nb_channels*av_get_bytes_per_sample(frame->format));tincr = 2 * M_PI * 440.0 / c->sample_rate;for (i = 0; i < 200; i++) {/* make sure the frame is writable -- makes a copy if the encoder* kept a reference internally */ret = av_frame_make_writable(frame);if (ret < 0)exit(1);samples = (uint16_t*)buffer;for (j = 0; j < c->frame_size; j++) {samples[2*j] = (int)(sin(t) * 10000);for (k = 1; k < c->ch_layout.nb_channels; k++)samples[2*j + k] = samples[2*j];t += tincr;}ret = av_samples_fill_arrays(frame->data, frame->linesize,buffer, frame->channels,frame->nb_samples,frame->format, 0);frame->pts += frame->nb_samples;encode(c, frame, pkt, f);}/* flush the encoder */encode(c, NULL, pkt, f);free(buffer);fclose(f);av_frame_free(&frame);av_packet_free(&pkt);avcodec_free_context(&c);return 0;
}

7-5ADTS格式_哔哩哔哩_bilibili 

AAC ADTS格式分析&AAC编码(adts_header详解)_adts header-CSDN博客

相关文章:

ffmpeg aac s16 encode_audio.c

用ffmpeg库时&#xff0c;用代码对pcm内容采用aac编码进行压缩&#xff0c;出现如下错误。 [aac 000002bc5edc6e40] Format aac detected only with low score of 1, misdetection possible! [aac 000002bc5edc8140] Error decoding AAC frame header. [aac 000002bc5edc81…...

vue3监听器

1.侦听数据源类型 watch 的第一个参数可以是不同形式的“数据源”&#xff1a;它可以是一个 ref (包括计算属性)、一个响应式对象、一个 getter 函数、或多个数据源组成的数组 const x ref(0) const y ref(0)// 单个 ref watch(x, (newX) > {console.log(x is ${newX}) …...

03-51单片机定时器和串口通信

一、51单片机定时器 1.定时器介绍 1.1为什么要使用定时器 在前面的学习中&#xff0c;用到了 Delay 函数延时&#xff0c;这里学习定时器以后&#xff0c;就可以通过定时器来完成&#xff0c;当然定时器的功能远不止这些&#xff1a; 51 单片机的定时器既可以定时&#xff…...

系统架构设计师考点—项目管理

一、备考指南 项目管理主要考查的是进度管理、软件配置管理、质量管理、风险管理等相关知识&#xff0c;近几年都没有考查过&#xff0c;但是有可能在案例分析中考查关键路径的技术问题&#xff0c;考生了解为主。 二、重点考点 1、项目的十大管理&#xff08;速记&#xff1…...

代码随想录算法训练营第三十二天|509.斐波那契数、70.爬楼梯、746.使用最小花费爬楼梯

目录 509.斐波那契数 动态规划五部曲&#xff1a; 1.确定dp数组&#xff08;dp table&#xff09;以及下标的含义 2.确定递推公式 3.dp数组如何初始化 4.确定遍历顺序 5.举例推导dp数组 70.爬楼梯 动态规划五部曲&#xff1a; 1.确定dp数组&#xff08;dp table&#xff09;…...

【2024年华为OD机试】 (A卷,100分)- 总最快检测效率(Java JS PythonC/C++)

一、问题描述 题目描述 在系统、网络均正常的情况下组织核酸采样员和志愿者对人群进行核酸检测筛查。 每名采样员的效率不同&#xff0c;采样效率为 N 人/小时。由于外界变化&#xff0c;采样员的效率会以 M 人/小时为粒度发生变化&#xff0c;M 为采样效率浮动粒度&#xf…...

【大数据】Apache Superset:可视化开源架构

Apache Superset是什么 Apache Superset 是一个开源的现代化数据可视化和数据探索平台&#xff0c;主要用于帮助用户以交互式的方式分析和展示数据。有不少丰富的可视化组件&#xff0c;可以将数据从多种数据源&#xff08;如 SQL 数据库、数据仓库、NoSQL 数据库等&#xff0…...

LabVIEW调用不定长数组 DLL数组

在使用 LabVIEW 调用 DLL 库函数时&#xff0c;如果函数中的结构体包含不定长数组&#xff0c;直接通过 调用库函数节点&#xff08;Call Library Function Node&#xff09; 调用通常会遇到问题。这是因为 LabVIEW 需要与 DLL 中的数据结构完全匹配&#xff0c;而包含不定长数…...

MySQL 17 章——触发器

在实际开发中&#xff0c;我们经常会遇到这样的情况&#xff1a;有2个或者多个相关联的表&#xff0c;比如商品信息表和库存信息表&#xff0c;分别存放在两个不同的数据表中&#xff0c;我们在添加一条新商品记录的时候&#xff0c;为了保证数据的完整性&#xff0c;必须同时在…...

面向对象分析与设计Python版 面向对象设计方法

文章目录 前言一、职责驱动设计二、职责驱动设计-案例 前言 面向对象设计目标&#xff1a;在面向对象分析建立的领域模型的基础上&#xff0c;定义对象操作&#xff08;职责&#xff09;。为对象分配职责的方法有&#xff1a; 职责驱动设计遵循GRASP设计原则&#xff08;Gene…...

GB/T 19582.1-2008主要内容

标准背景与概述 GB/T 19582.1-2008是由中国国家标准化管理委员会发布的国家标准&#xff0c;旨在指导和规范基于Modbus协议的工业自动化网络的设计和实施。该标准由全国工业过程测量控制和自动化标准化技术委员会&#xff08;TC124&#xff09;归口&#xff0c;并由中国机械工…...

[石榴翻译] 维吾尔语音识别 + TTS语音合成

API网址 丝路AI平台 获取 Access token 接口地址&#xff1a;https://open.xjguoyu.cn/api/auth/oauth/token&#xff0c;请求方式&#xff1a;GET&#xff0c;POST Access token是调用服务API的凭证&#xff0c;调用服务API之前需要获取 token。每次成功获取 token 以后只有…...

算法题(32):三数之和

审题&#xff1a; 需要我们找到满足以下三个条件的所有三元组&#xff0c;并存在二维数组中返回 1.三个元素相加为0 2.三个元素的下标不可相同 3.三元组的元素不可相同 思路&#xff1a; 混乱的数据不利于进行操作&#xff0c;所以我们先进行排序 我们可以采取枚举的方法进行解…...

webpack03

什么是source-map 将代码编译压缩之后&#xff0c;&#xff0c;可以通过source-map映射会原来的代码&#xff0c;&#xff0c;&#xff0c;在调试的时候可以准确找到原代码报错位置&#xff0c;&#xff0c;&#xff0c;进行修改 source-map有很多值&#xff1a; eval &#…...

组会 | SNN 的 BPTT(backpropagation through time)

目录 1 神经学基础知识1.1 神经元1.2 神经元之间的连接1.3 膜电位1.4 去极化与超极化 2 SNN2.1 LIF 模型2.2 BPTT 中存在的问题2.3 梯度爆炸或消失问题 前言&#xff1a; 本博仅为组会总结&#xff0c;如有谬误&#xff0c;请不吝指正&#xff01;虽然标题为 BPTT&am…...

CDA数据分析师一级经典错题知识点总结(3)

1、SEMMA 的基本思想是从样本数据开始&#xff0c;通过统计分析与可视化技术&#xff0c;发现并转换最有价值的预测变量&#xff0c;根据变量进行构建模型&#xff0c;并检验模型的可用性和准确性。【强调探索性】 2、CRISP-DM模型Cross Industry Standard Process of Data Mi…...

django基于Python的电影推荐系统

Django 基于 Python 的电影推荐系统 一、系统概述 Django 基于 Python 的电影推荐系统是一款利用 Django 框架开发的智能化应用程序&#xff0c;旨在为电影爱好者提供个性化的电影推荐服务。该系统通过收集和分析用户的观影历史、评分数据、电影的属性信息&#xff08;如类型…...

JVM与Java体系结构

一、前言: Java语言和JVM简介: Java是目前最为广泛的软件开发平台之一。 JVM:跨语言的平台 随着Java7的正式发布&#xff0c;Java虚拟机的设计者们通过JSR-292规范基本实现在Java虚拟机平台上运行非Java语言编写的程序。 Java虚拟机根本不关心运行在其内部的程序到底是使用何…...

网络授时笔记

SNTP的全称是Simple Network Time Protocol&#xff0c;意思是简单网络时间协议&#xff0c;用来从网络中获取当前的时间&#xff0c;也可以称为网络授时。项目中会使用LwIP SNTP模块从服务器(pool.ntp.org)获取时间 我们使用sntp例程&#xff0c;sntp例程路径为D:\Espressif\…...

【CSS】HTML页面定位CSS - position 属性 relative 、absolute、fixed 、sticky

目录 relative 相对定位 absolute 绝对定位 fixed 固定定位 sticky 粘性定位 position&#xff1a;relative 、absolute、fixed 、sticky &#xff08;四选一&#xff09; top&#xff1a;距离上面的像素 bottom&#xff1a;距离底部的像素 left&#xff1a;距离左边的像素…...

spark汇总

目录 描述运行模式1. Windows模式代码示例 2. Local模式3. Standalone模式 RDD描述特性RDD创建代码示例&#xff08;并行化创建&#xff09;代码示例&#xff08;读取外部数据&#xff09;代码示例&#xff08;读取目录下的所有文件&#xff09; 算子DAGSparkSQLSparkStreaming…...

【Rust自学】11.5. 在测试中使用Result<T, E>

喜欢的话别忘了点赞、收藏加关注哦&#xff0c;对接下来的教程有兴趣的可以关注专栏。谢谢喵&#xff01;(&#xff65;ω&#xff65;) 11.5.1. 测试函数返回值为Result枚举 到目前为止&#xff0c;测试运行失败的原因都是因为触发了panic&#xff0c;但可以导致测试失败的…...

Sping Boot教程之五十四:Spring Boot Kafka 生产者示例

Spring Boot Kafka 生产者示例 Spring Boot 是 Java 编程语言中最流行和使用最多的框架之一。它是一个基于微服务的框架&#xff0c;使用 Spring Boot 制作生产就绪的应用程序只需很少的时间。Spring Boot 可以轻松创建独立的、生产级的基于 Spring 的应用程序&#xff0c;您可…...

设计模式-结构型-组合模式

1. 什么是组合模式&#xff1f; 组合模式&#xff08;Composite Pattern&#xff09; 是一种结构型设计模式&#xff0c;它允许将对象组合成树形结构来表示“部分-整体”的层次结构。组合模式使得客户端对单个对象和组合对象的使用具有一致性。换句话说&#xff0c;组合模式允…...

基于Java的推箱子游戏设计与实现

基于Java的推箱子游戏设计与实现 摘 要 社会在进步&#xff0c;人们生活质量也在日益提高。高强度的压力也接踵而来。社会中急需出现新的有效方式来缓解人们的压力。此次设计符合了社会需求&#xff0c;Java推箱子游戏可以让人们在闲暇之余&#xff0c;体验游戏的乐趣。具有…...

Spark vs Flink分布式数据处理框架的全面对比与应用场景解析

1. 引言 1.1 什么是分布式数据处理框架 随着数据量的快速增长&#xff0c;传统的单机处理方式已经无法满足现代数据处理需求。分布式数据处理框架应运而生&#xff0c;它通过将数据分片分布到多台服务器上并行处理&#xff0c;提高了任务的处理速度和效率。 分布式数据处理框…...

python_excel列表单元格字符合并、填充、复制操作

读取指定sheet页&#xff0c;根据规则合并指定列&#xff0c;填充特定字符&#xff0c;删除多余的列&#xff0c;每行复制四次&#xff0c;最后写入新的文件中。 import pandas as pd""" 读取指定sheet页&#xff0c;根据规则合并指定列&#xff0c;填充特定字…...

nums[:]数组切片

问题&#xff1a;给定一个整数数组 nums&#xff0c;将数组中的元素向右轮转 k 个位置&#xff0c;其中 k 是非负数。 使用代码如下没有办法通过测试示例&#xff0c;必须将最后一行代码改成 nums[:]nums[-k:]nums[:-k]切片形式&#xff1a; 原因&#xff1a;列表的切片操作 …...

【Arthas 】Can not find Arthas under local: /root/.arthas/lib 解决办法

报错 [INFO] JAVA_HOME: /opt/java/openjdk [INFO] arthas-boot version: 4.0.4 [INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER. [1]: 12 org.springframework.boot.loader.JarLauncher 1 [ER…...

录用率23%!CCF推荐-B类,Early Access即可被SCI数据库收录,中美作者占比过半

International Journal of Human-Computer Interaction&#xff08;IJHCI&#xff09;创刊于1989年&#xff0c;由泰勒-弗朗西斯&#xff08;Taylor & Francis, Inc.&#xff09;出版&#xff0c;主要发表关于交互式计算&#xff08;认知和人体工程学&#xff09;、数字无障…...

做网站三级等保多少钱/南宁seo外包要求

本文进行Linux内核的移植。 1 Linux内核简介 官网&#xff1a;https://www.kernel.org/ NXP 会从linux内核官网下载某个版本&#xff0c;然后将其移植到自己的 CPU上&#xff0c;测试成功后就会将其开放给NXP的CPU开发者。开发者下载 NXP 提供的 Linux 内核&#xff0c;然后将…...

重庆建设机电网站/网络推广运营途径

最近在使用GridView做一个小项目&#xff0c;以下是本人使用过程中的个人总结&#xff0c;本文主要总结控件的属性设置&#xff0c;附上图片&#xff0c;给大家一个参考。后续会给大家分享功能实现和使用的小技巧。 GirdControl是数据的容器&#xff0c;它包含多种显示方式&…...

重庆建设工程造价管理协会网站/长沙网站seo排名

文章目录[点击展开](?)[] Unity中foreach会增加GC unity中for效率比foreach高&#xff1f; 在unity中使用foreach遍历集合会增加gc alloc&#xff0c;参考的话题&#xff1a;作为Unity3D的脚本而言&#xff0c;c#中for是否真的比foreach效率更高&#xff1f; foreach造成gc al…...

怎样做网站制作团队/sem竞价培训

查看前端面试题小程序 大量面试题和答案&#xff0c;请微信查看 var array1 [ {“Num”: "A " },{“Num”: “B” }]; var array2 [ {"Num": "A ","Name": "t1 " }, {"Num": "B","Name": …...

股票网站排名哪个好/关键词优化seo优化排名

由于自己重装了系统&#xff0c;所以需要重新配置环境变量&#xff0c;安装软件&#xff0c;所以记录一下。选择最下面的window x64进行下载&#xff0c;下载到本地后点击运行&#xff1a;之后傻瓜安装一直next就可以了&#xff0c;其实也不需要刻意把jdk换到其他盘&#xff0c…...

政府网站信息建设经验/b2b平台有哪些平台

本文实例为大家分享了python实现五子棋小程序的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下一、结合书上例子&#xff0c;分三段编写&#xff1a;wuziqi.py#coding:utf-8from win_notwin import *from show_qipan import *maxx10 #10行10列maxy10qipan[[0,0,0,0,1…...