-
Notifications
You must be signed in to change notification settings - Fork 27
/
video2frame.py
215 lines (174 loc) · 6.72 KB
/
video2frame.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
import json
import re
import shutil
import subprocess
import warnings
from concurrent import futures
from pathlib import Path
from random import randint, random, shuffle
from tqdm import tqdm
from storage import STORAGE_TYPES
from util import parse_args, retry
ffmpeg_duration_template = re.compile(r"time=\s*(\d+):(\d+):(\d+)\.(\d+)")
def get_video_duration(video_file):
cmd = [
"ffmpeg",
"-i", str(video_file),
"-f", "null", "-"
]
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
result_all = ffmpeg_duration_template.findall(output.decode())
if result_all:
result = result_all[-1]
duration = float(result[0]) * 60 * 60 \
+ float(result[1]) * 60 \
+ float(result[2]) \
+ float(result[3]) * (10 ** -len(result[3]))
else:
duration = -1
return duration
def get_video_meta(video_file):
try:
cmd = [
"ffprobe",
"-v", "quiet",
"-show_streams",
"-print_format", "json",
str(video_file)
]
output = subprocess.check_output(cmd)
output = json.loads(output)
streamsbytype = {}
for stream in output["streams"]:
streamsbytype[stream["codec_type"].lower()] = stream
return streamsbytype
except:
return {}
@retry()
def video_to_frames(args, video_file, video_meta, tmp_dir, error_when_empty=True):
# Random clip the video
clip_setting = []
if args.duration > 0:
try:
video_duration = float(video_meta["video"]["duration"])
except:
video_duration = get_video_duration(video_file)
if video_duration > 0:
sta = max(0., random() * (video_duration - args.duration))
dur = min(args.duration, video_duration - sta)
clip_setting.extend([
"-ss", "{}".format(sta),
"-t", "{}".format(dur)
])
else:
warnings.warn("Ignore `duration` parameter for video {}.".format(video_file))
cmd = [
"ffmpeg",
"-loglevel", "panic",
"-vsync", "vfr",
"-i", str(video_file),
*args.vf_setting,
*clip_setting,
"-qscale:v", "2",
str(tmp_dir / "%8d.jpg")
]
subprocess.call(cmd)
frames = [(int(f.name.split('.')[0]), f) for f in tmp_dir.iterdir()]
frames.sort(key=lambda x: x[0])
if error_when_empty and not frames:
raise RuntimeError("Extract frame failed")
return frames
@retry()
def sample_frames(args, frames, error_when_empty=True):
if args.sample_mode:
assert args.sample > 0, "Sample must >0, but get {}".format(args.sample)
tot = len(frames)
if args.sample_mode == 1: # Uniformly sample n frames
if args.sample == 1:
index = [tot >> 1]
else:
step = (tot - 1.) / (args.sample - 1)
index = [round(x * step) for x in range(args.sample)]
frames = [frames[x] for x in index]
elif args.sample_mode == 2: # Randomly sample n continuous frames
sta = randint(0, max(0, len(frames) - args.sample - 1))
frames = frames[sta:sta + args.sample]
elif args.sample_mode == 3: # Randomly sample n frames
shuffle(frames)
frames = frames[:min(args.sample, tot)]
frames.sort(key=lambda x: x[0])
elif args.sample_mode == 4: # Sample 1 frame every n frames
frames = frames[::args.sample]
else:
raise AttributeError("Sample mode is not supported")
if error_when_empty and not frames:
raise RuntimeError("No frame selected")
return frames
def process(args, video_key, video_info, frame_db):
video_file = Path(video_info['path'])
video_tmp_dir = Path(args.tmp_dir) / "{}".format(video_key)
video_tmp_dir.mkdir(exist_ok=True)
if not video_file.exists():
raise RuntimeError("Video not exists")
video_meta = get_video_meta(video_file)
if not video_meta:
raise RuntimeError("Can not get video info")
for ith_clip in range(args.clips):
clip_tmp_dir = video_tmp_dir / "{:03d}".format(ith_clip)
clip_tmp_dir.mkdir(exist_ok=True, parents=True)
# Get all frames
frames = video_to_frames(args, video_file, video_meta, clip_tmp_dir)
# Sample frames
frames = sample_frames(args, frames)
# Save to database
frame_db.put(video_key, ith_clip, clip_tmp_dir, frames)
if not args.keep:
shutil.rmtree(clip_tmp_dir, ignore_errors=True)
if not args.keep:
shutil.rmtree(video_tmp_dir, ignore_errors=True)
return "OK"
if "__main__" == __name__:
args = parse_args()
Path(args.tmp_dir).mkdir(exist_ok=True)
frame_db = STORAGE_TYPES[args.db_type](args.db_name)
annotation_all = json.load(Path(args.annotation_file).open())
annotation = annotation_all["annotation"]
total = len(annotation)
fails = []
if args.threads > 0:
with futures.ThreadPoolExecutor(max_workers=args.threads) as executor:
jobs = {
executor.submit(process, args, video_key, video_info, frame_db): video_info['path']
for video_key, video_info in annotation.items()
}
for future in tqdm(futures.as_completed(jobs), total=total):
try:
video_status = future.result()
except Exception as e:
tqdm.write("{} : {}".format(jobs[future], e))
fails.append(jobs[future])
else:
tqdm.write("{} : {}".format(jobs[future], video_status))
else:
for video_key, video_info in tqdm(annotation.items()):
try:
video_status = process(args, video_key, video_info, frame_db)
except Exception as e:
tqdm.write("{} : {}".format(video_info['path'], e))
fails.append(video_info['path'])
else:
tqdm.write("{} : {}".format(video_info['path'], video_status))
frame_db.close()
print("Processed {} videos".format(total))
if not fails:
print("All success! Congratulations!")
else:
print("{} Success, {} Error".format(total - len(fails), len(fails)))
annotation = {k: v for k, v in annotation.items() if v['path'] not in fails}
annotation_all["annotation"] = annotation
if args.annotation_file.lower().endswith(".json"):
save_path = args.annotation_file[:-5] + "-fix.json"
else:
save_path = args.annotation_file + "-fix.json"
json.dump(annotation_all, Path(save_path).open("w"), indent=4)
print("All Done!")