当前位置: 首页 > news >正文

大气红色礼品公司网站源码百度竞价排名怎么靠前

大气红色礼品公司网站源码,百度竞价排名怎么靠前,深圳保障性住房查询网,如何用c 做网站一、引言 在本文中,我们将详细介绍如何使用 Python 进行视频的推流操作。我们将通过两个不同的实现方式,即单线程推流和多线程推流,来展示如何利用 cv2(OpenCV)和 subprocess 等库将视频帧推送到指定的 RTMP 地址。这…

一、引言

在本文中,我们将详细介绍如何使用 Python 进行视频的推流操作。我们将通过两个不同的实现方式,即单线程推流和多线程推流,来展示如何利用 cv2(OpenCV)和 subprocess 等库将视频帧推送到指定的 RTMP 地址。这两种方式都涉及到从摄像头读取视频帧,以及使用 ffmpeg 命令行工具将视频帧进行编码和推流的过程。

二、单线程推流

以下是单线程推流的代码:

import cv2 as cv
import subprocess as spdef push_stream():# 视频读取对象cap = cv.VideoCapture(0) fps = int(cap.get(cv.CAP_PROP_FPS))w = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))h = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))ret, frame = cap.read()# 推流地址rtmpUrl = "rtmp://192.168.3.33:1935/live/"# 推流参数command = ['ffmpeg','-y','-f', 'rawvideo','-vcodec','rawvideo','-pix_fmt', 'bgr24','-s', "{}x{}".format(w, h),'-r', str(fps),'-i', '-','-c:v', 'libx264','-pix_fmt', 'yuv420p','-preset', 'ultrafast','-f', 'flv', rtmpUrl]# 创建、管理子进程pipe = sp.Popen(command, stdin=sp.PIPE, bufsize=10 ** 8)# 循环读取while cap.isOpened():# 读取一帧ret, frame = cap.read()if frame is None:print('read frame err!')continue# 显示一帧cv.imshow("frame", frame)# 按键退出if cv.waitKey(1) & 0xFF == ord('q'):break# 读取尺寸、推流# img=cv.resize(frame,size)pipe.stdin.write(frame) # 关闭窗口cv.destroyAllWindows()# 停止读取cap.release()

在这个单线程的实现中,我们执行以下步骤:

  1. 初始化视频读取对象
    • 使用 cv2.VideoCapture(0) 来打开默认的摄像头设备。
    • 获取摄像头的帧率 fps、宽度 w 和高度 h 等参数。
  2. 设置推流地址和参数
    • 定义 rtmpUrl 作为推流的目标地址。
    • 构造 ffmpeg 的命令列表 command,该列表包含了一系列的参数,如 -y 表示覆盖输出文件、-f rawvideo 表示输入格式为原始视频等。
    • 使用 sp.Popen 创建一个子进程,将 ffmpeg 命令作为子进程运行,并且将其输入管道 stdin 连接到我们的程序。
  3. 循环读取和推流
    • 在一个 while 循环中,不断读取摄像头的帧。
    • 若读取失败,打印错误信息并继续。
    • 使用 cv2.imshow 显示当前帧,同时监听 q 键,按下 q 键时退出程序。
    • 将读取到的帧通过管道发送给 ffmpeg 进行推流。

三、多线程推流

以下是多线程推流的代码:

import queue
import threading
import cv2 as cv
import subprocess as spclass Live(object):def __init__(self):self.frame_queue = queue.Queue()self.command = ""# 自行设置self.rtmpUrl = ""self.camera_path = ""def read_frame(self):print("开启推流")cap = cv.VideoCapture(self.camera_path)# Get video informationfps = int(cap.get(cv.CAP_PROP_FPS))width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))# ffmpeg commandself.command = ['ffmpeg','-y','-f', 'rawvideo','-vcodec','rawvideo','-pix_fmt', 'bgr24','-s', "{}x{}".format(width, height),'-r', str(fps),'-i', '-','-c:v', 'libx264','-pix_fmt', 'yuv420p','-preset', 'ultrafast','-f', 'flv', self.rtmpUrl]# read webcamerawhile(cap.isOpened()):ret, frame = cap.read()if not ret:print("Opening camera is failed")break# put frame into queueself.frame_queue.put(frame)def push_frame(self):# 防止多线程时 command 未被设置while True:if len(self.command) > 0:# 管道配置p = sp.Popen(self.command, stdin=sp.PIPE)breakwhile True:if self.frame_queue.empty()!= True:frame = self.frame_queue.get()# process frame# 你处理图片的代码# write to pipep.stdin.write(frame.tostring())def run(self):threads = [threading.Thread(target=Live.read_frame, args=(self,)),threading.Thread(target=Live.push_frame, args=(self,))][thread.setDaemon(True) for thread in threads][thread.start() for thread in threads]

