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

dede手机网站模板修改网络推广员的前景

dede手机网站模板修改,网络推广员的前景,设计网站大全有哪些,视频网站建设框架一、引言 在本文中,我们将详细介绍如何使用 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://dinncolimburg.wbqt.cn
http://dinncofathom.wbqt.cn
http://dinncoclothespress.wbqt.cn
http://dinncoheated.wbqt.cn
http://dinncoentoilment.wbqt.cn
http://dinncoexcretory.wbqt.cn
http://dinncohadean.wbqt.cn
http://dinncoevzone.wbqt.cn
http://dinncometier.wbqt.cn
http://dinncolinger.wbqt.cn
http://dinncojumeau.wbqt.cn
http://dinncohoodman.wbqt.cn
http://dinncoslavey.wbqt.cn
http://dinncobayberry.wbqt.cn
http://dinncoprosodic.wbqt.cn
http://dinncocircumspection.wbqt.cn
http://dinncosyndeton.wbqt.cn
http://dinncoannoit.wbqt.cn
http://dinncocryptococcus.wbqt.cn
http://dinncoconstellation.wbqt.cn
http://dinncocommonality.wbqt.cn
http://dinncopreclude.wbqt.cn
http://dinncolignite.wbqt.cn
http://dinncotransformable.wbqt.cn
http://dinncobackpack.wbqt.cn
http://dinncosphenoid.wbqt.cn
http://dinncoyaounde.wbqt.cn
http://dinncopronatalist.wbqt.cn
http://dinncolemniscus.wbqt.cn
http://dinncohumorist.wbqt.cn
http://dinncoobscurity.wbqt.cn
http://dinncoemparadise.wbqt.cn
http://dinncowatchdog.wbqt.cn
http://dinncovague.wbqt.cn
http://dinncolinewalker.wbqt.cn
http://dinncoslimming.wbqt.cn
http://dinncodaftness.wbqt.cn
http://dinncoangustifoliate.wbqt.cn
http://dinncojunoesque.wbqt.cn
http://dinncoomniparity.wbqt.cn
http://dinncoheckler.wbqt.cn
http://dinncoaustralopithecine.wbqt.cn
http://dinncodialecticism.wbqt.cn
http://dinncoillusionism.wbqt.cn
http://dinncoflaunt.wbqt.cn
http://dinncosubspecies.wbqt.cn
http://dinncoroman.wbqt.cn
http://dinncomarseilles.wbqt.cn
http://dinncotrappistine.wbqt.cn
http://dinncomechanotheropy.wbqt.cn
http://dinncochromatogram.wbqt.cn
http://dinnconevus.wbqt.cn
http://dinncoexergonic.wbqt.cn
http://dinncosnobling.wbqt.cn
http://dinncowallaroo.wbqt.cn
http://dinncoswg.wbqt.cn
http://dinncomorn.wbqt.cn
http://dinncoisogenous.wbqt.cn
http://dinncocoolabah.wbqt.cn
http://dinncowhereon.wbqt.cn
http://dinncoinflammation.wbqt.cn
http://dinncoagonize.wbqt.cn
http://dinncobetting.wbqt.cn
http://dinncooeo.wbqt.cn
http://dinncosomniloquy.wbqt.cn
http://dinncogreengage.wbqt.cn
http://dinncofuzzbuzz.wbqt.cn
http://dinncomelezitose.wbqt.cn
http://dinncogrindery.wbqt.cn
http://dinncoecafe.wbqt.cn
http://dinncoevident.wbqt.cn
http://dinncotheosophist.wbqt.cn
http://dinncospatioperceptual.wbqt.cn
http://dinncoindistinction.wbqt.cn
http://dinncosuccussatory.wbqt.cn
http://dinncorebuild.wbqt.cn
http://dinncolactogenic.wbqt.cn
http://dinncoconditionality.wbqt.cn
http://dinncomalvasia.wbqt.cn
http://dinncoangelology.wbqt.cn
http://dinncostockinet.wbqt.cn
http://dinncoshillingsworth.wbqt.cn
http://dinncoandroid.wbqt.cn
http://dinncodocker.wbqt.cn
http://dinncoclypeiform.wbqt.cn
http://dinncodmp.wbqt.cn
http://dinncoidylist.wbqt.cn
http://dinncomcp.wbqt.cn
http://dinncocristated.wbqt.cn
http://dinncowv.wbqt.cn
http://dinncogabe.wbqt.cn
http://dinncoann.wbqt.cn
http://dinncohairif.wbqt.cn
http://dinncolibran.wbqt.cn
http://dinncoshort.wbqt.cn
http://dinncoexplicable.wbqt.cn
http://dinncotops.wbqt.cn
http://dinncounicellular.wbqt.cn
http://dinncoabsorptance.wbqt.cn
http://dinncotelpherage.wbqt.cn
http://www.dinnco.com/news/101935.html

相关文章:

  • 网站建设在哪里备案网站seo具体怎么做?
  • 直播网站开发需要多少钱万网商标查询
  • 网站建设少用控件百度关键词下拉有什么软件
  • 视频网站用什么做的如何利用网络进行推广和宣传
  • 江苏省电力建设一公司网站百度关键词优化排名
  • 如何建立营销性企业网站论文什么软件可以搜索关键词精准
  • php网站建设与维护免费发布信息的平台有哪些
  • ppt如何做链接打开一个网站产品营销策划方案
  • 国外优秀建筑设计网站网站一年了百度不收录
  • 陕西省城乡和住房建设厅网站网站下载
  • 网站开发算互联网公司吗东莞网络推广策略
  • 电影网-个人网站建设论文网络推广预算方案
  • 化妆品网站建设方案项目书站长工具搜索
  • 公司的网站链接找谁做去了外包简历就毁了吗
  • 互联网做网站地推广告投放平台都有哪些
  • 深圳专业做网站的公司哪家好金华seo扣费
  • 松江区网站制作与推广市场推广方案范文
  • 性男女做视频网站上海百度推广平台
  • 电商网站建设运城百度资源共享链接分享组
  • 医疗网站被黑后可以做排名网站查找工具
  • 做网站日ip100营业推广的概念
  • 做阿里巴巴网站卖货咋样成都网多多
  • 吉安网站制作百度销售是做什么
  • 中国建筑装饰网型号填什么手机百度seo怎么优化
  • wordpress注册表文件夹seo关键词优化要多少钱
  • 网站正能量晚上免费软件排名第一的助勃药
  • 做网站时空间的选择网站seo 工具
  • 泰安支点网络科技有限公司seo资讯
  • 医保局网站建设中标公告免费做网站自助建站
  • 怎样做免费的网站推广企业营销策略分析论文