Skip to content

Commit

Permalink
added option to create pulseaudio sink for recording
Browse files Browse the repository at this point in the history
  • Loading branch information
Bleuzen committed May 16, 2018
1 parent 7e88619 commit 7a6c61e
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions spotifyrecorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import argparse
import traceback

app_version = "0.2"
app_version = "0.3.0"

# Settings with Defaults
_output_directory = "Audio"
_filename_pattern = "{trackNumber} - {artist} - {title}"

# Hard-coded settings
_pulse_sink_name = "spotrec"

is_script_paused = False

def main():
Expand All @@ -41,6 +44,10 @@ def main():
global _spotify
_spotify = Spotify()

# Load PulseAudio sink if user requested it
if _create_pa_sink:
PulseAudio.load_sink()

# Keep the main thread alive (to be able to handle KeyboardInterrupt)
while True:
time.sleep(1)
Expand All @@ -54,6 +61,10 @@ def doExit():
# Kill all FFmpeg subprocesses
FFmpeg.killAll()

# Unload PulseAudio sink if it was loaded
if _create_pa_sink:
PulseAudio.unload_sink()

print("[Recorder] Bye")

# Have to use os exit here, because otherwise GLib would print a strange error message
Expand All @@ -62,17 +73,21 @@ def doExit():

def handle_command_line():
global _debug_logging
global _create_pa_sink
global _output_directory
global _filename_pattern

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-d", "--debug", help="Print ffmpeg output", action="store_true", default=False)
parser.add_argument("-s", "--create-sink", help="Create an extra PulseAudio sink for recording", action="store_true", default=False)
parser.add_argument("-o", "--output-directory", help="Where to save the recordings", default=_output_directory)
parser.add_argument("-p", "--filename-pattern", help="A pattern for the file names of the recordings", default=_filename_pattern)
parser.add_argument("-d", "--debug", help="Print ffmpeg output", action="store_true", default=False)
args = parser.parse_args()

_debug_logging = args.debug

_create_pa_sink = args.create_sink

_filename_pattern = args.filename_pattern

_output_directory = args.output_directory
Expand Down Expand Up @@ -247,7 +262,7 @@ def stopBlocking(self):

print("[FFmpeg] [" + self.pid + "] terminated")

# Sometimes this is not enough and ffmpeg survives, so we have to kill it after some time
# Sometimes this is not enough and ffmpeg survives (also there is a bug with the Archlinux FFmpeg), so we have to kill it after some time
time.sleep(1)

if self.process.poll() == None:
Expand Down Expand Up @@ -298,6 +313,26 @@ def Popen(cmd):
with open("/dev/null", "w") as devnull:
return subprocess.Popen(cmd, stdin=None, stdout=devnull, stderr=devnull, shell=True)

@staticmethod
def check_output(cmd):
out = subprocess.check_output(cmd, shell=True)
return out.decode()

class PulseAudio:
sink_id = ""

@staticmethod
def load_sink():
print("[Recorder] Creating pulse sink")
PulseAudio.sink_id = Shell.check_output('pactl load-module module-remap-sink sink_name=' + _pulse_sink_name + ' sink_properties=device.description="' + _pulse_sink_name + '" channels=2 remix=no')
# To use another master sink where to play:
# pactl load-module module-remap-sink sink_name=spotrec sink_properties=device.description="spotrec" master=MASTER_SINK_NAME channels=2 remix=no

@staticmethod
def unload_sink():
print("[Recorder] Unloading pulse sink")
Shell.run('pactl unload-module ' + PulseAudio.sink_id)

if __name__ == "__main__":
# Handle exit (not print error when pressing Ctrl^C)
try:
Expand Down

0 comments on commit 7a6c61e

Please sign in to comment.