Skip to content

Commit d5e4289

Browse files
authored
Merge pull request #22 from moises-ai/optional_sample_rate
File conversion: Make sample rate optional
2 parents 77c7336 + 2f1d9e0 commit d5e4289

File tree

3 files changed

+63
-51
lines changed

3 files changed

+63
-51
lines changed

maestro_worker_python/convert_files.py

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ def __init__(self, message):
2222
class FileToConvert:
2323
input_file_path: str
2424
file_format: str
25-
output_file_path: str = None
25+
output_file_path: str | None = None
2626
max_duration: int = 1200
27-
sample_rate: int = 44100
27+
sample_rate: int | None = 44100
2828

2929

3030
def convert_files(convert_files: List[FileToConvert]):
@@ -92,48 +92,52 @@ def convert_files_manager(*convert_files: FileToConvert) -> None | str | list[st
9292
obj.close()
9393

9494

95-
def _convert_to_wav(input_file_path, output_file_path, max_duration, sample_rate=44100):
96-
_run_subprocess(
97-
[
98-
"ffmpeg",
99-
"-y",
100-
"-hide_banner",
101-
"-loglevel",
102-
"error",
103-
"-t",
104-
str(max_duration),
105-
"-i",
106-
str(input_file_path),
107-
"-ar",
108-
str(sample_rate),
109-
str(output_file_path),
110-
]
111-
)
112-
113-
114-
def _convert_to_m4a(input_file_path, output_file_path, max_duration, sample_rate=44100):
115-
_run_subprocess(
116-
[
117-
"ffmpeg",
118-
"-y",
119-
"-hide_banner",
120-
"-loglevel",
121-
"error",
122-
"-t",
123-
str(max_duration),
124-
"-i",
125-
str(input_file_path),
126-
"-c:a",
127-
"aac",
128-
"-b:a",
129-
"192k",
130-
"-ar",
131-
str(sample_rate),
132-
"-movflags",
133-
"+faststart",
134-
str(output_file_path),
135-
]
136-
)
95+
def _convert_to_wav(input_file_path: str, output_file_path: str, max_duration: int, sample_rate: int | None = 44100):
96+
command_list = [
97+
"ffmpeg",
98+
"-y",
99+
"-hide_banner",
100+
"-loglevel",
101+
"error",
102+
"-t",
103+
str(max_duration),
104+
"-i",
105+
str(input_file_path),
106+
]
107+
108+
if sample_rate is not None:
109+
command_list.extend(["-ar", str(sample_rate)])
110+
111+
command_list.append(str(output_file_path))
112+
113+
_run_subprocess(command_list)
114+
115+
116+
def _convert_to_m4a(input_file_path: str, output_file_path: str, max_duration: int, sample_rate: int | None = 44100):
117+
command_list = [
118+
"ffmpeg",
119+
"-y",
120+
"-hide_banner",
121+
"-loglevel",
122+
"error",
123+
"-t",
124+
str(max_duration),
125+
"-i",
126+
str(input_file_path),
127+
"-c:a",
128+
"aac",
129+
"-b:a",
130+
"192k",
131+
"-movflags",
132+
"+faststart",
133+
]
134+
135+
if sample_rate is not None:
136+
command_list.extend(["-ar", str(sample_rate)])
137+
138+
command_list.append(str(output_file_path))
139+
140+
_run_subprocess(command_list)
137141

138142

139143
def _run_subprocess(command):

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ packages = [{include = "maestro_worker_python"}]
99
exclude = ["tests/**/*"]
1010

1111
[tool.poetry.dependencies]
12-
python = "^3.9"
12+
python = ">3.9,<3.14"
1313
json-logging = ">=1.3,<2"
1414
uvicorn = ">=0.34.0,<0.34.3"
1515
sentry-sdk = {version = ">=1.16,<3", extras = ["fastapi"]}
1616
requests = ">=2.31,<3"
1717
psutil = ">=6.0.0,<7.1"
1818
fastapi = ">=0.100.0,<0.115.13"
1919
pydantic-settings = ">=2.9,<3"
20+
pydantic_core = ">=2.9,<3"
2021

2122
[tool.poetry.group.dev.dependencies]
2223
pytest = "^7.2.0"

tests/test_convert_files.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_should_raise_validation_error_if_audio_file_is_invalid(
5858
]
5959
)
6060

61-
assert "Invalid data" in str(exc.value)
61+
assert "invalid" in str(exc.value).lower()
6262

6363

6464
@pytest.mark.parametrize("file_format", ["m4a", "wav"])
@@ -76,7 +76,7 @@ def test_should_raise_validation_error_if_audio_file_is_corrupt(
7676
]
7777
)
7878

79-
assert "Invalid argument" in str(exc.value)
79+
assert "invalid" in str(exc.value).lower()
8080

8181

8282
@pytest.mark.parametrize("file_format", ["m4a", "wav"])
@@ -102,6 +102,7 @@ def test_should_raise_validation_error_if_source_has_no_audio(file_format, caplo
102102
@pytest.mark.parametrize(
103103
"input_name, output_name, format, sample_rate",
104104
[
105+
("silent.ogg", "converted_44100.wav", "wav", None),
105106
("silent.ogg", "converted_44100.wav", "wav", 44100),
106107
("silent with space.ogg", "converted_44100.wav", "wav", 44100),
107108
("silent.ogg", "converted_48000.wav", "wav", 48000),
@@ -130,6 +131,7 @@ def test_should_convert_valid_wav_audio_file(input_name, output_name, format, sa
130131
@pytest.mark.parametrize(
131132
"input_name, output_name, format, sample_rate",
132133
[
134+
("silent.ogg", "converted_44100.m4a", "m4a", None),
133135
("silent.ogg", "converted_44100.m4a", "m4a", 44100),
134136
("silent with space.wav", "converted_44100.m4a", "m4a", 44100),
135137
("silent.ogg", "converted_48000.m4a", "m4a", 48000),
@@ -184,14 +186,19 @@ def _get_hash(file_name, sample_rate):
184186
"error",
185187
"-i",
186188
str(file_name),
187-
"-ar",
188-
str(sample_rate),
189+
]
190+
if sample_rate:
191+
command.extend([
192+
"-ar",
193+
str(sample_rate),
194+
])
195+
command.extend([
189196
"-map",
190197
"0",
191198
"-f",
192199
"hash",
193-
"-"
194-
]
200+
"-",
201+
])
195202

196203
process = subprocess.run(command, shell=False, capture_output=True, check=True)
197204
return process.stdout.split(b"=")[1].strip()

0 commit comments

Comments
 (0)