VS2019+FFmpeg C++实现 FFmpeg将YUV视频编码为H264

最近在做视频编解码相关知识,我主要的参考文章是雷神的编码教程,里面讲的很详细具体博客参考
雷神参考
相关结构体内容解读
解读雷神代码详细
第三个链接是我认为解读比较详细的文章,另外,因为我本人这次要求的传输数据是mat类型,所以还涉及到mat 类型与AVFrame的互相转换。网上大多版本不可用,以下是可以使用的版本。

cv::Mat avframeToCvmat(const AVFrame* frame)
{
	int width = frame->width;
	int height = frame->height;
	std::cout << "frame" << frame->width << std::endl;
	cv::Mat image(height, width, CV_8UC3);
	int cvLinesizes[1];
	cvLinesizes[0] = image.step1();
	SwsContext* conversion = sws_getContext(width, height, AVPixelFormat::AV_PIX_FMT_YUV420P, width, height, AVPixelFormat::AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
	sws_scale(conversion, frame->data, frame->linesize, 0, height, &image.data, cvLinesizes);
	sws_freeContext(conversion);
	return image;
}
AVFrame* cvmatToAvframe(cv::Mat* image) {
	AVFrame* frame = NULL;
	int width = image->cols;
	int height = image->rows;
	std::cout << "cols :" << width << " rows :" << height << std::endl;
	int cvLinesizes[1];
	cvLinesizes[0] = image->step1();
	if (frame == NULL) {
		std::cout << "开辟内存空间" << std::endl;
		frame = av_frame_alloc();

		av_image_alloc(frame->data, frame->linesize, width, height, AVPixelFormat::AV_PIX_FMT_YUVJ420P, 1);
	}
	//源像素格式
	SwsContext* conversion = sws_getContext(width, height, AVPixelFormat::AV_PIX_FMT_BGR24, width, height, AVPixelFormat::AV_PIX_FMT_YUVJ420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);

	sws_scale(conversion, &image->data, cvLinesizes, 0, height, frame->data, frame->linesize);

	sws_freeContext(conversion);

	return  frame;
}

如果对于雷神代码配置不通的可以直接下载我配置好的
win10+vs2019+ffmpeg
对于直接使用雷神的代码因为雷神的FFMPEG版本比较旧所以有些函数已经被换掉可能出现 xxx声明被否决的情况,解决的办法有两个。
第一个就是根据新旧版本替换旧版本
弃用版本清单
第二个就是关掉sdl检查
关掉检查

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是将Mat YUV编码为H264流的C++代码示例,使用FFmpeg库: ```c++ #include <iostream> #include <fstream> #include <opencv2/opencv.hpp> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> int main(int argc, char* argv[]) { // Check input arguments if (argc < 4) { std::cerr << "Usage: " << argv[0] << " input_file width height output_file" << std::endl; return 1; } // Initialize FFmpeg av_register_all(); // Open input file const char* input_file = argv[1]; int width = std::stoi(argv[2]); int height = std::stoi(argv[3]); cv::Mat yuv_image(height * 3 / 2, width, CV_8UC1); std::ifstream input_stream(input_file, std::ios::binary); if (!input_stream.is_open()) { std::cerr << "Failed to open input file" << std::endl; return 1; } // Open output file const char* output_file = argv[4]; AVFormatContext* format_context = nullptr; if (avformat_alloc_output_context2(&format_context, nullptr, nullptr, output_file) < 0) { std::cerr << "Failed to allocate output context" << std::endl; return 1; } // Open output stream AVOutputFormat* output_format = format_context->oformat; AVStream* stream = avformat_new_stream(format_context, nullptr); if (!stream) { std::cerr << "Failed to create output stream" << std::endl; return 1; } stream->id = format_context->nb_streams - 1; AVCodecContext* codec_context = stream->codec; codec_context->codec_id = output_format->video_codec; codec_context->codec_type = AVMEDIA_TYPE_VIDEO; codec_context->width = width; codec_context->height = height; codec_context->pix_fmt = AV_PIX_FMT_YUV420P; codec_context->time_base = { 1, 25 }; if (format_context->oformat->flags & AVFMT_GLOBALHEADER) { codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } AVCodec* codec = avcodec_find_encoder(codec_context->codec_id); if (!codec) { std::cerr << "Failed to find encoder" << std::endl; return 1; } if (avcodec_open2(codec_context, codec, nullptr) < 0) { std::cerr << "Failed to open codec" << std::endl; return 1; } av_dump_format(format_context, 0, output_file, 1); if (!(output_format->flags & AVFMT_NOFILE)) { if (avio_open(&format_context->pb, output_file, AVIO_FLAG_WRITE) < 0) { std::cerr << "Failed to open output file" << std::endl; return 1; } } if (avformat_write_header(format_context, nullptr) < 0) { std::cerr << "Failed to write header" << std::endl; return 1; } // Initialize video converter struct SwsContext* converter = sws_getContext(width, height, AV_PIX_FMT_GRAY8, width, height, AV_PIX_FMT_YUV420P, 0, nullptr, nullptr, nullptr); if (!converter) { std::cerr << "Failed to create video converter" << std::endl; return 1; } // Encode frames AVFrame* frame = av_frame_alloc(); frame->width = width; frame->height = height; frame->format = codec_context->pix_fmt; if (av_frame_get_buffer(frame, 0) < 0) { std::cerr << "Failed to allocate frame buffer" << std::endl; return 1; } AVPacket packet = { 0 }; int frame_count = 0; while (input_stream.read(reinterpret_cast<char*>(yuv_image.data), yuv_image.total())) { // Convert YUV image to AVFrame sws_scale(converter, &yuv_image.data, &yuv_image.step, 0, height, frame->data, frame->linesize); // Encode frame frame->pts = frame_count++; int result = avcodec_send_frame(codec_context, frame); if (result < 0) { std::cerr << "Failed to send frame" << std::endl; return 1; } while (result >= 0) { result = avcodec_receive_packet(codec_context, &packet); if (result == AVERROR(EAGAIN) || result == AVERROR_EOF) { break; } if (result < 0) { std::cerr << "Failed to receive packet" << std::endl; return 1; } av_packet_rescale_ts(&packet, codec_context->time_base, stream->time_base); packet.stream_index = stream->index; if (av_interleaved_write_frame(format_context, &packet) < 0) { std::cerr << "Failed to write packet" << std::endl; return 1; } av_packet_unref(&packet); } } // Flush encoder int result = avcodec_send_frame(codec_context, nullptr); if (result < 0) { std::cerr << "Failed to send frame" << std::endl; return 1; } while (result >= 0) { result = avcodec_receive_packet(codec_context, &packet); if (result == AVERROR(EAGAIN) || result == AVERROR_EOF) { break; } if (result < 0) { std::cerr << "Failed to receive packet" << std::endl; return 1; } av_packet_rescale_ts(&packet, codec_context->time_base, stream->time_base); packet.stream_index = stream->index; if (av_interleaved_write_frame(format_context, &packet) < 0) { std::cerr << "Failed to write packet" << std::endl; return 1; } av_packet_unref(&packet); } // Close output file av_write_trailer(format_context); if (format_context && !(output_format->flags & AVFMT_NOFILE)) { avio_close(format_context->pb); } avcodec_close(codec_context); avformat_free_context(format_context); av_frame_free(&frame); sws_freeContext(converter); input_stream.close(); return 0; } ``` 请注意,此示例假设输入文件为YUV 4:2:0格式的二进制文件,其中Y,U和V平面以交替方式存储。如果您的输入格式不同,请根据需要进行修改。此外,此示例假设您已安装OpenCVFFmpeg库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值