-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertMusics.py
39 lines (33 loc) · 1.31 KB
/
convertMusics.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
from pydub import AudioSegment
import re, os, sys
def convertFile(inputFile, outputFile):
if '.wav' not in inputFile:
raise ValueError()
music = AudioSegment.from_wav(inputFile)
outputFolder = os.path.split(outputFile)[0]
if not os.path.exists(outputFolder):
os.mkdir(outputFolder)
music.export(outputFile, format="mp3")
def convertFiles(inputFolder, outputFolder):
if not os.path.exists(outputFolder):
os.mkdir(outputFolder)
for dirname, dirnames, filenames in os.walk(inputFolder):
filenames.sort(reverse=True)
for filename in filenames:
try:
inputFile = os.path.join(dirname, filename)
outputFile = os.path.join(outputFolder, os.path.split(dirname)[1], filename.replace('.wav', '.mp3'))
print('converting ' + inputFile + ' into ' + outputFile)
convertFile(inputFile, outputFile)
except ValueError:
print('Wrong filename. Skipping this file.')
if __name__ == "__main__":
inputFolder = ''
outputFolder = ''
if len(sys.argv) < 3:
inputFolder = input('>> Input folder : ')
outputFolder = input('>> Output folder : ')
else:
inputFolder = sys.argv[1]
outputFolder = sys.argv[2]
convertFiles(inputFolder, outputFolder)