Last active
April 9, 2023 15:21
-
-
Save 0x07dc/04fdf8cea70322577fea48d3db39623a to your computer and use it in GitHub Desktop.
Windows Audio Sleep Timer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script mutes your windows audio after a certain amount of entered time | |
# Requires PyCaw (>pip install pycaw) | |
# Usage: | |
# > python sleepTimer.py <minutes until silence> | |
import sys | |
if(len(sys.argv)<2): | |
exit("Error. No countdown time provided") | |
if(sys.argv[1]=="-h" or | |
sys.argv[1]=="--help"): | |
exit( | |
""" | |
Usage: | |
> python sleepTimer.py <minutes until silence> | |
Example: | |
> python sleepTimer.py 60 | |
You can use decimals too. | |
For a 1.5 minute countdown: | |
> python sleepTimer.py 1.5 | |
""" | |
) | |
# turn off volume after input time | |
sleepTime = int(round(float(sys.argv[1]) * 60)) | |
from threading import Timer | |
from ctypes import cast, POINTER | |
from comtypes import CLSCTX_ALL | |
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume | |
import math | |
# Get default audio device using PyCAW | |
devices = AudioUtilities.GetSpeakers() | |
interface = devices.Activate( | |
IAudioEndpointVolume._iid_, CLSCTX_ALL, None) | |
volume = cast(interface, POINTER(IAudioEndpointVolume)) | |
def turnDownAudio(): | |
global sleepTime | |
sleepTime -= 1 | |
if sleepTime>0: | |
print(sleepTime) | |
Timer(1,turnDownAudio).start() | |
else: | |
volume.SetMasterVolumeLevelScalar(0, None) | |
print("volume turned down") | |
turnDownAudio() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks