chaitanya9 commited on
Commit
7b727e4
·
1 Parent(s): 9af4f2c

Upload convert_wavs.py

Browse files
Files changed (1) hide show
  1. convert_wavs.py +75 -0
convert_wavs.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ v = 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
+ return v
25
+
26
+
27
+ def convert_audios(path, target_path, remove=False):
28
+ """Converts a path of wav files to:
29
+ - 16000Hz Sampling rate
30
+ - one audio channel ( mono )
31
+ and then put them into a new folder called `target_path`
32
+ Params:
33
+ audio_path (str): the path of audio wav file you want to convert
34
+ target_path (str): target path to save your new converted wav file
35
+ remove (bool): whether to remove the old file after converting
36
+ Note that this function requires ffmpeg installed in your system."""
37
+
38
+ for dirpath, dirnames, filenames in os.walk(path):
39
+ for dirname in dirnames:
40
+ dirname = os.path.join(dirpath, dirname)
41
+ target_dir = dirname.replace(path, target_path)
42
+ if not os.path.isdir(target_dir):
43
+ os.mkdir(target_dir)
44
+
45
+ for dirpath, _, filenames in os.walk(path):
46
+ for filename in filenames:
47
+ file = os.path.join(dirpath, filename)
48
+ if file.endswith(".wav"):
49
+ # it is a wav file
50
+ target_file = file.replace(path, target_path)
51
+ convert_audio(file, target_file, remove=remove)
52
+
53
+
54
+ if __name__ == "__main__":
55
+ import argparse
56
+ parser = argparse.ArgumentParser(description="""Convert ( compress ) wav files to 16MHz and mono audio channel ( 1 channel )
57
+ This utility helps for compressing wav files for training and testing""")
58
+ parser.add_argument("audio_path", help="Folder that contains wav files you want to convert")
59
+ parser.add_argument("target_path", help="Folder to save new wav files")
60
+ parser.add_argument("-r", "--remove", type=bool, help="Whether to remove the old wav file after converting", default=False)
61
+
62
+ args = parser.parse_args()
63
+ audio_path = args.audio_path
64
+ target_path = args.target_path
65
+
66
+ if os.path.isdir(audio_path):
67
+ if not os.path.isdir(target_path):
68
+ os.makedirs(target_path)
69
+ convert_audios(audio_path, target_path, remove=args.remove)
70
+ elif os.path.isfile(audio_path) and audio_path.endswith(".wav"):
71
+ if not target_path.endswith(".wav"):
72
+ target_path += ".wav"
73
+ convert_audio(audio_path, target_path, remove=args.remove)
74
+ else:
75
+ raise TypeError("The audio_path file you specified isn't appropriate for this operation")