Improving Video Recordings With Python
A unique Python X OBS collaboration
Anyone who has watched enough video game YouTubers in their life probably knows about the WASD keystroke setting. People will display on their screen the keys that they are pressing, specifically the WASD keys, along with LMB, RMB, Space, and maybe Shift.
While I have mostly seen this in Minecraft recordings, it can be pretty useful in other scenarios. In this article, I aim to cover 3 topics:
- Why you should use this
- Code in PyGame
- Setting it up in OBS
- Further Applications
Why you should use this:
Using Python x OBS has a lot of power. Once you finish this project, you should be equipped with the skills to do virtually anything related to the display of OBS. You can highlight stuff on the screen, add custom watermarks (like I do in my videos), and generally make OBS more reactive to what is going on. One of my favorite applications is setting up pygame to simply run in the background and play a sound effect on a keystroke.
The possibilities are limitless. Your recordings will become so much better if you just use the simple tricks shown in this article, in any way you want to.
Without further ado, let’s dive straight into the code.
The Code Bit
The first thing to do is create a file called wasdkey.pyw
or something of that sort. Make sure that the extension is “pyw.” This will allow you to double-click the file to run it.
First, I’m going to show the code, and then I will break it down. If you don’t care about why the code works (which you should care, because it becomes that much more powerful if you understand how to manipulate it) then you can skip forward to setting it up in OBS.
Break Down:
First, we need our imports. We will use PyGame for the actual GUI, and then a bunch of win32api stuff for setting transparency and detecting keystrokes even if the window is not in focus.
First, pip install the packages you need:
pip install pywin32
Then, import them into the file:
Next, we set up pygame, and the colors we will be using. Fuschia is the background color, so anything which is Fuschia will appear transparent.
After this, we set the background transparent in a bunch of complicated code with win32gui.
Next, we check if a key is pressed, again using win32api. The value in “key” is usually the order of the letter (capital letters folks)
So, if you pressed a
then you would call isKeyPressed(ord('A'))
Lines 26 to 42 simply configure the text, and the location of the text, so its all basic pygame.
The same thing happens with lines 45 to 74. However, setting up the mouse press is very difficult. Experiment with isKeyPressed(someNumber)
where someNumber is a number in the range 1 to 32. Check what happens when you press the left and right mouse buttons. It is different for every computer. For me, it just so happened that LMB was 0x01=1
and RMB was what win32con thought LMB was.
The rest of the code is straight forward.
Setting it up in OBS
First, open up the file by double clicking it in file explorer. Press ⊞ Win
+Tab ↹
and then drag the tab to a new window. Make sure this window has nothing else in it.
Now, in OBS (not in the same window as the above), create a new scene, and add a Display Capture to the sources tab. Make sure this selects the whole display. Then, add a window capture of the WASDKey (python pygame). Then, right click on the Window Capture, and select Filters. Add a filter for a green screen, and make the color custom Fuschia. Now the background is transparent in OBS.
If everything is correct, OBS should look like:
Further Applications:
There are a lot of applications of PyGame x OBS, and if you stop to think I’m sure you will find some.
For example, suppose while recording, you want to make the screen highlight your mouse when you press, say, CTRL+SHIFT+1
. Then, simply detect when all of the buttons are pressed at once, and add a yellow circle around the mouse position. You can use this to make sure that the screen is full screened, and get the mouse X and Y positions. Using the transparency hack from earlier, simply overlay the pygame window onto OBS, and you are done!
Learned something new? Leave some claps to let me know!
Have any interesting ideas to use with PyGame + OBS? Or have any questions? Let me know down in the comments!