-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.py
94 lines (68 loc) · 2.27 KB
/
split.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
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
import os
import sys
import wavio
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
ffmpegBinary = "\"%s\"" % """C:\\\\Program Files\\\\ffmpeg\\\\bin\\\\ffmpeg.exe"""
soxBinary = "\"%s\"" % """C:\\\\Program Files (x86)\\\\sox-14-4-2\\\\sox.exe"""
def to_wav(mp4Filename, wavFilename):
if os.path.exists(wavFilename):
return
tmpWav = wavFilename+".wav"
cmd = "%s -y -i %s %s" %(ffmpegBinary, mp4Filename, tmpWav)
print(cmd)
os.system(cmd)
cmd = "%s %s -c 1 -r 8000 %s" % (soxBinary, tmpWav, wavFilename)
print(cmd)
#subprocess.call([cmd])
os.system(cmd)
cmd = "del %s" % tmpWav
print(cmd)
os.system(cmd)
def parse(wavFilename):
w = wavio.read(wavFilename)
output = []
data = w.data
wSize = 160
threshold = 100
for i in range(data.shape[0] // wSize):
startPos, endPos = i*wSize, (i+1)* wSize
chunk = data[startPos:endPos]
stats = np.mean(chunk), np.max(np.abs(chunk))
if (stats[1] <= threshold ):
#print(startPos/w.rate, endPos/w.rate, stats)
output.append([0, stats[1], stats[0]])
else:
output.append([1, stats[1], stats[0]])
output = np.array(output)
gap = int(0.1 * w.rate) // wSize
X = []
for i in range(output.shape[0]):
if (output[i,0] == 0):
X.append([output[i,0], i])
X = np.array(X)
clustering = DBSCAN(eps=10, min_samples=2).fit(X)
clusters, currLabel = [], -1
for i in range(len(clustering.labels_)):
if currLabel != clustering.labels_[i]:
clusters.append([X[i][1], X[i][1]])
currLabel = clustering.labels_[i]
else:
clusters[-1][1] = X[i][1]
for i in range(len(clusters)):
a = int(float(clusters[i][0] * wSize) / w.rate)
clusters[i] = ("%2d:%2d:%2d"%(a //3600, (a - a//3600 *3600 )//60, (a % 60))).replace(" ","0")
print("\r\n".join(clusters))
a = int(data.shape[0] / w.rate)
print(("%2d:%2d:%2d"%(a //3600, (a - a//3600 *3600 )//60, (a % 60))).replace(" ","0"))
plt.plot(output[:,2])
plt.plot(output[:,0] * 1600)
plt.show()
def main(argv):
mp4Filename= argv[1]
wavFilename = "w.wav"
to_wav(mp4Filename, wavFilename)
parse(wavFilename)
if __name__ == "__main__":
main(sys.argv)