FFmpeg支持多线程编码并保存mp4文件示例

      之前介绍的示例:

      (1).https://blog.csdn.net/fengbingchun/article/details/132129988 中对编码后数据保存成mp4

      (2).https://blog.csdn.net/fengbingchun/article/details/132128885 中通过AVIOContext实现从内存读取数据

      (3).https://blog.csdn.net/fengbingchun/article/details/132389734 中将图像加载到视频中

      这里将三部分整合到类中,便于后面增加测试代码,下面的示例是两个线程:从内存中读取数据,并将指定的图像加载到视频,将结果保存成mp4。

      示例代码如下:

      1. 类PacketScaleQueue:用于持续的从指定内存中读取原始数据,上面的示例中已包含此代码

      2.类CodecQueue:用于将解码数据存入队列中,并通过单独的线程进行编码

class AVFrameQueue {
public:
	AVFrameQueue() = default;
	~AVFrameQueue() {}

	void push(AVFrame** frame) {
		std::unique_lock<std::mutex> lck(mtx);
		queue.push(*frame);
		cv.notify_all();
	}

	void pop(AVFrame** frame) {
		std::unique_lock<std::mutex> lck(mtx);
		while (queue.empty()) {
			//cv.wait(lck);
			if (cv.wait_for(lck, std::chrono::milliseconds(150)) == std::cv_status::timeout) {
				fprintf(stderr, "#### Warning: wait timeout\n");
				*frame = nullptr;
				return;
			}
		}
		*frame = queue.front();
		queue.pop();
	}

	size_t size() const {
		return queue.size();
	}

private:
	std::queue<AVFrame*> queue;
	std::mutex mtx;
	std::condition_variable cv;
};

class CodecQueue {
public:
	CodecQueue() = default;
	void init(unsigned int frame_num) {
		for (auto i = 0; i < frame_num; ++i) {
			AVFrame* frame = nullptr;
			pushDecode(&frame);
		}
	}

	~CodecQueue() { release(); }

	void release() {
		AVFrame* frame = nullptr;

		while (getDecodeSize() > 0) {
			popDecode(&frame);
			av_frame_free(&frame);
		}

		while (getEncodeSize() > 0) {
			popEncode(&frame);
			av_frame_free(&frame);
		}
	}

	void pushDecode(AVFrame** frame) { decode_queue.push(frame); }
	void popDecode(AVFrame** frame) { decode_queue.pop(frame); }
	size_t getDecodeSize() const { return decode_queue.size(); }

	void pushEncode(AVFrame** frame) { encode_queue.push(frame); }
	void popEncode(AVFrame** frame) { encode_queue.pop(frame); }
	size_t getEncodeSize() const { return encode_queue.size(); }

private:
	AVFrameQueue decode_queue, encode_queue;
};

      3.类VideoCodec:供外面的接口调用,封装了视频的解码和编码过程

      声明如下:

typedef struct CodecCtx {
	char outfile_name[VIDEO_CODEC_MAX_STRING_SIZE];
	char video_size[VIDEO_CODEC_MAX_STRING_SIZE];
	char bitrate_str[VIDEO_CODEC_MAX_STRING_SIZE];
	char pixel_format[VIDEO_CODEC_MAX_STRING_SIZE];
	char filter_descr[VIDEO_CODEC_MAX_STRING_SIZE];
	AVFormatContext* ifmt_ctx;
	AVFormatContext* ofmt_ctx;
	AVCodecContext* dec_ctx;
	AVCodecContext* enc_ctx;
	AVFrame* dec_frame;
	AVFilterContext* buffersink_ctx;
	AVFilterContext* buffersrc_ctx;
	AVFilterGraph* filter_graph;
	AVPacket* enc_pkt;
	AVRational frame_rate;
	int term_status;
	int stream_index;
	int frame_count;
	bool encode_thread_end;
} CodecCtx;

class VideoCodec {
public:
	VideoCodec() = default;
	~VideoCodec() {  }

	void setOutfileName(const std::string& name) { outfile_name_ = name; }
	void setVideoSize(const std::string& size) { video_size_ = size; }
	void setPixelFormat(const std::string& format) { pixel_format_ = format; }
	void setFilterDescr(const std::string& filter_descr) { filter_descr_ = filter_descr; }

	void stopEncode() {
		while (raw_packet_queue_.getScaleSize() != 0);

		codec_ctx_->term_status = 1;

		Buffer buffer;
		raw_packet_queue_.popPacket(buffer);
		memset(buffer.data, 0, block_size_);
		raw_packet_queue_.pushScale(buffer); // for av_read_frame to exit normally
	}

	PacketScaleQueue& get_raw_packet_queue(unsigned int buffer_num, size_t buffer_size) {
		raw_packet_queue_.init(buffer_num, buffer_size);
		block_size_ = buffer_size;
		return raw_packet_queue_;
	}

	int openEncode();
	int processEncode();
	int closeEncode();

private:
	std::string outfile_name_ = "";
	std::string video_size_ = "";
	std::string pixel_format_ = "";
	std::string filter_descr_ = "";
	PacketScaleQueue raw_packet_queue_;
	int block_size_ = 0;
	CodecCtx* codec_ctx_ = nullptr;
	AVIOContext* avio_ctx_ = nullptr;
	CodecQueue codec_queue_;
	std::thread encode_thread_;

	int get_decode_context();
	int get_encode_context();
	int init_filters();
	int filter_encode_write_frame(AVFrame* frame);
	int get_output_format_context();

	int flush_encode_write_frame();
	int flush_decoder();
	int flush_encoder();
	void flush_codec();
};

      类VideoCodec实现部分:是之前示例的整理,参考之前示例

      4.测试代码,即调用VideoCodec接口,以下是同时两个线程进行编码写

namespace {

const int total_push_count = 121;
bool flag1 = true;
const size_t block_size_1 = 640 * 480 * 3;
size_t total_push_count_1 = 0;

void fill_raw_data_1(PacketScaleQueue& raw_packet)
{
    unsigned char value = 0;
    while (total_push_count_1 < total_push_count) {
        value += 10;
        if (value >= 255) value = 0;

        Buffer buffer;
        raw_packet.popPacket(buffer);
        memset(buffer.data, value, block_size_1);
        raw_packet.pushScale(buffer);

        std::this_thread::sleep_for(std::chrono::milliseconds(33));
        ++total_push_count_1;
    }

    flag1 = false;
}

void sleep_seconds_1(VideoCodec& video_codec)
{
    while (flag1) {
        std::this_thread::sleep_for(std::chrono::milliseconds(33));
    }

    video_codec.stopEncode();
}

void encode_1()
{
    VideoCodec video_codec;
    video_codec.setOutfileName("out1.mp4");
    video_codec.setVideoSize("640x480");
    video_codec.setPixelFormat("bgr24");
    video_codec.setFilterDescr("movie=1.jpg[logo];[in][logo]overlay=10:20[out]");

    auto& raw_queue = video_codec.get_raw_packet_queue(16, block_size_1);
    std::thread thread_fill(fill_raw_data_1, std::ref(raw_queue));

    auto ret = video_codec.openEncode();
    if (ret != 0) {
        std::cout << "fail to openEncode: " << ret << std::endl;
        //return -1;
    }

    std::thread thread_sleep(sleep_seconds_1, std::ref(video_codec));

    ret = video_codec.processEncode();
    if (ret != 0) {
        std::cout << "fail to processEncode: " << ret << std::endl;
        //return -1;
    }

    thread_fill.join();
    thread_sleep.join();

    video_codec.closeEncode();

    std::cout << "1 total push count: " << total_push_count_1 << std::endl;
}

bool flag2 = true;
const size_t block_size_2 = 640 * 480 * 3;
size_t total_push_count_2 = 0;

void fill_raw_data_2(PacketScaleQueue& raw_packet)
{
    unsigned char value = 0;
    while (total_push_count_2 < total_push_count) {
        value += 10;
        if (value >= 255) value = 0;

        Buffer buffer;
        raw_packet.popPacket(buffer);
        memset(buffer.data, value, block_size_2);
        raw_packet.pushScale(buffer);

        std::this_thread::sleep_for(std::chrono::milliseconds(33));
        ++total_push_count_2;
    }

    flag2 = false;
}

void sleep_seconds_2(VideoCodec& video_codec)
{
    while (flag2) {
        std::this_thread::sleep_for(std::chrono::milliseconds(33));
    }

    video_codec.stopEncode();
}

void encode_2()
{
    VideoCodec video_codec;
    video_codec.setOutfileName("out2.mp4");
    video_codec.setVideoSize("640x480");
    video_codec.setPixelFormat("bgr24");
    video_codec.setFilterDescr("movie=1.jpg[logo];[in][logo]overlay=10:20[out]");

    auto& raw_queue = video_codec.get_raw_packet_queue(16, block_size_2);
    std::thread thread_fill(fill_raw_data_2, std::ref(raw_queue));

    auto ret = video_codec.openEncode();
    if (ret != 0) {
        std::cout << "fail to openEncode: " << ret << std::endl;
        //return -1;
    }

    std::thread thread_sleep(sleep_seconds_2, std::ref(video_codec));

    ret = video_codec.processEncode();
    if (ret != 0) {
        std::cout << "fail to processEncode: " << ret << std::endl;
        //return -1;
    }

    thread_fill.join();
    thread_sleep.join();

    std::cout << "2 total push count: " << total_push_count_2 << std::endl;
}

} // namespce

