Skip to content

Commit

Permalink
Add api any to mp3
Browse files Browse the repository at this point in the history
  • Loading branch information
dekink committed Jun 24, 2020
1 parent 0278629 commit 9ea5db0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ db/
.pytest_cache/
deploy.yml
deploy-test.yml
.DS_Store
33 changes: 33 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,39 @@ def wav_to_mp3():
mimetype='audio/mpeg')


@app.route('/any_to_mp3', methods=['POST'])
def any_to_mp3():
if 'file' not in request.files:
abort(400, description='file is not attached')
data = request.form.to_dict()
file = request.files['file']
file_data = BytesIO(file.read())
if os.path.splitext(file.filename)[1] == '.wav':
audio = AudioSegment.from_wav(file_data)
elif os.path.splitext(file.filename)[1] == '.mp3':
audio = AudioSegment.from_mp3(file_data)
else:
abort(400, description='unsupported file format')

if data.get('silence', False):
silence_ms = int(float(data.get('silence')) * 1000)
audio += AudioSegment.silent(duration=silence_ms)

mp3_params = _mp3_parameters(data)
output_data = BytesIO()
r = audio.export(output_data, format='mp3', codec='libmp3lame', **mp3_params)
if not r:
abort(400, description='failed wave to mp3')

name = shortuuid.ShortUUID().random(length=8)
output_data.seek(0)
return send_file(
output_data,
as_attachment=True,
attachment_filename=f'{name}.mp3',
mimetype='audio/mpeg')


@app.route("/merge", methods=['POST'])
def merge():
rmse_dano = 0.0777645544406
Expand Down
Binary file added tests/assets/test.mp3
Binary file not shown.
44 changes: 44 additions & 0 deletions tests/test_mp3_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,47 @@ def test_wav_to_mp3_sampling_rate(self):
higher_bitrate_data_size = len(r.data)

self.assertTrue(default_bitrate_data_size != higher_bitrate_data_size)

def test_any_to_mp3_sampling_rate(self):
cli = app.test_client()
# wav to mp3
with open('./tests/assets/test.wav', 'rb') as f:
r = cli.post('/any_to_mp3', content_type='multipart/form-data', data={
'file': f,
'bitrate': '16k'
}, buffered=True)
self.assertEqual(200, r.status_code)
default_bitrate_data_size = len(r.data)

with open('./tests/assets/test.wav', 'rb') as f:
r = cli.post('/any_to_mp3', content_type='multipart/form-data', data={
'file': f,
'bitrate': '16k',
'sampling_rate': '44100',
'sample_fmt': 'flt'
}, buffered=True)
self.assertEqual(200, r.status_code)
higher_bitrate_data_size = len(r.data)

self.assertTrue(default_bitrate_data_size != higher_bitrate_data_size)

# mp3 to mp3
with open('./tests/assets/test.mp3', 'rb') as f:
r = cli.post('/any_to_mp3', content_type='multipart/form-data', data={
'file': f,
'bitrate': '16k'
}, buffered=True)
self.assertEqual(200, r.status_code)
default_bitrate_data_size = len(r.data)

with open('./tests/assets/test.mp3', 'rb') as f:
r = cli.post('/any_to_mp3', content_type='multipart/form-data', data={
'file': f,
'bitrate': '16k',
'sampling_rate': '44100',
'sample_fmt': 'flt'
}, buffered=True)
self.assertEqual(200, r.status_code)
higher_bitrate_data_size = len(r.data)

self.assertTrue(default_bitrate_data_size != higher_bitrate_data_size)

0 comments on commit 9ea5db0

Please sign in to comment.