File size: 2,574 Bytes
bc18ad5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export const frameToTimeString = (
  { frame }: { frame: number },
  { fps }: { fps: number }
): string => {
  // Calculate the total time in seconds
  const totalSeconds = frame / fps;

  // Calculate hours, minutes, seconds, and milliseconds
  const hours = Math.floor(totalSeconds / 3600);
  const remainingSeconds = totalSeconds % 3600;
  const minutes = Math.floor(remainingSeconds / 60);
  const seconds = Math.floor(remainingSeconds % 60);

  // Format the time string based on whether hours are zero or not
  if (hours > 0) {
    return `${hours}:${minutes.toString().padStart(2, "0")}:${seconds
      .toString()
      .padStart(2, "0")}`;
  }
  return `${minutes.toString().padStart(2, "0")}:${seconds
    .toString()
    .padStart(2, "0")}`;
};

export const timeToString = ({ time }: { time: number }): string => {
  // Calculate the total time in seconds
  const totalSeconds = time / 1000;

  // Calculate hours, minutes, seconds, and milliseconds
  const hours = Math.floor(totalSeconds / 3600);
  const remainingSeconds = totalSeconds % 3600;
  const minutes = Math.floor(remainingSeconds / 60);
  const seconds = Math.floor(remainingSeconds % 60);

  // Format the time string based on whether hours are zero or not
  if (hours > 0) {
    return `${hours}:${minutes.toString().padStart(2, "0")}:${seconds
      .toString()
      .padStart(2, "0")}`;
  }
  return `${minutes.toString().padStart(2, "0")}:${seconds
    .toString()
    .padStart(2, "0")}`;
};

export const getCurrentTime = () => {
  const currentTimeElement = document.getElementById("video-current-time");
  const currentTimeSeconds = currentTimeElement
    ? Number.parseFloat(
        currentTimeElement.getAttribute("data-current-time") ?? "0"
      )
    : 0;
  const currentTimeMiliseconds = currentTimeSeconds * 1000;
  return currentTimeMiliseconds;
};

/**
 * Safely gets the current frame from a player reference
 * @param playerRef - The player reference
 * @returns The current frame as a finite number, or 0 if invalid
 */
export const getSafeCurrentFrame = (playerRef: any): number => {
  try {
    if (!playerRef?.current) {
      return 0;
    }

    const frame = playerRef.current.getCurrentFrame();

    // Check if frame is a valid finite number
    if (typeof frame !== "number" || !Number.isFinite(frame)) {
      console.warn("getCurrentFrame returned non-finite value:", frame);
      return 0;
    }

    // Ensure frame is non-negative
    return Math.max(0, frame);
  } catch (error) {
    console.error("Error getting current frame:", error);
    return 0;
  }
};