Skip to content

Commit b3568ba

Browse files
authored
Merge pull request #36 from jsingh811/audio-read
Audio read
2 parents 1b93521 + eac9cff commit b3568ba

File tree

3 files changed

+157
-1
lines changed

3 files changed

+157
-1
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,25 @@ python pyAudioProcessing/extract_features.py -f "data_samples/testing" -feats "
185185
```
186186
Features extracted get saved in `audio_features.json`.
187187

188+
## Audio format conversion
189+
190+
You can convert you audio in `.mp4`, `.mp3`, `.m4a` and `.aac` to `.wav`. This will allow you to use audio feature generation and classification functionalities.
191+
192+
In order to convert your audios, the following code sample can be used.
193+
194+
```
195+
from pyAudioProcessing.convert_audio import convert_files_to_wav
196+
197+
# dir_path is the path to the directory/folder on your machine containing audio files
198+
dir_path = "data/mp4_files"
199+
200+
# simple change audio_format to "mp3", "m4a" or "acc" depending on the format
201+
# of audio that you are trying to convert to wav
202+
convert_files_to_wav(dir_path, audio_format="mp4")
203+
204+
# the converted wav files will be saved in the same dir_path location.
205+
206+
```
188207

189208
## Author
190209

pyAudioProcessing/convert_audio.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Thu Jun 10 15:23:55 2021
5+
6+
@author: jsingh
7+
"""
8+
# Imports
9+
10+
import os
11+
import glob
12+
13+
from pydub import AudioSegment
14+
15+
16+
# Functions
17+
18+
def convert_from_m4a(file_path):
19+
"""
20+
Converts m4a audio into wav format.
21+
"""
22+
try:
23+
track = AudioSegment.from_file(file_path, "m4a")
24+
file_handle = track.export(
25+
file_path.replace(".m4a", ".wav"), format='wav'
26+
)
27+
except FileNotFoundError:
28+
print("{} does not appear to be valid. Please check.")
29+
except Exception as e:
30+
print(e)
31+
32+
33+
def convert_from_mp3(file_path):
34+
"""
35+
Converts mp3 audio into wav format.
36+
"""
37+
try:
38+
track = AudioSegment.from_file(file_path, "mp3")
39+
file_handle = track.export(
40+
file_path.replace(".mp3", ".wav"), format='wav'
41+
)
42+
except FileNotFoundError:
43+
print("{} does not appear to be valid. Please check.")
44+
except Exception as e:
45+
print(e)
46+
47+
48+
def convert_from_mp4(file_path):
49+
"""
50+
Converts mp4 audio into wav format.
51+
"""
52+
try:
53+
track = AudioSegment.from_file(file_path, "mp4")
54+
file_handle = track.export(
55+
file_path.replace(".mp4", ".wav"), format='wav'
56+
)
57+
except FileNotFoundError:
58+
print("{} does not appear to be valid. Please check.")
59+
except Exception as e:
60+
print(e)
61+
62+
63+
def convert_from_aac(file_path):
64+
"""
65+
Converts aac audio into wav format.
66+
"""
67+
try:
68+
track = AudioSegment.from_file(file_path, "aac")
69+
file_handle = track.export(
70+
file_path.replace(".aac", ".wav"), format='wav'
71+
)
72+
except FileNotFoundError:
73+
print("{} does not appear to be valid. Please check.")
74+
except Exception as e:
75+
print(e)
76+
77+
78+
def convert_files_to_wav(dir_path, audio_format="m4a"):
79+
"""
80+
Converts all the audio files in the input directory path
81+
with the extension specified by audio_format input
82+
into .wav audio files, and saves them in the same directory.
83+
"""
84+
# Read contents of dir
85+
# Only select files with the mentioned extension
86+
files = glob.glob(os.path.join(dir_path, "*." + audio_format))
87+
88+
# Convert to wav
89+
# The wav files save in the same dir as specified by dir_path
90+
if audio_format == "m4a":
91+
cntr = 0
92+
for aud_file in files:
93+
convert_from_m4a(aud_file)
94+
cntr += 1
95+
print(
96+
"{} {} files converted to .wav and saved in {}".format(
97+
cntr, audio_format, dir_path
98+
)
99+
)
100+
elif audio_format == "mp3":
101+
cntr = 0
102+
for aud_file in files:
103+
convert_from_mp3(aud_file)
104+
cntr += 1
105+
print(
106+
"{} {} files converted to .wav and saved in {}".format(
107+
cntr, audio_format, dir_path
108+
)
109+
)
110+
elif audio_format == "aac":
111+
cntr = 0
112+
for aud_file in files:
113+
convert_from_aac(aud_file)
114+
cntr += 1
115+
print(
116+
"{} {} files converted to .wav and saved in {}".format(
117+
cntr, audio_format, dir_path
118+
)
119+
)
120+
elif audio_format == "mp4":
121+
cntr = 0
122+
for aud_file in files:
123+
convert_from_mp4(aud_file)
124+
cntr += 1
125+
print(
126+
"{} {} files converted to .wav and saved in {}".format(
127+
cntr, audio_format, dir_path
128+
)
129+
)
130+
else:
131+
print(
132+
"File format {} is not in supported types (mp3, mp4, m4a, aac)".format(
133+
audio_format
134+
)
135+
)
136+
137+

pyAudioProcessing/run_classification.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def train_and_classify(
132132
Train on the data under folder_path or classify the data in folder path
133133
using features specified by feature_names and the specified classifier.
134134
"""
135-
# Get all direcotiers under folder_path
135+
# Get all directories under folder_path
136136
data_dirs = [x[0] for x in os.walk(folder_path)][1:]
137137
print(
138138
"\n There are {} classes in the specified data folder\n".format(

0 commit comments

Comments
 (0)