Skip to content

Commit 7e4a567

Browse files
committed
add recording specific window tutorial
1 parent abab573 commit 7e4a567

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
181181
- [How to Extract Frames from Video in Python](https://www.thepythoncode.com/article/extract-frames-from-videos-in-python). ([code](python-for-multimedia/extract-frames-from-video))
182182
- [How to Reverse Videos in Python](https://www.thepythoncode.com/article/reverse-video-in-python). ([code](python-for-multimedia/reverse-video))
183183
- [How to Extract Video Metadata in Python](https://www.thepythoncode.com/article/extract-media-metadata-in-python). ([code](python-for-multimedia/extract-video-metadata))
184+
- [How to Record a Specific Window in Python](https://www.thepythoncode.com/article/record-a-specific-window-in-python). ([code](python-for-multimedia/record-specific-window))
184185

185186

186187
For any feedback, please consider pulling requests.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [How to Record a Specific Window in Python](https://www.thepythoncode.com/article/record-a-specific-window-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- Example: for recording Chrome: `python record_specific_window.py Chrome`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import cv2
2+
import numpy as np
3+
import pyautogui
4+
import pygetwindow as gw
5+
import sys
6+
7+
# the window name, e.g "notepad", "Chrome", etc.
8+
window_name = sys.argv[1]
9+
10+
# define the codec
11+
fourcc = cv2.VideoWriter_fourcc(*"XVID")
12+
# frames per second
13+
fps = 12.0
14+
# the time you want to record in seconds
15+
record_seconds = 10
16+
# search for the window, getting the first matched window with the title
17+
w = gw.getWindowsWithTitle(window_name)[0]
18+
# activate the window
19+
w.activate()
20+
# create the video write object
21+
out = cv2.VideoWriter("output.avi", fourcc, fps, tuple(w.size))
22+
23+
for i in range(int(record_seconds * fps)):
24+
# make a screenshot
25+
img = pyautogui.screenshot(region=(w.left, w.top, w.width, w.height))
26+
# convert these pixels to a proper numpy array to work with OpenCV
27+
frame = np.array(img)
28+
# convert colors from BGR to RGB
29+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
30+
# write the frame
31+
out.write(frame)
32+
# show the frame
33+
cv2.imshow("screenshot", frame)
34+
# if the user clicks q, it exits
35+
if cv2.waitKey(1) == ord("q"):
36+
break
37+
38+
# make sure everything is closed when exited
39+
cv2.destroyAllWindows()
40+
out.release()
41+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
numpy
2+
opencv-python
3+
pyautogui
4+
pygetwindow

0 commit comments

Comments
 (0)