Screen recording principle
Screen recording, that is, screen capture, refers to recording activities on the computer screen and generating video files. Screen recording tools usually include the following core parts:
Image Capture: Capture image data on the screen.
Audio Capture: System sound or microphone input when recording screen activity.
Encoding compression: Encoding and compressing the captured image and audio data to generate video files.
Output saving: Save the encoded video data to a file.
Image Capture
Image capture is the basis of screen recording. In Python, you can use the pyautogui library to capture screen images. The pyautogui library can easily capture specified areas on the screen.
import pyautogui # Capture the entire screenscreenshot = () # Capture the specified arearegion_screenshot = (region=(0, 0, 300, 400))
Audio Capture
Audio capture often requires the use of additional libraries such as pyaudio. The pyaudio library allows us to capture system sound or microphone input.
import pyaudio import wave # Initialize PyAudio instancep = () # Turn on the audio streamstream = (format=pyaudio.paInt16, channels=2, rate=44100, input=True, frames_per_buffer=1024) # Read audio dataframes = [] while True: data = (1024) (data) # Stop and close the audio streamstream.stop_stream() () () # Save audio data to filewf = ('', 'wb') (2) (p.get_sample_size(pyaudio.paInt16)) (44100) (b''.join(frames)) ()
Encoding compression
Captured image and audio data usually need to be encoded and compressed to generate a video file. In Python, you can use the ffmpeg library for video encoding.
import subprocess # Use ffmpeg to synthesize images and audio into videoscommand = "ffmpeg -f image2 -pattern_type glob -i 'images/*.png' -i -c:v libx264 -c:a aac -strict experimental output.mp4" (command, shell=True)
Output save
Finally, save the encoded video data to the file. In the example above, we have used ffmpeg to synthesize the image and audio into video and save the output as output.mp4.
Complete screen recording tool
Now we can combine the above parts to create a complete screen recording tool.
import pyautogui import pyaudio import wave import subprocess import cv2 import numpy as np from datetime import datetime def capture_screen(region=None): if region: return (region=region) else: return () def capture_audio(): p = () stream = (format=pyaudio.paInt16, channels=2, rate=44100, input=True, frames_per_buffer=1024) frames = [] while True: data = (1024) (data) stream.stop_stream() () () return frames def save_audio(frames, filename): p = () wf = (filename, 'wb') (2) (p.get_sample_size(pyaudio.paInt16)) (44100) (b''.join(frames)) () def save_video(images, audio_filename, output_filename): image_pattern = "images/image%" (f"ffmpeg -f image2 -pattern_type glob -i '{image_pattern}' -i {audio_filename} -c:v libx264 -c:a aac -strict experimental {output_filename}", shell=True) def record_screen(region=None, duration=10): images = [] frames = capture_audio() start_time = () while (() - start_time).seconds < duration: screenshot = capture_screen(region=region) (screenshot) save_audio(frames, "") for i, image in enumerate(images): (f"images/image{i:04d}.png") save_video(images, "", "output.mp4") #User Examplerecord_screen(region=(0, 0, 1280, 720), duration=20)
In the above code, we define a record_screen function that accepts the area and duration of the screen captured as parameters. This function first captures the audio and then captures the screen image for the specified duration. Finally, it saves the captured audio and images as video files.
Advanced features
Live preview
Providing a real-time preview feature enhances the user experience when recording screen activities. Real-time preview can be achieved using the opencv library.
import cv2 def show_preview(region=None): while True: screenshot = capture_screen(region=region) image = (screenshot) ('Preview', image) if (1) & 0xFF == ord('q'): break ()
Add watermark
To protect copyright or identify the recorder, a watermark can be added to the recorded video. You can use the Pillow library to add text watermarks to images.
from PIL import Image, ImageDraw, ImageFont def add_watermark(image, text, position=(0, 0)): draw = (image) font = ('', 36) (position, text, (255, 255, 255), font=font) return image
Before saving the image, call the add_watermark function.
watermarked_image = add_watermark(screenshot, "CSDN Blogger", position=(10, 10)) watermarked_image.save(f"images/image{i:04d}.png")
Multi-platform support
In order for screen recording tools to run on multiple platforms, the characteristics and limitations of different platforms need to be considered. You can use the platform module to detect the current operating system and adjust the code as needed.
import platform def get_platform(): return () if get_platform() == "Windows": # Windows-specific codeelif get_platform() == "Darwin": # macOS specific codeelse: # LinuxSpecific code
This is the end of this article about the implementation of the screen recording function of Python in actual combat. For more related Python screen recording content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!