Bright Screen Flash Alert
Most tech streamers I watch in Software and Game Dev on Twitch use dark mode. They also tend to hit pages without dark mode when looking up documentation. The bright flash can knock you back. I thought it would be fun to trigger a yelling sound effect when that happens.
Overview
This script plays a sound file when the overall screen brightness crosses a certain threshold. This version works on a mac. Pull requests to produce versions for other platforms are welcome.
The Script
#!/bin/bash
SCREEN=1
BRIGHTNESS=40
AUDIO_FILE="ahhhh.mp3"
THROTTLE=0
mkdir -p images
trap "trap - SIGTERM && kill -- -$$" \
SIGINT SIGTERM EXIT
ffmpeg -f avfoundation \
-hide_banner \
-loglevel panic \
-i "$SCREEN:0" \
-vf "fps=5,scale=100:-2" \
-y "images/%04d.png" &
fswatch -l 0.1 -0 images/ | \
while read -d "" event
do
if [ -e "$event" ]
then
BRIGHT_F=`convert "$event" \
-colorspace gray \
-format "%[fx:100*mean]" \
info:`
BRIGHT_I=`printf %.0f "$BRIGHT_F"`
echo -n "$BRIGHT_I "
if [ $BRIGHT_I -gt $BRIGHTNESS ]
then
if [ $THROTTLE -eq 0 ]
then
THROTTLE=1
afplay "$AUDIO_FILE" &
echo "YELL"
fi
else
THROTTLE=0
fi
rm "$event"
fi
done
wait
Prerequisites
You'll need ffmpeg
,
imagemagick
, and
fswatch
to run the script. They can be
installed with Homebrew via these
commands if you don't already have them:
brew install ffmpeg
brew install imagemagick
brew install fswatch
Usage
Here's how to set things up:
-
Save the code for the script above into a new file called:
yell-when-it-gets-bright.bash
- Download the sound file and put it in the same directory as the script
- Configure the screen to watch via the instructions in the "Picking A Screen" section below
-
And to start it up, run the script from a command line terminal
with:
/bin/bash yell-when-it-gets-bright.bash
If all goes well, you'll hear the sound trigger each time your screen crosses a certain brightness threshold.
Press CTRL c
in the terminal window when
you're ready to stop the process.
Picking A Screen
The ffmpeg command in the script needs to be configured to watch a specific screen. Run this command to get the data you need to set it up:
ffmpeg -f avfoundation -list_devices true -i ""
That will spit out a bunch of stuff ending with something like this:
[AVFoundation indev @ 0x15a709999] AVFoundation video devices:
[AVFoundation indev @ 0x15a709999] [0] FaceTime HD Camera
[AVFoundation indev @ 0x15a709999] [1] Capture screen 0
[AVFoundation indev @ 0x15a709999] [2] Capture screen 1
[AVFoundation indev @ 0x15a709999] AVFoundation audio devices:
[AVFoundation indev @ 0x15a709999] [0] MacBook Pro Microphone
Pick the video device you want to use and update the
SCREEN
variable in the script. For example,
the output I get when I run the command includes
[1] Capture screen 0
so I setup my copy of
the script with
SCREEN=1
If you have multiple screen and you're not sure which one to use, try one and see if it's right. If not, try the other ones until you hit the one you want.
Watch Out For Spaces
The script is written in bash. One trick with that is that can't leave white space around the equal sign when setting varaibles. For example, this wont' work:
SCREEN = 1
You have to do this instead:
SCREEN=1
The Sound File
The entire point of the script is to play a sound file when the screen gets bright. I made this one which you can download if you'd like to use it:
You can also use your own sound file by changing the path of the
AUDIO_FILE
variable in the script to point
to the one you'd like. (The script uses the
afplay
command which should handle most
things you throw at it.)
The Brightness Threshold
Changing the level of brightness that causes the alert is done with the
BRIGHTNESS
variable in the script. Play
with it to find something that works for you. (As with the other
variables, make sure not to leave spaces around the equal sign when
updating it)
How It Works
-
The script creates an
images
directory when it starts -
ffmpeg
captures five images of the screen every second into theimages
directory (controlled via thefps=5
setting in the script) -
The
fswatch
command watches theimages
directory and throws files over to theconvert
command fromImageMagick
-
That
convert
command analyses the images and reports an overall brightness level -
The yell is triggered if that brightness level is over the one defined
in the
BRIGHTNESS
varaible in the script - The value is stored so the yell doesn't occur again until the brighness level has dipped below the threshold and jump back up again
And that's it. Enjoy!
Notes
- The script prints out the brightness levels to the terminal while it's running. You can use that to determine where you want to set the threshold if you decide to change it
-
The script is deisgned to clean up after itself by removing the images
it creates in the
images
directory. Sometimes that glitches and some get left. You can safely delete them when your finished running the script -
When I run the script I get an error like:
I think this is something to do with how ffmpeg is installed, but it doesn't seem to cause me any issues. So, I just roll with it.objc[86274]: class `NSKVONotifying_AVCaptureScreenInput' not linked into application