| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""Command-line tool to decode audio files to WAV files.""" |
| 16 |
import audioread |
| 17 |
import sys |
| 18 |
import os |
| 19 |
import wave |
| 20 |
import contextlib |
| 21 |
|
| 22 |
|
| 23 |
def decode(filename): |
| 24 |
filename = os.path.abspath(os.path.expanduser(filename)) |
| 25 |
if not os.path.exists(filename): |
| 26 |
print("File not found.", file=sys.stderr) |
| 27 |
sys.exit(1) |
| 28 |
|
| 29 |
try: |
| 30 |
with audioread.audio_open(filename) as f: |
| 31 |
print('Input file: %i channels at %i Hz; %.1f seconds.' % |
| 32 |
(f.channels, f.samplerate, f.duration), |
| 33 |
file=sys.stderr) |
| 34 |
print('Backend:', str(type(f).__module__).split('.')[1], |
| 35 |
file=sys.stderr) |
| 36 |
|
| 37 |
with contextlib.closing(wave.open(filename + '.wav', 'w')) as of: |
| 38 |
of.setnchannels(f.channels) |
| 39 |
of.setframerate(f.samplerate) |
| 40 |
of.setsampwidth(2) |
| 41 |
|
| 42 |
for buf in f: |
| 43 |
of.writeframes(buf) |
| 44 |
|
| 45 |
except audioread.DecodeError: |
| 46 |
print("File could not be decoded.", file=sys.stderr) |
| 47 |
sys.exit(1) |
| 48 |
|
| 49 |
|
| 50 |
if __name__ == '__main__': |
| 51 |
decode(sys.argv[1]) |