Skip to content

Commit c149e89

Browse files
committed
Better video FPS determination
1 parent 2522bf7 commit c149e89

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

superannotate/db/projects.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,43 +249,44 @@ def upload_video_to_project(
249249
rot
250250
)
251251
except Exception as e:
252-
logger.warning("Couldn't read video metadata. %s", e)
252+
logger.warning("Couldn't read video metadata %s", e)
253253

254254
video = cv2.VideoCapture(str(video_path), cv2.CAP_FFMPEG)
255255
if not video.isOpened():
256256
raise SABaseException(0, "Couldn't open video file " + str(video_path))
257257

258258
total_num_of_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
259259
if total_num_of_frames < 0:
260-
if target_fps is not None:
261-
logger.warning(
262-
"Number of frames indicated in the video is negative number. Disabling FPS change."
263-
)
264-
target_fps = None
265-
else:
266-
logger.info("Video frame count is %s.", total_num_of_frames)
260+
total_num_of_frames = 0
261+
flag = True
262+
while flag:
263+
flag, frame = video.read()
264+
if flag:
265+
total_num_of_frames += 1
266+
else:
267+
break
268+
video = cv2.VideoCapture(str(video_path), cv2.CAP_FFMPEG)
269+
logger.info("Video frame count is %s.", total_num_of_frames)
267270

268271
if target_fps is not None:
269-
video_fps = video.get(cv2.CAP_PROP_FPS)
272+
video_fps = float(video.get(cv2.CAP_PROP_FPS))
270273
logger.info(
271274
"Video frame rate is %s. Target frame rate is %s.", video_fps,
272275
target_fps
273276
)
274-
if target_fps > video_fps:
277+
if target_fps >= video_fps:
275278
target_fps = None
276279
else:
277280
r = video_fps / target_fps
278-
frames_count_to_drop = total_num_of_frames - (
279-
total_num_of_frames / r
280-
)
281-
percent_to_drop = frames_count_to_drop / total_num_of_frames
281+
percent_to_drop = 1.0 - 1.0 / r
282282
my_random = random.Random(122222)
283283

284284
zero_fill_count = len(str(total_num_of_frames))
285285
tempdir = tempfile.TemporaryDirectory()
286286

287287
video_name = Path(video_path).stem
288288
frame_no = 1
289+
logger.info("Extracting frames from video to %s.", tempdir.name)
289290
while True:
290291
success, frame = video.read()
291292
if not success:

superannotate/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.5.11"
1+
__version__ = "2.5.12"

0 commit comments

Comments
 (0)