FFmpeg菜鸡互啄#第4篇#音频解码
时间:2022-05-03
本文章向大家介绍FFmpeg菜鸡互啄#第4篇#音频解码,主要内容包括解码过程、Code、Github、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
解码过程
音频解码跟上一篇的视频解码过程是一样的:打开输入文件,查找音频流,打开解码器,循环读帧解码帧,关闭解码器,关闭输入文件。
Code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/*
#define __STDC_CONSTANT_MACROS
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
*/
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavdevice/avdevice.h"
}
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")
#define INPUT "in.mkv"
#define OUTVIDEO "video.yuv"
#define OUTAUDIO "audio.pcm"
int main()
{
int res = 0;
int videoStream = -1;//标记视频流的编号
int audioStream = -1;//标记音频流的编号
char errBuf[BUFSIZ] = { 0 };
FILE* fp_video = fopen(OUTVIDEO, "wb+");
FILE* fp_audio = fopen(OUTAUDIO, "wb+");
//初始化FFMPEG 调用了这个才能正常适用编码器和解码器
av_register_all();
printf("FFmpeg's version is: %dn", avcodec_version());
//FFMPEG所有的操作都要通过这个AVFormatContext来进行
AVFormatContext* pFormatCtx = NULL;
//打开输入视频文件
//Open an input stream and read the header. The codecs are not opened.
if ((res = avformat_open_input(&pFormatCtx, INPUT, NULL, NULL)) < 0)
{
av_strerror(res, errBuf, sizeof(errBuf));
printf("%sn", errBuf);
return -1;
}
//Read packets of a media file to get stream information. This is useful for file formats with no headers such as MPEG.
//相当于对输入进行 “预处理”
avformat_find_stream_info(pFormatCtx, NULL);
av_dump_format(pFormatCtx, 0, NULL, 0); //输出视频流的信息
//查找流
for (int i = 0; i < pFormatCtx->nb_streams; ++i)
{
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
videoStream = i;
else if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
audioStream = i;
}
if (videoStream == -1)
{
printf("Didn't find a video stream.n");
return -1;
}
if (audioStream == -1)
{
printf("Didn't find a audio stream.n");
return -1;
}
///查找解码器
AVCodecContext* pVCodecCtx = pFormatCtx->streams[videoStream]->codec;
AVCodec* pVCodec = avcodec_find_decoder(pVCodecCtx->codec_id);
if (pVCodec == NULL)
{
printf("Video Codec not found.n");
return -1;
}
AVCodecContext* pACodecCtx = pFormatCtx->streams[audioStream]->codec;
AVCodec* pACodec = avcodec_find_decoder(pACodecCtx->codec_id);
if (pACodec == NULL)
{
printf("Audio Codec not found.n");
return -1;
}
///打开解码器
if (avcodec_open2(pVCodecCtx, pVCodec, NULL) < 0)
{
printf("Could not open Video codec.n");
return -1;
}
if (avcodec_open2(pACodecCtx, pACodec, NULL) < 0)
{
printf("Could not open Audio codec.n");
return -1;
}
AVFrame Frame = { 0 };//不初始化,avcodec_decode_video2会报错
AVPacket packet;
int got_picture;
while (1)
{
//读取视频帧
//return 0 if OK, < 0 on error or end of file
if (av_read_frame(pFormatCtx, &packet) < 0)
{
break; //这里认为视频读取完了
}
if (packet.stream_index == videoStream)
{
//解码视频帧
if (avcodec_decode_video2(pVCodecCtx, &Frame, &got_picture, &packet) < 0)
{
printf("decode Video error.n");
return -1;
}
if (got_picture)
{
if (Frame.format == PIX_FMT_YUV420P)
{
//解码后YUV格式的视频像素数据保存在AVFrame的data[0]、data[1]、data[2]中。
//但是这些像素值并不是连续存储的,每行有效像素之后存储了一些无效像素。
//以亮度Y数据为例,data[0]中一共包含了linesize[0] * height个数据。
//但是出于优化等方面的考虑,linesize[0]实际上并不等于宽度width,而是一个比宽度大一些的值。
fwrite(Frame.data[0], Frame.linesize[0] * Frame.height, 1, fp_video);
fwrite(Frame.data[1], Frame.linesize[1] * Frame.height / 2, 1, fp_video);
fwrite(Frame.data[2], Frame.linesize[2] * Frame.height / 2, 1, fp_video);
}
}
}
else if (packet.stream_index == audioStream)
{
//解码音频帧
if (avcodec_decode_audio4(pACodecCtx, &Frame, &got_picture, &packet) < 0)
{
printf("decode Audio error.n");
return -1;
}
if (got_picture)
{
if (Frame.format == AV_SAMPLE_FMT_S16P)//signed 16 bits, planar 16位 平面数据
{
//AV_SAMPLE_FMT_S16P
//代表每个data[]的数据是连续的(planar),每个单位是16bits
for (int i = 0; i < Frame.linesize[0]; i += 2)
{
//如果是多通道的话,保存成c1低位、c1高位、c2低位、c2高位...
for (int j = 0; j < Frame.channels; ++j)
fwrite(Frame.data[j] + i, 2, 1, fp_audio);
}
}
else if (Frame.format == AV_SAMPLE_FMT_FLTP)
{
for (int i = 0; i < Frame.linesize[0]; i += 4)
{
for (int j = 0; j < Frame.channels; ++j)
fwrite(Frame.data[j] + i, 4, 1, fp_audio);
}
}
}
}
av_free_packet(&packet);//清除packet里面指向的缓冲区
}
fclose(fp_video);
fclose(fp_audio);
avcodec_close(pVCodecCtx);//关闭解码器
avcodec_close(pACodecCtx);
avformat_close_input(&pFormatCtx);//关闭输入视频文件。avformat_free_context(pFormatCtx);就不需要了
return 0;
}
保存的音频PCM可以用Audacity进行播放。我的例子里AV_SAMPLE_FMT_FLTP出现了播放有很大杂音的情况,一直没找到解决办法。因为大部分都是把AV_SAMPLE_FMT_FLTP转换成(重采样)AV_SAMPLE_FMT_S16P。
Github
https://github.com/gongluck/FFmpegTest.git
- 使用jQuery Validation插件来验证表单
- 如何用Wireshark捕获USB数据?
- QEMU 2: 参数解析
- [WCF安全系列]认证与凭证:X.509证书
- 深度学习:能击败欧洲围棋冠军,还能防恶意软件
- Metasploit中的JAVA反向TCP做法的研究
- 商业级别Fortify白盒神器介绍与使用分析
- [WCF安全系列]消息的保护等级[上篇]
- QEMU 1: 使用QEMU创建虚拟机
- [WCF安全系列]绑定、安全模式与客户端凭证类型:NetNamedPipeBinding、NetTcpBinding与NetMsmqBinding
- 操作系统级虚拟化概述
- 让javascript中的异步请求同步起来
- [WCF REST] WebHttpBinding与消息编码
- React 概要
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法