在 https://blog.csdn.net/fengbingchun/article/details/93975325 中介绍过通过FFmpeg可以直接获取usb视频流并解码显示的测试代码,当时通过usb获取到的视频流编码类型为AV_CODEC_ID_RAWVIDEO,像素格式为AV_PIX_FMT_YUYV422,其实编码类型为rawvideo的视频流可以不经过解码操作直接就可进行显示,测试代码如下:

#include "funset.hpp"
#include <stdio.h>
#include <iostream>
#include <memory>
#include <fstream>#ifdef __cplusplus
extern "C" {
#endif#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/mem.h>
#include <libavutil/imgutils.h>#ifdef __cplusplus
}
#endif#include <opencv2/opencv.hpp>int test_ffmpeg_stream_show()
{avdevice_register_all();#ifndef _MSC_VERconst char* input_format_name = "video4linux2";const char* url = "/dev/video0";AVInputFormat* input_fmt = av_find_input_format(input_format_name);AVFormatContext* format_ctx = avformat_alloc_context();int ret = avformat_open_input(&format_ctx, url, input_fmt, nullptr);if (ret != 0) {fprintf(stderr, "fail to open url: %s, return value: %d\n", url, ret);return -1;}ret = avformat_find_stream_info(format_ctx, nullptr);if (ret < 0) {fprintf(stderr, "fail to get stream information: %d\n", ret);return -1;}int video_stream_index = -1;for (unsigned int i = 0; i < format_ctx->nb_streams; ++i) {const AVStream* stream = format_ctx->streams[i];if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {video_stream_index = i;fprintf(stdout, "type of the encoded data: %d, dimensions of the video frame in pixels: width: %d, height: %d, pixel format: %d\n",stream->codecpar->codec_id, stream->codecpar->width, stream->codecpar->height, stream->codecpar->format);}}if (video_stream_index == -1) {fprintf(stderr, "no video stream\n");return -1;}AVCodecParameters* codecpar = format_ctx->streams[video_stream_index]->codecpar;const AVCodec* codec = avcodec_find_decoder(codecpar->codec_id);if (!codec) {fprintf(stderr, "fail to avcodec_find_decoder\n");return -1;}AVCodecContext* codec_ctx = avcodec_alloc_context3(codec);if (!codec_ctx) {fprintf(stderr, "fail to avcodec_alloc_context3\n");return -1;}codec_ctx->pix_fmt = AVPixelFormat(codecpar->format);codec_ctx->height = codecpar->height;codec_ctx->width = codecpar->width;codec_ctx->thread_count = 16;ret = avcodec_open2(codec_ctx, codec, nullptr);if (ret != 0) {fprintf(stderr, "fail to avcodec_open2: %d\n", ret);return -1;}AVPacket* packet = (AVPacket*)av_malloc(sizeof(AVPacket));SwsContext* sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_BGR24, 0, nullptr, nullptr, nullptr);if (!packet || !sws_ctx) {fprintf(stderr, "fail to alloc\n");return -1;}uint8_t *bgr_data[4], *yuyv422_data[4];int bgr_linesize[4], yuyv422_linesize[4];av_image_alloc(bgr_data, bgr_linesize, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_BGR24, 1);av_image_alloc(yuyv422_data, yuyv422_linesize, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_YUYV422, 1);cv::Mat mat(codec_ctx->height, codec_ctx->width, CV_8UC3);const char* winname = "usb video";cv::namedWindow(winname);const uint8_t *srcSlice[1];while (1) {ret = av_read_frame(format_ctx, packet);if (ret >= 0 && packet->stream_index == video_stream_index) {srcSlice[0] = packet->data;sws_scale(sws_ctx, srcSlice, yuyv422_linesize, 0, codec_ctx->height, bgr_data, bgr_linesize);mat.data = bgr_data[0];cv::imshow(winname, mat);}av_packet_unref(packet);int key = cv::waitKey(25);if (key == 27) break;}cv::destroyWindow(winname);sws_freeContext(sws_ctx);avformat_close_input(&format_ctx);av_freep(packet);av_freep(&bgr_data[0]);av_freep(&yuyv422_data[0]);fprintf(stdout, "test finish\n");return 0;
#elsefprintf(stderr, "Error: this interface only support usb camera: encode type: rawvideo\n");return -1;
#endif
}

执行结果如下:

GitHub:https://github.com//fengbingchun/OpenCV_Test

