Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Users/modnaruser/audio codec flag #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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:

Expand Down
22 changes: 16 additions & 6 deletions spotrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
_filename_pattern = "{trackNumber} - {artist} - {title}"
_underscored_filenames = False
_use_internal_track_counter = False
_audio_codec = "flac"
_add_cover_art = False

# Hard-coded settings
Expand Down Expand Up @@ -130,6 +131,7 @@ def handle_command_line():
global _filename_pattern
global _underscored_filenames
global _use_internal_track_counter
global _audio_codec
global _add_cover_art

parser = argparse.ArgumentParser(
Expand All @@ -142,6 +144,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\n"
"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"
Expand Down Expand Up @@ -170,6 +175,8 @@ def handle_command_line():

_use_internal_track_counter = args.internal_track_counter

_audio_codec = args.audio_codec

_add_cover_art = args.add_cover_art


Expand Down Expand Up @@ -453,10 +460,13 @@ def record(self, out_dir: str, file: str, metadata_for_file={}):

self.pulse_input = _pa_recording_sink_name + ".monitor"

# 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"
if _tmp_file:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line raises a Name Error:

[Spotify] State changed: Playing
[SpotRec] Starting recording
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "/opt/SpotRec/./spotrec.py", line 358, in run
    ff.record(self.out_dir,
  File "/opt/SpotRec/./spotrec.py", line 463, in record
    if _tmp_file:
NameError: name '_tmp_file' is not defined
[Spotify] State changed: Paused
``

# 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) + "." + _audio_codec
else:
self.filename = os.path.basename(file) + "." +_audio_codec

# save this to self because metadata_params is discarded after this function
self.cover_url = metadata_for_file.pop('cover_url')
Expand All @@ -471,12 +481,12 @@ def record(self, out_dir: str, file: str, metadata_for_file={}):
# "-ac 2": always use 2 audio channels (stereo) (same as Spotify)
# "-ar 44100": always use 44.1k samplerate (same as Spotify)
# "-fragment_size 8820": set recording latency to 50 ms (0.05*44100*2*2) (very high values can cause ffmpeg to not stop fast enough, so post-processing fails)
# "-acodec flac": use the flac lossless audio codec, so we don't lose quality while recording
# "-acodec": use flac or mp3 audio codec, specified as command line argument
self.process = Shell.Popen(_ffmpeg_executable + ' -hide_banner -y '
'-f pulse ' +
'-ac 2 -ar 44100 -fragment_size 8820 ' +
'-i ' + self.pulse_input + metadata_params + ' '
'-acodec flac' +
' -acodec ' + _audio_codec +
' ' + shlex.quote(os.path.join(self.out_dir, self.filename)))

self.pid = str(self.process.pid)
Expand Down