录制mp4

目录

单线程保存mp4

多线程保存mp4 rtsp

ffmpeg录制mp4


单线程保存mp4


import cv2
import imageiocv2.namedWindow('photo', 0)  # 0窗口大小可以任意拖动,1自适应
cv2.resizeWindow('photo', 1280, 720)
url ="rtsp://admin:aa123456@192.168.1.64/h264/ch1/main/av_stream"
cap = cv2.VideoCapture(1)
ret = cap.isOpened()
imgs = []
fps =30
index = 0
count = 0
strat_record = False
while (ret):ret, img = cap.read()if not ret: breakcv2.imshow('photo', img)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)if strat_record:imgs.append(img)index +=1if index %300 == 299 and strat_record:count+=1save_video_path = f'lanqiu_{count}.mp4'imageio.mimsave(save_video_path, imgs, fps=fps, macro_block_size=None)imgs=[]key = cv2.waitKey(1) & 0xFFif key == ord('q'):breakelif key == ord('s'):strat_record = Trueprint("start_record", strat_record)elif key == ord('e'):strat_record = Falseprint("end_record", strat_record)
cap.release()
save_video_path = f'lanqiu_{count}.mp4'
imageio.mimsave(save_video_path, imgs, fps=fps, macro_block_size=None)

多线程保存mp4 rtsp

import cv2
import threading
import queue
import time# 参数设置
url = "rtsp://admin:aa123456@192.168.1.64/h264/ch1/main/av_stream"
fps = 30
segment_time = 10  # 每段录制 10 秒
fourcc = cv2.VideoWriter_fourcc(*'mp4v')# 用于保存帧的队列
frame_queue = queue.Queue()
recording = False
stop_signal = False
video_count = 0# 保存线程函数
def save_video_worker():global video_countwhile True:if stop_signal and frame_queue.empty():breakframes = []start_time = time.time()while time.time() - start_time < segment_time:try:frame = frame_queue.get(timeout=1)frames.append(frame)except queue.Empty:continueif frames:h, w = frames[0].shape[:2]video_count += 1save_path = f'lanqiu_{video_count}.mp4'out = cv2.VideoWriter(save_path, fourcc, fps, (w, h))for f in frames:out.write(f)out.release()print(f"[保存完成] {save_path}")# 启动摄像头
cap = cv2.VideoCapture(url)
ret = cap.isOpened()cv2.namedWindow('photo', 0)
cv2.resizeWindow('photo', 1280, 720)# 开启保存线程(一直运行,直到设置 stop_signal)
thread = threading.Thread(target=save_video_worker)
thread.start()while ret:ret, frame = cap.read()if not ret:breakcv2.imshow('photo', frame)key = cv2.waitKey(1) & 0xFFif key == ord('q'):breakelif key == ord('s') and not recording:recording = Trueprint("[开始录制]")elif key == ord('e') and recording:recording = Falseprint("[停止录制]")if recording:frame_queue.put(frame.copy())  # 用 copy 避免线程间冲突cap.release()
stop_signal = True
thread.join()
cv2.destroyAllWindows()

ffmpeg录制mp4

import subprocess
import threading
import queue
import time
import cv2
import numpy as np# === 参数设置 ===
rtsp_url = "rtsp://admin:aa123456@192.168.1.64/h264/ch1/main/av_stream"
width, height = 1280, 720
fps = 25
segment_time = 10  # 每段录制时间(秒)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')recording = False
stop_signal = False
video_count = 0frame_queue = queue.Queue()# === 保存线程函数 ===
def save_video_worker():global video_countwhile not stop_signal or not frame_queue.empty():frames = []start_time = time.time()while time.time() - start_time < segment_time:try:frame = frame_queue.get(timeout=1)frames.append(frame)except queue.Empty:continueif frames:video_count += 1out = cv2.VideoWriter(f'video_segment_{video_count}.mp4', fourcc, fps, (width, height))for f in frames:out.write(f)out.release()print(f"[保存完成] video_segment_{video_count}.mp4")# === 启动 FFmpeg 读取 RTSP ===
ffmpeg_cmd = [r'E:\soft\ffmpeg-7.1.1-full_build\ffmpeg-7.1.1-full_build\bin\ffmpeg.exe','-rtsp_transport', 'tcp','-i', rtsp_url,'-f', 'rawvideo','-pix_fmt', 'bgr24','-vf', f'scale={width}:{height}','-'
]pipe = subprocess.Popen(ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=10**8)# === 启动保存线程 ===
thread = threading.Thread(target=save_video_worker)
thread.start()# === 实时显示和按键控制 ===
cv2.namedWindow("photo", 0)
cv2.resizeWindow("photo", width, height)try:while True:raw_frame = pipe.stdout.read(width * height * 3)if not raw_frame:print("视频读取失败,退出")breakframe = np.frombuffer(raw_frame, np.uint8).reshape((height, width, 3))cv2.imshow("photo", frame)key = cv2.waitKey(1) & 0xFFif key == ord('q'):breakelif key == ord('s') and not recording:recording = Trueprint("[开始录制]")elif key == ord('e') and recording:recording = Falseprint("[停止录制]")if recording:frame_queue.put(frame.copy())except KeyboardInterrupt:print("中断退出")# === 清理资源 ===
stop_signal = True
thread.join()
pipe.terminate()
cv2.destroyAllWindows()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/web/82785.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ISBN书号查询接口如何用PHP实现调用?

