forked from Sundrops/video-caption.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocess_frames.py
84 lines (68 loc) · 2.27 KB
/
process_frames.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
"""
Re-tooled version of the script found on VideoToTextDNN:
https://github.com/OSUPCVLab/VideoToTextDNN/blob/master/data/process_frames.py
"""
import sys
import os
import argparse
import time
from multiprocessing import Pool
def main(args):
src_dir = args.src_dir
dst_dir = args.dst_dir
start = int(args.start)
end = int(args.end)
PREPEND = args.prepend
src_files = os.listdir(src_dir)
if not os.path.isdir(dst_dir):
os.mkdir(dst_dir)
tuple_list = []
for video_file in src_files[start:end]:
src_path = os.path.join(src_dir, video_file)
dst_path = os.path.join(dst_dir, video_file)
tuple_list.append((PREPEND, video_file, src_path, dst_path))
pool = Pool() # Default to number cores
pool.map(process_vid, tuple_list)
pool.close()
pool.join()
def process_vid(args):
(PREPEND, video_file, src_path, dst_path) = args
if not os.path.isdir(dst_path):
os.mkdir(dst_path)
# command = 'ffmpeg -i '+ src_path+' -s 256x256 '+ dst_path + '/%5d.jpg' #with resize
command = PREPEND + 'ffmpeg -i '+ src_path+' -r 20 '+ dst_path + '/%6d.jpg > /dev/null 2>&1' #6 is to be in accordance with C3D features.
print(command)
os.system(command)
else:
print("Frames directory already found at {}".format(dst_path))
if __name__=='__main__':
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
'src_dir',
help='directory where videos are'
)
arg_parser.add_argument(
'dst_dir',
help='directory where to store frames'
)
arg_parser.add_argument(
'start',
help='start index (inclusive)'
)
arg_parser.add_argument(
'end',
help='end index (noninclusive)'
)
arg_parser.add_argument(
'--prepend',
default='',
help='optional prepend to start of ffmpeg command (in case you want to use a non-system wide version of ffmpeg)'
'For example: --prepend ~/anaconda2/bin/ will use ffmpeg installed in anaconda2'
)
if not len(sys.argv) > 1:
print(arg_parser.print_help())
sys.exit(0)
args = arg_parser.parse_args()
start_time = time.time()
main(args)
print("Job took %s mins" % ((time.time() - start_time)/60))