ffmpeg推流延迟(已解决)

2024-06-13 09:48
文章标签 ffmpeg 解决 延迟 推流

本文主要是介绍ffmpeg推流延迟(已解决),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考自: https://www.cnblogs.com/tla001/p/7040344.html
尊重原创,请读原文

之前实现了yolo图像的在线检测,这次主要完成远程视频的检测。主要包括推流–収流–检测显示三大部分

首先说一下推流,主要使用ffmpeg命令进行本地摄像头的推流,为了实现首屏秒开使用-g设置gop大小,同时使用-b降低网络负载,保证流畅度。

linuxffmpeg -r 30  -i /dev/video0 -vcodec h264 -max_delay 100 -f flv -g 5 -b 700000 rtmp://219.216.87.170/live/test1windowffmpeg -r 30  -f vfwcap -i 0 -vcodec h264 -max_delay 100 -f flv -g 5 -b 700000 rtmp://219.216.87.170/live/test1ffmpeg -list_devices true -f dshow -i dummy  
ffmpeg -r 30  -f dshow -i video="1.3M HD WebCam" -vcodec h264 -max_delay 100 -f flv -g 5 -b 700000 rtmp://219.216.87.170/live/tes
t1

其次是収流,収流最开始的时候,有很大的延迟,大约5秒,后来通过优化,现在延时保证在1s以内,还是可以接收的,直接上収流的程序

复制代码
AVFormatContext *pFormatCtx;int i, videoindex;AVCodecContext *pCodecCtx;AVCodec *pCodec;AVFrame *pFrame, *pFrameRGB;uint8_t *out_buffer;AVPacket *packet;//int y_size;int ret, got_picture;struct SwsContext *img_convert_ctx;//输入文件路径
//    char filepath[] = "rtmp://219.216.87.170/vod/test.flv";char filepath[] = "rtmp://219.216.87.170/live/test1";int frame_cnt;printf("wait for playing %s\n", filepath);av_register_all();avformat_network_init();pFormatCtx = avformat_alloc_context();printf("size %ld\tduration %ld\n", pFormatCtx->probesize,pFormatCtx->max_analyze_duration);pFormatCtx->probesize = 20000000;pFormatCtx->max_analyze_duration = 2000;
//    pFormatCtx->interrupt_callback.callback = timout_callback;
//    pFormatCtx->interrupt_callback.opaque = pFormatCtx;
//    pFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;AVDictionary* options = NULL;av_dict_set(&options, "fflags", "nobuffer", 0);
//    av_dict_set(&options, "max_delay", "100000", 0);
//    av_dict_set(&options, "rtmp_transport", "tcp", 0);
//    av_dict_set(&options, "stimeout", "6", 0);printf("wating for opening file\n");if (avformat_open_input(&pFormatCtx, filepath, NULL, &options) != 0) {printf("Couldn't open input stream.\n");return -1;}av_dict_free(&options);printf("wating for finding stream\n");if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {printf("Couldn't find stream information.\n");return -1;}videoindex = -1;for (i = 0; i < pFormatCtx->nb_streams; i++)if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {videoindex = i;break;}if (videoindex == -1) {printf("Didn't find a video stream.\n");return -1;}pCodecCtx = pFormatCtx->streams[videoindex]->codec;pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (pCodec == NULL) {printf("Codec not found.\n");return -1;}if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {printf("Could not open codec.\n");return -1;}/** 在此处添加输出视频信息的代码* 取自于pFormatCtx,使用fprintf()*/pFrame = av_frame_alloc();pFrameRGB = av_frame_alloc();out_buffer = (uint8_t *) av_malloc(avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width,pCodecCtx->height));avpicture_fill((AVPicture *) pFrameRGB, out_buffer, AV_PIX_FMT_BGR24,pCodecCtx->width, pCodecCtx->height);packet = (AVPacket *) av_malloc(sizeof(AVPacket));//Output Info-----------------------------printf("--------------- File Information ----------------\n");av_dump_format(pFormatCtx, 0, filepath, 0);printf("-------------------------------------------------\n");img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);CvSize imagesize;imagesize.width = pCodecCtx->width;imagesize.height = pCodecCtx->height;IplImage *image = cvCreateImageHeader(imagesize, IPL_DEPTH_8U, 3);cvSetData(image, out_buffer, imagesize.width * 3);cvNamedWindow(filepath, CV_WINDOW_AUTOSIZE);frame_cnt = 0;int num = 0;while (av_read_frame(pFormatCtx, packet) >= 0) {if (packet->stream_index == videoindex) {/** 在此处添加输出H264码流的代码* 取自于packet,使用fwrite()*/ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture,packet);if (ret < 0) {printf("Decode Error.\n");return -1;}if (got_picture) {sws_scale(img_convert_ctx,(const uint8_t* const *) pFrame->data, pFrame->linesize,0, pCodecCtx->height, pFrameRGB->data,pFrameRGB->linesize);printf("Decoded frame index: %d\n", frame_cnt);/** 在此处添加输出YUV的代码* 取自于pFrameYUV,使用fwrite()*/frame_cnt++;cvShowImage(filepath, image);cvWaitKey(30);}}av_free_packet(packet);}sws_freeContext(img_convert_ctx);av_frame_free(&pFrameRGB);av_frame_free(&pFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);return 0;

将解压后的数据区与opencv的IplImage的数据区映射,实现opencv显示。

检测部分,主要使用IplImage与yolo中的图像进行对接,在图像转换方面,进行了部分优化,缩减一些不必要的步骤。然后使用线程区接收ffmepg流,主循环里区做检测并显示。需要做线程同步处理,只有当收到新流时,才去检测。

这篇关于ffmpeg推流延迟(已解决)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1056954

相关文章

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

深度剖析SpringBoot日志性能提升的原因与解决

《深度剖析SpringBoot日志性能提升的原因与解决》日志记录本该是辅助工具,却为何成了性能瓶颈,SpringBoot如何用代码彻底破解日志导致的高延迟问题,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言第一章:日志性能陷阱的底层原理1.1 日志级别的“双刃剑”效应1.2 同步日志的“吞吐量杀手”

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

SysMain服务可以关吗? 解决SysMain服务导致的高CPU使用率问题

《SysMain服务可以关吗?解决SysMain服务导致的高CPU使用率问题》SysMain服务是超级预读取,该服务会记录您打开应用程序的模式,并预先将它们加载到内存中以节省时间,但它可能占用大量... 在使用电脑的过程中,CPU使用率居高不下是许多用户都遇到过的问题,其中名为SysMain的服务往往是罪魁

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功

MySQ中出现幻读问题的解决过程

《MySQ中出现幻读问题的解决过程》文章解析MySQLInnoDB通过MVCC与间隙锁机制在可重复读隔离级别下解决幻读,确保事务一致性,同时指出性能影响及乐观锁等替代方案,帮助开发者优化数据库应用... 目录一、幻读的准确定义与核心特征幻读 vs 不可重复读二、mysql隔离级别深度解析各隔离级别的实现差异

Java报错:org.springframework.beans.factory.BeanCreationException的五种解决方法

《Java报错:org.springframework.beans.factory.BeanCreationException的五种解决方法》本文解析Spring框架中BeanCreationExce... 目录引言一、问题描述1.1 报错示例假设我们有一个简单的Java类,代表一个用户信息的实体类:然后,