最近給客戶演示程序運(yùn)行結(jié)果,我就想到用Python寫一個(gè)錄屏程序,在網(wǎng)上能找到現(xiàn)成的源碼,但是它的錄屏是錄制整個(gè)屏幕的。但是在屏幕桌面下方的任務(wù)欄工具欄里有些東西,不希望被錄制到視頻里,因此需要實(shí)現(xiàn)一個(gè)選擇區(qū)域錄屏,就像qq截圖那樣的。我編寫的程序如下,在主函數(shù)的輸入?yún)?shù)里有一個(gè)選項(xiàng)控制是全屏錄制還是選擇區(qū)域錄制。在編寫這個(gè)程序時(shí),我有一個(gè)疑問(wèn),在初始化寫視頻VideoWriter函數(shù)的第4個(gè)參數(shù),它表示視頻幀的高和寬,
全屏錄制方式的參數(shù)是(height,width),選擇區(qū)域錄制的參數(shù)是(width,height)。我曾嘗試過(guò)把這個(gè)參數(shù)統(tǒng)一寫成(height,width)或者(width,height)。但是運(yùn)行程序后,總有一種錄屏方式保存的視頻文件是無(wú)法打開的,文件大小是1kb,不知這是為何?
import cv2
from PIL import ImageGrab
import numpy as np
import argparse
import time
global img
global point1, point2
def on_mouse(event, x, y, flags, param):
global img, point1, point2
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: # 左鍵點(diǎn)擊
point1 = (x, y)
cv2.circle(img2, point1, 10, (0, 255, 0), thickness=2)
cv2.imshow('image', img2)
elif event == cv2.EVENT_MOUSEMOVE and (flags cv2.EVENT_FLAG_LBUTTON): # 按住左鍵拖曳
cv2.rectangle(img2, point1, (x, y), (255, 0, 0), thickness=2)
cv2.imshow('image', img2)
elif event == cv2.EVENT_LBUTTONUP: # 左鍵釋放
point2 = (x, y)
cv2.rectangle(img2, point1, point2, (0, 0, 255), thickness=2)
cv2.imshow('image', img2)
def select_roi(frame):
global img, point1, point2
img = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR)
winname = 'image'
cv2.namedWindow(winname, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(winname, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.setMouseCallback(winname, on_mouse)
cv2.imshow(winname, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
return point1, point2
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--fps', type=int, default=10, help='frame per second')
parser.add_argument('--total_time', type=int, default=15, help='video total time')
parser.add_argument('--savename', type=str, default='video.mp4', help='save file name')
parser.add_argument('--screen_type', default=1, type=int, choices=[0, 1], help='1: full screen, 0: region screen')
args = parser.parse_args()
print('等到3秒,請(qǐng)切換到錄屏的頁(yè)面')
if args.screen_type==0:
print('Press Esc to close window')
time.sleep(3)
curScreen = ImageGrab.grab() # 獲取屏幕對(duì)象
if args.screen_type:
height, width = curScreen.size
min_x, min_y, max_x, max_y = 0, 0, width, height
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(args.savename, fourcc, args.fps, (height, width))
else:
point1, point2 = select_roi(curScreen)
min_x = min(point1[0], point2[0])
min_y = min(point1[1], point2[1])
max_x = max(point1[0], point2[0])
max_y = max(point1[1], point2[1])
height, width = max_y - min_y, max_x - min_x
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(args.savename, fourcc, args.fps, (width, height))
imageNum = 0
while True:
imageNum += 1
captureImage = ImageGrab.grab() # 抓取屏幕
frame = cv2.cvtColor(np.array(captureImage), cv2.COLOR_RGB2BGR)
if args.screen_type==0:
frame = frame[min_y:max_y, min_x:max_x, :]
if imageNum args.fps * args.total_time:
video.write(frame)
# 退出條件
if cv2.waitKey(50) == ord('q') or imageNum > args.fps * args.total_time:
break
video.release()
cv2.destroyAllWindows()
到此這篇關(guān)于python實(shí)現(xiàn)錄制全屏和選擇區(qū)域錄屏功能的文章就介紹到這了,更多相關(guān)python錄屏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!