FFmpeg中编码类型为rawvideo无须解码直接显示测试代码相关推荐

  1. FFmpeg通过摄像头实现对视频流进行解码并显示测试代码(新接口)

    在https://blog.csdn.net/fengbingchun/article/details/93975325 中给出了通过旧接口即FFmpeg中已废弃的接口实现通过摄像头获取视频流然后解码 ...

  2. FFmpeg通过摄像头实现对视频流进行解码并显示测试代码(旧接口)

    这里通过USB摄像头(注:windows7/10下使用内置摄像头,linux下接普通的usb摄像头(Logitech))获取视频流,然后解码,最后再用opencv显示.用到的模块包括avformat. ...

  3. FFmpeg中一个线程获取视频流一个线程执行scale测试代码

    在https://blog.csdn.net/fengbingchun/article/details/94712986 中介绍过如果usb视频流编码类型为rawvideo则无需进行解码,可直接通过a ...

  4. ffmpeg中字典类型的描述

    原创:https://blog.csdn.net/ice_ly000/article/details/90599713?spm=1001.2014.3001.5501 ffmpeg中字典类型的描述: ...

  5. FFmpeg在Windows上设置dshow mjpeg编码+libyuv解码显示测试代码

    之前在https://blog.csdn.net/fengbingchun/article/details/103444891中介绍过在Windows上通过ffmpeg dshow设置为mjpeg编解 ...

  6. FFmpeg在Windows上通过dshow编解码方式设置为mjpeg并实时显示测试代码

    Windows上默认的内置摄像头一般支持两种编解码格式:rawvideo和mjpeg.在调用FFmpeg接口时默认的采用rawvideo.这里通过DirectShow实现为mjpeg进行编解码. 通过 ...

  7. FFmpeg中拉取rtsp视频流并缩放显示测试代码

    之前在https://blog.csdn.net/fengbingchun/article/details/92198857中给出过仅拉取rtsp视频流的测试代码,这里在此代码的基础上进行扩充,包括设 ...

  8. Dlib库中实现正脸人脸关键点(landmark)检测的测试代码

    Dlib库中提供了正脸人脸关键点检测的接口,这里参考dlib/examples/face_landmark_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸关键点检测的 ...

  9. js 中编码(encode)和解码(decode)的三种方法(传递是特殊符号丢失问题,如‘+’)

    转自:https://www.cnblogs.com/luckyuns/p/6396701.html js对文字进行编码涉及3个函数:escape,encodeURI,encodeURICompone ...

最新文章

  1. 为什么在系统中使用JSON
  2. Maven exec:java 运行 Java main 方法
  3. log4j2 配置文件
  4. WPF ChromiumWebBrowser 网页背景透明
  5. 【坐在马桶上看算法】算法4:队列——解密QQ号
  6. Qt+VS2005(配置步骤)
  7. TypeError: can't pickle _thread.RLock objects
  8. 【Elasticsearch】ElasticSearch里面的偏好查询
  9. rabbitmq实战指南_太香了这份架构解密:从分布式到微服务(第二版),神仙进阶指南...
  10. ppt设置外观样式_PPT中设置视频外观样式的方法
  11. 如何设置内网和外网同时使用
  12. 网站速度优化的三套解决方案
  13. Ubuntu 重置密码
  14. 压缩包文件密码如何解密
  15. 2022茶艺师(初级)特种作业证考试题库及模拟考试
  16. 安装Activemq5.13.2异常
  17. 【运筹学】整数规划 ( 相关概念 | 整数规划 | 整数线性规划 | 整数线性规划分类 )
  18. 移动端布局三种视口_什么是视口?移动端浏览器中的3种视口
  19. 最新整理:阿里、腾讯、字节、华为、百度等大厂Android岗面经分享!
  20. 【观察】打造智能决策“新引擎”,杉数科技勇闯“无人区”

热门文章

  1. LabVIEW图像模式匹配(基础篇—11)
  2. c语言大顶堆数组维护,图解大顶堆的构建、排序过程
  3. MySQL拦截器获取xml id_关于mybatis拦截器,有谁知道怎么对结果集进行拦截,将指定字段查询结果进行格式化...
  4. 线上分享会预告之深度学习在3D场景中的应用
  5. 在Ubuntu 16.04.6 LTS上安装python3.7和pip3后出现Command '('lsb_release', '-a')' 出错问题的解决方法
  6. 3G重选至4G--基于优先级
  7. Cachegrind--缓存命中检查工具及其可视化
  8. 【Python】解析Python中类的使用
  9. python 3下对stm32串口数据做解析
  10. 【BZOJ4282】慎二的随机数列 乱搞