一、什么是ISBN书号查询接口 ISBN数据查询接口是一项图书信息查询服务。它基于全球通用的ISBN编码系统&#xff0c;帮助用户快速获取图书的详细信息&#xff0c;包括书名、作者、出版社、出版时间、价格、封面等关键字段。 该接口广泛应用于电商平台、图书馆管理系统、二手书…

Redis底层数据结构之深入理解跳表(2)

上一篇文章中我们详细讲述了跳表的增添、查找和修改的操作&#xff0c;这篇文章我们来讲解一下跳表在多线程并发时的安全问题。在Redis中&#xff0c;除了网络IO部分和大文件的后台复制涉及到多线程外&#xff0c;其余任务执行时全部都是单线程&#xff0c;这也就意味着在Redis…

Go语言依赖管理与版本控制-《Go语言实战指南》

在现代软件开发中&#xff0c;项目的第三方依赖和版本控制扮演着至关重要的角色。Go 语言自 Go 1.11 引入 Modules&#xff08;模块化管理&#xff09;以来&#xff0c;已经实现了内建的依赖管理机制&#xff0c;彻底摆脱了传统 GOPATH 模式的限制。 本章将深入探讨如何使用 Go…

Appium+python自动化(十一)- 元素定位- 下

1、 List定位 List顾名思义就是一个列表&#xff0c;在python里面也有list这一个说法&#xff0c;如果你不是很理解什么是list&#xff0c;这里暂且理解为一个数组或者说一个集合。首先一个list是一个集合&#xff0c;那么他的个数也就成了不确定性&#xff0c;所以这里需要用复…

stress 服务器压力测试的工具学习

一、stress 工具介绍 tress 是一种工具&#xff0c;可以对符合 POSIX 标准的操作系统施加可配置数量的 CPU、内存、I/O 或磁盘压力&#xff0c;并报告其检测到的任何错误。 stress 不是一个基准测试。它是由系统管理员用来评估其系统扩展性的工具&#xff0c;由内核程序员用来…

不止抓请求:5种开发场景中的抓包组合策略(含 Charles 等工具)

很多开发者用抓包&#xff0c;只在“接口调不通”的时候。 但在复杂项目中&#xff0c;抓包早已不仅是调错工具&#xff0c;更是开发节奏提速器、协作问题解耦器、架构瓶颈探测器。 关键在于——不同场景下&#xff0c;你要用对方法、配对工具。 以下是我根据日常开发实战&a…

蓝桥杯3498 01串的熵

问题描述 对于一个长度为 23333333的 01 串, 如果其信息熵为 11625907.5798&#xff0c; 且 0 出现次数比 1 少, 那么这个 01 串中 0 出现了多少次? #include<iostream> #include<cmath> using namespace std;int n 23333333;int main() {//枚举 0 出现的次数//因…

计算机系统大作业——程序人生

计算机系统 大作业 题 目 程序人生-Hello’s P2P 专 业 物联网工程 学   号 2022112820 班 级 2237301 学 生 孟宇航 指 导 教 师 吴 锐 计算机科学与技术学院 2024年…

〈软件安装管家软件目录〉▷Windows系统版

①装机常用 ☞压缩解压WinRAR7-ZIPBandZip360压缩☞文件工具EverythingOneCommander XYplorer ReNamer ☞卸载软件CCleanerIObitUninstallerUninstall toolGeekAutodesk卸载Adobe卸载Ashampoo☞驱动软件驱动人生&#xff08;离线版&#xff09;驱动精灵&#xff08;离线版&…

