ffmpeg h264文件转mp4

news2024/10/5 18:27:28

h264文件不能直接在网页上播放,比如在浏览器上输入http://10.0.0.2/2022-01-08T22-32-58.h264,变成了下载。

若在浏览器上输入http://10.0.0.2/2022-01-08T22-32-58.mp4,则可以播放。

本文讲解用ffmpeg将h264文件转换成mp4。

首先,准备h264文件,这个可以用ffmpeg将一个mp4的视频部分转成h264,命令如下:

ffmpeg -i 2022-01-08T22-32-58.mp4 -an -vcodec copy 2022-01-08T22-32-58.h264

注意,我这里mp4里面的视频编码格式是h264,故用的vcodec copy,否则用vcodec libx264。

下面是将这个2022-01-08T22-32-58.h264封装成mp4,注意,没有解码编码过程,速度会很快。
大概过程如下:
1.启动两个线程
2.第一个线程av_read_frame读到packet,然后av_packet_clone拷贝出一个packet,塞入队列
3.第二个线程从队列中读取packet,进行pts,dts,duration设置,然后调用av_interleaved_write_frame写入。

这里面需要注意的地方在于pts,dts的设置,从ffmpeg源码上看,对于pts,dts的设置,也是很麻烦的,源码中,很多地方,动不动就看pts,dts是否需要设置。

本人之前采取下列方式进行dts和pts的设置,即由于读的context和写入的context的时间基(time_base)不一致,需要转换成目标文件context的时间基。

pPacketVideoA->dts = av_rescale_q_rnd(pPacketVideoA->dts, m_pFormatCtx_File->streams[m_iVideoIndex]->time_base, m_pFormatCtx_Out->streams[m_iVideoIndex]->time_base, AVRounding(1));

但实际运行时,出现了问题,读取出来的packet的pts和dts都是AV_NOPTS_VALUE,经过转换还是AV_NOPTS_VALUE,然后调用av_interleaved_write_frame出错。

最后调试ffmpeg源码,得到pts和dts的处理如下:

pPacketVideoA->duration = av_rescale_q_rnd(pPacketVideoA->duration, m_pFormatCtx_File->streams[m_iVideoIndex]->time_base, m_pFormatCtx_Out->streams[m_iVideoIndex]->time_base, AVRounding(1));
if (pPacketVideoA->dts != AV_NOPTS_VALUE)
{
	pPacketVideoA->dts = av_rescale_q_rnd(pPacketVideoA->dts, m_pFormatCtx_File->streams[m_iVideoIndex]->time_base, m_pFormatCtx_Out->streams[m_iVideoIndex]->time_base, AVRounding(1));
	pPacketVideoA->pts = pPacketVideoA->dts;
}
else
{
	pPacketVideoA->pts = pPacketVideoA->dts = m_iTotalDuration;
}
m_iTotalDuration += pPacketVideoA->duration;

m_iTotalDuration的初始值,本人设置的是0,即第一帧的pts和dts都设置成0,下一帧的dts就是前面总帧的duration和,这个是ffmpeg里面的逻辑。

当然,ffmpeg里面判断复杂多了,比如参考了是否有B帧,本人所写的没考虑B帧,只考虑一个文件里面只有I帧,P帧的情况。

下面是代码结构
在这里插入图片描述
其中FfmpegH264ToMp4.cpp的内容如下:

// FfmpegH264ToMp4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "H264ToMp4.h"

int main()
{
	CVideoConvert cVideoConvert;

	std::string strVideoFile = "2022-01-08T22-32-58.h264";
	std::string strOutFile = "out.mp4";
	cVideoConvert.StartConvert(strVideoFile, strOutFile);
	cVideoConvert.WaitFinish();
    return 0;
}



H264ToMp4.h里面的内容如下:

#pragma once
#include <string>

#include <Windows.h>
#include <deque>

#define MAX_VIDEOPACKET_NUM 100

#ifdef	__cplusplus
extern "C"
{
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavdevice/avdevice.h"
#include "libavutil/audio_fifo.h"
#include "libavutil/avutil.h"
#include "libavutil/fifo.h"
#include "libavutil/frame.h"
#include "libavutil/imgutils.h"

#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"


#ifdef __cplusplus
};
#endif

class CVideoConvert
{
public:
	CVideoConvert();
	~CVideoConvert();
public:
	int StartConvert(std::string strFileName, std::string strFileOut);
	void StopConvert();
public:
	int OpenFile(const char *pFile);
	int OpenOutPut(const char *pFileOut);
private:
	static DWORD WINAPI FileReadProc(LPVOID lpParam);
	void FileRead();

