ffmpeg音频解码重采样(立体声转成单声道)
https://blog.csdn.net/qq_37003193/article/details/93870141
PCM双声道分离为单声道
https://blog.csdn.net/chinabinlang/article/details/72932947?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_baidulandingword-0&spm=1001.2101.3001.4242
#include <iostream>
#include <string>
extern "C" // 因为FFmpeg是纯C程序
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
};
using namespace std;
#define FRAME_SIZE 3200
#define SAMPLE_RATE 16000
int main()
{AVCodecContext *pCodecCtx;AVCodec *pCodec;AVPacket packet;char *input = "/workspace/src/ffmpegdemo/data/3.mp4";enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;av_register_all();avformat_network_init();AVFormatContext* format = avformat_alloc_context();// 打开input文件if (avformat_open_input(&format, input, NULL, NULL) != 0) {return NULL;}if (avformat_find_stream_info(format, NULL) < 0) {return NULL;}// 找到input文件中的音频流int stream_index =- 1;for (int i=0; i<format->nb_streams; i++) {if (format->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {stream_index = i;break;}}if (stream_index == -1) {return NULL;}AVStream* stream = format->streams[stream_index];pCodecCtx = format->streams[stream_index]->codec;// 找到解码器pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if(pCodec == NULL){printf("Codec not found.\n");}if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){printf("Could not open codec.\n");}// 准备重采样struct SwrContext* swr = swr_alloc();av_opt_set_int(swr, "in_channel_count", pCodecCtx->channels, 0);av_opt_set_int(swr, "out_channel_count", 1, 0);av_opt_set_int(swr, "in_channel_layout", pCodecCtx->channel_layout, 0);av_opt_set_int(swr, "out_channel_layout", AV_CH_LAYOUT_MONO, 0);av_opt_set_int(swr, "in_sample_rate", pCodecCtx->sample_rate, 0);av_opt_set_int(swr, "out_sample_rate", SAMPLE_RATE, 0);av_opt_set_sample_fmt(swr, "in_sample_fmt", pCodecCtx->sample_fmt, 0);av_opt_set_sample_fmt(swr, "out_sample_fmt", out_sample_fmt, 0);swr_init(swr);if (!swr_is_initialized(swr)) {fprintf(stderr, "Resampler has not been properly initialized\n");return NULL;}av_init_packet(&packet);// 初始化frameAVFrame* frame = av_frame_alloc();if (!frame) {fprintf(stderr, "Error allocating the frame\n");return NULL;}char *data = NULL;while (av_read_frame(format, &packet) >= 0) {// 判断是否为音频if (packet.stream_index != stream_index){continue;}int gotFrame;// 解码一帧音频if (avcodec_decode_audio4(pCodecCtx, frame, &gotFrame, &packet) < 0) {break;}if (!gotFrame) {continue;}uint8_t* buffer;av_samples_alloc((uint8_t**) &buffer, NULL, 1, frame->nb_samples,out_sample_fmt, 0);// 重采样int frame_count = swr_convert(swr, (uint8_t**) &buffer, frame->nb_samples, (const uint8_t**) frame->data, frame->nb_samples);data = (char*) realloc(data, frame_count * sizeof(char));// 采样完的音频数据复制到datamemcpy(data, buffer, frame_count * sizeof(char));}av_frame_free(&frame);swr_free(&swr);avcodec_close(pCodecCtx);avformat_free_context(format);
}
ffmpeg音频解码重采样(立体声转成单声道)相关推荐
- 杰理之做蓝牙发射时,将立体声修改成单声道差分输出时,接收端出现卡音【篇】
- ffmpeg 音频解码一
1. ffmpeg 视频解码一 2. ffmpeg 视频解码二 3. ffmpeg 音频解码一 4. ffmpeg 音频解码二 5. ffmpeg 音视频解码 6. ffmpeg 视频编码一 7. f ...
- ffmpeg中音频解码方法(附代码)+ffmpeg音频解码播放速度快的问题(随手笔记,以供查阅)
最近在做一款取名为变速不变调播放器的时候,解码音频遇到了些问题(ffmpeg音频解码播放速度快的问题),网络上的方法对绝大多数的音视频文件有效,但是对于某些音频会有问题,比如某些ADPCM编码的WAV ...
- ffmpeg 音频解码二
1. ffmpeg 视频解码一 2. ffmpeg 视频解码二 3. ffmpeg 音频解码一 4. ffmpeg 音频解码二 5. ffmpeg 音视频解码 6. ffmpeg 视频编码一 7. f ...
- FFMPEG音频解码浅析
结合各种资料和自己的理解,估计有些浅显. FFMPEG解码流程: 1. 注册所有容器格式和CODEC: av_register_all() 2. 打开文件: av_open_input_file() ...
- 深入浅出:FFmpeg 音频解码与处理AVFrame全解析
深入浅出:FFmpeg 音频解码与处理全解析 一.FFmpeg 简介 1.1 FFmpeg 的历史与发展 1.2 FFmpeg 的主要组成部分 二.音频编解码基础 (Basics of Audio E ...
- FFmpeg音频解码流程详解及简单demo参考
本文主要讲解FFmpeg的音频解码具体流程,API使用.最后再以一个非常简单的demo演示将一个mp3格式的音频文件解码为原始数据pcm文件. 本文主要基于FFmpeg音频解码新接口. 一.FFmpe ...
- PP视频怎么把输出声音设置成单声道输出
在使用PP视频看电影时,怎么把输出声音设置成单声道输出呢?下面小编就介绍下方法. 1.先安装PP视频安装,然后点击快捷键启动. 2.进入到主页后点击右上角的三角形图标. 3.我们点击窗口中的" ...
- Android NDK开发之旅31 FFmpeg音频解码
###前言 #####基于Android NDK开发之旅30--FFmpeg视频播放这篇文章,我们已经学会视频解码基本过程.这篇文章就对音频解码进行分析. #####音频解码和视频解码的套路基本是一样 ...
最新文章
- 解决 C3P0 中文乱码问题
- 解决git push 中remote: Permission to xxxxx.git denied to xxx. fatal: unable to access xxxx 403(转)
- 如何设置ad18捕捉图标_图标设计中的像素捕捉
- binarysearch java,java数据结构之二分查找法 binarySearch的实例
- groovy 兼容 java_java – eclipse插件和maven依赖项中存在的’groovy-all’jar之间的兼容性问题...
- Java 基础类型int 与 Integer
- leetcode--57--Insert Interval
- C++11中内联函数(inline)
- 2021信创产业分类排行
- android表白代码大全,微信表白代码汇总:
- bootdo图片上传
- JD-GUI工具下载安装
- Centos7.5 BCM4322无线网卡驱动安装踩坑记录
- 低代码”革了谁的命?
- SAP 第四代增强-BTE
- java xmap_xml和java Bean的直接转换,Xmap
- 在家用电饼铛自制潮汕美食肠粉过程,想学的赶紧来看
- 猿创征文|我的四个月Java学习成长之路——从基础到框架再到项目
- 域管理:windows server分发与分配软件
- DB2存储过程——begin end的作用