FFmpeg+OpenCV 读取摄像头
提供两种方式使用 FFmpeg 从摄像头中读取数据,并使用 OpenCV 显示视频帧。
读取摄像头
方法一
void VideoCapture() {avdevice_register_all();AVFormatContext *context = avformat_alloc_context();AVInputFormat *inputFormat = av_find_input_format("dshow");// 这里的 video=Logitech Webcam C930e 需要填写你自己的摄像头信息// 可以在终端下使用 ffmpeg -list_devices true -f dshow -i dummy 查看avformat_open_input(&context, "video=Logitech Webcam C930e", inputFormat, nullptr);int video_index = av_find_best_stream(context, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);if (video_index == -1) {printf("Could not find a video stream");avformat_close_input(&context);return;}// 获取流参数AVCodecParameters *params = context->streams[video_index]->codecpar;// 获取解码器AVCodec *codec = avcodec_find_decoder(params->codec_id);// 初始化一个解码器的上下文AVCodecContext *codecContext = avcodec_alloc_context3(codec);if (avcodec_parameters_to_context(codecContext, params) != 0) {printf("Could not copy codec context");avformat_close_input(&context);return;}avcodec_open2(codecContext, codec, nullptr);AVFrame *frame = av_frame_alloc();AVFrame *frameYUV = av_frame_alloc();int width = codecContext->width;int height = codecContext->height;// 声明图像的空间用以保存 YUV 图像auto* out_buffer = (unsigned char*)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, width, height, 1));av_image_fill_arrays(frameYUV->data, frameYUV->linesize, (const uint8_t*)out_buffer,AV_PIX_FMT_YUV420P, width, height, 1);AVPacket packet;struct SwsContext *swsContext;swsContext = sws_getContext(width, height, codecContext->pix_fmt,width, height, AV_PIX_FMT_YUV420P, SWS_BICUBIC,nullptr, nullptr, nullptr);while (av_read_frame(context, &packet) >= 0) {if (packet.stream_index == video_index) {avcodec_send_packet(codecContext, &packet);while (avcodec_receive_frame(codecContext, frame) == 0) {// 将解码后的图片转换为 YUV 格式sws_scale(swsContext, (const unsigned char* const *)frame->data, frame->linesize, 0,height, frameYUV->data, frameYUV->linesize);int size = width * height;// FFmpeg frame 转换为 OpenCV Matcv::Mat img = cv::Mat::zeros(height * 3 / 2, width, CV_8UC1);memcpy(img.data, frameYUV->data[0], size);memcpy(img.data + width * height, frameYUV->data[1], size / 4);memcpy(img.data + width * height * 5 / 4, frameYUV->data[2], size / 4);cv::cvtColor(img, img, CV_YUV2BGR_I420);cv::imshow("__main__", img);cv::waitKey(1);}}av_packet_unref(&packet);}// 释放资源av_frame_free(&frame);avcodec_close(codecContext);avcodec_parameters_free(¶ms);avformat_close_input(&context);
}
方法二
void VideoCapture() {AVFormatContext *context = avformat_alloc_context();AVInputFormat *inputFormat = av_find_input_format("dshow");avformat_open_input(&context, "video=Logitech Webcam C930e", inputFormat, nullptr);int video_index = av_find_best_stream(context, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);if (video_index == -1) {printf("Could not find a video stream");avformat_close_input(&context);return;}// 获取流参数AVCodecParameters *params = context->streams[video_index]->codecpar;// 获取解码器AVCodec *codec = avcodec_find_decoder(params->codec_id);// 初始化一个解码器的上下文AVCodecContext *codecContext = avcodec_alloc_context3(codec);if (avcodec_parameters_to_context(codecContext, params) != 0) {printf("Could not copy codec context");avformat_close_input(&context);return;}avcodec_open2(codecContext, codec, nullptr);AVFrame *frame = av_frame_alloc();int width = codecContext->width;int height = codecContext->height;AVPacket packet;while (av_read_frame(context, &packet) >= 0) {if (packet.stream_index == video_index) {avcodec_send_packet(codecContext, &packet);while (avcodec_receive_frame(codecContext, frame) == 0) {// AVFrameToCVMat 的实现可以看下方 FFmpeg 转为 Mat 中的代码cv::Mat img(AVFrameToCVMat(frame));cv::imshow("__main__", img);cv::waitKey(1);}}av_packet_unref(&packet);}av_frame_free(&frame);avcodec_close(codecContext);avcodec_parameters_free(¶ms);avformat_close_input(&context);
}
FFmpeg frame 转换为 OpenCV Mat
方法一
AVFrame* frameYUV;
/**
* ... 处理 YUV 格式的 AVFrame
*/
cv::Mat img = cv::Mat::zeros(height * 3 / 2, width, CV_8UC1);
memcpy(img.data, frameYUV->data[0], size);
memcpy(img.data + width * height, frameYUV->data[1], size / 4);
memcpy(img.data + width * height * 5 / 4, frameYUV->data[2], size / 4);
方法二
转载自 https://zhuanlan.zhihu.com/p/80350418
cv::Mat AVFrameToCVMat(const AVFrame* frame) {int width = frame->width;int height = frame->height;cv::Mat image(height, width, CV_8UC3);int cv_lines_sizes[1];cv_lines_sizes[0] = image.step1();SwsContext* conversion = sws_getContext(width, height, (AVPixelFormat)frame->format,width, height, AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR,nullptr, nullptr, nullptr);sws_scale(conversion, frame->data, frame->linesize, 0, height, &image.data, cv_lines_sizes);sws_freeContext(conversion);return image;
}
声明一个 YUV 图像的空间
声明一个 YUV 图像的空间,该空间用作将读取的视频帧转换为 YUV 格式。如果没有这个空间会显示 bad dst image pointer
。
// 声明图像的空间用以保存 YUV 图像auto* out_buffer = (unsigned char*)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, width, height, 1));av_image_fill_arrays(frameYUV->data, frameYUV->linesize, (const uint8_t*)out_buffer,AV_PIX_FMT_YUV420P, width, height, 1);