From 08e769557b25758964b829823e0f42f8fcfe75cc Mon Sep 17 00:00:00 2001 From: ModnarUser Date: Fri, 27 Aug 2021 23:45:27 +0200 Subject: [PATCH 1/2] Added cli flag for setting audio codec flac/mp3 --- README.md | 11 ++++++++++- spotrec.py | 12 +++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0696c81..b3a9c59 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,22 @@ python3 spotrec.py ### Example -First of all run spotify. +First of all run spotify. To circumvent incompatibilities occuring on some distros, install it via snap: + +```bash +snap install spotify +``` Then you can run the python script which will record the music: ``` ./spotrec.py -o ./my_song_dir --skip-intro ``` +By default spotrec it will output `*.flac` files. If you want to change the file type to `*.mp3` use the `--audio-codec` flag: + +```bash +./spotrec.py -o ./my_song_dir --skip-intro --audio-codec mp3 +``` Check the pulseaudio configuration: diff --git a/spotrec.py b/spotrec.py index c84e859..d2c6805 100755 --- a/spotrec.py +++ b/spotrec.py @@ -40,6 +40,7 @@ _tmp_file = True _underscored_filenames = False _use_internal_track_counter = False +_audio_codec = "flac" # Hard-coded settings _pa_recording_sink_name = "spotrec" @@ -126,6 +127,7 @@ def handle_command_line(): global _tmp_file global _underscored_filenames global _use_internal_track_counter + global _audio_codec parser = argparse.ArgumentParser( description=app_name + " v" + app_version, formatter_class=argparse.RawTextHelpFormatter) @@ -137,6 +139,9 @@ def handle_command_line(): action="store_true", default=_mute_pa_recording_sink) parser.add_argument("-o", "--output-directory", help="Where to save the recordings\n" "Default: " + _output_directory, default=_output_directory) + parser.add_argument("-ac", "--audio-codec", help="Set the audio codec of the recorded files\n" + "Available: flac, mp3" + "Default: flac", default=_audio_codec) parser.add_argument("-p", "--filename-pattern", help="A pattern for the file names of the recordings\n" "Available: {artist}, {album}, {trackNumber}, {title}\n" "Default: \"" + _filename_pattern + "\"\n" @@ -167,6 +172,7 @@ def handle_command_line(): _use_internal_track_counter = args.internal_track_counter + _audio_codec = args.audio_codec def init_log(): global log @@ -445,9 +451,9 @@ def record(self, out_dir: str, file: str, metadata_for_file={}): # Use a dot as filename prefix to hide the file until the recording was successful self.tmp_file_prefix = "." self.filename = self.tmp_file_prefix + \ - os.path.basename(file) + ".flac" + os.path.basename(file) + "." + _audio_codec else: - self.filename = os.path.basename(file) + ".flac" + self.filename = os.path.basename(file) + "." +_audio_codec # build metadata param metadata_params = '' @@ -458,7 +464,7 @@ def record(self, out_dir: str, file: str, metadata_for_file={}): # "-hide_banner": short the debug log a little # "-y": overwrite existing files self.process = Shell.Popen(_ffmpeg_executable + ' -hide_banner -y -f pulse -ac 2 -ar 44100 -i ' + - self.pulse_input + metadata_params + ' -acodec flac ' + + self.pulse_input + metadata_params + ' -acodec ' + _audio_codec+ ' ' + shlex.quote(os.path.join(self.out_dir, self.filename))) self.pid = str(self.process.pid) From e8686271ed8447c9dbb2d883c1dad851ac48bb04 Mon Sep 17 00:00:00 2001 From: ModnarUser Date: Fri, 27 Aug 2021 23:47:13 +0200 Subject: [PATCH 2/2] Removed typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b3a9c59..5304bfa 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Then you can run the python script which will record the music: ``` ./spotrec.py -o ./my_song_dir --skip-intro ``` -By default spotrec it will output `*.flac` files. If you want to change the file type to `*.mp3` use the `--audio-codec` flag: +By default spotrec will output `*.flac` files. If you want to change the file type to `*.mp3` use the `--audio-codec` flag: ```bash ./spotrec.py -o ./my_song_dir --skip-intro --audio-codec mp3