int test_ffmpeg_libavfilter_movie_multi_thread()
{
    std::thread thread_1(encode_1);
    std::thread thread_2(encode_2);

    thread_1.join();
    thread_2.join();

    std::cout << "test finish" << std::endl;
    return 0;
}

      生成的mp4文件结果如下:在release下生成的两个视频文件完全一致;在debug下编码过程中有时会timeout

      GitHubhttps://github.com/fengbingchun/OpenCV_Test

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FFmpeg是一个非常强大的开源多媒体处理框架,提供了丰富的编解码器和工具,可以对音视频进行录制、转码、剪辑、播放等操作。在使用FFmpeg进行编码时,可以通过多线程技术提高编码效率。 下面是一个简单的C++多线程编码示例: ```cpp #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> #include <chrono> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/opt.h> } using namespace std::chrono_literals; // 用于存储编码数据的队列 std::queue<AVPacket*> packet_queue; std::mutex packet_mutex; std::condition_variable packet_cond; // 编码线程函数 void encode_thread(AVCodecContext* codec_ctx, AVFrame* frame, AVFormatContext* fmt_ctx) { int ret; AVPacket* pkt = av_packet_alloc(); while (true) { // 从队列中取出一帧待编码数据 std::unique_lock<std::mutex> lock(packet_mutex); packet_cond.wait(lock, [] { return !packet_queue.empty(); }); pkt = packet_queue.front(); packet_queue.pop(); // 编码该帧数据 ret = avcodec_send_frame(codec_ctx, frame); if (ret < 0) { std::cerr << "Error sending frame to encoder: " << av_err2str(ret) << std::endl; break; } while (ret >= 0) { ret = avcodec_receive_packet(codec_ctx, pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { std::cerr << "Error receiving packet from encoder: " << av_err2str(ret) << std::endl; break; } // 将编码后的数据写入文件 av_packet_rescale_ts(pkt, codec_ctx->time_base, fmt_ctx->streams[0]->time_base); av_interleaved_write_frame(fmt_ctx, pkt); av_packet_unref(pkt); } av_packet_free(&pkt); } } int main(int argc, char** argv) { // 初始化FFmpeg av_register_all(); avcodec_register_all(); // 打开输入文件并获取视频流信息 AVFormatContext* in_fmt_ctx = nullptr; if (avformat_open_input(&in_fmt_ctx, "input.mp4", nullptr, nullptr) < 0) { std::cerr << "Error opening input file" << std::endl; return 1; } if (avformat_find_stream_info(in_fmt_ctx, nullptr) < 0) { std::cerr << "Error finding stream information" << std::endl; return 1; } int video_stream_index = av_find_best_stream(in_fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0); if (video_stream_index < 0) { std::cerr << "Error finding video stream" << std::endl; return 1; } AVStream* in_video_stream = in_fmt_ctx->streams[video_stream_index]; // 打开输出文件并初始化视频编码器 AVFormatContext* out_fmt_ctx = nullptr; if (avformat_alloc_output_context2(&out_fmt_ctx, nullptr, nullptr, "output.mp4") < 0) { std::cerr << "Error allocating output context" << std::endl; return 1; } AVStream* out_video_stream = avformat_new_stream(out_fmt_ctx, nullptr); if (!out_video_stream) { std::cerr << "Error creating new video stream" << std::endl; return 1; } AVCodec* codec = avcodec_find_encoder(out_fmt_ctx->oformat->video_codec); if (!codec) { std::cerr << "Error finding video encoder" << std::endl; return 1; } AVCodecContext* codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { std::cerr << "Error allocating video codec context" << std::endl; return 1; } codec_ctx->width = in_video_stream->codecpar->width; codec_ctx->height = in_video_stream->codecpar->height; codec_ctx->pix_fmt = codec->pix_fmts[0]; codec_ctx->time_base = { 1, in_video_stream->codecpar->frame_rate.num }; codec_ctx->framerate = { in_video_stream->codecpar->frame_rate, 1 }; if (avcodec_open2(codec_ctx, codec, nullptr) < 0) { std::cerr << "Error opening video codec" << std::endl; return 1; } if (avcodec_parameters_from_context(out_video_stream->codecpar, codec_ctx) < 0) { std::cerr << "Error copying codec parameters" << std::endl; return 1; } out_video_stream->time_base = codec_ctx->time_base; if (avio_open(&out_fmt_ctx->pb, "output.mp4", AVIO_FLAG_WRITE) < 0) { std::cerr << "Error opening output file" << std::endl; return 1; } if (avformat_write_header(out_fmt_ctx, nullptr) < 0) { std::cerr << "Error writing output file header" << std::endl; return 1; } // 初始化视频帧 AVFrame* frame = av_frame_alloc(); frame->format = codec_ctx->pix_fmt; frame->width = codec_ctx->width; frame->height = codec_ctx->height; if (av_frame_get_buffer(frame, 0) < 0) { std::cerr << "Error allocating frame buffer" << std::endl; return 1; } // 启动编码线程 std::thread encode_thr(encode_thread, codec_ctx, frame, out_fmt_ctx); // 读取视频帧并送入编码队列 AVPacket* pkt = av_packet_alloc(); AVFrame* in_frame = av_frame_alloc(); while (av_read_frame(in_fmt_ctx, pkt) == 0) { if (pkt->stream_index == video_stream_index) { avcodec_send_packet(codec_ctx, pkt); avcodec_receive_frame(codec_ctx, in_frame); // 将原始帧数据转换为目标格式 SwsContext* sws_ctx = sws_getContext( in_frame->width, in_frame->height, static_cast<AVPixelFormat>(in_frame->format), codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr); if (!sws_ctx) { std::cerr << "Error creating sws context" << std::endl; return 1; } sws_scale(sws_ctx, in_frame->data, in_frame->linesize, 0, in_frame->height, frame->data, frame->linesize); sws_freeContext(sws_ctx); // 将转换后的帧数据送入编码队列 std::unique_lock<std::mutex> lock(packet_mutex); packet_queue.push(av_packet_clone(pkt)); packet_cond.notify_one(); } av_packet_unref(pkt); } av_packet_free(&pkt); av_frame_free(&in_frame); // 结束编码线程 encode_thr.join(); // 写入文件尾部并释放资源 av_write_trailer(out_fmt_ctx); avformat_close_input(&in_fmt_ctx); if (out_fmt_ctx && !(out_fmt_ctx->oformat->flags & AVFMT_NOFILE)) { avio_closep(&out_fmt_ctx->pb); } avformat_free_context(out_fmt_ctx); avcodec_free_context(&codec_ctx); av_frame_free(&frame); return 0; } ``` 该示例中,主线程读取原始视频帧并将其送入编码队列中,编码线程从队列中取出待编码数据,并将编码后的数据写入文件多线程编码可以有效利用多核CPU提高编码效率,但同时也需要注意线程同步和数据安全等问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值