Skip to main content

A Python high-performance screenshot library for Windows 10+ using Direct3D11CaptureFramePool.

Project description

wincam

A fast screen capture library for python on Windows 10 and above (x64 platform only).

from wincam import DXCamera

with DXCamera(x, y, w, h, fps=30) as camera:
    while True:
        frame, timestamp = camera.get_bgr_frame()

See Demo Video

Introduction

When you need to capture video frames in a low latency (< 5 milliseconds into a numpy array) to get a nice smooth 30 or 60 fps video this library can do it.

This is using a new Windows 10 API called Direct3D11CaptureFramePool which requires DirectX 11 and a GPU.

To get the fastest time possible, this library is implemented in C++ and the C++ library copies each frame directly into a buffer provided by the python code. This C++ library is loaded into your python process. Only one instance of DXCamera can be used per python process.

Prerequisites

Requires Python 3.10 and Windows 10.0.19041.0 or newer on an x64 platform.

Installation

pip install wincam

See https://pypi.org/project/wincam/.

Multiple Monitors

This supports multiple monitors. Windows can define negative X, and Y locations when a monitor is to the left or above the primary monitor. The DXCamera will find and capture the appropriate monitor from the x, y locations you provide and it will crop the image to the bounds you provide.

The DXCamera does not support regions that span more than one monitor and it will report an error if you try.

Examples

The following example scripts are provided in this repo.

  • examples/mirror.py - shows the captured frames in real time so you can see how it is performing on your machine. Have some fun with infinite frames with frames! Press ESCAPE to close the window.

  • examples/video.py - records an .mp4 video to disk.

In each example can specify what to record using:

  • --x, --y, --width, --height, in screen coordinates
  • --hwnd, a native win32 window handle (which you can find using the inspect tool that comes with the windows SDK)
  • --process, a win32 process id
  • --point, an x,y screen location from which to find the window that you want to record.

Performance

Each call to camera.get_bgr_frame() can be as fast as 1 millisecond because the C++ code is asynchronously writing to the buffer provided. This way your python code is not blocking waiting for that frame. For this reason it is crucial that you use DXCamera in a with block as shown above since this ensures the life time of the python buffer used by the C++ code is managed correctly. If you cannot use a with block for some reason then you must call the stop() method.

In order to hit a smooth target frame rate while recording video the DXCamera takes a target fps as input, which defaults to 30 frames per second. The calls to camera.get_bgr_frame() will self regulate with an accurate sleep to hit that target as closely as possible so that the frames you collect form a nice smooth video as shown in the video.py example.

Note, there is no point providing an fps target greater than the windows monitor refresh rate. You can find this refresh rate on your Display Settings Advanced settings tab. If you go higher than this rate you will only get duplicate frames since the underlying Direct3D11CaptureFramePool is only getting new frames at the refresh rate. This is normally 60fps, unless you have a fancy new GPU and monitor. On a remote desktop this refresh rate can be lower, like 30 fps.

Note that this sleep is more accurate that python time.sleep() which on Windows is very inaccurate with a tolerance of +/- 15 milliseconds. But this more accurate sleep is using a spin wait which uses one core of your CPU.

Credits

This project was inspired by dxcam and the C++ Win32CaptureSample and was made possible with lots of help from Windows team members Robert Mikhayelyan and Shawn Hargreaves.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wincam-0.0.4.tar.gz (4.3 MB view details)

Uploaded Source

Built Distribution

wincam-0.0.4-py3-none-any.whl (4.3 MB view details)

Uploaded Python 3

File details

Details for the file wincam-0.0.4.tar.gz.

File metadata

  • Download URL: wincam-0.0.4.tar.gz
  • Upload date:
  • Size: 4.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for wincam-0.0.4.tar.gz
Algorithm Hash digest
SHA256 ac96f5ba9f5e4b7695025747bbee405d5c40e54cc569b6554ab77ef699dac386
MD5 298e5b997505a3dd98a40cadc133d866
BLAKE2b-256 66db22ed541748bccfaf18b5c2562e527165b13666f42b4de4924c288b05be8c

See more details on using hashes here.

File details

Details for the file wincam-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: wincam-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 4.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for wincam-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 33f2ba136a09b0e2b5739a7da38162c664b43d69289e953331fd7bbb2a84acb6
MD5 80754ee49ecd6f9db312462fe210dad6
BLAKE2b-256 cf15557290dfdb23d60e07af8a2151d40e455fd6e343cc0bdc6feae4d01b60c1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page