Skip to content

Commit 9b6f5e7

Browse files
author
arch
committed
refactoring
1 parent facf608 commit 9b6f5e7

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

funscript_editor/algorithms/funscriptgenerator.py

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,14 @@ def drawTime(self, img: np.ndarray, frame_num: int) -> np.ndarray:
154154
np.ndarray: opencv image with Time Text
155155
"""
156156
annotated_img = img.copy()
157-
current_timestamp = FFmpegStream.millisec_to_timestamp(
158-
self.frame_to_millisec(frame_num, self.video_info.fps)
159-
)
157+
current_timestamp = FFmpegStream.frame_to_timestamp(frame_num, self.video_info.fps)
160158
current_timestamp = ''.join(current_timestamp[:-4])
161159

162160
if self.params.end_frame < 1:
163-
end_timestamp = FFmpegStream.millisec_to_timestamp(
164-
self.frame_to_millisec(self.video_info.length, self.video_info.fps)
165-
)
161+
end_timestamp = FFmpegStream.frame_to_timestamp(self.video_info.length, self.video_info.fps)
166162
end_timestamp = ''.join(end_timestamp[:-4])
167163
else:
168-
end_timestamp = FFmpegStream.millisec_to_timestamp(
169-
self.frame_to_millisec(self.params.end_frame, self.video_info.fps)
170-
)
164+
end_timestamp = FFmpegStream.frame_to_timestamp(self.params.end_frame, self.video_info.fps)
171165
end_timestamp = ''.join(end_timestamp[:-4])
172166

173167
txt = current_timestamp + ' / ' + end_timestamp
@@ -295,21 +289,6 @@ def calculate_score(self) -> None:
295289
self.score['y'] = sp.scale_signal(self.score['y'], 0, 100)
296290

297291

298-
@staticmethod
299-
def frame_to_millisec(frame: int, fps: float) -> int:
300-
""" Convert frame number to timestamp in video
301-
302-
Args:
303-
frame (int): the framenumber to convert to an timestamp
304-
fps (float): Video FPS
305-
306-
Returns:
307-
int: the timestamp in milliseconds in video for the given framenumber
308-
"""
309-
if frame < 0: return 0
310-
return int(round(float(frame)*float(1000)/fps))
311-
312-
313292
def scale_score(self, status: str, direction : str = 'y') -> None:
314293
""" Scale the score to desired stroke high
315294
@@ -814,15 +793,15 @@ def run(self) -> None:
814793
min(output_score) \
815794
if output_score[idx] < min(output_score) + self.params.bottom_threshold \
816795
else round(output_score[idx]),
817-
self.frame_to_millisec(self.apply_shift(idx, 'min'), self.video_info.fps)
796+
FFmpegStream.frame_to_millisec(self.apply_shift(idx, 'min'), self.video_info.fps)
818797
)
819798

820799
for idx in idx_dict['max']:
821800
self.funscript.add_action(
822801
max(output_score) \
823802
if output_score[idx] > max(output_score) - self.params.top_threshold \
824803
else round(output_score[idx]),
825-
self.frame_to_millisec(self.apply_shift(idx, 'max'), self.video_info.fps)
804+
FFmpegStream.frame_to_millisec(self.apply_shift(idx, 'max'), self.video_info.fps)
826805
)
827806

828807
self.finished(status, True)

funscript_editor/data/ffmpegstream.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,35 @@ def get_projection(
162162
return projection
163163

164164

165-
def frame_to_millisec(self, frame_number) -> int:
165+
@staticmethod
166+
def frame_to_timestamp(frame_number: int, fps: float) -> str:
167+
"""Get timestamp for given frame number
168+
169+
Args:
170+
frame_number (int): frame number
171+
fps (float): frames per seconds
172+
173+
Returns:
174+
str: position in video as timestamp with H:M:S.XXX
175+
"""
176+
return FFmpegStream.millisec_to_timestamp(
177+
FFmpegStream.frame_to_millisec(frame_number, fps)
178+
)
179+
180+
181+
@staticmethod
182+
def frame_to_millisec(frame_number: int, fps: float) -> int:
166183
"""Get timestamp for given frame number
167184
168185
Args:
169186
frame_number (int): frame number
187+
fps (float): frames per seconds
170188
171189
Returns:
172-
int: timestamp in video
190+
int: timestamp in milliseconds
173191
"""
174192
if frame_number <= 0: return 0
175-
return int(round(float(frame_number)*float(1000)/self.video_info.fps))
193+
return int(round(float(frame_number)*float(1000)/fps))
176194

177195

178196
@staticmethod
@@ -244,9 +262,7 @@ def run(self) -> None:
244262
for k, v in self.config['parameter'].items():
245263
video_filter = video_filter.replace('${' + k + '}', str(v))
246264

247-
seek = self.millisec_to_timestamp(
248-
self.frame_to_millisec(self.start_frame)
249-
)
265+
seek = FFmpegStream.frame_to_timestamp(self.start_frame, self.video_info.fps)
250266

251267
command = [
252268
'ffmpeg',

0 commit comments

Comments
 (0)