-
Notifications
You must be signed in to change notification settings - Fork 4
/
record_videos.py
93 lines (74 loc) · 2.84 KB
/
record_videos.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
import ffmpy
from threading import Thread
from time import sleep, time
import argparse
import json
import os
import subprocess
from action_recognition.analysis import Labelling
def main(args):
record = True
i = 0
while record:
val = input("(n)ew video, (d)one\n")
if val == 'n':
out_file_video = "{}-{}.mp4".format(args.video_name, i)
out_file_timestamps = "{}-{}-timestamps.json".format(args.video_name, i)
ff, start_time = start_recording(out_file_video, args.video_path, args.video_size)
# Sleep in order to allow the program some time to start the recording.
sleep(2)
timestamps = label_recording(start_time)
ff.process.terminate()
i += 1
sleep(2)
with open(out_file_timestamps, 'w') as f:
json.dump(timestamps, f)
elif val == 'd':
record = False
def label_recording(video_start_time):
timestamps = []
labelling = True
while labelling:
keypress = input('Label? ' + Labelling().valid_actions_string())
if Labelling().keypress_valid(keypress):
start_time = time() - video_start_time
input('Press enter to stop')
end_time = time() - video_start_time
label = Labelling().parse_keypress_to_label(keypress)
timestamp = {
'start_time': start_time,
'end_time': end_time,
'label': label
}
timestamps.append(timestamp)
elif keypress == 'q':
labelling = False
return timestamps
def start_recording(out_file, video_path, video_size):
ff = ffmpy.FFmpeg(
global_options=['-video_size {}'.format(video_size), '-loglevel panic'],
inputs={video_path: None},
outputs={out_file: None}
)
def _execute():
try:
ff.run()
except ffmpy.FFRuntimeError as ex:
# In case of normal exit, we terminated the process
if ex.exit_code and ex.exit_code != 255:
raise
process = Thread(target=_execute)
process.start()
start_time = time()
return ff, start_time
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Helper script recording and generating timestamps with labels during record-time.')
parser.add_argument('--video-name', type=str, help='Prefix of the outputted video files.')
parser.add_argument('--video-path', type=str, default='/dev/video1',
help='Path to the video device (e.g. /dev/video1)')
parser.add_argument('--video-size', type=str, default='960x544',
help=('Resolution of output video,'
'make sure the camera can capture the given size.'))
args = parser.parse_args()
main(args)