pythonffmpeg

Screen Recording Issues using Python and FFMPEG


I am trying to record a walkthrough of a 3D space contained within a Chrome browser session. The goal is to create deterministic output. If I run this command from a VS Code terminal, it records 30 seconds of video from the window that's about 6MB:

ffmpeg -f gdigrab -framerate 30 -i "title=My3D - Google Chrome" -t 30 "My3D - Google Chrome_screenRecording_v0.mp4"

However, if I call it from Python like so:

subprocess.run("ffmpeg -f gdigrab -framerate 30 -i \"title=My3D - Google Chrome\" -t 15 \"My3D - Google Chrome_screenRecording_v0.mp4\"")

The output video is 30 seconds, but while I can see the mouse moving on the screen, the image is all black and the filesize is only 94KB.

This is on a Windows 11 machine with GPU acceleration disabled in the OS as well as in Chrome. I've also tried it with other web pages and those are also recorded black. Any ideas what is causing this or how to record the screen using Python?

I've got other alternatives working (using Selenium or PyGet to capture screenshots and then using the VideoWriter class from OpenCV to write them to a video file), but that has a limitation of only about 10 FPS and you can't control the recording time like you can with FFMPEG. If you screen capture for 30 seconds, the actual output may only be 12-18 seconds.


Solution

  • Try to use desktop grabbing. It does not give black screen for me.

    Unfortunately you should position the area relatively to the screen with options -offset_x 100 -offset_y 100 -video_size 800x600 . You can also see the grabbed region for tuning coordinates with option -show_region 1

    So the command will be like this:

    ffmpeg -f gdigrab -framerate 30 -offset_x 100 -offset_y 100 -video_size 800x600 -show_region 1 -i desktop -t 30 "My3D - Google Chrome_screenRecording_v0.mp4"
    
    

    If it works you can use WinAPI functions to find programmatically the window and its position and substitute coordinates to command