	static DWORD WINAPI FileWriteProc(LPVOID lpParam);
	void FileWrite();

public:
	void WaitFinish();
public:
	AVFormatContext *m_pFormatCtx_File = NULL;
	AVFormatContext *m_pFormatCtx_Out = NULL;

	std::deque<AVPacket *> m_vecReadVideo;


	int m_iVideoWidth = 1920;
	int m_iVideoHeight = 1080;
	int m_iYuv420FrameSize = 0;
	int m_iVideoFrameNum = 0;

	int m_iVideoIndex = -1;
	bool m_bStartConvert = false;
	int64_t m_iTotalDuration = 0;
private:
	CRITICAL_SECTION m_csVideoReadSection;
	HANDLE m_hFileReadThread = NULL;
	HANDLE m_hFileWriteThread = NULL;
};


H264ToMp4.cpp里面的内容如下:

#include "H264ToMp4.h"


#ifdef	__cplusplus
extern "C"
{
#endif

#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")


#ifdef __cplusplus
};
#endif

CVideoConvert::CVideoConvert()
{
	InitializeCriticalSection(&m_csVideoReadSection);
}

CVideoConvert::~CVideoConvert()
{
	DeleteCriticalSection(&m_csVideoReadSection);
}

int CVideoConvert::StartConvert(std::string strFileName, std::string strFileOut)
{
	int ret = -1;
	do
	{
		ret = OpenFile(strFileName.c_str());
		if (ret != 0)
		{
			break;
		}

		ret = OpenOutPut(strFileOut.c_str());
		if (ret != 0)
		{
			break;
		}

		if (ret < 0)
		{
			break;
		}

		m_bStartConvert = true;
		m_hFileReadThread = CreateThread(NULL, 0, FileReadProc, this, 0, NULL);
		m_hFileWriteThread = CreateThread(NULL, 0, FileWriteProc, this, 0, NULL);
	} while (0);

	return ret;
}

void CVideoConvert::StopConvert()
{
	m_bStartConvert = false;
}

