-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for audio format convertion
- Loading branch information
Showing
5 changed files
with
140 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,6 @@ venv.bak/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
# Editor config | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import io | ||
import sys | ||
import warnings | ||
import concurrent.futures | ||
from functools import partial | ||
|
||
with warnings.catch_warnings(): | ||
warnings.simplefilter("ignore") | ||
import pydub | ||
|
||
def convert_sound(src_ext, target_ext, snd_data): | ||
if src_ext == target_ext: | ||
return snd_data | ||
with io.BytesIO(snd_data) as in_snd: | ||
snd = pydub.AudioSegment.from_file(in_snd, format=src_ext) | ||
with io.BytesIO() as out_snd: | ||
snd.export(out_snd, format=target_ext) | ||
return out_snd.getvalue() | ||
|
||
def convert_streams(streams, src_ext, target_ext): | ||
offs, tags_info, sounds = zip(*streams) | ||
convert = partial(convert_sound, src_ext, target_ext) | ||
|
||
with concurrent.futures.ProcessPoolExecutor() as executor: | ||
try: | ||
converted = executor.map(convert, sounds) | ||
for offset, tags, sound in zip(offs, tags_info, converted): | ||
yield offset, tags, sound | ||
except KeyboardInterrupt as kbi: | ||
executor.shutdown(wait=False) | ||
raise kbi | ||
|
||
def test_converter(target_ext): | ||
try: | ||
with io.BytesIO() as stream: | ||
pydub.AudioSegment.empty().export(stream, format=target_ext) | ||
except OSError: | ||
print('ERROR: ffmpeg not available.') | ||
print('To convert audio, please make sure ffmpeg binaries can be found in PATH.') | ||
sys.exit(1) | ||
|
||
def format_streams(streams, src_ext, target_ext): | ||
if src_ext == target_ext: | ||
return streams | ||
test_converter(target_ext) | ||
return convert_streams(streams, src_ext, target_ext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
fsb5==1.0 | ||
pydub==0.23.1 | ||
PyInstaller==3.5 |