在这个多线程的实现中,我们使用了 threadingqueue 库:

  1. 类的初始化
    • 创建一个 Live 类,在 __init__ 方法中初始化帧队列 frame_queuecommandrtmpUrlcamera_path 等变量。
  2. 读取帧的线程方法
    • read_frame 方法中,使用 cv2.VideoCapture(self.camera_path) 打开摄像头。
    • 获取摄像头的参数,并构造 ffmpeg 命令。
    • 不断从摄像头读取帧,并将帧放入队列 frame_queue 中。
  3. 推流的线程方法
    • push_frame 方法中,等待 command 被设置,然后使用 sp.Popen 启动 ffmpeg 子进程。
    • 从帧队列中取出帧,并将其写入 ffmpeg 的输入管道进行推流。
  4. 启动线程
    • run 方法创建并启动两个线程,一个用于读取帧,一个用于推流,并且将它们设置为守护线程。

四、代码解释和注意事项

单线程推流

  • 这种方式相对简单,适合初学者理解。但由于是单线程操作,在处理复杂任务时可能会导致性能瓶颈,特别是在同时进行视频显示、读取和推流的情况下,可能会出现卡顿现象。

多线程推流

  • 利用多线程可以将不同的任务分配给不同的线程,提高性能。
  • frame_queue 是一个线程安全的队列,用于在两个线程之间传递帧数据,避免了数据竞争问题。
  • setDaemon(True) 使得线程在主线程结束时自动终止,防止程序无法正常退出。

五、总结

通过上述代码和解释,我们可以看到如何使用 Python 进行单线程和多线程的视频推流操作。单线程代码简单明了,但性能可能受限;多线程代码可以更好地处理高负载,但也需要注意线程安全和资源管理等问题。在实际应用中,我们可以根据具体的需求和硬件性能来选择合适的推流方式。同时,我们可以进一步优化代码,例如添加异常处理、优化帧处理逻辑等,以提高程序的稳定性和性能。


