11
11
12
12
from video import extract_frames
13
13
from video import rebuild_video
14
+ from video import extract_video_info
14
15
15
16
16
- THRESHOLD_NS = 10 * 1000 * 1000 # millis * micros * nanos
17
+ DEFAULT_THRESHOLD_MILLIS = 10
18
+ DEFAULT_THRESHOLD_NANOS = DEFAULT_THRESHOLD_MILLIS * 1000 * 1000 # millis * micros * nanos
17
19
18
20
19
21
def scan_session_dir (input_dir : Path ) -> Tuple [List [str ], List [pd .DataFrame ], List [str ]]:
@@ -65,14 +67,13 @@ def scan_session_dir(input_dir: Path) -> Tuple[List[str], List[pd.DataFrame], Li
65
67
#
66
68
#
67
69
#
68
- def main (input_dir : Path , output_dir : Path ):
70
+ def main (input_dir : Path , output_dir : Path , threshold_ns : int ):
69
71
70
72
print (f"Scanning dir { str (input_dir )} ..." )
71
73
clientIDs , df_list , mp4_list = scan_session_dir (input_dir )
72
74
73
75
n_clients = len (clientIDs )
74
76
75
-
76
77
#
77
78
# Print collected info
78
79
for i in range (n_clients ):
@@ -96,18 +97,21 @@ def main(input_dir: Path, output_dir: Path):
96
97
# Find time ranges
97
98
min_common , max_common = compute_time_range (repaired_df_list )
98
99
# Trim the data frames to the time range
99
- trimmed_dataframes = trim_repaired_into_interval (repaired_df_list , min_common , max_common , THRESHOLD_NS )
100
+ trimmed_dataframes = trim_repaired_into_interval (repaired_df_list , min_common , max_common , threshold_ns )
100
101
101
102
assert len (clientIDs ) == len (trimmed_dataframes ), f"Expected { len (clientIDs )} trimmed dataframes. Found f{ len (trimmed_dataframes )} "
102
103
103
104
# Check that all the resulting dataframes have the same number of rows
105
+ print ("Checking if all clients we have the same number of frames in the repaired amd trimmed tables..." )
104
106
client0ID = clientIDs [0 ]
105
107
client0size = len (trimmed_dataframes [0 ])
106
- print (f"For client { client0ID } : { client0size } frames" )
108
+ print (f"Client { client0ID } has { client0size } frames. " )
107
109
for cID , df in zip (clientIDs [1 :], trimmed_dataframes [1 :]):
108
110
dfsize = len (df )
109
111
if client0size != dfsize :
110
- raise Exception (f"For client { cID } : expecting { client0size } frames, found { dfsize } " )
112
+ raise Exception (f"For client { cID } : expecting { client0size } frames. Found { dfsize } ."
113
+ f" This might be due to an excessive phase offset during recording."
114
+ f" Try to increase the threshold." )
111
115
112
116
print ("Good. All trimmed dataframes have the same number of entries." )
113
117
@@ -125,8 +129,10 @@ def main(input_dir: Path, output_dir: Path):
125
129
extract_frames (video_file = video_file , timestamps_df = orig_df , output_dir = tmp_dir )
126
130
127
131
# Reconstruct videos
132
+ vinfo = extract_video_info (video_path = video_file )
133
+ input_fps = vinfo .fps
128
134
video_out_filepath = output_dir / (cID + ".mp4" )
129
- rebuild_video (dir = Path (tmp_dir ), frames = trimmed_df , outfile = video_out_filepath )
135
+ rebuild_video (dir = Path (tmp_dir ), frames = trimmed_df , fps = input_fps , outfile = video_out_filepath )
130
136
# And save also the CSV
131
137
csv_out_filepath = video_out_filepath .with_suffix (".csv" )
132
138
trimmed_df .to_csv (path_or_buf = csv_out_filepath , header = True , index = False )
@@ -149,16 +155,25 @@ def main(input_dir: Path, output_dir: Path):
149
155
"--outfolder" , "-o" , type = str , help = "The folder where the repaired and aligned frames will be stored." ,
150
156
required = True
151
157
)
158
+ parser .add_argument (
159
+ "--threshold" , "-t" , type = int , help = "The allowed difference in ms between corresponding frames on different videos."
160
+ " Increase this is post processing fails with trimmed tables of different sizes."
161
+ f" Default is now { DEFAULT_THRESHOLD_MILLIS } ms." ,
162
+ required = False ,
163
+ default = DEFAULT_THRESHOLD_MILLIS
164
+ )
152
165
153
166
args = parser .parse_args ()
154
167
155
168
infolder = Path (args .infolder )
156
169
outfolder = Path (args .outfolder )
170
+ threshold_millis = args .threshold
171
+ threshold_nanos = threshold_millis * 1000 * 1000
157
172
158
173
if not infolder .exists ():
159
174
raise Exception (f"Input folder '{ infolder } ' doesn't exist." )
160
175
161
176
if not outfolder .exists ():
162
177
raise Exception (f"Output folder '{ outfolder } ' doesn't exist." )
163
178
164
- main (infolder , outfolder )
179
+ main (infolder , outfolder , threshold_nanos )
0 commit comments