Skip to content

Commit a94ec6f

Browse files
committed
Convert to Suitable Audio
1 parent 42f6b55 commit a94ec6f

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

convert_wavs.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
A utility script used for converting audio samples to be
3+
suitable for feature extraction
4+
"""
5+
6+
import os
7+
8+
def convert_audio(audio_path, target_path, remove=False):
9+
"""This function sets the audio `audio_path` to:
10+
- 16000Hz Sampling rate
11+
- one audio channel ( mono )
12+
Params:
13+
audio_path (str): the path of audio wav file you want to convert
14+
target_path (str): target path to save your new converted wav file
15+
remove (bool): whether to remove the old file after converting
16+
Note that this function requires ffmpeg installed in your system."""
17+
18+
os.system(f"ffmpeg -i {audio_path} -ac 1 -ar 16000 {target_path}")
19+
# os.system(f"ffmpeg -i {audio_path} -ac 1 {target_path}")
20+
if remove:
21+
os.remove(audio_path)
22+
23+
24+
def convert_audios(path, target_path, remove=False):
25+
"""Converts a path of wav files to:
26+
- 16000Hz Sampling rate
27+
- one audio channel ( mono )
28+
and then put them into a new folder called `target_path`
29+
Params:
30+
audio_path (str): the path of audio wav file you want to convert
31+
target_path (str): target path to save your new converted wav file
32+
remove (bool): whether to remove the old file after converting
33+
Note that this function requires ffmpeg installed in your system."""
34+
35+
for dirpath, dirnames, filenames in os.walk(path):
36+
for dirname in dirnames:
37+
dirname = os.path.join(dirpath, dirname)
38+
target_dir = dirname.replace(path, target_path)
39+
if not os.path.isdir(target_dir):
40+
os.mkdir(target_dir)
41+
42+
for dirpath, _, filenames in os.walk(path):
43+
for filename in filenames:
44+
file = os.path.join(dirpath, filename)
45+
if file.endswith(".wav"):
46+
# it is a wav file
47+
target_file = file.replace(path, target_path)
48+
convert_audio(file, target_file, remove=remove)
49+
50+
51+
if __name__ == "__main__":
52+
import argparse
53+
parser = argparse.ArgumentParser(description="""Convert ( compress ) wav files to 16MHz and mono audio channel ( 1 channel )
54+
This utility helps for compressing wav files for training and testing""")
55+
parser.add_argument("audio_path", help="Folder that contains wav files you want to convert")
56+
parser.add_argument("target_path", help="Folder to save new wav files")
57+
parser.add_argument("-r", "--remove", type=bool, help="Whether to remove the old wav file after converting", default=False)
58+
59+
args = parser.parse_args()
60+
audio_path = args.audio_path
61+
target_path = args.target_path
62+
63+
if os.path.isdir(audio_path):
64+
if not os.path.isdir(target_path):
65+
os.makedirs(target_path)
66+
convert_audios(audio_path, target_path, remove=args.remove)
67+
elif os.path.isfile(audio_path) and audio_path.endswith(".wav"):
68+
if not target_path.endswith(".wav"):
69+
target_path += ".wav"
70+
convert_audio(audio_path, target_path, remove=args.remove)
71+
else:
72+
raise TypeError("The audio_path file you specified isn't appropriate for this operation")

0 commit comments

Comments
 (0)