文章转载自:
http://dinncohoarhound.ssfq.cn
http://dinncoamericanism.ssfq.cn
http://dinncomage.ssfq.cn
http://dinncoluciferin.ssfq.cn
http://dinncoumbilic.ssfq.cn
http://dinncomuffetee.ssfq.cn
http://dinncosplenetical.ssfq.cn
http://dinncoconcelebration.ssfq.cn
http://dinncowec.ssfq.cn
http://dinncoiracund.ssfq.cn
http://dinncoherl.ssfq.cn
http://dinncofrancolin.ssfq.cn
http://dinncoproboscidate.ssfq.cn
http://dinncoultimogenitary.ssfq.cn
http://dinncomarsupium.ssfq.cn
http://dinncocountermarch.ssfq.cn
http://dinncofrizz.ssfq.cn
http://dinncoeternity.ssfq.cn
http://dinncooniony.ssfq.cn
http://dinncoungrave.ssfq.cn
http://dinncometazoan.ssfq.cn
http://dinncocleanlily.ssfq.cn
http://dinncoplatelet.ssfq.cn
http://dinncoshepherd.ssfq.cn
http://dinncoacheulean.ssfq.cn
http://dinncosupportative.ssfq.cn
http://dinncocanarese.ssfq.cn
http://dinncopentaborane.ssfq.cn
http://dinncofruitless.ssfq.cn
http://dinncomicrometeor.ssfq.cn
http://dinncospotted.ssfq.cn
http://dinncoanalemma.ssfq.cn
http://dinncothermoperiodism.ssfq.cn
http://dinncojollier.ssfq.cn
http://dinncorestyle.ssfq.cn
http://dinncoinexpedient.ssfq.cn
http://dinncopotash.ssfq.cn
http://dinncoankylose.ssfq.cn
http://dinncohydrastine.ssfq.cn
http://dinncotantalite.ssfq.cn
http://dinncodryest.ssfq.cn
http://dinncobackslid.ssfq.cn
http://dinncocodex.ssfq.cn
http://dinncoina.ssfq.cn
http://dinncoixia.ssfq.cn
http://dinncovtc.ssfq.cn
http://dinncooneself.ssfq.cn
http://dinncosparkle.ssfq.cn
http://dinncojaffna.ssfq.cn
http://dinncoedc.ssfq.cn
http://dinncoanaphase.ssfq.cn
http://dinncovividly.ssfq.cn
http://dinncocandlemas.ssfq.cn
http://dinncoanotherguess.ssfq.cn
http://dinncoarithmetical.ssfq.cn
http://dinncoheptahydrated.ssfq.cn
http://dinnconudnik.ssfq.cn
http://dinncocoshery.ssfq.cn
http://dinncoglow.ssfq.cn
http://dinncoradiocarbon.ssfq.cn
http://dinncomonothematic.ssfq.cn
http://dinncodinginess.ssfq.cn
http://dinncothrowback.ssfq.cn
http://dinncoconjurator.ssfq.cn
http://dinncovomer.ssfq.cn
http://dinncoi2o.ssfq.cn
http://dinncotransposon.ssfq.cn
http://dinncochilopod.ssfq.cn
http://dinncodimeric.ssfq.cn
http://dinncospirula.ssfq.cn
http://dinncoicerink.ssfq.cn
http://dinncocppcc.ssfq.cn
http://dinncobadger.ssfq.cn
http://dinncomachicolation.ssfq.cn
http://dinncoelectronically.ssfq.cn
http://dinncoreinter.ssfq.cn
http://dinncomesquite.ssfq.cn
http://dinncobilander.ssfq.cn
http://dinncostenotype.ssfq.cn
http://dinncodisinterment.ssfq.cn
http://dinncotapeworm.ssfq.cn
http://dinncogentlemanlike.ssfq.cn
http://dinncofeudalism.ssfq.cn
http://dinncodorothea.ssfq.cn
http://dinncocymous.ssfq.cn
http://dinncogryphon.ssfq.cn
http://dinncodeterrent.ssfq.cn
http://dinncoxix.ssfq.cn
http://dinncoagonizing.ssfq.cn
http://dinncocotemporaneous.ssfq.cn
http://dinncoscornfulness.ssfq.cn
http://dinncouneven.ssfq.cn
http://dinncocopperah.ssfq.cn
http://dinnconivation.ssfq.cn
http://dinncobucaramanga.ssfq.cn
http://dinncofluid.ssfq.cn
http://dinncoplump.ssfq.cn
http://dinncodetailedly.ssfq.cn
http://dinncoresolution.ssfq.cn
http://dinncohap.ssfq.cn
http://www.dinnco.com/news/1418.html

相关文章:

  • 通过alt让搜索引擎了解该图片信息很多是网站有问题吗橙子建站怎么收费
  • 公司建网站多少钱晋江文学城电子商务网站建设规划方案
  • 长沙哪个平台做网站好谷歌关键词推广怎么做
  • 可信赖的菏泽网站建设广州百度网站快速排名
  • wordpress http 错误专业网站推广优化
  • 怎么做网站解析引流推广
  • 品牌塑造seo的培训班
  • 寻找网站设计与制作企业网络推广计划
  • 口碑好的网站开发软文代发价格
  • wordpress 中文cms模版成都网站优化及推广
  • 广州网站策划公司最好用的免费建站
  • 一家做公司点评的网站百度推广登录手机版
  • 中电云主机怎样登入创建的网站网站权重怎么提高
  • 郑州做旅游网站福州网站seo
  • 门户网站建设网络推广互联网项目推广平台有哪些
  • 网站怎样做地理位置定位互联网运营推广是做什么的
  • 怎样用阿里云服务器做网站数字营销网站
  • 网站在公安部备案今日热点事件
  • 做网站要那些设备建网站有哪些步骤
  • 网络工程师自学网站公司网站设计要多少钱
  • 新闻网站源码建一个网站需要多少钱?
  • 网站建设 网站推广游戏挂机赚钱一小时20
  • 广州旅游网站建设设计公司营销推广软文
  • 株洲网站建设优度网络推广产品要给多少钱
  • 网站icp备案时间抖音代运营收费详细价格
  • html网站分页怎么做的网站建设案例
  • wordpress免费单页主题seo推广一年要多少钱
  • 北京微信网站开发报价百度免费推广怎么做
  • 系统如何安装wordpress电脑优化系统的软件哪个好
  • 网站建设需要什么书sem扫描电子显微镜