第4课 FFmpeg读取本地mp4文件并显示

在上节课,我们使用FFmpeg实现了一个最简单的rtmp播放器,它看起来工作正常。这节课,我们尝试让它来播放本地的mp4文件试试。

1.压缩备份上节课工程文件夹为demo3.rar,并修改工程文件夹demo3为demo4,重要的事情再说一遍:及时备份源文件并在原基础上继续迭代开发是一种好习惯。

将原rtmp地址修改为本地mp4地址:

cpp 复制代码
const char *inFileName = "d:\\mp4\\dtz.mp4";	

调试运行,会发现视频显示一卡一卡,音频也断断续续的。这是什么原因呢?

2.经过很长时间的研究学习,我才发现:原来是流的时间基与当前ffmpeg的时间基不一致造成的。根据以上信息,将延时函数修改如下:

cpp 复制代码
//延时以使当前视频记录的播放时间与实际时间同步
if (normalPkt.stream_index == videoIndex)
{
	AVRational videoTimeBase = inFormatCtx->streams[videoIndex]->time_base;
	AVRational currentTimeBase = { 1, AV_TIME_BASE };
	//计算视频播放时间
	int64_t videoTime = av_rescale_q(normalPkt.dts, videoTimeBase, currentTimeBase);
	//计算实际视频的播放时间
	int64_t currentTime = av_gettime() - startTime;
	if (videoTime > currentTime) {
av_usleep((unsigned int)(videoTime - currentTime));
	}
}

//延时以使当前音频记录的播放时间与实际时间同步
if (normalPkt.stream_index == audioIndex)
{
	AVRational audioTimeBase = inFormatCtx->streams[audioIndex]->time_base;
	AVRational currentTimeBase = { 1, AV_TIME_BASE };
	//计算视频播放时间
	int64_t audioTime = av_rescale_q(normalPkt.dts, audioTimeBase, currentTimeBase);
	//计算实际视频的播放时间
	int64_t currentTime = av_gettime() - startTime;
	if (audioTime > currentTime) {
av_usleep((unsigned int)(audioTime - currentTime));
	}
}

3.再次调试运行,mp4视频部分看起来播放正常了。换个mp4试试,好象也正常,但世事哪有那么简单,声音听起来总是有些杂音,这又是怎么回事呢?又经过很长时间的研究学习,我发现:原来是音频流的采样率与扬声器的采样率不一致造成的。要保证音频流听起来正常,就需要保证打开的扬声器的采样率及解码转换后的采样率保证一致才可以。比如,如果mp4文件中音频的采样率为44100,则以下两处的采样率也要做相应修改:

cpp 复制代码
//将音频帧转换到数组
int fmlp::convertAudioFrameToAudioBuff(AVFrame*frame, char**pBuf, int&len){
	int outSampleNum = 0;
	SwrContext* audioSwrCtx = NULL;
	audioSwrCtx = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, AVSampleFormat::AV_SAMPLE_FMT_S16, 44100, AV_CH_LAYOUT_STEREO, (AVSampleFormat)frame->format, frame->sample_rate, NULL, NULL);
	swr_init(audioSwrCtx);
	outSampleNum = swr_convert(audioSwrCtx, (uint8_t**)pBuf, len / frame->channels / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16), (const uint8_t**)frame->data, frame->nb_samples);
	swr_free(&audioSwrCtx);
	return outSampleNum;

}
cpp 复制代码
//打开扬声器
void fmlp::openSpeaker(){
	outWaveform.wFormatTag = WAVE_FORMAT_PCM;
	outWaveform.nSamplesPerSec = 44100;
	outWaveform.wBitsPerSample = 16;
	outWaveform.nChannels = 2;
	//waveform.nBlockAlign = (waveform.wBitsPerSample * waveform.nChannels) / 8;
	outWaveform.nBlockAlign = (outWaveform.wBitsPerSample*outWaveform.nChannels) >> 3;
	outWaveform.nAvgBytesPerSec = outWaveform.nBlockAlign * outWaveform.nSamplesPerSec;
	outWaveform.cbSize = 0;

	waveOutOpen(&hWaveOut, WAVE_MAPPER, &outWaveform, (DWORD)(speakerCallback), 0L, CALLBACK_FUNCTION);
	waveOutSetVolume(hWaveOut, 4 * 0xffffffff);
	waveHdrArr = new WAVEHDR[audioDataArrNum];
	for (int i = 0; i < audioDataArrNum; i++)
	{
		waveHdrArr[i].lpData = new char[finalAudioDataSize];
		waveHdrArr[i].dwBufferLength = finalAudioDataSize;
		waveHdrArr[i].dwBytesRecorded = 0;
		waveHdrArr[i].dwUser = 0;
		waveHdrArr[i].dwFlags = 0;
		waveHdrArr[i].dwLoops = 0;
		waveHdrArr[i].lpNext = NULL;
		waveHdrArr[i].reserved = 0;
		waveOutPrepareHeader(hWaveOut, &waveHdrArr[i], sizeof(WAVEHDR));
	}

}

4.再次调试运行,视频和音频都能正常播放了。

写这篇教程用了不到一个小时,但问题的排查却历尽艰辛,各位同行都有类似的经历吧。

相关推荐
编程小白20262 分钟前
从 C++ 基础到效率翻倍:Qt 开发环境搭建与Windows 神级快捷键指南
开发语言·c++·windows·qt·学习
Sagittarius_A*14 分钟前
特征检测:SIFT 与 SURF(尺度不变 / 加速稳健特征)【计算机视觉】
图像处理·人工智能·python·opencv·计算机视觉·surf·sift
浩瀚之水_csdn27 分钟前
av_packet_alloc详解
ffmpeg
.小墨迹40 分钟前
apollo学习之借道超车的速度规划
linux·c++·学习·算法·ubuntu
历程里程碑1 小时前
Linux20 : IO
linux·c语言·开发语言·数据结构·c++·算法
郝学胜-神的一滴1 小时前
深入浅出:使用Linux系统函数构建高性能TCP服务器
linux·服务器·开发语言·网络·c++·tcp/ip·程序人生
天若有情6731 小时前
【自研实战】轻量级ASCII字符串加密算法:从设计到落地(防查岗神器版)
网络·c++·算法·安全·数据安全·加密
czy87874752 小时前
深入了解 C++ 中的 `std::bind` 函数
开发语言·c++
我在人间贩卖青春2 小时前
C++之继承的方式
c++·private·public·protected·继承方式
nLsUCWFJR3 小时前
(Matlab)基于贝叶斯优化卷积双向长短期记忆网络(CNN-BiLSTM)回归预测
opencv