-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVideoSync.py
More file actions
283 lines (247 loc) · 12.2 KB
/
VideoSync.py
File metadata and controls
283 lines (247 loc) · 12.2 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import numpy as np
import matplotlib.pyplot as plt
from subprocess import run, call
from pathlib import Path
import yaml
import os
from scipy.io import wavfile
from scipy.io.wavfile import read as wavread
import mne
class VideoSync:
def __init__(self, input_folder=None, output_folder="/output"):
"""
Summary: VideoSync represents an object performing synchronization between multiple streams of data.
Attributes:
input_folder: Directory where data is supplied from.
output_folder: Directory where analysis files will be published to. Defaults to "input_folder/output"
self.video_paths: List of RELATIVE paths to video files.
self.audio_paths: List of RELATIVE paths to audio files.
self.meg_paths: List of RELATIVE paths to meg files.
"""
# User defined paths for input/output directories
self.__input_folder = input_folder
self.__output_folder = output_folder
# Paths of data within a folder
self.__video_paths = []
self.__audio_paths = []
self.__meg_paths= []
# Instance variables representing data
def get_video_paths(self):
"""
Returns:
None: if no path has been defined
string: List of paths (strings) representing videos
"""
if len(self.__video_paths) == 0:
print("video_path has not yet been instantiated. Please use set_video_path or parse_files_from_folder to instantiate this attribute.")
return None
return self.__video_paths.copy()
def get_audio_paths(self):
"""
Returns:
None: if no path has been defined
string: List of paths (strings) representing audio files
"""
if len(self.__audio_paths) == 0:
print("audio_path has not yet been instantiated. Please use set_audio_path or parse_files_from_folder to instantiate this attribute.")
return None
return self.__audio_paths.copy()
def get_meg_path(self):
"""
Returns:
None: if path has not yet been defined
string: Path of the meg file for a given VideoSync object.
"""
if len(self.__meg_paths) == 0:
print("meg_path has not yet been instantiated. Please use set_meg_path or parse_files_from_folder to instantiate this attribute.")
return None
return self.__meg_paths.copy()
def get_input_folder(self):
"""
Returns:
None: if path has not yet been defined
string: Path of the input folder for a given VideoSync object.
"""
if self.__input_folder is None:
print("input_path has not yet been instantiated. Please use set_input_path to instantiate this attribute.")
return None
return self.__input_folder
def get_output_folder(self):
"""
Returns:
None: if path has not yet been defined
string: Path of the output folder for a given VideoSync object.
"""
if self.__output_folder is None:
print("output_path has not yet been instantiated. Please use set_output_path to instantiate this attribute.")
return None
return self.__output_folder
def set_video_paths(self, paths):
"""
Summary: Sets video_paths to a list of paths supplied as arguments.
"""
temp = []
if type(paths) != list:
raise TypeError("Cannot set video paths to given input. Expected list, recieved " + type(paths) + " Paths supplied must be contained within a list in format [s1, s2, ..., sn].")
for i,item in enumerate(paths):
if not type(item) == str:
raise TypeError("Could not set video paths to the provided path. Item " + str(item) + " at index " + str(i) + " is not a string")
else:
temp.append(item)
if not os.path.exists(os.path.join(self.__input_folder, item)):
print("Warning: \'"+ item + "\' could not be found as an item or directory.")
self.__video_paths = temp
def set_audio_paths(self, paths):
"""
Summary: Sets audio_paths to a list of paths supplied as arguments.
"""
temp = []
if type(paths) != list:
raise TypeError("Cannot set audio paths to given input. Expected list, recieved " + type(paths) + " Paths supplied must be contained within a list in format [s1, s2, ..., sn].")
for i,item in enumerate(paths):
if not type(item) == str:
raise TypeError("Could not set audio paths to the provided path. Item " + str(item) + " at index " + str(i) + " is not a string")
else:
temp.append(item)
if not os.path.exists(os.path.join(self.__input_folder, item)):
print("Warning: \'"+ item + "\' could not be found as an item or directory.")
self.__audio_paths = temp
def set_meg_paths(self, paths):
"""
Summary: Sets meg_paths to a list of paths supplied as arguments.
"""
temp = []
if type(paths) != list:
raise TypeError("Cannot set meg paths to given input. Expected list, recieved " + type(paths) + " Paths supplied must be contained within a list in format [s1, s2, ..., sn].")
for i,item in enumerate(paths):
if not type(item) == str:
raise TypeError("Could not set meg paths to the provided path. Item " + str(item) + " at index " + str(i) + " is not a string")
else:
temp.append(item)
if not os.path.exists(os.path.join(self.__input_folder, item)):
print("Warning: \'"+ item + "\' could not be found as an item or directory.")
self.__meg_paths = temp
def set_input_folder(self, path):
"""
Summary: Sets the input folder to the specified path.
Arguments:
path: The path of the input directory
"""
if type(path) != str:
raise TypeError('Path must be provided as a string.')
if not os.path.exists(path):
print("Warning: "+ path +" could not be found as a file or directory.")
self.__input_folder == path
def set_output_folder(self,path):
"""
Summary: Sets the output folder to the specified path. This is the location where outputs will be stored. Will create this
directory if it does not already exist.
Arguments:
path: The path of the desired output directory
"""
if type(path) != str:
raise TypeError('Path must be provided as a string.')
if not os.path.exists(path):
print("Warning: "+ path +" could not be found as a file or directory.")
self.__output_folder == path
def parse_files_from_folder(self):
"""
Summary: If an input folder has been declared, read through this folder and set paths to each of the videos, audio, and
"""
files = os.listdir(self.__input_folder)
audio_paths = []
video_paths = []
meg_paths = []
for file in files:
ext = os.path.splitext(file)[1]
if ext == ".wav":
audio_paths.append(file)
elif ext == ".fif":
meg_paths.append(file)
elif ext == ".mp4":
video_paths.append(file)
else:
pass
self.set_audio_paths(audio_paths)
self.set_meg_paths(meg_paths)
self.set_video_paths(video_paths)
def extract_audio_from_videos(self, channels='all'):
"""
Summary: Extracts audio tracks from each video requested, and places the audio files in input_folder.
Arguments:
channels: Path names of videos that should have their audio extracted. By default, this function will extract from all videos.
"""
if channels == 'all':
channels = self.__video_paths
if len(channels) == 0:
raise Exception("self.__audio_paths has not been instantiated. Please use set_audio_paths or parse_files_from_folder to populate with paths.")
if len(channels) == 0:
raise Exception("Channels must not be empty.")
if type(channels) != list:
raise TypeError("Expected list, recieved " + type(channels) + ". Channels supplied must be contained within a list in format [s1, s2, ..., sn].")
audio_codecout = 'pcm_s16le'
audio_suffix = '_16bit'
for video in channels:
audio_file = os.path.splitext(video)[0] + audio_suffix + '.wav'
if os.path.exists(os.path.join(self.__input_folder, audio_file)):
continue
video_file = os.path.join(self.__input_folder, video)
command = ['ffmpeg',
'-acodec', 'pcm_s24le', # force little-endian format (req'd for Linux)
'-i', video_file,
'-map', '0:a', # audio only (per DM)
# '-af', 'highpass=f=0.1',
'-acodec', audio_codecout,
'-ac', '2', # no longer mono output, so setting to "2"
'-y', '-vn', # overwrite output file without asking; no video
'-loglevel', 'error',
audio_file]
pipe = run(command, timeout=50)
if pipe.returncode==0:
print('Audio extraction was successful for ' + video_file)
output_path = os.path.join(self.__input_folder, self.__output_folder, audio_file)
os.renames(audio_file, output_path)
self.set_audio_paths(self.__audio_paths + [output_path])
def display_pulses(self, channels='all', tmin=0, tmax=np.inf):
"""
Summary: Displays pulses from audio files supplied.
Parameters:
channels: Which audio files to show. Defaults to all, which uses self.__audio_paths.
tmin: Minimum time allowed to show on graph.
tmax Maximum time allowed to show on graph.
"""
if channels == 'all':
channels = self.__audio_paths
if len(channels) == 0:
raise Exception("self.__audio_paths has not been instantiated. Please use set_audio_paths or parse_files_from_folder to populate with paths.")
if len(channels) == 0:
raise Exception("Channels must not be empty.")
if type(channels) != list:
raise TypeError("Expected list, recieved " + type(channels) + ". Channels supplied must be contained within a list in format [s1, s2, ..., sn].")
if any(type(c)!=str or not os.path.exists(os.path.join(self.__input_folder, c)) for c in channels):
raise TypeError("Channels provided must be in valid paths, provided as a string.")
sync_channel = 1;
audio_channel = 0
assert set((sync_channel, audio_channel))=={0,1}
fig, axset = plt.subplots(len(channels)+1, 1, figsize = [8,6]) #show individual channels seperately, and the 0th plot is the combination of these.
for i, audio in enumerate(channels):
splitName = np.array(audio.split("_")) #name should be split by underscores, check for index that contains word "CAM"
title = splitName[np.flatnonzero(np.core.defchararray.find(splitName,"CAM")!=-1)]
srate, wav_signal = wavread(os.path.join(self.__input_folder,audio))
npts = wav_signal.shape[0]
# print(f'Numpy array of size {wav_signal.shape} created.')
# print(f'Sampling rate is {srate} Hz for a total duration of {npts/srate:.2f} seconds.')
tt = np.arange(npts) / srate
idx = np.where((tt>=tmin) & (tt<tmax))
axset[0].plot(tt[idx], wav_signal[idx, sync_channel].T, label=title)
axset[i+1].plot(tt[idx], wav_signal[idx, sync_channel].T)
# Make label equal to simply the cam number
axset[i+1].set_ylabel(title)
axset[0].set_title("Sync Channels for " + self.__input_folder)
axset[0].legend()
plt.show()
def align_pulses(self, channels=[]):
"""
Summary:
"""
pass