-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
77 lines (74 loc) · 2.9 KB
/
script.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
######
#
# Marco A. Flores-Coronado, Universidad Autónoma del Estado de Morelos (UAEM)
# 2020
#
# This code extracts audio information from a video file and then computes
# Mel Frecuency Cepstral Coefficient (MFCC) from the aforementioned. Resulting
# vectors may be saved as .txt file
############# Libraries ################
from moviepy.editor import *
import scipy
import glob
############# main #################
def main():
openFile=True
writeaudio=True
MFCC=True
analizeExtracted=True
fileDirection="/home/marco/Documents/spyder-py3/stimuli/ga/"
audioDirection="/direction/to/audio"
files=glob.glob(fileDirection+"*.mp4")
saveplain=True
for videoDirection in files:
if (openFile==True):
import sys
print("Opening the target video and extracting audio")
audiovisualfile=VideoFileClip(videoDirection)#opening video file
audio=audiovisualfile.audio#extracting audio information
if (writeaudio==True):
import os
print("Saving audio information as .wav file")
output_path="./output/"
if not os.path.exists(output_path):
os.makedirs(output_path)
direction=videoDirection.split("/")
syllable=direction[-2]
ext=direction[-1]
ext=ext.split(".")
ext=ext[0]
name=output_path+syllable+ext+".wav"
audio.write_audiofile(name)
audiovisualfile.close()#we need to close the objects
audio.close()
if (MFCC==True):
import scipy.io.wavfile as wav
from python_speech_features import mfcc
if (analizeExtracted==True):
(sampleRate,clip)=wav.read(name)
else:
(sampleRate,clip)=wav.read(audioDirection)
mfcc_extracted=mfcc(clip,
sampleRate,
winlen=0.025,#width of the analysis window (ms)
winstep=0.01,#step within windows (s)
numcep=13,#number of cepstrals exctracted
nfilt=26,#number of filters used for mfcc
nfft=512)#number of fast Fourier Transform from
#which mfcc are extracted
if (saveplain==True):
text_path="./plain/"
if not os.path.exists(text_path):
os.makedirs(text_path)
direction=videoDirection.split("/")
syllable=direction[-2]
ext=direction[-1]
ext=ext.split(".")
ext=ext[0]
document=text_path+syllable+ext+".csv"
from numpy import savetxt
savetxt(document,mfcc_extracted,fmt='%1.32f',delimiter=",")
if __name__ == "__main__":
main()