int CVideoConvert::OpenFile(const char *pFile)
{
	int ret = -1;

	do
	{
		if ((ret = avformat_open_input(&m_pFormatCtx_File, pFile, 0, 0)) < 0) {
			printf("Could not open input file.");
			break;
		}
		if ((ret = avformat_find_stream_info(m_pFormatCtx_File, 0)) < 0) {
			printf("Failed to retrieve input stream information");
			break;
		}
		if (m_pFormatCtx_File->nb_streams > 2)
		{
			break;
		}
		for (int i = 0; i < m_pFormatCtx_File->nb_streams; i++)
		{
			if (m_pFormatCtx_File->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
			{
				m_iVideoIndex = i;
			}
		}

		if (m_iVideoIndex == -1)
		{
			break;
		}

		ret = 0;
	} while (0);


	return ret;
}


int CVideoConvert::OpenOutPut(const char *pFileOut)
{
	int iRet = -1;

	AVStream *pVideoStream = NULL;
	do
	{
		avformat_alloc_output_context2(&m_pFormatCtx_Out, NULL, NULL, pFileOut);
		if (m_iVideoIndex != -1)
		{
			AVCodecID eTargetVideoCodecId = m_pFormatCtx_File->streams[m_iVideoIndex]->codecpar->codec_id;
			AVCodec* pCodecEncode_Video = (AVCodec *)avcodec_find_encoder(eTargetVideoCodecId);
			pVideoStream = avformat_new_stream(m_pFormatCtx_Out, pCodecEncode_Video);
			if (!pVideoStream)
			{
				break;
			}

			avcodec_parameters_copy(pVideoStream->codecpar, m_pFormatCtx_File->streams[m_iVideoIndex]->codecpar);
			pVideoStream->codecpar->codec_tag = 0;
		}

		if (!(m_pFormatCtx_Out->oformat->flags & AVFMT_NOFILE))
		{
			if (avio_open(&m_pFormatCtx_Out->pb, pFileOut, AVIO_FLAG_WRITE) < 0)
			{
				break;
			}
		}

		if (avformat_write_header(m_pFormatCtx_Out, NULL) < 0)
		{
			break;
		}

		iRet = 0;
	} while (0);


	if (iRet != 0)
	{
		if (m_pFormatCtx_Out != NULL)
		{
			avformat_free_context(m_pFormatCtx_Out);
			m_pFormatCtx_Out = NULL;
		}
	}

	return iRet;
}

DWORD WINAPI CVideoConvert::FileReadProc(LPVOID lpParam)
{
	CVideoConvert *pVideoConvert = (CVideoConvert *)lpParam;
	if (pVideoConvert != NULL)
	{
		pVideoConvert->FileRead();
	}
	return 0;
}

void CVideoConvert::FileRead()
{
	AVPacket packet = { 0 };
	int ret = 0;
	int iPicCount = 0;
	const int genpts = m_pFormatCtx_File->flags & AVFMT_FLAG_GENPTS;
	while (m_bStartConvert)
	{
		av_packet_unref(&packet);

		ret = av_read_frame(m_pFormatCtx_File, &packet);
		if (ret == AVERROR(EAGAIN))
		{
			continue;
		}
		else if (ret == AVERROR_EOF)
		{
			break;
		}
		else if (ret < 0)
		{
			break;
		}

		if (packet.stream_index == m_iVideoIndex)
		{
			while (1)
			{
				int iAudioPacketNum = m_vecReadVideo.size();
				if (iAudioPacketNum >= MAX_VIDEOPACKET_NUM)
				{
					Sleep(10);
					continue;
				}
				else
				{
					AVPacket *pAudioPacket = av_packet_clone(&packet);
					if (pAudioPacket != NULL)
					{
						EnterCriticalSection(&m_csVideoReadSection);
						m_vecReadVideo.push_back(pAudioPacket);
						LeaveCriticalSection(&m_csVideoReadSection);
					}
				}
				break;
			}
		}
		
	}
	//FlushVideoDecoder(iPicCount);
}



DWORD WINAPI CVideoConvert::FileWriteProc(LPVOID lpParam)
{
	CVideoConvert *pVideoConvert = (CVideoConvert *)lpParam;
	if (pVideoConvert != NULL)
	{
		pVideoConvert->FileWrite();
	}
	return 0;
}


void CVideoConvert::FileWrite()
{
	int ret = 0;


	AVPacket packet = { 0 };
	int iRealPicCount = 0;

	while (m_bStartConvert)
	{

		int iVideoSize = m_vecReadVideo.size();

		if (iVideoSize > 0)
		{
			AVPacket *pPacketVideoA = NULL;
			EnterCriticalSection(&m_csVideoReadSection);
			if (!m_vecReadVideo.empty())
			{
				pPacketVideoA = m_vecReadVideo.front();
				m_vecReadVideo.pop_front();
			}
			LeaveCriticalSection(&m_csVideoReadSection);

			pPacketVideoA->stream_index = m_iVideoIndex;
			pPacketVideoA->duration = av_rescale_q_rnd(pPacketVideoA->duration, m_pFormatCtx_File->streams[m_iVideoIndex]->time_base, m_pFormatCtx_Out->streams[m_iVideoIndex]->time_base, AVRounding(1));
			if (pPacketVideoA->dts != AV_NOPTS_VALUE)
			{
				pPacketVideoA->dts = av_rescale_q_rnd(pPacketVideoA->dts, m_pFormatCtx_File->streams[m_iVideoIndex]->time_base, m_pFormatCtx_Out->streams[m_iVideoIndex]->time_base, AVRounding(1));
				pPacketVideoA->pts = pPacketVideoA->dts;
			}
			else
			{
				pPacketVideoA->pts = pPacketVideoA->dts = m_iTotalDuration;
			}
			m_iTotalDuration += pPacketVideoA->duration;

			//ret = av_write_frame(m_pFormatCtx_Out, pPacketVideoA);
			ret = av_interleaved_write_frame(m_pFormatCtx_Out, pPacketVideoA);
			av_packet_free(&pPacketVideoA);
			if (ret == AVERROR(EAGAIN))
			{
				continue;
			}
			else if (ret == AVERROR_EOF)
			{
				break;
			}
			iRealPicCount++;
		}
		else
		{
			Sleep(10);
		}
	}
	///FlushEncoder(iRealPicCount);
	av_write_trailer(m_pFormatCtx_Out);
	avio_close(m_pFormatCtx_Out->pb);
}

void CVideoConvert::WaitFinish()
{
	int ret = 0;
	do
	{
		if (NULL == m_hFileReadThread)
		{
			break;
		}
		WaitForSingleObject(m_hFileReadThread, INFINITE);

		CloseHandle(m_hFileReadThread);
		m_hFileReadThread = NULL;

		while (m_vecReadVideo.size() > 0)
		{
			Sleep(1000);
		}
		m_bStartConvert = false;
		WaitForSingleObject(m_hFileWriteThread, INFINITE);
		CloseHandle(m_hFileWriteThread);
		m_hFileWriteThread = NULL;
	} while (0);
}



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/350868.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

视频融合 flv流格式对接(上)

FLV 是FLASH VIDEO的简称&#xff0c;FLV流媒体格式是随着Flash MX的推出发展而来的视频格式。由于它形成的文件极小、加载速度极快&#xff0c;使得网络观看视频文件成为可能&#xff0c;它的出现有效地解决了视频文件导入Flash后&#xff0c;使导出的SWF文件体积庞大&#xf…

R-Drop: Regularized Dropout for Neural Networks 论文笔记(介绍,模型结构介绍、代码、拓展KL散度等知识)

目录前言一、摘要二、R-Drop介绍三、R-Drop公式详解四、R-Drop计算流程附录0&#xff1a;代码附录一&#xff1a;熵以及信息熵附录二&#xff1a;KL散度&#xff08;相对熵&#xff09;附录三&#xff1a;JS散度附录四&#xff1a;互信息总结前言 R-Drop——神经网络的正则化Dr…

必看!Salesforce管理员职场如何快速晋升?

2023年的开局略显艰难&#xff0c;在当前的经济环境下&#xff0c;许多行业仍面临挑战。虽然交易周期可能会变得更长&#xff0c;但对新的Salesforce实施仍有巨大需求&#xff0c;现有客户仍然需要经验丰富的专业人员来优化和维护他们的Salesforce组织。 在过去的三年中&#x…

大规模即时云渲染技术,追求体验与成本的最佳均衡

现实世界映射其中&#xff0c;传统文化沉浸其境&#xff0c;旧时记忆交互其间。 仲升&#xff5c;技术作者 IMMENSE&#xff5c;内容编辑 在刚刚过温的春节&#xff0c;云之上&#xff0c;带来了一场「数字文化」新体验。 游花车、舞狮子、踩高跷、放烟花、写福字……还记得儿…

2023最强软件测试面试题,精选100 道,内附答案版,冲刺金3银4

精挑细选&#xff0c;整理了100道软件测试面试题&#xff0c;都是非常常见的面试题&#xff0c;篇幅较长&#xff0c;所以只放出了题目&#xff0c;答案在评论区&#xff01; 测试技术面试题 1、什么是兼容性测试&#xff1f;兼容性测试侧重哪些方面&#xff1f; 2、我现在有…

css复习3

精灵图的使用 为了有效地减少服务器接收和发送请求的次数&#xff0c;提高页面的加载速度&#xff0c;出现了 CSS 精灵技术&#xff08;也称 CSS Sprites、CSS 雪碧&#xff09;。 核心原理&#xff1a;将网页中的一些小背景图像整合到一张大图中 &#xff0c;这样服务器只需要…

新S/MIME标准将于今年九月生效

1月份&#xff0c;行业领导者通过了新的 S/MIME基线要求&#xff0c;旨在规范全球范围内公开信任电子邮件签名证书的颁发和管理。以下是关于此次更新的重点……根据abnormal security发布的报告称&#xff0c;近92%的受访者表示&#xff0c;他们在过去一年中至少经历过一次或多…

全网最全的Ansible中常用模块讲解

目录 前言 一、ansible实现管理的方式 二、Ad-Hoc执行方式中如何获得帮助 三、ansible命令运行方式及常用参数 四、ansible的基本颜色代表信 五、ansible中的常用模块 1、command 2、shell 3、script 4、copy 5、fetch 6、file 7、 unarchive 8、archive 9、h…

Python基础1

1. 注释 单行注释&#xff1a;以#开头。一般建议注释和内容用空格隔开。 多行注释&#xff1a;以一对三个双引号括起来的内容是注释。“““示例注释”””。 2. 数据类型 验证数据类型的方法&#xff1a;type&#xff08;被查看类型的数据&#xff09;。 注意&#xff1a;…

< 每日小技巧:N个很棒的 Vue 开发技巧, 持续记录ing >

每日小技巧&#xff1a;6 个很棒的 Vue 开发技巧&#x1f449; ① Watch 妙用> watch的高级使用> 一个监听器触发多个方法> watch 监听多个变量&#x1f449; ② 自定义事件 $emit() 和 事件参数 $event&#x1f449; ③ 监听组件生命周期常规写法hook写法&#x1f44…

扫码过磅+车牌识别,内蒙古蒙维过磅实现信息化管理

扫码过磅、车牌识别、对接SAP ERP系统设计思路&#xff1a; 无人值守系统升级改造包括车牌自动识别系统、信息化&#xff08;扫码等方式&#xff09;管理系统、智能自动控制系统等实现信息无纸化传递。远程监管地点设于公司东磅房&#xff0c;可以实现远程监测监控画面、称重过…

前端之HTML

一、概念1.页面组成结构&#xff1a;HTML&#xff08;Hyper Text Markup Language--超文本标记语言&#xff09;页面原始和内容 表现&#xff1a;CSS网页原始的外观和位置等页面样式&#xff08;如颜色、大小等&#xff09; 行为&#xff1a;JavaScript网页模型的定义与交互&am…

【仓库管理】搭建 Maven 私服之一--Nexus仓库(Repository)管理软件

文章目录Nexus是什么Nexus下载和安装1. 进入 Nexus 2.x 下载页面&#xff0c;根据本机操作系统&#xff0c;选择对应的版本进行下载&#xff0c;如下图所示。2. 将下载 Nexus 安装包解压到本地磁盘&#xff0c;可获得 nexus-2.14.20-02 和 sonatype-work 2 个目录&#xff0c;如…

Python3,2分钟掌握Doscoart库,你也能成为艺术家。

2行代码绘制水彩画1、引言2、 代码实战2.1 模块介绍2.2 模块安装2.3 代码示例2.3.1 创建默认图片2.3.2 设置参数创建图片2.3.3 查看设置参数2.3.4 查看配置2.3.5 保存配置2.3.6 加载配置2.3.7 导出配置文件2.3.7 生成Python代码2.3.8 调用文档3、总结1、引言 小屌丝&#xff1…

分布式新闻项目实战 - 11.定时计算热点文章(xxl-Job)

男人过了四十&#xff0c;千万要少说话&#xff0c;拉长脸&#xff0c;闭紧嘴&#xff0c;买件立领风衣&#xff0c;浓个眉大个眼&#xff0c;一直走&#xff0c;不要往两边看&#xff0c;还能再混几十年。 —— 冯唐 系列文章目录 项目搭建App登录及网关App文章自媒体平台&am…

DQL 数据查询语言(单表查询)

导入数据 登录mysql数据库管理系统 mysql -uroot -pXXX查看有哪些数据库 show databases; (这个不是SQL语句&#xff0c;属于MySQL的命令。)创建属于我们自己的数据库 create database db1; (这个不是SQL语句&#xff0c;属于MySQL的命令。)使用bjpowernode数据 use db1; …

带你了解达人营销的概况

现在&#xff0c;达人营销的格局在不断变化。社交媒体平台想方设法希望吸引更多用户。如果普通用户的内容能够实现爆炸性传播&#xff0c;他们就可以成为冉冉升起的新星。企业需要尽一切努力保持受众的兴趣&#xff0c;所以现如今许多品牌正在转向达人营销工具。当你拥有了许多…

面试篇——计算机网络面试核心问题汇总

前言 前言&#xff1a;总结前后端岗位面试中计算机网络部分常见的面试题。 文章目录前言一、OSI七层模型1、物理层2、数据链路层3、网络层4、传输层5、会话层6、表示层7、应用层8、网络数据处理的整个流程二、TCP/IP 四层模型三、TCP的三次握手1、TCP简介2、三次握手1&#xff…

预训练机制(3)~GPT、BERT

目录 1. BERT、GPT 核心思想 1.1 word2vec和ELMo区别 2 GPT​编辑 3. Bert 3.1 Bert集大成者 extension&#xff1a;单向编码--双向编码区别 3.2 Bert和GPT、EMLo区别 3.3 Bert Architecture 3.3.1 explanation&#xff1a;是否参数多、数据量大&#xff0c;是否过拟…

天干地支蓝桥杯国赛

题目 分析 蓝桥杯国赛2020简单模拟题&#xff0c;你敢信&#xff0c;就是弄两个字符串数组。重点在于知道0000年是从哪个天干和地支开始的。 代码 #include <iostream> using namespace std;int year;int main() {cin >> year;string tiangan[10] {"geng&…