@banou/media-player 0.10.22 → 0.10.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/engine/index.js +1 -1
- package/dist/{engine-Cr_7OImR.js → engine-BdouHadr.js} +6 -6
- package/dist/index.js +287 -236
- package/dist/react/hooks/use-frame-step.d.ts +14 -0
- package/dist/react/player.d.ts +1 -0
- package/dist/react/source-feature.d.ts +4 -0
- package/package.json +3 -3
- package/src/lib/engine/playback.ts +16 -7
- package/src/lib/engine/subtitles.ts +11 -7
- package/src/lib/react/components/control-bar.tsx +6 -1
- package/src/lib/react/hooks/use-frame-step.ts +109 -0
- package/src/lib/react/source-feature.ts +3 -0
- package/src/lib/react/video-player.tsx +2 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PlayerMedia } from '../media';
|
|
2
|
+
/**
|
|
3
|
+
* Offers `stepFrame` for a media whose frames this document can see, one with `requestVideoFrameCallback`.
|
|
4
|
+
*
|
|
5
|
+
* Paused by a seek, the element shows the frame holding `currentTime` (Chrome 155 and Firefox 157), so a step is
|
|
6
|
+
* `currentTime` plus or minus a frame. Paused from playing, Chrome shows the frame after that one in 15 of 40 pauses,
|
|
7
|
+
* so the step starts from the last frame presented while playing and aims at the middle of the next one. A frame a
|
|
8
|
+
* seek presented is never read for its time: Firefox reports the seek's target as its `mediaTime`.
|
|
9
|
+
*
|
|
10
|
+
* The frame's length is learned from the gaps between frames presented one after another while playing at up to 1x:
|
|
11
|
+
* faster, a 60 Hz display presents every other frame of a 60 fps file. A step while a seek waits for its data starts
|
|
12
|
+
* from that seek's target.
|
|
13
|
+
*/
|
|
14
|
+
export declare const useFrameStep: (media: PlayerMedia | null) => void;
|
package/dist/react/player.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export declare const Player: import("@videojs/react").CreatePlayerResult<import(
|
|
|
25
25
|
burnedInSubtitles: boolean;
|
|
26
26
|
volumeNormalizer: import("./source-feature").VolumeNormalizerControl | null;
|
|
27
27
|
requestSeek?: (time: number) => void;
|
|
28
|
+
stepFrame?: (direction: 1 | -1) => void;
|
|
28
29
|
playbackError: unknown;
|
|
29
30
|
playbackErrors: import("./source-feature").PlaybackErrorEntry[];
|
|
30
31
|
ready: boolean;
|
|
@@ -218,6 +218,8 @@ export type SourceState = {
|
|
|
218
218
|
* caller should seek directly.
|
|
219
219
|
*/
|
|
220
220
|
requestSeek?: (time: number) => void;
|
|
221
|
+
/** One frame back or forward, pausing first. Undefined for a media whose frames this document cannot see. */
|
|
222
|
+
stepFrame?: (direction: 1 | -1) => void;
|
|
221
223
|
/** Set when the pipeline fails. Cleared when it recovers. */
|
|
222
224
|
playbackError: unknown;
|
|
223
225
|
/**
|
|
@@ -365,6 +367,8 @@ export declare const sourceFeature: import("@videojs/react").PlayerFeature<{
|
|
|
365
367
|
* caller should seek directly.
|
|
366
368
|
*/
|
|
367
369
|
requestSeek?: (time: number) => void;
|
|
370
|
+
/** One frame back or forward, pausing first. Undefined for a media whose frames this document cannot see. */
|
|
371
|
+
stepFrame?: (direction: 1 | -1) => void;
|
|
368
372
|
/** Set when the pipeline fails. Cleared when it recovers. */
|
|
369
373
|
playbackError: unknown;
|
|
370
374
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@banou/media-player",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.24",
|
|
4
4
|
"description": "A video player for containers and codecs the browser cannot play natively, remuxed on the fly",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"@videojs/core": "10.0.0-beta.26",
|
|
44
44
|
"@videojs/react": "10.0.0-beta.26",
|
|
45
45
|
"ass-compiler": "^0.1.16",
|
|
46
|
-
"jassub": "^2.5.
|
|
47
|
-
"libav-wasm": "^0.8.
|
|
46
|
+
"jassub": "^2.5.23",
|
|
47
|
+
"libav-wasm": "^0.8.12",
|
|
48
48
|
"lucide-react": "^0.536.0",
|
|
49
49
|
"osra": "^0.6.12",
|
|
50
50
|
"react-tooltip": "^5.28.0"
|
|
@@ -499,14 +499,18 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
499
499
|
* BOTH the input and the output context and re-runs write_header, because the fragmented mp4 muxer
|
|
500
500
|
* cannot rewind. Skipping it when the target is already playable is most of what a seek costs.
|
|
501
501
|
*
|
|
502
|
-
* Buffered alone is not enough:
|
|
503
|
-
* somewhere ahead
|
|
504
|
-
* would stall at the end of this range instead.
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
*
|
|
502
|
+
* Buffered alone is not enough: the range holding the target has to reach the last remuxer position,
|
|
503
|
+
* since the reader is somewhere ahead of it and a gap between them can never be filled without a seek,
|
|
504
|
+
* so playback would stall at the end of this range instead. A target behind that position is just as
|
|
505
|
+
* safe once its range reaches it: seeking there anyway re-read the target's whole GOP over buffered
|
|
506
|
+
* data, and a quick seek forward during that re-read showed a frame from 15 s away in Chrome 155
|
|
507
|
+
* (measured 2026-10-23).
|
|
508
|
+
* Nor is reaching it enough: the range can be an island ahead of the reader, so the target also needs
|
|
509
|
+
* its runway, or the element plays to the island's end and waits there until the reader has read the
|
|
510
|
+
* whole gap, which over a torrent is as long as downloading it.
|
|
508
511
|
*/
|
|
509
|
-
const alreadyPlayable = (time: number) =>
|
|
512
|
+
const alreadyPlayable = (time: number) =>
|
|
513
|
+
getTimeRanges(sourceBuffer).some((r) => r.start <= time && time < r.end && r.end >= lastSeekPosition) && hasRunway(time)
|
|
510
514
|
|
|
511
515
|
/**
|
|
512
516
|
* Whether the element can play from a GIVEN time without a fetch.
|
|
@@ -558,6 +562,11 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
|
|
|
558
562
|
const prepareSeek = async (time: number) => {
|
|
559
563
|
if (destroyed) return
|
|
560
564
|
if (playableAt(time) && hasRunway(time)) return
|
|
565
|
+
// A drag still settling holds every read, so the runway would never be read, and its settle would then position the
|
|
566
|
+
// remuxer back where the drag stopped. A seek back under 250 ms after a seek forward is one (Firefox 153 and 157,
|
|
567
|
+
// 2026-10-23).
|
|
568
|
+
if (dragTimer) clearTimeout(dragTimer)
|
|
569
|
+
dragTimer = undefined
|
|
561
570
|
finished = false
|
|
562
571
|
preparing++
|
|
563
572
|
prepareTarget = time
|
|
@@ -10,7 +10,7 @@ export type SubtitleStream = { streamIndex: number, title: string, language: str
|
|
|
10
10
|
export const SUBTITLES_OFF = -1
|
|
11
11
|
|
|
12
12
|
type SubtitleHeaderPart = { type: 'header', streamIndex: number, content: string, eventsContent: string, styles: Map<string, number> }
|
|
13
|
-
type SubtitleDialoguePart = { type: 'dialogue', streamIndex: number,
|
|
13
|
+
type SubtitleDialoguePart = { type: 'dialogue', streamIndex: number, assEvent: ASSEvent }
|
|
14
14
|
|
|
15
15
|
export type SubtitleRendererOptions = {
|
|
16
16
|
video: HTMLVideoElement
|
|
@@ -125,7 +125,6 @@ const toDialoguePart = (header: SubtitleHeaderPart, fragment: SubtitleFragment &
|
|
|
125
125
|
return {
|
|
126
126
|
type: 'dialogue',
|
|
127
127
|
streamIndex: fragment.streamIndex,
|
|
128
|
-
index: dialogueIndex,
|
|
129
128
|
assEvent: {
|
|
130
129
|
Name: event.Name ?? '',
|
|
131
130
|
// libass wants the raw effect field and ass-compiler has already destructured it into an
|
|
@@ -158,7 +157,7 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
|
|
|
158
157
|
let attachments: [string, Uint8Array][] = []
|
|
159
158
|
const headers = new Map<number, SubtitleHeaderPart>()
|
|
160
159
|
const streams: SubtitleStream[] = []
|
|
161
|
-
const dialogues = new Map<number, Map<
|
|
160
|
+
const dialogues = new Map<number, Map<string, SubtitleDialoguePart>>()
|
|
162
161
|
let selected: number | undefined
|
|
163
162
|
let onStreams = options.onStreams
|
|
164
163
|
/**
|
|
@@ -333,11 +332,16 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
|
|
|
333
332
|
} else {
|
|
334
333
|
const header = headers.get(fragment.streamIndex)
|
|
335
334
|
if (!header) continue
|
|
336
|
-
let
|
|
337
|
-
if (!
|
|
335
|
+
let byLine = dialogues.get(fragment.streamIndex)
|
|
336
|
+
if (!byLine) { byLine = new Map(); dialogues.set(fragment.streamIndex, byLine) }
|
|
337
|
+
// Everything but the leading ReadOrder: a line libav converts from SubRip, WebVTT or mov_text
|
|
338
|
+
// takes it from a counter in FFmpeg's decoder, so the same cue read again after a seek comes
|
|
339
|
+
// back under a new one. The cost: an ASS line repeating another in every other field draws
|
|
340
|
+
// once, where libass draws both.
|
|
341
|
+
const key = `${fragment.start},${fragment.end},${fragment.content.slice(fragment.content.indexOf(',') + 1)}`
|
|
342
|
+
if (byLine.has(key)) continue
|
|
338
343
|
const part = toDialoguePart(header, fragment)
|
|
339
|
-
|
|
340
|
-
byIndex.set(part.index, part)
|
|
344
|
+
byLine.set(key, part)
|
|
341
345
|
if (selected === fragment.streamIndex) incoming.push(part.assEvent)
|
|
342
346
|
}
|
|
343
347
|
}
|
|
@@ -178,6 +178,7 @@ export const ControlBar = (
|
|
|
178
178
|
// Keyboard seeks go through the same door as the seek bar: data first, then the playhead. See
|
|
179
179
|
// `requestSeek` on the source state for why an element that seeks into a hole is the problem.
|
|
180
180
|
const requestSeek = usePlayer((state) => state.requestSeek)
|
|
181
|
+
const stepFrame = usePlayer((state) => state.stepFrame)
|
|
181
182
|
const [volumeElement, setVolumeElement] = useState<HTMLButtonElement | null>(null)
|
|
182
183
|
const passThroughButton = useRef<HTMLButtonElement>(null)
|
|
183
184
|
const refocus = useRef<ReturnType<typeof setInterval>>(undefined)
|
|
@@ -215,6 +216,9 @@ export const ControlBar = (
|
|
|
215
216
|
// on the window, so typing into a consumer's own input opts out
|
|
216
217
|
const target = targetOf(ev)
|
|
217
218
|
if (target?.isContentEditable || /^(input|textarea|select)$/i.test(target?.tagName ?? '')) return
|
|
219
|
+
// Ctrl+F, Alt+Left and the like are the browser's, but Windows reports AltGr, which types < and > on some
|
|
220
|
+
// layouts, as Ctrl and Alt
|
|
221
|
+
if ((ev.ctrlKey || ev.metaKey || ev.altKey) && !ev.getModifierState('AltGraph')) return
|
|
218
222
|
const fromPlayer = root.current?.contains(target ?? null) || (target === document.body && engaged.current)
|
|
219
223
|
if (shortcuts === 'player' && !player.fullscreen && !fromPlayer) return
|
|
220
224
|
|
|
@@ -237,6 +241,7 @@ export const ControlBar = (
|
|
|
237
241
|
else if (ev.key === 'ArrowLeft') {
|
|
238
242
|
seek(Math.max(player.currentTime - SEEK_STEP, 0))
|
|
239
243
|
}
|
|
244
|
+
else if ((ev.key === '<' || ev.key === '>') && stepFrame) stepFrame(ev.key === '>' ? 1 : -1)
|
|
240
245
|
else {
|
|
241
246
|
shouldPreventDefault = false
|
|
242
247
|
}
|
|
@@ -253,7 +258,7 @@ export const ControlBar = (
|
|
|
253
258
|
window.removeEventListener('pointerdown', engage, true)
|
|
254
259
|
window.removeEventListener('focusin', engage, true)
|
|
255
260
|
}
|
|
256
|
-
}, [player, modifyVolume, requestSeek, shortcuts, root])
|
|
261
|
+
}, [player, modifyVolume, requestSeek, stepFrame, shortcuts, root])
|
|
257
262
|
|
|
258
263
|
const handleWheel = useCallback((e: WheelEvent) => {
|
|
259
264
|
e.preventDefault()
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { PlayerMedia } from '../media'
|
|
2
|
+
|
|
3
|
+
import { useEffect } from 'react'
|
|
4
|
+
|
|
5
|
+
import { usePlayer } from '../player'
|
|
6
|
+
|
|
7
|
+
/** Film's frame, until the media has been seen playing. */
|
|
8
|
+
const FALLBACK_FRAME = 1001 / 24000
|
|
9
|
+
|
|
10
|
+
const presented = (video: HTMLVideoElement, timeout: number) => new Promise<void>((done) => {
|
|
11
|
+
const timer = setTimeout(done, timeout)
|
|
12
|
+
video.requestVideoFrameCallback(() => { clearTimeout(timer); done() })
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Offers `stepFrame` for a media whose frames this document can see, one with `requestVideoFrameCallback`.
|
|
17
|
+
*
|
|
18
|
+
* Paused by a seek, the element shows the frame holding `currentTime` (Chrome 155 and Firefox 157), so a step is
|
|
19
|
+
* `currentTime` plus or minus a frame. Paused from playing, Chrome shows the frame after that one in 15 of 40 pauses,
|
|
20
|
+
* so the step starts from the last frame presented while playing and aims at the middle of the next one. A frame a
|
|
21
|
+
* seek presented is never read for its time: Firefox reports the seek's target as its `mediaTime`.
|
|
22
|
+
*
|
|
23
|
+
* The frame's length is learned from the gaps between frames presented one after another while playing at up to 1x:
|
|
24
|
+
* faster, a 60 Hz display presents every other frame of a 60 fps file. A step while a seek waits for its data starts
|
|
25
|
+
* from that seek's target.
|
|
26
|
+
*/
|
|
27
|
+
export const useFrameStep = (media: PlayerMedia | null) => {
|
|
28
|
+
const player = usePlayer()
|
|
29
|
+
const setSourceState = usePlayer((state) => state.setSourceState)
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const video = media as HTMLVideoElement | null
|
|
32
|
+
if (!video || typeof video.requestVideoFrameCallback !== 'function') return
|
|
33
|
+
|
|
34
|
+
let played: VideoFrameCallbackMetadata | undefined
|
|
35
|
+
let bySeek = false
|
|
36
|
+
const gaps: number[] = []
|
|
37
|
+
const onSeeking = () => { bySeek = true; played = undefined }
|
|
38
|
+
// a new media on the same element
|
|
39
|
+
const onEmptied = () => { played = undefined; gaps.length = 0 }
|
|
40
|
+
video.addEventListener('seeking', onSeeking)
|
|
41
|
+
video.addEventListener('emptied', onEmptied)
|
|
42
|
+
let handle = 0
|
|
43
|
+
const onFrame = (_: number, frame: VideoFrameCallbackMetadata) => {
|
|
44
|
+
const fromSeek = bySeek
|
|
45
|
+
// the first frame once a seek has landed while playing can still be the seek's own
|
|
46
|
+
if (!video.paused && !video.seeking) bySeek = false
|
|
47
|
+
if (
|
|
48
|
+
!fromSeek && !video.paused && video.playbackRate <= 1 &&
|
|
49
|
+
played && frame.presentedFrames === played.presentedFrames + 1 && frame.mediaTime > played.mediaTime
|
|
50
|
+
) {
|
|
51
|
+
gaps.push(frame.mediaTime - played.mediaTime)
|
|
52
|
+
if (gaps.length > 60) gaps.shift()
|
|
53
|
+
}
|
|
54
|
+
played = fromSeek ? undefined : frame
|
|
55
|
+
handle = video.requestVideoFrameCallback(onFrame)
|
|
56
|
+
}
|
|
57
|
+
handle = video.requestVideoFrameCallback(onFrame)
|
|
58
|
+
|
|
59
|
+
const frameLength = () => {
|
|
60
|
+
if (gaps.length < 5) return FALLBACK_FRAME
|
|
61
|
+
const sorted = [...gaps].sort((a, b) => a - b)
|
|
62
|
+
const median = sorted[sorted.length >> 1]!
|
|
63
|
+
// Matroska's millisecond timestamps give gaps of 41 and 42 ms for a 41.708 ms frame: steps by their median of 42
|
|
64
|
+
// drift by a frame within about 65, steps by the mean of the gaps near it do not
|
|
65
|
+
const near = sorted.filter((gap) => Math.abs(gap - median) <= median / 4)
|
|
66
|
+
return near.reduce((sum, gap) => sum + gap, 0) / near.length
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let busy = false
|
|
70
|
+
const stepFrame = async (direction: 1 | -1) => {
|
|
71
|
+
// a key repeat while a step is on its way is dropped, so a held key steps as fast as frames land
|
|
72
|
+
if (busy) return
|
|
73
|
+
busy = true
|
|
74
|
+
try {
|
|
75
|
+
if (!video.paused) {
|
|
76
|
+
video.pause()
|
|
77
|
+
// Chrome can still report the frame on screen after the pause
|
|
78
|
+
await presented(video, 100)
|
|
79
|
+
}
|
|
80
|
+
const length = frameLength()
|
|
81
|
+
// off `state`: the store has a getter only for the fields it was created with
|
|
82
|
+
const pending = player.state.seekingTo
|
|
83
|
+
const target = pending !== undefined
|
|
84
|
+
? pending + direction * length
|
|
85
|
+
: played
|
|
86
|
+
? played.mediaTime + (direction > 0 ? 1.5 : -0.5) * length
|
|
87
|
+
: video.currentTime + direction * length
|
|
88
|
+
const time = Math.max(0, Math.min(target, video.duration || target))
|
|
89
|
+
const landed = new Promise<void>((done) => {
|
|
90
|
+
const timer = setTimeout(done, 1000)
|
|
91
|
+
video.addEventListener('seeked', () => { clearTimeout(timer); done() }, { once: true })
|
|
92
|
+
})
|
|
93
|
+
const { requestSeek } = player.state
|
|
94
|
+
if (requestSeek) requestSeek(time)
|
|
95
|
+
else video.currentTime = time
|
|
96
|
+
await landed
|
|
97
|
+
} finally {
|
|
98
|
+
busy = false
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
setSourceState({ stepFrame })
|
|
102
|
+
return () => {
|
|
103
|
+
video.removeEventListener('seeking', onSeeking)
|
|
104
|
+
video.removeEventListener('emptied', onEmptied)
|
|
105
|
+
video.cancelVideoFrameCallback(handle)
|
|
106
|
+
setSourceState({ stepFrame: undefined })
|
|
107
|
+
}
|
|
108
|
+
}, [media, player, setSourceState])
|
|
109
|
+
}
|
|
@@ -238,6 +238,9 @@ export type SourceState = {
|
|
|
238
238
|
*/
|
|
239
239
|
requestSeek?: (time: number) => void
|
|
240
240
|
|
|
241
|
+
/** One frame back or forward, pausing first. Undefined for a media whose frames this document cannot see. */
|
|
242
|
+
stepFrame?: (direction: 1 | -1) => void
|
|
243
|
+
|
|
241
244
|
/** Set when the pipeline fails. Cleared when it recovers. */
|
|
242
245
|
playbackError: unknown
|
|
243
246
|
/**
|
|
@@ -11,6 +11,7 @@ import { useContainerAttach, useMediaAttach } from '@videojs/react'
|
|
|
11
11
|
|
|
12
12
|
import { Player, usePlayer } from './player'
|
|
13
13
|
import { usePlayback } from './hooks/use-playback'
|
|
14
|
+
import { useFrameStep } from './hooks/use-frame-step'
|
|
14
15
|
import { useSeekThumbnails } from './hooks/use-thumbnails'
|
|
15
16
|
import { usePictureInPicture } from './hooks/use-picture-in-picture'
|
|
16
17
|
import { usePassThroughPictureInPicture } from './hooks/use-pass-through-picture-in-picture'
|
|
@@ -273,6 +274,7 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
|
|
|
273
274
|
// permanent no-op.
|
|
274
275
|
const media = remote?.media ?? video
|
|
275
276
|
useEffect(() => { setMedia?.(media); return () => setMedia?.(null) }, [media, setMedia])
|
|
277
|
+
useFrameStep(media)
|
|
276
278
|
|
|
277
279
|
// Served after it is attached, and re-served when the media changes: what an embedder drives is
|
|
278
280
|
// whatever this player drives at the time. Keyed on the option's `origin` and `channel`, not on the
|