emgucv.坑02_摄像头视频fps不对

Posted csskill

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了emgucv.坑02_摄像头视频fps不对相关的知识,希望对你有一定的参考价值。

1、C# 中 VideoCapture.GetCaptureProperty(CapProp.Fps);

  (海康)有的摄像头,得到的 FPS数据是 180000.00 但是 用 浏览器上去看,它的值确实是25...

 

2、C++版的OpenCV3查看:

 (1)

    VideoCapture capture;
    capture.open("rtsp://admin:admin123@192.168.1.164/h264/ch1/main/av_stream");//创建视频流
    double dFps = capture.get(CV_CAP_PROP_FPS);
    printf("dFps : %lf", dFps);

 控制台输出:

  技术图片

 

 

 

 

  (2)

    CvCapture *pCapture = NULL;
    pCapture = cvCreateFileCapture("rtsp://admin:admin123@192.168.1.164/h264/ch1/main/av_stream");
    double fps = cvGetCaptureProperty(pCapture, CV_CAP_PROP_FPS);
    printf("dFps : %lf", fps);

 控制台输出:

  技术图片

 

 

 

 

  (3)包含目录:...OpenCV_somethingopencv-3.4.6-vc14_vc15uildx86_zzinclude

    库目录: ...OpenCV_somethingopencv-3.4.6-vc14_vc15uildx86_zzdebugvc14lib

    链接器-->输入-->附加依赖项:

opencv_calib3d346d.lib
opencv_core346d.lib
opencv_dnn346d.lib
opencv_features2d346d.lib
opencv_flann346d.lib
opencv_highgui346d.lib
opencv_imgcodecs346d.lib
opencv_imgproc346d.lib
opencv_ml346d.lib
opencv_objdetect346d.lib
opencv_photo346d.lib
opencv_shape346d.lib
opencv_stitching346d.lib
opencv_superres346d.lib
opencv_video346d.lib
opencv_videoio346d.lib
opencv_videostab346d.lib

 

3、ffmpeg尝试:

// ffmpeg_test01.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

// 代码参考网址:
//    用ffmpeg获取rtsp视频流的宽高帧率及其他信息 - weixin_42432281的博客 - CSDN博客.html(https://blog.csdn.net/weixin_42432281/article/details/88351227)
// 官方编译 ffmepeg的网址,但是 貌似 写的不全...
//    Platform Specific Information.html (https://ffmpeg.org/platform.html#Microsoft-Visual-C_002b_002b-or-Intel-C_002b_002b-Compiler-for-Windows)


#include "pch.h"
#include <iostream>

//int main()
//{
//    std::cout << "Hello World!
"; 
//}

#include <stdio.h>  

#define __STDC_CONSTANT_MACROS  
#ifdef __cplusplus  
extern "C"
{
#endif  
#include "libavcodec/avcodec.h"  
#include "libavformat/avformat.h"  
#include "libswscale/swscale.h"   
#ifdef __cplusplus  
};
#endif  

#pragma warning(disable:4996)