CentOS Stream 8 Unit network.service not found

一、问题现象 在 CentOS Stream 8 操作系统中&#xff0c;配置完静态IP 信息&#xff0c;想重启网络服务。 执行如下命令&#xff1a; systemctl restart network 提示信息如下&#xff1a; Failed to restart network.service: Unit network.service not found. 二、问题…

极空间z4pro配置gitea mysql,内网穿透

极空间z4pro配置gitea mysql等记录&#xff0c;内网穿透 1、mysql、gitea镜像下载&#xff0c;极空间不成功&#xff0c;先用自己电脑科学后下载镜像,拉取代码&#xff1a; docker pull --platform linux/amd64 gitea/gitea:1.23 docker pull --platform linux/amd64 mysql:5.…

[假面骑士] 龙骑浅谈

作为一个伪二次元的我&#xff0c;总感觉目前没有什么好番可追。结果某一天在小破站刷到了一个假面骑士相关的视频&#xff0c;我就突发奇想&#xff0c;要不把假面骑士补完算了。 搜了搜&#xff0c;版权全在企鹅哪儿&#xff0c;不想充&#xff0c;于是去找了某盘的资源。果…

F5 GSLB 最佳实践:如何手动将Wide IP 故障转移到另一个数据中心

下面简要介绍如何手动将 Wide IP(用于 DNS 负载均衡)故障转移到另一个数据中心,并提供一些最佳实践。假设您使用 F5 BIG-IP DNS(以前称为 GTM)管理一个 Wide IP,该 IP 引用位于不同数据中心的虚拟服务器 (VIP)。 典型的 GSLB (BIG-IP DNS) 设置 Wide IP:表示您想要全局负…

FART 脱壳某大厂 App + CodeItem 修复 dex + 反编译还原源码

版权归作者所有&#xff0c;如有转发&#xff0c;请注明文章出处&#xff1a;https://cyrus-studio.github.io/blog/ FART 脱壳 fartthread 方法在 app 启动的时候&#xff08;ActivityThread&#xff09;开启 fart 线程&#xff0c;休眠 60 秒&#xff0c;等待 app 启动完成后…

在maven项目中 继续增加maven 项目

背景项目 基于若依项目 由于若依项目都是Maven项目有父子结构因此自己建项目 也需如此管理 添加子Maven项目 利用idea 自带工具 maven archetype 这里选 webapp 骨架 在这里构建自己的项目架子即可 将 这个架子加入到启动类中

网络攻防技术十四:入侵检测与网络欺骗

文章目录 一、入侵检测概述二、入侵系统的分类三、入侵检测的分析方法1、特征检测&#xff08;滥用检测、误用检测&#xff09;2、异常检测 四、Snort入侵检测系统五、网络欺诈技术1、蜜罐2、蜜网3、网络欺骗防御 六、简答题1. 入侵检测系统对防火墙的安全弥补作用主要体现在哪…

吴恩达MCP课程(5):mcp_chatbot_prompt_resource.py

前提条件&#xff1a; 1、吴恩达MCP课程&#xff08;5&#xff09;&#xff1a;research_server_prompt_resource.py 2、server_config_prompt_resource.json文件 {"mcpServers": {"filesystem": {"command": "npx","args"…

【Linux】Linux基础指令3

1. which指令 功能&#xff1a;搜索系统指定的命令 2. whereis指令 功能&#xff1a;⽤于找到程序的源、⼆进制⽂件或⼿册 3. grep指令 语法&#xff1a; grep [ 选项 ] 搜寻字符串 ⽂件 功能&#xff1a;在⽂件中搜索字符串&#xff0c;将找到的⾏打印出来 常⽤选项&…

李沐《动手学深度学习》d2l安装教程

文章目录 最新回答报错提醒安装对应版本安装C工具和Windows SDK 最新回答 安装旧版本即可 pip install d2l0.17.0 WARNING: Ignoring invalid distribution -pencv-python (e:\python3.10\lib\site-packages) Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple C…

CMake 为 Debug 版本的库或可执行文件添加 d 后缀

在使用 CMake 构建项目时,我们经常需要区分 Debug 和 Release 构建版本。一个常见的做法是为 Debug 版本的库或可执行文件添加后缀(如 d),例如 libmylibd.so 或 myappd.exe。 本文将介绍几种在 CMake 中实现为 Debug 版本自动添加 d 后缀的方法。 方法一:使用 CMAKE_DEBU…