60行Python代码打造自己的录屏软件(附源码)

1. 功能描述

编写录屏软件(屏幕+录音)。

2. 技术思路

使用两个线程,一个使用pyaudio录音,一个使用pillow不停地截屏保存图片,最后再把音频和所有图片合成为视频文件。中间过程需要一定的硬盘空间。

3. 技术难点

使用opencv+pyaudio是目前网上主流的录屏技术原理,但是音频和视频的同步是个大问题,本文pillow+pyaudio的技术不存在同步问题。
用到的扩展库:numpy、pyaudio、pillow、moviepy
要注意,pyaudio暂时不支持Python 3.7和3.8,如果使用3.6的话可以直接使用pip安装,如果是3.7或3.8的话需要下载whl文件然后离线安装,具体参考:python3.8和3.7安装pyaudio

4. 参考代码

# -*- coding: utf-8 -*-
# @Author : Coco
# @About author: https://coco56.gitee.io/blog/about/
# @IDE: PyCharm
# @Python: Python3.8.1
# @Created Time :2020/2/5 23:37
# @File : screenRecorder.py


import wave
import threading
from os import remove, mkdir, listdir
from os.path import exists, splitext, basename, join
from datetime import datetime
from time import sleep
from shutil import rmtree
import pyaudio
from PIL import ImageGrab
from moviepy.editor import *

CHUNK_SIZE = 1024
CHANNELS = 2
FORMAT = pyaudio.paInt16
RATE = 48000
allowRecording = True


def record_audio():
	p = pyaudio.PyAudio()
	# 创建输入流
	stream = p.open(format=FORMAT, channels=CHANNELS,
					rate=RATE, input=True,
					frames_per_buffer=CHUNK_SIZE)
	wf = wave.open(audio_filename, 'wb')
	wf.setnchannels(CHANNELS)
	wf.setsampwidth(p.get_sample_size(FORMAT))
	wf.setframerate(RATE)
	while allowRecording:
		# 从录音设备读取数据,直接写入wav文件
		data = stream.read(CHUNK_SIZE)
		wf.writeframes(data)
	wf.close()
	stream.stop_stream()
	stream.close()
	p.terminate()


def record_screen():
	index = 1
	while allowRecording:
		ImageGrab.grab().save(f'{pic_dir}\{index}.jpg',
							  quality=95, subsampling=0)
		sleep(0.04)
		index = index + 1


audio_filename = str(datetime.now())[:19].replace(':', '_') + '.mp3'
pic_dir = 'pics'
if not exists(pic_dir):
	mkdir(pic_dir)
video_filename = audio_filename[:-3] + 'avi'
# 创建两个线程,分别录音和录屏
t1 = threading.Timer(3, record_audio)
t2 = threading.Timer(3, record_screen)
t1.start()
t2.start()
print('3秒后开始录制,按q键结束录制')
while (ch := input()) != 'q':
	pass
allowRecording = False
t1.join()
t2.join()

# 把录制的音频和屏幕截图合成为视频文件
audio = AudioFileClip(audio_filename)
pic_files = [join(pic_dir, fn) for fn in listdir(pic_dir)
			 if fn.endswith('.jpg')]
# 按文件名编号升序排序
pic_files.sort(key=lambda fn: int(splitext(basename(fn))[0]))
# 计算每个图片的显示时长
each_duration = round(audio.duration / len(pic_files), 4)
# 连接多个图片
image_clips = []
for pic in pic_files:
	image_clips.append(ImageClip(pic,
								 duration=each_duration))
video = concatenate_videoclips(image_clips)
video = video.set_audio(audio)
video.write_videofile(video_filename, codec='mpeg4', fps=24)
# 删除临时音频文件和截图
remove(audio_filename)
rmtree(pic_dir)

运行界面如下,在cmd环境执行程序,3秒钟后开始录屏,最后按q退出并合成视频。
在这里插入图片描述

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页