int main(int argc, char* argv[])
{
    AVFormatContext *pFormatCtx;
    int             i, videoindex;
    AVCodecContext  *pCodecCtx;
    AVCodec         *pCodec;
    AVFrame *pFrame, *pFrameYUV;
    uint8_t *out_buffer;
    AVPacket *packet;
    int ret, got_picture;
    struct SwsContext *img_convert_ctx;

    //char filepath[] = "rtsp://admin:admin@172.168.0.128:554/11";
    char filepath[] = "rtsp://admin:admin123@192.168.1.164/h264/ch1/main/av_stream";

    av_register_all();
    avformat_network_init();
    pFormatCtx = avformat_alloc_context();

    AVDictionary* options = NULL;
    av_dict_set(&options, "buffer_size", "1024000", 0); //设置最大缓存,1080可调到最大
    av_dict_set(&options, "rtsp_transport", "tcp", 0); //以tcp的方式传送
    av_dict_set(&options, "stimeout", "5000000", 0); //设置超时断开链接时间,单位us
    av_dict_set(&options, "max_delay", "500000", 0); //设置最大时延
    av_dict_set(&options, "framerate", "20", 0);

    //ff_dump_stream_info(filepath);

    if (avformat_open_input(&pFormatCtx, filepath, NULL, &options) != 0)
    {
        printf("Couldn‘t open input stream.
");
        return -1;
    }
    if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
    {
        printf("Couldn‘t find stream information.
");
        return -1;
    }

    // 这个就是帧率?
    printf("--> --> --> %d, %d
", pFormatCtx->streams[0]->r_frame_rate.den, pFormatCtx->streams[0]->r_frame_rate.num);

    videoindex = -1;
    for (i = 0; i < pFormatCtx->nb_streams; i++)
    {
        if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            videoindex = i;
            break;
        }
        else if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
            int aud_idx = i;
        }
    }
    if (videoindex == -1)
    {
        printf("Didn‘t find a video stream.
");
        return -1;
    }
    pCodecCtx = pFormatCtx->streams[videoindex]->codec;
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if (pCodec == NULL)
    {
        printf("Codec not found.
");
        return -1;
    }
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
    {
        printf("Could not open codec.
");
        return -1;
    }

    pFrame = av_frame_alloc();
    pFrameYUV = av_frame_alloc();
    out_buffer = (uint8_t *)av_malloc(avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height));
    avpicture_fill((AVPicture *)pFrame, out_buffer, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);

    printf("-----------------------------------------------dumping stream info------------------------------------------------
");
    av_dump_format(pFormatCtx, 0, filepath, 0);
    printf("input format: %s", pFormatCtx->iformat->name);//
    printf("  nb_streams: %d", pFormatCtx->nb_streams);//记录了该 URL 中包含有几路流
    int64_t start_time = pFormatCtx->start_time;
    printf("  start_time: %lld
", start_time);//第一帧的时间戳
    printf("------------------------------------------------------------------------------------------------------------------
");

    img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);

    packet = (AVPacket *)av_malloc(sizeof(AVPacket));

    FILE *fpSave;
    if ((fpSave = fopen("geth264_testi1.h265", "w+")) == NULL)
        return 0;
    int j = 0;
    for (j; j < 5; j++)
    {
        //------------------------------  
        if (av_read_frame(pFormatCtx, packet) >= 0)
        {
            if (packet->stream_index == videoindex)
            {

                fwrite(packet->data, 1, packet->size, fpSave);
                int k = 0;
                for (k = 0; k < 5; k++) {
                    printf("%02X ", packet->data[k]);
                }
                printf(" len %d
", packet->size);
                printf("-----------------------------------------------dumping stream info------------------------------------------------
");
                AVStream *video_stream = pFormatCtx->streams[videoindex]; 
                printf("  video nb_frames: %lld", video_stream->nb_frames);//该码流的总帧数
                printf("  video codec_id: %d", video_stream->codecpar->codec_id); // 编码编号
                printf("  video codec_name: %s", avcodec_get_name(video_stream->codecpar->codec_id));//编码类型
                printf("  video width x height: %d x %d", video_stream->codecpar->width, video_stream->codecpar->height);//长、宽
                printf("  video pix_fmt: %d
", video_stream->codec->pix_fmt); // 原始图像的格式,码流中不一定存在该信息,会由解码后覆盖
                printf("  video bitrate %lld kb/s
", video_stream->codecpar->bit_rate / 1000);// 平均码率
                //printf("read video frame, timestamp = %lld 
", packet->pts);
                printf("------------------------------------------------------------------------------------------------------------------
");

                av_free_packet(packet);
            }
        }
    }

    //-------------- 
    fclose(fpSave);
    av_frame_free(&pFrameYUV);
    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);

    return 0;
}

 控制台输出:

--> --> --> 12, 299
-----------------------------------------------dumping stream info------------------------------------------------
Input #0, rtsp, from ‘rtsp://admin:admin123@192.168.1.164/h264/ch1/main/av_stream‘:
  Metadata:
    title           : Media Presentation
  Duration: N/A, start: 0.040000, bitrate: N/A
    Stream #0:0: Video: h264 (Main), yuvj420p(pc, bt709, progressive), 1920x1080
, 24.92 tbr, 90k tbn, 180k tbc
input format: rtsp  nb_streams: 1  start_time: 40000
------------------------------------------------------------------------------------------------------------------
[swscaler @ 050d9e00] deprecated pixel format used, make sure you did set range
correctly
00 00 00 01 67  len 300053
-----------------------------------------------dumping stream info------------------------------------------------
  video nb_frames: 0  video codec_id: 27  video codec_name: h264  video width x
height: 1920 x 1080  video pix_fmt: 12
  video bitrate 0 kb/s
------------------------------------------------------------------------------------------------------------------
00 00 00 01 61  len 2976
-----------------------------------------------dumping stream info------------------------------------------------
  video nb_frames: 0  video codec_id: 27  video codec_name: h264  video width x
height: 1920 x 1080  video pix_fmt: 12
  video bitrate 0 kb/s
------------------------------------------------------------------------------------------------------------------
00 00 00 01 67  len 299407
-----------------------------------------------dumping stream info------------------------------------------------
  video nb_frames: 0  video codec_id: 27  video codec_name: h264  video width x
height: 1920 x 1080  video pix_fmt: 12
  video bitrate 0 kb/s
------------------------------------------------------------------------------------------------------------------
00 00 00 01 61  len 2908
-----------------------------------------------dumping stream info------------------------------------------------
  video nb_frames: 0  video codec_id: 27  video codec_name: h264  video width x
height: 1920 x 1080  video pix_fmt: 12
  video bitrate 0 kb/s
------------------------------------------------------------------------------------------------------------------
00 00 00 01 61  len 10877
-----------------------------------------------dumping stream info------------------------------------------------
  video nb_frames: 0  video codec_id: 27  video codec_name: h264  video width x
height: 1920 x 1080  video pix_fmt: 12
  video bitrate 0 kb/s
------------------------------------------------------------------------------------------------------------------

???ffmpeg_test01Debugffmpeg_test01.exe (进程 10708)已退出,返回代
码为: 0。
若要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时
自动关闭控制台”。
按任意键关闭此窗口...

  包含目录:...Downloadsffmpegffmpeg-4.2.1

  库目录:...Downloadsffmpegffmpeg-4.2.1uildlibswscale

      ...Downloadsffmpegffmpeg-4.2.1uildlibswresample

      ...Downloadsffmpegffmpeg-4.2.1uildlibpostproc

      ...Downloadsffmpegffmpeg-4.2.1uildlibavutil

      ...Downloadsffmpegffmpeg-4.2.1uildlibavformat

      ...Downloadsffmpegffmpeg-4.2.1uildlibavfilter

      ...Downloadsffmpegffmpeg-4.2.1uildlibavdevice

      ...Downloadsffmpegffmpeg-4.2.1uildlibavcodec

  链接器-->输入-->附加依赖项:

swscale.lib
swresample.lib
postproc.lib
avutil.lib
avformat.lib
avfilter.lib
avdevice.lib
avcodec.lib

 

4、

5、

 

以上是关于emgucv.坑02_摄像头视频fps不对的主要内容,如果未能解决你的问题,请参考以下文章

在 OpenCV 上输入更高分辨率的视频文件时 fps 低

EMgucv 保存从网络摄像头检测到的多张人脸

使用 EmguCV 3.1.0.1 从视频捕获中检测

机器视觉行业实践技巧 -- OpenCV技巧与方法:避坑指南

OpenCV VideoCapture 有时会返回空白帧

EmguCV 类型初始化异常