-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Converts current media time and duration into Unix timestamps | ||
* @param {number} elementTime Current playback position in seconds | ||
* @param {number} elementDuration Total duration of the media in seconds | ||
* @returns {[number, number]} Array containing [startTimestamp, endTimestamp] as Unix timestamps in seconds | ||
*/ | ||
export function getTimestamps( | ||
elementTime: number, | ||
elementDuration: number, | ||
): [startTimestamp: number, endTimestamp: number] { | ||
const startTime = (Date.now() / 1000) - elementTime | ||
const endTime = startTime + elementDuration | ||
return [Math.floor(startTime), Math.floor(endTime)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { getTimestamps } from './getTimestamps.js' | ||
|
||
/** | ||
* Gets timestamps from an HTML media element (audio or video) | ||
* @param {HTMLMediaElement} media The media element to get timestamps from (works with both <audio> and <video> elements) | ||
* @returns {[number, number]} Array containing [startTimestamp, endTimestamp] as Unix timestamps in seconds | ||
*/ | ||
export function getTimestampsFromMedia( | ||
media: HTMLMediaElement, | ||
): [startTimestamp: number, endTimestamp: number] { | ||
//* Return early if media is not loaded or has no duration | ||
if (media.readyState === 0 || !Number.isFinite(media.duration)) | ||
return [0, 0] | ||
|
||
return getTimestamps(media.currentTime, media.duration) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Converts a time string into seconds | ||
* @param {string} timestamp Time in format HH:MM:SS, MM:SS, or SS | ||
* @returns {number} Time in seconds, or 0 if format is invalid | ||
*/ | ||
export function timestampFromFormat(timestamp: string): number { | ||
//* Handle invalid input | ||
if (!timestamp || typeof timestamp !== 'string') | ||
return 0 | ||
|
||
//* Check if format matches expected pattern | ||
if (!/^\d+(?::\d{1,2}(?::\d{1,2})?)?$/.test(timestamp)) | ||
return 0 | ||
|
||
//* Split the timestamp into parts and pad from the left with zeros | ||
const parts = timestamp.split(':').map(str => Number.parseInt(str, 10)) | ||
while (parts.length < 3) parts.unshift(0) | ||
|
||
const [hours, minutes, seconds] = parts as [number, number, number] | ||
|
||
//* Validate ranges | ||
if (minutes >= 60 || seconds >= 60) | ||
return 0 | ||
|
||
//* Convert everything to seconds | ||
return (hours * 3600) + (minutes * 60) + seconds | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters