C++用VideoWriter写入视频

C++用VideoWriter写入视频

warnning:要用resize转换大小.

#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main() {
    string img_name = "/*/*.png";
    string save_path = "*.avi";
    cv::Mat img = cv::imread(img_name);
    cv::VideoWriter track_writer;

    track_writer.open (save_path.c_str(), cv::VideoWriter::fourcc('M', 'P', '4', '2'), 30.0, cv::Size(960, 540));
    if(!track_writer.isOpened())
    {
        assert("track writer open failed!\n");
    }

    for (int i = 0; i < 100; ++i)
    {
        cv::imshow("test", img);
        cv::resize(img, img, cv::Size(960, 540));
        track_writer.write(img);
        cout << "frame" << i << endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
cv::VideoWriter 可以用来将 OpenCV 的图像序列写入本地视频文件,而不是网络流。如果你要将视频写入网络流,你需要使用不同的库和方法。例如,FFmpeg 是一个流媒体处理库,它可以将视频数据编码为流,并将其发送到网络。你可以使用 FFmpeg 的 API 在 C++ 中编写代码来实现这一点。以下是一个使用 FFmpeg 将本地视频文件写入网络流的简单示例: ```c++ #include <iostream> #include <sstream> #include <string> #include <cstdio> #include <chrono> #include <thread> #include <cstdlib> #include <cstring> #include <stdexcept> #include <sstream> #include <opencv2/opencv.hpp> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/opt.h> #include <libavutil/imgutils.h> } using namespace std; using namespace cv; int main(int argc, char** argv) { // Open the local video file VideoCapture cap("video.mp4"); if (!cap.isOpened()) { cerr << "Error: Unable to open video file" << endl; return -1; } // Initialize FFmpeg av_register_all(); avcodec_register_all(); avformat_network_init(); // Create the output context AVFormatContext* outctx = nullptr; if (avformat_alloc_output_context2(&outctx, nullptr, "flv", "rtmp://localhost/live") < 0) { cerr << "Error: Unable to create output context" << endl; return -1; } // Open the output stream AVStream* outstream = avformat_new_stream(outctx, nullptr); if (!outstream) { cerr << "Error: Unable to create output stream" << endl; return -1; } // Copy the codec parameters from the input stream AVCodecParameters* inparams = cap.getBackendType() == VideoCapture::CAP_V4L ? nullptr : avcodec_parameters_alloc(); if (inparams) { inparams->codec_type = AVMEDIA_TYPE_VIDEO; inparams->codec_id = AV_CODEC_ID_H264; inparams->width = cap.get(CV_CAP_PROP_FRAME_WIDTH); inparams->height = cap.get(CV_CAP_PROP_FRAME_HEIGHT); inparams->format = AV_PIX_FMT_BGR24; inparams->profile = FF_PROFILE_H264_BASELINE; inparams->bit_rate = 1000000; inparams->bit_rate_tolerance = 10000000; inparams->ticks_per_frame = 2; inparams->time_base.num = 1; inparams->time_base.den = 30; avcodec_parameters_to_context(outstream->codecpar, inparams); } // Open the codec AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (!codec) { cerr << "Error: Unable to find codec" << endl; return -1; } AVCodecContext* codecctx = avcodec_alloc_context3(codec); if (!codecctx) { cerr << "Error: Unable to create codec context" << endl; return -1; } avcodec_parameters_to_context(codecctx, inparams); codecctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; if (avcodec_open2(codecctx, codec, nullptr) < 0) { cerr << "Error: Unable to open codec" << endl; return -1; } avcodec_parameters_from_context(outstream->codecpar, codecctx); // Open the output stream if (avio_open(&outctx->pb, "rtmp://localhost/live", AVIO_FLAG_WRITE) < 0) { cerr << "Error: Unable to open output stream" << endl; return -1; } avformat_write_header(outctx, nullptr); // Write the frames to the output stream AVPacket pkt; av_init_packet(&pkt); Mat frame; while (cap.read(frame)) { // Encode the frame AVFrame* avframe = av_frame_alloc(); avframe->format = codecctx->pix_fmt; avframe->width = codecctx->width; avframe->height = codecctx->height; av_frame_get_buffer(avframe, 32); Mat dst(codecctx->height, codecctx->width, CV_8UC3, avframe->data[0], avframe->linesize[0]); cvtColor(frame, dst, COLOR_BGR2YUV_I420); avframe->pts = cap.get(CV_CAP_PROP_POS_FRAMES); avcodec_send_frame(codecctx, avframe); while (avcodec_receive_packet(codecctx, &pkt) == 0) { pkt.stream_index = outstream->index; av_interleaved_write_frame(outctx, &pkt); av_packet_unref(&pkt); } av_frame_free(&avframe); } // Flush the codec avcodec_send_frame(codecctx, nullptr); while (avcodec_receive_packet(codecctx, &pkt) == 0) { pkt.stream_index = outstream->index; av_interleaved_write_frame(outctx, &pkt); av_packet_unref(&pkt); } // Close the output stream av_write_trailer(outctx); avcodec_close(codecctx); avcodec_free_context(&codecctx); avformat_close_input(&outctx); avformat_free_context(outctx); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值