-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.py
executable file
·321 lines (263 loc) · 9.94 KB
/
decode.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from PIL import Image
from PIL.ExifTags import TAGS
from datetime import datetime
from tqdm import tqdm
from multiprocess import Pool, Lock
from collections import defaultdict
import re
import os
import argparse
import json
import subprocess
log_lock = Lock()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--job_array_num", required=False, type=int)
parser.add_argument("--backup_dir", required=True, type=str)
parser.add_argument("--dest_dir", required=True, type=str)
parser.add_argument("--video_dim", nargs=2, type=int, default=(1080, 1920))
parser.add_argument("--log_file", type=str, default=None)
parser.add_argument("--skip_extraction", action="store_true")
parser.add_argument(
"--buffer",
nargs=2,
type=float,
default=(-0.5, 0.5),
help="Buffer for start/end of video",
)
parser.add_argument(
"--invert", action="store_true", help="Switch start/end timestamps."
)
parser.add_argument("--num_threads", type=int, default=5)
parser.add_argument("--make_structured_dirs", action="store_true")
parser.add_argument("--use_cuda", type=bool, default=False)
parser.add_argument(
"--ffmpeg_loglevel",
choices=[
"quiet",
"panic",
"fatal",
"error",
"warning",
"info",
"verbose",
"debug",
"trace",
],
default="fatal",
)
args = parser.parse_args()
return args
def get_image_description(exifdata):
description = ""
# iterating over all EXIF data fields
for tag_id in exifdata:
# get the tag name, instead of human unreadable tag id
tag = TAGS.get(tag_id, tag_id)
data = exifdata.get(tag_id)
# decode bytes
if isinstance(data, bytes):
data = data.decode()
# print(f"{tag:25}: {data}")
if tag == "ImageDescription":
description = data
# print(description)
return description
def get_data_from_description(description):
is_valid_exists = "isValid" in description
subbed = re.sub(r"file=(.*?),", r'"\1",', description)
subbed = re.sub(r"videoStart=(.*?),", r'"\1",', subbed)
subbed = re.sub(r"signStart=(.*?),", r'"\1",', subbed)
if is_valid_exists:
subbed = re.sub(r"signEnd=(.*?),", r'"\1",', subbed)
subbed = re.sub(r"isValid=(.*?)\)", r"\1)", subbed)
else:
subbed = re.sub(r"signEnd=(.*?)\)", r'"\1")', subbed)
data = eval(subbed)
return data, is_valid_exists
def clean_sign(sign):
sign = sign.replace(" / ", "")
sign = sign.replace(" ", "")
sign = sign.replace("-", "")
sign = sign.replace(",", "")
sign = sign.replace("(", "")
sign = sign.replace(")", "")
return sign
def extract_clip_from_video(
args, uid, sign, recording_idx, recording, videopath, is_valid_exists
):
with open("config.json") as f:
config = json.load(f)
print("Recording: ", recording)
if is_valid_exists:
filename, video_start_time, sign_start_time, sign_end_time, is_valid = recording
else:
filename, video_start_time, sign_start_time, sign_end_time = recording
is_valid = True
video_start_time_date = datetime.strptime(
video_start_time + "000", "%Y_%m_%d_%H_%M_%S.%f"
)
sign_start_time_date = datetime.strptime(
sign_start_time + "000", "%Y_%m_%d_%H_%M_%S.%f"
)
sign_end_time_date = datetime.strptime(
sign_end_time + "000", "%Y_%m_%d_%H_%M_%S.%f"
)
sign = clean_sign(sign)
print(
"Video Info: ", sign, filename, video_start_time, sign_start_time, sign_end_time
)
start_seconds = sign_start_time_date - video_start_time_date
end_seconds = sign_end_time_date - video_start_time_date
start_subclip = start_seconds.seconds + start_seconds.microseconds / 1e6
end_subclip = end_seconds.seconds + end_seconds.microseconds / 1e6
if uid in config:
buffer_0 = config[uid]["buffer_start"]
buffer_1 = config[uid]["buffer_end"]
invert = config[uid]["invert"]
if invert:
start_subclip = end_subclip + buffer_0
end_subclip += buffer_1
else:
start_subclip += buffer_0
end_subclip += buffer_1
else:
print(f"Could not find {uid} in config.json file")
if args.invert:
start_subclip = end_subclip + args.buffer[0]
end_subclip += args.buffer[1]
else:
start_subclip += args.buffer[0]
end_subclip += args.buffer[1]
output_dir = args.dest_dir
if not is_valid:
output_dir = os.path.join(args.dest_dir, "error")
if args.make_structured_dirs:
video_filename = f"{sign_start_time}-{recording_idx}.mp4"
output_dir = os.path.join(args.dest_dir, f"{uid}", f"{sign}")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
else:
output_dir = args.dest_dir
print("Filename Struct: ", uid, sign, video_start_time, recording_idx)
video_filename = f"{uid}-{sign}-{video_start_time}-{recording_idx}.mp4"
time = end_subclip - start_subclip
full_filename = os.path.join(output_dir, video_filename)
if args.use_cuda:
subprocess.run(
[
"ffmpeg",
"-hwaccel",
"cuda",
"-hwaccel_output_format",
"cuda",
"-i",
videopath,
"-vf",
f"scale={str(args.video_dim[0])}:{str(args.video_dim[1])}",
"-ss",
start_subclip.strftime("%H:%M:%S.%f")[:-3],
"-t",
end_subclip.strftime("%H:%M:%S.%f")[:-3],
"-c:v",
"hevc_nvenc",
"-c:a",
"copy",
str(full_filename),
"-loglevel",
args.ffmpeg_loglevel,
]
)
else:
args = (
f"ffmpeg -y -nostdin -ss {start_subclip:.2f} -i {videopath} "
f"-t {time:.2f} -c:v libx264 {full_filename}"
)
# Call ffmpeg directly
subprocess.run(args, shell=True, check=True)
def get_uid(args, filename):
imagepath = os.path.join(args.backup_dir, filename)
videopath = re.sub(r"-timestamps.jpg", r".mp4", imagepath)
_, videoname = os.path.split(videopath)
uid = videoname.split("-")[0]
return uid, videopath
def count_recording(sign, recording, is_valid_exists, recording_count):
if is_valid_exists:
_, _, _, _, is_valid = recording
if is_valid:
recording_count[sign] += 1
else:
recording_count[sign] += 1
return recording_count
def process_file(args, pool, pbar, filename, results, signs, recording_count):
pbar.set_description("Processing Timestamps File: %s" % filename)
# results = []
# signs = set()
# recording_count = 0
if filename.endswith("-timestamps.jpg"):
image = Image.open(os.path.join(args.backup_dir, filename))
exifdata = image.getexif()
description = get_image_description(exifdata)
data, is_valid_exists = get_data_from_description(description)
uid, videopath = get_uid(args, filename)
if os.path.exists(videopath):
for sign, recording_list in data.items():
signs.add(clean_sign(sign))
for idx, recording in enumerate(recording_list):
count_recording(sign, recording, is_valid_exists, recording_count)
if not args.skip_extraction:
thread_args = (
args,
uid,
sign,
idx,
recording,
videopath,
is_valid_exists,
)
result = pool.apply_async(
extract_clip_from_video, args=thread_args
)
results.append(result)
# return results, signs, recording_count
def make_missing_dirs(args):
if not os.path.exists(args.dest_dir):
os.makedirs(args.dest_dir)
if not os.path.exists(os.path.join(args.dest_dir, "error")):
os.makedirs(os.path.join(args.dest_dir, "error"))
if not os.path.exists("logs"):
os.mkdir("logs")
if __name__ == "__main__":
args = parse_args()
print("Args: ", args)
make_missing_dirs(args)
if args.log_file is None:
args.log_file = os.path.join(
"logs", "decode_" + datetime.now().strftime("%Y-%m-%d_%H-%M")
)
if args.job_array_num is not None:
backup_dir = os.fsencode(args.backup_dir)
with open(
f"/data/sign_language_videos/batches/batch_{args.job_array_num}.txt"
) as fin:
assigned_videos = fin.read().splitlines()
pbar = tqdm(assigned_videos)
pool = Pool(args.num_threads)
else:
backup_dir = os.fsencode(args.backup_dir)
pbar = tqdm(os.listdir(backup_dir))
pool = Pool(args.num_threads)
results = []
signs = set()
recording_count = defaultdict(int)
for file in pbar:
filename = os.fsdecode(file)
if filename.endswith(".zip"):
continue
process_file(args, pool, pbar, filename, results, signs, recording_count)
for result in results:
result.get()
pool.close()
print("Signs: ", signs)
print("Recording Count (Total): ", sum(recording_count.values()))
print("Recording Count (by Sign): ", recording_count)