-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncMatch.py
151 lines (112 loc) · 4.88 KB
/
syncMatch.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'''
Created on Jan 24, 2014
@author: rick
'''
import csv
import os
import shutil
import fnmatch
import numpy as np
import pylab
from collections import Counter
import Tkinter, tkFileDialog
from subprocess import Popen, PIPE
root = Tkinter.Tk()
root.withdraw()
vamp_cmd_strings = {
'prep': '~/../../opt/local/bin/ffmpeg -i {0} -ac 1 -ar 16000 {1}',
'merge': '~/../../opt/local/bin/ffmpeg -i {0} -i {1} -filter_complex amerge {2}',
'sync': '/Users/danilogr/Projects/syncmatch/vamp/./vamp-simple-host match-vamp-plugin.dylib:match {0} 3 -o {1}' #hardcoding location of vamp files
}
def ffmpeg_prep(source, target, encoding):
"""Uses a shell call to ffmpeg to convert a video
to the desired encoding"""
print 'Prep started'
# Popen calls the ffmpeg process, and collects the standard out/error
p = Popen(vamp_cmd_strings[encoding].format(source, target), stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = p.communicate(input=None)
print 'Prep complete'
return stdout, stderr
def ffmpeg_merge(source1, source2, target, encoding):
"""Uses a shell call to ffmpeg to convert a video
to the desired encoding"""
print 'Merge started'
# Popen calls the ffmpeg process, and collects the standard out/error
p = Popen(vamp_cmd_strings[encoding].format(source1, source2, target), stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = p.communicate(input=None)
print 'Merge complete'
return stdout, stderr
def vamp_sync(source, target, encoding):
"""Uses a shell call to ffmpeg to convert a video
to the desired encoding"""
print 'Vamp sync started'
# Popen calls the ffmpeg process, and collects the standard out/error
p = Popen(vamp_cmd_strings[encoding].format(source, target), stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = p.communicate(input=None)
print 'Vamp sync complete'
return stdout, stderr
def findMode(data):
rawdata = np.genfromtxt(data,dtype='float', delimiter = ',', skiprows=0, skip_header=0, skip_footer=0, usecols=1, usemask=True, invalid_raise=False)
pylab.figure()
pylab.hist(rawdata, 250, histtype='stepfilled')
pylab.savefig(os.path.join(os.path.dirname(os.path.dirname(data)), 'histo.png'))
print "Finding Sync Point"
nData = []
for el in rawdata:
nData.append(el)
syncData = Counter(nData)
sync = syncData.most_common(1)
return "Merge,"+str((sync[0])[0])+"\n"
def syncPrep(video1, video2):
tempOutput = os.path.join(os.path.dirname(video1), "temp")
if not os.path.exists(tempOutput):
os.makedirs(tempOutput)
print ffmpeg_prep(video1, os.path.join(tempOutput,'v1.wav'), 'prep')
print ffmpeg_prep(video2, os.path.join(tempOutput,'v2.wav'), 'prep')
print ffmpeg_merge(os.path.join(tempOutput,'v1.wav'), os.path.join(tempOutput,'v2.wav'), os.path.join(tempOutput,'merge.wav'), 'merge')
print vamp_sync(os.path.join(tempOutput,'merge.wav'), os.path.join(tempOutput,'merge.txt'), 'sync')
return tempOutput
def syncTxt2csv(tempdirectory):
for filename in (os.listdir(tempdirectory)):
#print filename
if fnmatch.fnmatch(filename, '*.txt'):
filepath = os.path.join(tempdirectory, filename)
savename = os.path.splitext(filepath)[0]+".csv"
txt_file = r""+filepath+""
csv_file = r""+savename+""
with open(txt_file, 'rb') as in_txt, open(csv_file, 'wb') as out_csv:
inReader = csv.reader(in_txt, delimiter = ':')
outWriter = csv.writer(out_csv)
for row in inReader:
outWriter.writerow(row)
in_txt.close()
out_csv.close()
def syncMatch(video1, video2):
tempSyncDir = syncPrep(video1, video2)
syncTxt2csv(tempSyncDir)
syncHeader = "Data Stream, Sync Point (in seconds)\n"
writeSyncHeader = True
syncOutputData = []
syncsavename = os.path.join(os.path.dirname(video1), "syncData.csv")
sync_file = r""+syncsavename+""
with open(sync_file, 'wb') as syncOutput:
syncWriter = csv.writer(syncOutput)
for csvfile in os.listdir(tempSyncDir):
#print csvfile
if fnmatch.fnmatch(csvfile, 'merge.csv'):
syncOutputData.append(findMode(os.path.join(tempSyncDir,csvfile)))
if writeSyncHeader:
syncWriter.writerow(syncHeader.split(','))
writeSyncHeader = False
syncRows = list(syncOutputData)
for el in syncRows:
syncWriter.writerow(el.split(','))
syncOutput.close()
#danger this last line deletes the temp folder
shutil.rmtree(tempSyncDir)
print "Synchronization Complete"
if __name__ == "__main__":
video1 = tkFileDialog.askopenfilename()
video2 = tkFileDialog.askopenfilename()
root.destroy()
syncMatch(video1, video2)