-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp3_to_wavogg.py
57 lines (42 loc) · 1.47 KB
/
mp3_to_wavogg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pydub import AudioSegment
import os, glob, sys
root_mp3_path = 'indec_mp3'
root_output = 'audios'
wav_path = os.path.join(root_output, 'wav')
ogg_path = os.path.join(root_output, 'ogg')
mp3_path = os.path.join(root_output, 'mp3')
def main(folder = root_mp3_path, files_type ='*.mp3'):
for mp3 in glob.glob(os.path.join(folder, files_type) ):
# for mp3 in mp3s:
""" convert mp3 to wav """
wav_name = mp3.split('/')[-1].replace('.mp3', '.wav')
ogg_name = mp3.split('/')[-1].replace('.mp3', '.ogg')
mp3_name = mp3.split('/')[-1]
src_mp3 = os.path.join(mp3_path, mp3_name).replace(' ', '_').lower()
src_wav = os.path.join(wav_path, wav_name).replace(' ', '_').lower()
src_ogg = os.path.join(ogg_path, ogg_name).replace(' ', '_').lower()
sound = AudioSegment.from_file(mp3)
sound.export(src_mp3, format="mp3")
sound.export(src_ogg, format="ogg")
sound = sound.set_channels(1) # set to mono
sound.export(src_wav, format="wav")
if __name__ == "__main__":
if not os.path.isdir(root_output):
""" Check if OUTPUT_DIR exits """
os.mkdir(root_output)
if not os.path.isdir(mp3_path):
""" Check if OUTPUT_DIR exits """
os.mkdir(mp3_path)
if not os.path.isdir(wav_path):
""" Check if OUTPUT_DIR exits """
os.mkdir(wav_path)
if not os.path.isdir(ogg_path):
""" Check if OUTPUT_DIR exits """
os.mkdir(ogg_path)
try:
main(folder=sys.argv[1])
except:
print 'error'
main()