-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertwav.py
More file actions
96 lines (77 loc) · 3.13 KB
/
convertwav.py
File metadata and controls
96 lines (77 loc) · 3.13 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python
from pydub import AudioSegment
import sys
import re
appname = "convertwav.py"
gaindB = 30
silencethold = -60.0
leadingtime = 1000 # milliseconds
usage = """ This script reads an input WAV file and converts it to
MP3 with a """ + str(gaindB) + """ dB gain.
Usage:
> python """ + appname + """ FILENAME.WAV
Output: FILENAME_converted.mp3
"""
def detect_leading_silence(sound, silence_threshold=silencethold, chunk_size=100):
'''
sound is a pydub.AudioSegment
silence_threshold in dB
chunk_size in ms
iterate over chunks until you find the first one with sound
'''
trim_ms = 0 # ms
assert chunk_size > 0 # to avoid infinite loop
while sound[trim_ms:trim_ms+chunk_size].dBFS < silence_threshold and trim_ms < len(sound):
trim_ms += chunk_size
#print("trim_ms\t" + str(trim_ms) + "\tdBFS\t" + str(sound[trim_ms:trim_ms+chunk_size].dBFS))
return trim_ms
myargs = sys.argv # read command line args
if len(myargs) < 2: # if there are not enough args, print usage and exit
print("ERROR: not enough parameters.\n")
sys.exit(usage)
infile = myargs[1]
if not re.search("\.WAV$", infile, re.IGNORECASE):
print("ERROR: Input", infile, "is not a WAV file.\n")
sys.exit(usage)
outfile = infile.split('.')[0] + "_converted.mp3"
print("Input =", infile)
print("Output =", outfile)
# files
src = infile
dst = outfile
print("Preparing to convert WAV file to MP3 with a " + str(gaindB) + " dB gain.")
print("A " + str(silencethold) + " dB silence threshhold will be applied to the start.")
print("Conversion will begin up to " + str(leadingtime) + " milliseconds before silence threshhold detection.")
input("Press Enter to continue...")
#sys.exit("Goodbye")
print("Converting from WAV to MP3...")
# convert wav to mp3
sound = AudioSegment.from_wav(src)
duration = len(sound)
print("Track duration = " + str(len(sound)) + " milliseconds.")
trim_start = detect_leading_silence(sound)
print("trim_start = " + str(trim_start))
trim_start = max([0,trim_start - leadingtime])
print("trim_start = " + str(trim_start))
#trim_end = detect_leading_silence(sound.reverse()) # trim silence from the end
trim_end = 0
print("trim_end = " + str(trim_end))
extractduration = duration - trim_end - trim_start
print("Extracting " + str(extractduration) + " milliseconds.")
sound = sound[trim_start:duration-trim_end]
print(str(trim_start/1000.0) + " seconds trimmed from start.")
print(str(trim_end/1000.0) + " seconds trimmed from end.")
# apply gain factor
chan = sound.split_to_mono() # split multiple channels into mono
sound = sound + gaindB
sound.export(dst, format="mp3")
for i in range(0, sound.channels):
print("Channel " + str(i))
if (trim_start > 0):
print("Average background level = ", str(chan[i][0:trim_start].dBFS))
dst = infile.split('.')[0] + "_chan" + str(i) + ".mp3"
print("Writing channel output to " + dst)
chan[i] = chan[i] + gaindB
chan[i].export(dst, format="mp3")
print("Conversion complete.")
input("Press Enter to continue...")