Skip to content

Commit

Permalink
Bugfix: -o m4a would fail [Fixes #720]
Browse files Browse the repository at this point in the history
In FFmpeg, a given encoding may not always point to the same format
string.
  • Loading branch information
ritiek committed May 18, 2020
1 parent 675d180 commit cd5f224
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
16 changes: 16 additions & 0 deletions spotdl/encode/encode_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
"""

_TARGET_FORMATS_FROM_ENCODING = {
"m4a": "mp4",
"mp3": "mp3",
"opus": "opus",
"flac": "flac"
}


class EncoderBase(ABC):
"""
Expand All @@ -44,6 +51,7 @@ def __init__(self, encoder_path, loglevel, additional_arguments=[]):
self.encoder_path = encoder_path
self._loglevel = loglevel
self._additional_arguments = additional_arguments
self._target_formats_from_encoding = _TARGET_FORMATS_FROM_ENCODING

def set_argument(self, argument):
"""
Expand Down Expand Up @@ -94,6 +102,14 @@ def re_encode(self, input_path, target_path):
"""
pass

def target_format_from_encoding(self, encoding):
"""
This method generates the target stream format from given
input encoding.
"""
target_format = self._target_formats_from_encoding[encoding]
return target_format

def re_encode_from_stdin(self, input_encoding, target_path):
"""
This method must invoke the encoder to encode stdin to a
Expand Down
2 changes: 1 addition & 1 deletion spotdl/encode/encoders/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _generate_encode_command(self, input_path, target_file,
+ ["-i", input_path] \
+ arguments.split() \
+ self._additional_arguments \
+ ["-f", target_encoding] \
+ ["-f", self.target_format_from_encoding(target_encoding)] \
+ [target_file]

return command
Expand Down
6 changes: 3 additions & 3 deletions spotdl/encode/encoders/tests/test_ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def m4a_to_m4a_encoder(input_path, target_path):
'-acodec', 'copy',
'-b:a', '192k',
'-vn',
'-f', 'm4a',
'-f', 'mp4',
target_path
]
return command
Expand Down Expand Up @@ -112,7 +112,7 @@ def m4a_to_m4a_encoder_with_debug(input_path, target_path):
'-acodec', 'copy',
'-b:a', '192k',
'-vn',
'-f', 'm4a',
'-f', 'mp4',
target_path
]
return command
Expand Down Expand Up @@ -180,7 +180,7 @@ def m4a_to_m4a_encoder_and_trim_silence(input_path, target_path):
'-b:a', '192k',
'-vn',
'-af', 'silenceremove=start_periods=1',
'-f', 'm4a',
'-f', 'mp4',
target_path
]
return command
Expand Down
9 changes: 9 additions & 0 deletions spotdl/encode/tests/test_encode_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,12 @@ def test_get_encoding(self, encoderkid, filename, encoding):
def test_encoder_not_found_error(self):
with pytest.raises(EncoderNotFoundError):
self.EncoderKid("/a/nonexistent/path", "0", [])

@pytest.mark.parametrize("encoding, target_format", [
("m4a", "mp4"),
("mp3", "mp3"),
("opus", "opus"),
("flac", "flac"),
])
def test_target_format_from_encoding(self, encoderkid, encoding, target_format):
assert encoderkid.target_format_from_encoding(encoding) == target_format
3 changes: 2 additions & 1 deletion spotdl/metadata/providers/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ def __init__(self, streams):
self.all = []

for stream in audiostreams:
encoding = "m4a" if "mp4a" in stream.audio_codec else stream.audio_codec
standard_stream = {
# Store only the integer part for bitrate. For example
# the given bitrate would be "192kbps", we store only
# the integer part (192) here and drop the rest.
"bitrate": int(stream.abr[:-4]),
"connection": None,
"download_url": stream.url,
"encoding": stream.audio_codec,
"encoding": encoding,
"filesize": None,
}
establish_connection = threading.Thread(
Expand Down

0 comments on commit cd5f224

Please sign in to comment.