錄制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
繁體地址,請注明出處:http://hk.pswp.cn/web/82785.shtml
英文地址,請注明出處:http://en.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…