Python Screen Capture Tutorial ( Let's Code )
Published 2023-08-06
a few mistakes were made in the making of the code video such as indentation errors in the ffmpeg_cmd sub routine. I have included the correct code below and please customize and use it for your own stuff.
import os
import time
import subprocess
import pyautogui
from moviepy.editor import *
def record_screen_with_audio(duration=10, output_filename="output.mp4"):
screen_width, screen_height = pyautogui.size()
original_mouse_position = pyautogui.position()
pyautogui.moveTo(0, 0)
temp_file = "temp.mp4"
ffmpeg_cmd = (
f"ffmpeg -y -f x11grab -s {screen_width}x{screen_height} -i :0.0 "
f"-f alsa -ac 2 -i default -t {duration} -c:v libx264 -c:a aac -strict experimental -r 30 {temp_file}"
)
subprocess.Popen(ffmpeg_cmd, shell=True)
time.sleep(duration)
pyautogui.moveTo(original_mouse_position.x, original_mouse_position.y)
video_clip = VideoFileClip(temp_file)
video_clip.write_videofile(output_filename)
video_clip.close()
os.remove(temp_file)
if _name_ == "__main__":
record_duration = 180
output_filename = "captured_video.mp4"
record_screen_with_audio(duration=record_duration, output_filename=output_filename)
thanks for watching like and subscribe!