forked from mit-han-lab/temporal-shift-module
-
Notifications
You must be signed in to change notification settings - Fork 3
/
vid2img_sthv2.py
executable file
·50 lines (37 loc) · 1.62 KB
/
vid2img_sthv2.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
# Code for "TSM: Temporal Shift Module for Efficient Video Understanding"
# arXiv:1811.08383
# Ji Lin*, Chuang Gan, Song Han
# {jilin, songhan}@mit.edu, [email protected]
import os
import threading
NUM_THREADS = 100
VIDEO_ROOT = '/ssd/video/something/v2/20bn-something-something-v2' # Downloaded webm videos
FRAME_ROOT = '/ssd/video/something/v2/20bn-something-something-v2-frames' # Directory for extracted frames
def split(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
def extract(video, tmpl='%06d.jpg'):
# os.system(f'ffmpeg -i {VIDEO_ROOT}/{video} -vf -threads 1 -vf scale=-1:256 -q:v 0 '
# f'{FRAME_ROOT}/{video[:-5]}/{tmpl}')
cmd = 'ffmpeg -i \"{}/{}\" -threads 1 -vf scale=-1:256 -q:v 0 \"{}/{}/%06d.jpg\"'.format(VIDEO_ROOT, video,
FRAME_ROOT, video[:-5])
os.system(cmd)
def target(video_list):
for video in video_list:
os.makedirs(os.path.join(FRAME_ROOT, video[:-5]))
extract(video)
if __name__ == '__main__':
if not os.path.exists(VIDEO_ROOT):
raise ValueError('Please download videos and set VIDEO_ROOT variable.')
if not os.path.exists(FRAME_ROOT):
os.makedirs(FRAME_ROOT)
video_list = os.listdir(VIDEO_ROOT)
splits = list(split(video_list, NUM_THREADS))
threads = []
for i, split in enumerate(splits):
thread = threading.Thread(target=target, args=(split,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()