-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_frames.py
More file actions
47 lines (35 loc) · 1.25 KB
/
extract_frames.py
File metadata and controls
47 lines (35 loc) · 1.25 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
import os
import threading
NUM_THREADS = 30
# VIDEO_ROOT = r'data\ucf5\videos\train' # Directory for videos
VIDEO_ROOT = r'D:\vcdb_split\test' # Directory for videos
FRAME_ROOT = r'data\vcdb\test' # 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='%05d.jpg'):
'''
video[-4]: to get the name of video without .avi
'''
os.system(f'ffmpeg -i {VIDEO_ROOT}/{video} -vf scale=256:256 ' f'{FRAME_ROOT}/{video[:-4]}/{tmpl}')
def target(video_list):
# video[:-4] means substract .avi
for video in video_list:
video_path = os.path.join(FRAME_ROOT, video[:-4])
if not os.path.exists(video_path):
os.makedirs(video_path)
extract(video)
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()