Last active
July 29, 2018 19:25
-
-
Save MCOfficer/63a84433938a9c748c64385494d0a73e to your computer and use it in GitHub Desktop.
[Python] Script that automatically records tracks played by the linux spotify client.
This file contains 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
from subprocess import run, PIPE, Popen | |
import shutil | |
import string | |
try: | |
import psutil | |
except ModuleNotFoundError: | |
print("psutil not found, please install it.\n$pip install psutil\n$python3 -m pip install psutil") | |
exit(1) | |
def format_filename(s): | |
""" | |
Shamelessly stolen from https://gist.github.com/seanh/93666 | |
""" | |
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits) | |
filename = ''.join(c for c in s if c in valid_chars) | |
return filename.replace(' ', '_') | |
def wait_for_spotify(): | |
print("Waiting for Spotify to launch...") | |
while True: | |
pidlist = [p.info for p in psutil.process_iter(attrs=['pid', 'name']) if 'spotify' in p.info['name']] | |
if pidlist: | |
pid = pidlist[0]["pid"] | |
print("Found Spotify, PID " + str(pid)) | |
return pid | |
def get_playing(pid): | |
p = run(["wmctrl", "-l", "-p"], stdout=PIPE, encoding="UTF-8") | |
for line in p.stdout.splitlines(): | |
args = line.split() | |
if args and int(args[2]) == pid: | |
title = " ".join(args[4:]) | |
return title if not title == "Spotify" else None | |
def get_monitor(): | |
print("Attempting to find PulseAudio Monitor...") | |
pacmd = run(["pacmd", "list"], stdout=PIPE, encoding="UTF-8") | |
for line in pacmd.stdout.splitlines(): | |
if ".monitor" in line: | |
monitor = line[line.index("<") + 1 :line.index(">")] | |
print("Found Monitor " + monitor) | |
return monitor | |
def start_recording(monitor, title): | |
print("Starting Recording...") | |
return Popen(["parec", "-d", monitor, "--file-format=wav", format_filename(title + ".wav")]) | |
def test_for_command(cmd): | |
return shutil.which(cmd) is not None | |
print("Starting...") | |
print("Checking for pacat...") | |
if not test_for_command("pacat"): | |
print("pacat is not installed, exiting...") | |
exit(1) | |
print("Checking for wmctrl...") | |
if not test_for_command("wmctrl"): | |
print("wmctrl is not installed, exiting...") | |
exit(1) | |
print("OK\n") | |
monitor = get_monitor() | |
pid = wait_for_spotify() | |
print("Ready.\n") | |
old_title = None | |
rec = None | |
while True: | |
try: | |
title = get_playing(pid) | |
if not old_title == title: | |
if old_title: | |
# The old track has ended | |
print("Track '" + old_title + "' has ended, stopping Recording...") | |
rec.terminate() | |
rec = None | |
if title: | |
# A new track has started playing | |
print("Now Playing: " + title) | |
rec = start_recording(monitor, title) | |
old_title = title | |
except KeyboardInterrupt: | |
print("Received KeyboardInterrupt, exiting...") | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment