Skip to content

Commit cd5f224

Browse files
committed
Bugfix: -o m4a would fail [Fixes #720]
In FFmpeg, a given encoding may not always point to the same format string.
1 parent 675d180 commit cd5f224

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

spotdl/encode/encode_base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
2222
"""
2323

24+
_TARGET_FORMATS_FROM_ENCODING = {
25+
"m4a": "mp4",
26+
"mp3": "mp3",
27+
"opus": "opus",
28+
"flac": "flac"
29+
}
30+
2431

2532
class EncoderBase(ABC):
2633
"""
@@ -44,6 +51,7 @@ def __init__(self, encoder_path, loglevel, additional_arguments=[]):
4451
self.encoder_path = encoder_path
4552
self._loglevel = loglevel
4653
self._additional_arguments = additional_arguments
54+
self._target_formats_from_encoding = _TARGET_FORMATS_FROM_ENCODING
4755

4856
def set_argument(self, argument):
4957
"""
@@ -94,6 +102,14 @@ def re_encode(self, input_path, target_path):
94102
"""
95103
pass
96104

105+
def target_format_from_encoding(self, encoding):
106+
"""
107+
This method generates the target stream format from given
108+
input encoding.
109+
"""
110+
target_format = self._target_formats_from_encoding[encoding]
111+
return target_format
112+
97113
def re_encode_from_stdin(self, input_encoding, target_path):
98114
"""
99115
This method must invoke the encoder to encode stdin to a

spotdl/encode/encoders/ffmpeg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _generate_encode_command(self, input_path, target_file,
7676
+ ["-i", input_path] \
7777
+ arguments.split() \
7878
+ self._additional_arguments \
79-
+ ["-f", target_encoding] \
79+
+ ["-f", self.target_format_from_encoding(target_encoding)] \
8080
+ [target_file]
8181

8282
return command

spotdl/encode/encoders/tests/test_ffmpeg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def m4a_to_m4a_encoder(input_path, target_path):
4848
'-acodec', 'copy',
4949
'-b:a', '192k',
5050
'-vn',
51-
'-f', 'm4a',
51+
'-f', 'mp4',
5252
target_path
5353
]
5454
return command
@@ -112,7 +112,7 @@ def m4a_to_m4a_encoder_with_debug(input_path, target_path):
112112
'-acodec', 'copy',
113113
'-b:a', '192k',
114114
'-vn',
115-
'-f', 'm4a',
115+
'-f', 'mp4',
116116
target_path
117117
]
118118
return command
@@ -180,7 +180,7 @@ def m4a_to_m4a_encoder_and_trim_silence(input_path, target_path):
180180
'-b:a', '192k',
181181
'-vn',
182182
'-af', 'silenceremove=start_periods=1',
183-
'-f', 'm4a',
183+
'-f', 'mp4',
184184
target_path
185185
]
186186
return command

spotdl/encode/tests/test_encode_base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,12 @@ def test_get_encoding(self, encoderkid, filename, encoding):
8686
def test_encoder_not_found_error(self):
8787
with pytest.raises(EncoderNotFoundError):
8888
self.EncoderKid("/a/nonexistent/path", "0", [])
89+
90+
@pytest.mark.parametrize("encoding, target_format", [
91+
("m4a", "mp4"),
92+
("mp3", "mp3"),
93+
("opus", "opus"),
94+
("flac", "flac"),
95+
])
96+
def test_target_format_from_encoding(self, encoderkid, encoding, target_format):
97+
assert encoderkid.target_format_from_encoding(encoding) == target_format

spotdl/metadata/providers/youtube.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,15 @@ def __init__(self, streams):
138138
self.all = []
139139

140140
for stream in audiostreams:
141+
encoding = "m4a" if "mp4a" in stream.audio_codec else stream.audio_codec
141142
standard_stream = {
142143
# Store only the integer part for bitrate. For example
143144
# the given bitrate would be "192kbps", we store only
144145
# the integer part (192) here and drop the rest.
145146
"bitrate": int(stream.abr[:-4]),
146147
"connection": None,
147148
"download_url": stream.url,
148-
"encoding": stream.audio_codec,
149+
"encoding": encoding,
149150
"filesize": None,
150151
}
151152
establish_connection = threading.Thread(

0 commit comments

Comments
 (0)