-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_audio_video.py
94 lines (72 loc) · 3.27 KB
/
fix_audio_video.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
#!/usr/bin/python
"""
Author-skarumbaiah
Date-06/23/2016
Script to create elan files for all participants in the video corpus for event and remark annotation
Links to the appropriate video and audio files
Creates new file with name elan<participant#>.eaf
prereq-
place the template elan.eaf in the same folder as audio and video files
template (elan.eaf) can be found in SVN
-e : elan file path
-a : audio video path
input:
new_elan_<num>.eaf files from elan_offset_calibrator script
example-
-e "E:/ENGAGE_Videos/Video Corpus Annotation/offset corrected elans with game log"
-a "E:/ENGAGE_Videos"
"""
from xml.dom.minidom import parse as pr
import os
import sys
import argparse
def generate_elan(elan,audio):
for j in range(80):
i = j+1
if os.path.isfile(elan+'new_elan' + str(i) +'.eaf'):
count = 0
tree = pr(elan+'new_elan' + str(i) +'.eaf')
annotationTree = tree.documentElement
headers = annotationTree.getElementsByTagName("HEADER")
for header in headers:
media = header.getElementsByTagName("MEDIA_DESCRIPTOR")
for med in media:
if count == 0:
if med.hasAttribute("MEDIA_URL") and not med.hasAttribute("EXTRACTED_FROM") :
count = 1
name = "file:///" + audio + str(i) + ".mp4"
med.setAttribute("MEDIA_URL", name )
if med.hasAttribute("RELATIVE_MEDIA_URL") and not med.hasAttribute("EXTRACTED_FROM"):
count = 1
name = "./" + str(i) + ".mp4"
med.setAttribute("RELATIVE_MEDIA_URL", name )
else:
if med.hasAttribute("EXTRACTED_FROM"):
name = "file:///" + audio + str(i) + ".wav"
med.setAttribute("EXTRACTED_FROM", name )
if med.hasAttribute("MEDIA_URL"):
name = "file:///" + audio + str(i) + ".wav"
med.setAttribute("MEDIA_URL", name )
if med.hasAttribute("RELATIVE_MEDIA_URL"):
name = "./" + str(i) + ".wav"
med.setAttribute("RELATIVE_MEDIA_URL", name )
open(elan + 'elan' + str(i) +'.eaf', 'w').close()
new_efname = elan + 'elan' + str(i) +'.eaf'
tree.writexml( open(new_efname, 'w'),
indent=" ",
addindent=" ",
newl='\n')
print("Created new elan files")
def main(args):
""" parse command like argument"""
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--elan")
parser.add_argument("-a", "--audio")
args = parser.parse_args()
elan = args.elan
audio = args.audio
print("Looking for audio in the directory -", audio)
print("Looking for elan in the directory -", elan)
generate_elan(elan,audio)
if __name__ == '__main__':
main(sys.argv[1:])