ZBLOG

C++ FFMPEG 解码MP4得到的每一帧图片数据,使用opencv将图片数据转成1920*1080的RGB

要使用FFmpeg解码MP4视频并将每一帧转换为1920x1080的RGB格式图片数据,你可以结合FFmpeg和OpenCV来实现。以下是一个简单的示例代码,演示如何完成这个任务。

环境准备

确保你已经安装了FFmpeg和OpenCV库。

示例代码

#include <iostream>
#include <opencv2/opencv.hpp>
extern "C" {
    #include <libavformat/avformat.h>
    #include <libavcodec/avcodec.h>
    #include <libswscale/swscale.h>
}

void decodeAndConvert(const char* filename) {
    av_register_all();
    
    AVFormatContext* formatCtx = nullptr;
    if (avformat_open_input(&formatCtx, filename, nullptr, nullptr) != 0) {
        std::cerr << "Could not open file." << std::endl;
        return;
    }

    if (avformat_find_stream_info(formatCtx, nullptr) < 0) {
        std::cerr << "Could not find stream information." << std::endl;
        return;
    }

    int videoStreamIndex = -1;
    for (unsigned int i = 0; i < formatCtx->nb_streams; ++i) {
        if (formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoStreamIndex = i;
            break;
        }
    }
    
    if (videoStreamIndex == -1) {
        std::cerr << "Could not find a video stream." << std::endl;
        return;
    }

    AVCodecParameters* codecParams = formatCtx->streams[videoStreamIndex]->codecpar;
    AVCodec* codec = avcodec_find_decoder(codecParams->codec_id);
    
    if (!codec) {
        std::cerr << "Unsupported codec!" << std::endl;
        return;
    }
    
    AVCodecContext* codecCtx = avcodec_alloc_context3(codec);
    
    if (avcodec_parameters_to_context(codecCtx, codecParams) < 0 || 
        avcodec_open2(codecCtx, codec, nullptr) < 0) {
        std::cerr << "Could not open codec." << std::endl;
        return;
    }

   AVFrame* frame = av_frame_alloc();
   AVPacket packet;

   struct SwsContext* sws_ctx = sws_getContext(
       codecCtx->width,
       codecCtx->height,
       codecCtx->pix_fmt,
       1920,
       1080,
       AV_PIX_FMT_BGR24,
       SWS_BILINEAR,
       nullptr,
       nullptr,
       nullptr
   );

   while (av_read_frame(formatCtx, &packet) >= 0) {
       if (packet.stream_index == videoStreamIndex) {
           avcodec_send_packet(codecCtx, &packet);

           while (avcodec_receive_frame(codecCtx, frame) >= 0) {
               // Allocate memory for the RGB image
               cv::Mat rgbImage(1080, 1920, CV_8UC3);
               
               // Convert the image from its native format to RGB
               uint8_t *dest[AV_NUM_DATA_POINTERS] = { rgbImage.data };
               int destLinesize[AV_NUM_DATA_POINTERS] = { rgbImage.step[0] };

               sws_scale(sws_ctx, frame->data, frame->linesize, 0,
                         codecCtx->height, dest, destLinesize);

               // Now you can use `rgbImage`, e.g., display it with OpenCV
               cv::imshow("Frame", rgbImage);
               cv::waitKey(30); // Display each frame for a brief moment
           }
       }
       av_packet_unref(&packet);
   }

   // Clean up resources
   sws_freeContext(sws_ctx);
   av_frame_free(&frame);
   avcodec_free_context(&codecCtx);
   avformat_close_input(&formatCtx);
}

int main(int argc, char* argv[]) {
   if (argc < 2) {
      std::cerr << "Usage: " << argv[0] << " <video file>" << std::endl;
      return -1;
   }

   decodeAndConvert(argv[1]);
   
   return 0;
}

编译与运行

请确保在编译时链接FFmpeg和OpenCV库,例如:

g++ your_file.cpp -o output -lavformat -lavcodec -lavutil -lswscale `pkg-config --cflags --libs opencv4`

然后运行生成的程序,传入你的MP4文件名作为参数:

./output your_video.mp4

注意事项

  • 确保正确安装和配置FFmpeg和OpenCV。
  • 如果要处理不同的视频分辨率,可能需要调整SwsContextcv::Mat的尺寸。
  • 上述代码只是一个简单示例,实际应用中建议加入更多错误处理机制。


    内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=7704

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?