Skip to content

Commit 1c42878

Browse files
committed
added ser
1 parent eff47fa commit 1c42878

File tree

1,450 files changed

+354
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,450 files changed

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

0 commit comments

Comments
 (0)