首页 > 编程语言 >python: Screen Recording

python: Screen Recording

时间:2023-08-02 16:12:44浏览次数:30  
标签:img python Screen cv2 Recording fourcc output screen out

 用VLC media player播放录屏文件

"""
python.exe -m pip install --upgrade pip
pip install pyautogui
pip install opencv-python
pip install pywin32

python 3.11 """ # This is a sample Python script. import cv2 import pyautogui import numpy as np import os import win32api from win32api import GetSystemMetrics from win32.lib import win32con import time # Press Shift+F10 to execute it or replace it with your code. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. def win32screen(): # Take resolution from system automatically w = GetSystemMetrics(0) h = GetSystemMetrics(1) SCREEN_SIZE = (w, h) fourcc = cv2.VideoWriter_fourcc(*"XVID") out = cv2.VideoWriter("recording.mp4", fourcc, 20.0, (SCREEN_SIZE)) tim = time.time() tp = int(input('How many times you want to record screen?->(Define value in Seconds): ')) tp = tp + tp f = tim + tp while True: img = pyautogui.screenshot() frame = np.array(img) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) out.write(frame) tu = time.time() if tu > f: break cv2.destroyAllWindows() out.release() def screen(output:str): """ :param outputfile: :return: """ #output = "video.avi" img = pyautogui.screenshot() img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) # get info from img height, width, channels = img.shape # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(output, fourcc, 20.0, (width, height)) while (True): try: img = pyautogui.screenshot() image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) out.write(image) StopIteration(0.5) except KeyboardInterrupt: break out.release() cv2.destroyAllWindows() def record_screen(output_file, fps=30): """ :param output_file: :param fps: :return: """ # 获取屏幕宽度和高度 screen_width, screen_height = pyautogui.size() # 创建视频编码器 fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(output_file, fourcc, fps, (screen_width, screen_height)) # 设置屏幕录制区域 screen_area = (0, 0, screen_width, screen_height) while True: # 捕获屏幕图像 img = pyautogui.screenshot(region=screen_area) # 转换图像颜色空间 frame = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) # 将图像写入视频文件 out.write(frame) # 按下q键停止录屏 if cv2.waitKey(1) == ord('q'): break # 释放资源 out.release() cv2.destroyAllWindows() print(f'录制已保存为{output_file}') def main(): """ :return: """ output_file = 'screencast.mp4' fps = 30 print('按下q键停止录屏') record_screen(output_file, fps) def print_hi(name): """ :param name: :return: """ # Use a breakpoint in the code line below to debug your script. print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. # Press the green button in the gutter to run the script. if __name__ == '__main__': print_hi('PyCharm,涂聚文,Geovin Du') main() # See PyCharm help at https://www.jetbrains.com/help/pycharm/

  

 

标签:img,python,Screen,cv2,Recording,fourcc,output,screen,out
From: https://www.cnblogs.com/geovindu/p/17600937.html

相关文章

  • python开发实战——ip池
    前言代理IP池是一组可用的代理IP地址,用于访问网站或执行其他网络请求。它可以帮助我们在网络请求时隐藏我们的真实IP地址,从而提高网络安全性、匿名性和稳定性。同时,代理IP池还可以通过定时更新和测试代理IP,保证代理IP的有效性和稳定性。本文将介绍如何使用Python编写代理IP池,包括......
  • 17道经典考题,检验你的 Python 基本功
    Python是一门非常优美的语言,其简洁易用令人不得不感概人生苦短。在本文中,作者GauthamSanthosh带我们回顾了17个非常有用的Python技巧,例如查找、分割和合并列表等。这17个技巧都非常简单,但它们都很常用且能激发不一样的思路。人生苦短,为什么我要用Python?很多读者都知道Py......
  • 记一次 gunicorn+python+flask+venv 部署过程
    记一次gunicorn+python+flask+venv部署过程flask直接部署到服务器会有警告,本身也是不稳定的,它只是一个应用。需要一个独立的Server来承担WSGI角色和责任。venv是pytohn的虚拟环境,用来隔离不同项目的包版本不一致的问题。python3.6+以上都自带有,在部署之前先创建虚拟环境。1、......
  • [算法题python]822.翻转卡片游戏
    在桌子上有 n 张卡片,每张卡片的正面和背面都写着一个正数(正面与背面上的数有可能不一样)。我们可以先翻转任意张卡片,然后选择其中一张卡片。如果选中的那张卡片背面的数字 x 与任意一张卡片的正面的数字都不同,那么这个数字是我们想要的数字。哪个数是这些想要的数字中最小的......
  • Python | 基础语法
    1.字面量字面量:在代码中,被写下来的固定的值1.1Python常用的6种值(数据)的类型类型描述说明数字整数(int)、浮点数(float)、复数(complex)、布尔(bool)字符串描述文本的一种数据类型由任意数量的字符组成列表有序的可变序列元组有序的不可变序列可......
  • [算法题python]14. 最长公共前缀
    编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。 示例1:输入:strs=["flower","flow","flight"]输出:"fl"示例2:输入:strs=["dog","racecar","car"]输出:""解释:输入不存在公共前缀。 提示:......
  • python3 压缩图片到合理范围
    importosfromPILimportImagefromPILimportImageFileimportimghdrdefcompress_image(outfile,mb=200,quality=85,k=0.9):#修改mb大小,就是想要设定的压缩后的大小。"""不改变图片尺寸压缩到指定大小:paramoutfile:压缩文件保存地址:parammb:压缩目标,KB:pa......
  • 关于命令行运行python 报错的问题
    装好python以后,想看看是不是装好了,能否运行,在命令行中输入python以后,蹦出来这么一个东西:看这意思是环境变量没有配置好,但是很奇怪,在装的时候我是勾上了添加环境变量的呀,没办法,智能求助于度娘,网上说是要添加PYTHONHOME和PYTHONPATH两个环境变量,然后怎么怎么操作,但是我尝试后发现......
  • Python爬虫爬取B站评论区
    写了两天,参考其他大牛的文章,摸着石头过河,终于写出了一个可以爬B站评论区的爬虫,人裂了……致谢:致谢:SmartCrane马哥python说该程序具有以下功能:①输入B站视频链接,即可爬取B站评论区评论、IP、ID、点赞数、回复数,并保存在当前目录的以视频名字为标题的csv文件中。②由视频链......
  • python最简单的传参方法-第一次见这种方法
    我又一个python文件,名为grounding_dino_demo.py,其代码为:fromgroundingdino.util.inferenceimportload_model,load_image,predict,annotate,Modelimportcv2CONFIG_PATH="GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py"CHECKPOINT_PATH=&......