【python小工具-2】录屏录音

录屏录音-合成视频

在上网时,只能看却不能下载。

爬取链接下载后发现视频加了密。

加密了还不好破解。

是时候祭出杀招了!

自动录屏录音。

 

1.Video & Audio 录制

  • 截取屏幕
class VideoCapThread(threading.Thread):
    def __init__(self, videofile='record.avi'):
        threading.Thread.__init__(self)
        self.bRecord = True
        self.video = cv2.VideoWriter(videofile,
                                     cv2.VideoWriter_fourcc(*'XVID'), 32,
                                     ImageGrab.grab().size)  # 帧率为32,可以调节

    def run(self):
        while self.bRecord:
            im = ImageGrab.grab()
            imm = cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)
            self.video.write(imm)
        self.video.release()
        cv2.destroyAllWindows()

    def stoprecord(self):
        self.bRecord = False
  • 录制音频
class SoundRecThread(threading.Thread):
    def __init__(self, audiofile='record.wav'):
        threading.Thread.__init__(self)
        self.bRecord = True
        self.filename = audiofile
        self.samplerate = 44100
        self.channels = 2

    def run(self):
        q = queue.Queue()

        def callback(indata, frames, time, status):
            """This is called (from a separate thread) for each audio block."""
            if status:
                print(status, file=sys.stderr)
            q.put(indata.copy())

        with sf.SoundFile(self.filename,
                          mode='x',
                          samplerate=self.samplerate,
                          channels=self.channels) as file:
            with sd.InputStream(samplerate=self.samplerate,
                                channels=self.channels,
                                callback=callback):
                while self.bRecord:
                    file.write(q.get())

    def stoprecord(self):
        self.bRecord = False

2.合成视频

 

# coding=utf-8
import os
import subprocess
import time

from video_audio_cap import SoundRecThread, VideoCapThread

if __name__ == "__main__":
    output_path = ''
    avi_file = output_path + 'tmp.avi'
    wav_file = output_path + 'tmp.wav'
    t1 = VideoCapThread(avi_file)
    t2 = SoundRecThread(wav_file)
    t1.start()
    t2.start()
    time.sleep(5)  # 录制5s
    t1.stoprecord()
    t2.stoprecord()
    mp4_file = output_path + 'result.mp4'
    subprocess.call('ffmpeg -i {} -i {} -strict -2 -f mp4 {}'.format(
        avi_file, wav_file, mp4_file))
    os.remove(avi_file)
    os.remove(wav_file)

3.自动浏览网页

需要用selenuim自行调制哈。

源码地址:https://github.com/Zweo/Video_Audio_Record

你可能感兴趣的:(Python)