-
Notifications
You must be signed in to change notification settings - Fork 0
/
concatenate.py
86 lines (67 loc) · 2.75 KB
/
concatenate.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
from pathlib import Path
import cv2
import argparse
import os
PATH = Path().resolve()
def list_timelapses(args):
'''
Summary:
Lists all daily_timelapse.mp4 files in the timelapse_id directory
Inputs:
ArgumentParser args : args.path and args.timelapse_id required
Outputs:
list tl_list_sorted : list of exact paths to the daily timelapse files
'''
path = args.path + '/' + args.timelapse_id
tl_list = Path(path).rglob('daily_timelapse.mp4')
tl_list_sorted = sorted(tl_list, key=os.path.getmtime)
return tl_list_sorted
def concatenate_timelapses(args, tl_paths):
'''
Summary:
Concatenates all daily timelapses into one long timelapse from a single timelapse ID
Inputs:
ArgumentParser args : args.timelapse_id and args.fps required
tl_paths : Absolute paths to the timelapses, generated by 'list_timelapses'
Outputs:
None. Saves complete timelapse to disk.
'''
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
out = cv2.VideoWriter(str(PATH) + '/' + args.timelapse_id + '/' + args.timelapse_id + '_timelapse.mp4', fourcc, args.fps, (1280, 720))
for tl_path in tl_paths:
# VideoCapture can't load a file with backslashes so we make sure foreward slashes are used.
tl_path_fixed = str(tl_path).replace(os.sep, '/')
print(tl_path_fixed)
curr_tl = cv2.VideoCapture(tl_path_fixed)
while curr_tl.isOpened():
s, img = curr_tl.read()
if s:
out.write(img)
else:
break
out.release()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--timelapse_id',
type=str,
help='ID that was used to create a base directory to store images. Check your base dir for the generated ID.',
required=True)
parser.add_argument('--path',
type=str,
default=str(PATH),
help='Base path where all directories were created. Default is current directory. Supply the path if it was used in ')
parser.add_argument('--start_date',
type=str,
help='Date to start the concatenated timelapse.')
parser.add_argument('--end_date',
type=str,
help='Date to end the concatenated timelapse.')
parser.add_argument('--fps',
type=int,
default=30,
help='Frames per second for the timelapse. 24 or 30 is recommended. Default is 30.')
args = parser.parse_args()
tl_paths = list_timelapses(args)
concatenate_timelapses(args, tl_paths)
if __name__ == '__main__':
main()