ffmpeg系列:使用C++类封装ffmpeg,实现打开视频文件功能

在visual studio 2013中新建c++类MyFFmpeg;

在菜单栏点“项目----添加类”



在弹出的选择窗体中依次点击选择“Visual C++ ----> C++类 ---->添加”



在接下来的c++类添加向导窗体中填写相关类名,然后勾选“虚析构函数”选项点完成按钮



MyFFmpeg.h头文件如下 :

#pragma once
extern "C"{
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#include <string>
#include <QMutex>
class MyFFmpeg
{
public:
	/*设置成为单件模式*/
	static MyFFmpeg *Get()
	{
		static MyFFmpeg ff;
		return &ff;
	}

	/*打开指定路径的视频文件*/
	bool Open(const char *path);

	/*关闭之前打开的视频文件*/
	void Close();

	/*获取相关错误信息*/
	std::string GetError();

	/*类析构函数*/
	virtual ~MyFFmpeg();

	/*视频文件总的毫秒数*/
	int totalMs = 0;
protected:
	/*相关错误信息*/
	char errorbuf[1024];

	//应对多线程访问时的同步锁
	QMutex mutex;

	AVFormatContext *ac = NULL;

	/*设置成为单件模式,所以要把构造函数设置为私有*/
	MyFFmpeg();
};




MyFFmpeg.cpp实现文件如下 :


#include "MyFFmpeg.h"
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"swscale.lib")
bool MyFFmpeg::Open(const char *path){
	Close();
	mutex.lock();
	int re = avformat_open_input(&ac, path, 0, 0);
	if (re != 0){//打开文件失败
		mutex.unlock();
		av_strerror(re, errorbuf, sizeof(errorbuf));
		return false;
	}
	//得到视频总时长的毫秒数
	totalMs = ((ac->duration / AV_TIME_BASE)*1000);
	mutex.unlock();
	return true;
}
void MyFFmpeg::Close(){
	mutex.lock();
	if (ac) avformat_close_input(&ac);
	mutex.unlock();
}
std::string MyFFmpeg::GetError(){
	mutex.lock();
	std::string re = this->errorbuf;
	mutex.unlock();
	return re;
}

MyFFmpeg::MyFFmpeg()
{
	errorbuf[0] = '\0';
	av_register_all();
}


main.pp调用代码:


#include "myplayer.h"
#include <QtWidgets/QApplication>
#include "MyFFmpeg.h"

int main(int argc, char *argv[])
{
	char path[1024] = "test1.mp4";
	if (MyFFmpeg::Get()->Open(path)){
		printf("文件[%s]打开成功",path);
	}
	else
	{
		printf("\n文件[%s]打开失败;错误信息:%s", path,MyFFmpeg::Get()->GetError().c_str());
		getchar();
		return -1;
	}
	
	QApplication a(argc, argv);
	MyPlayer w;
	w.show();
	return a.exec();
}




运行结果如下:




如果把文件名写错,比如把test.mp4写成test1.mpr会是下面的结果;



使用C++FFmpeg将h264文件封装成mp4文件,可以按照以下步骤进行操作: 1. 安装FFmpeg库,可以从官网下载预编译的库,或者自行编译源代码。 2. 打开h264文件并读取其中的数据,可以使用FFmpeg提供的AVFormatContext和AVPacket来实现。 3. 创建输出文件并设置输出格式,这里需要使用FFmpeg提供的AVFormatContext和avformat_alloc_output_context2函数来创建输出文件。 4. 将h264数据写入输出文件,需要使用FFmpeg提供的av_interleaved_write_frame函数将数据写入输出文件。 5. 关闭输入和输出文件,释放资源。 下面是一个示例代码,演示如何使用C++FFmpeg将h264文件封装成mp4文件: ```cpp #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <stdint.h> #include <errno.h> #include <ctime> #include <vector> extern "C" { #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> } using namespace std; int main(int argc, char* argv[]) { if(argc < 3) { cout << "Usage: " << argv[0] << " input-file output-file" << endl; return -1; } const char* input_file = argv[1]; const char* output_file = argv[2]; av_register_all(); AVFormatContext* ifmt_ctx = NULL; if(avformat_open_input(&ifmt_ctx, input_file, NULL, NULL) < 0) { cout << "Could not open input file: " << input_file << endl; return -1; } if(avformat_find_stream_info(ifmt_ctx, NULL) < 0) { cout << "Could not find stream information" << endl; return -1; } av_dump_format(ifmt_ctx, 0, input_file, 0); AVFormatContext* ofmt_ctx = NULL; if(avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, output_file) < 0) { cout << "Could not create output context" << endl; return -1; } AVOutputFormat* ofmt = ofmt_ctx->oformat; for(int i = 0; i < ifmt_ctx->nb_streams; i++) { AVStream* in_stream = ifmt_ctx->streams[i]; AVStream* out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec); if(!out_stream) { cout << "Failed allocating output stream" << endl; return -1; } if(avcodec_copy_context(out_stream->codec, in_stream->codec) < 0) { cout << "Failed to copy codec context" << endl; return -1; } out_stream->codec->codec_tag = 0; if(ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) { out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } } av_dump_format(ofmt_ctx, 0, output_file, 1); if(!(ofmt->flags & AVFMT_NOFILE)) { if(avio_open(&ofmt_ctx->pb, output_file, AVIO_FLAG_WRITE) < 0) { cout << "Could not open output file: " << output_file << endl; return -1; } } if(avformat_write_header(ofmt_ctx, NULL) < 0) { cout << "Error occurred when opening output file" << endl; return -1; } AVPacket pkt; int frame_index = 0; while(true) { AVStream* in_stream = NULL; AVStream* out_stream = NULL; if(av_read_frame(ifmt_ctx, &pkt) < 0) { break; } in_stream = ifmt_ctx->streams[pkt.stream_index]; out_stream = ofmt_ctx->streams[pkt.stream_index]; if(av_interleaved_write_frame(ofmt_ctx, &pkt) < 0) { cout << "Error muxing packet" << endl; break; } cout << "Write frame " << frame_index << " (size=" << pkt.size << ")" << endl; frame_index++; av_free_packet(&pkt); } av_write_trailer(ofmt_ctx); avformat_close_input(&ifmt_ctx); if(ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE)) { avio_close(ofmt_ctx->pb); } avformat_free_context(ofmt_ctx); return 0; } ``` 需要注意的是,这只是一个简单的示例代码,不一定适用于所有情况。在实际使用中,还需要根据具体情况进行修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值