From 8def6e2d6cf955f8b3267cae92451c77a376773d Mon Sep 17 00:00:00 2001 From: Samuel Newman <10959775+mozzius@users.noreply.github.com> Date: Tue, 6 Aug 2024 13:54:35 +0100 Subject: [PATCH] try different hacky approach --- .../util/post-embeds/VideoEmbedInner.web.tsx | 122 ++++++------------ 1 file changed, 41 insertions(+), 81 deletions(-) diff --git a/src/view/com/util/post-embeds/VideoEmbedInner.web.tsx b/src/view/com/util/post-embeds/VideoEmbedInner.web.tsx index 851fd9ca4b..bdc72e917e 100644 --- a/src/view/com/util/post-embeds/VideoEmbedInner.web.tsx +++ b/src/view/com/util/post-embeds/VideoEmbedInner.web.tsx @@ -1,6 +1,6 @@ import React, {useEffect, useRef, useState} from 'react' import {View} from 'react-native' -import Hls, {CuesInterface} from 'hls.js' +import Hls from 'hls.js' import {atoms as a} from '#/alf' import {Controls} from './VideoWebControls' @@ -27,7 +27,11 @@ export function VideoEmbedInner({ if (!ref.current) return if (!Hls.isSupported()) throw new HLSUnsupportedError() - const hls = new Hls({capLevelToPlayerSize: true, cueHandler}) + const hls = new Hls({ + // unsure whether to enable this -sfn + // capLevelToPlayerSize: true, + startLevel: 0, + }) hlsRef.current = hls hls.attachMedia(ref.current) @@ -49,6 +53,41 @@ export function VideoEmbedInner({ } }, [source]) + // HACK: modify cues to move them up a bit -sfn + useEffect(() => { + if (!ref.current) return + function moveCues() { + if (!ref.current) return + try { + for (let i = 0; i < ref.current.textTracks.length; i++) { + const track = ref.current.textTracks[i] + console.log(track) + if (!track.cues) continue + for (let j = 0; j < track.cues.length; j++) { + const cue = track.cues[j] + console.log(cue) + if (cue instanceof VTTCue) { + if (cue.line === 'auto') { + cue.line = -2 + } + } + } + } + } catch (err) { + console.log(err) + } + } + + moveCues() + + const current = ref.current + current.textTracks.addEventListener('change', moveCues) + + return () => { + current.textTracks.removeEventListener('change', moveCues) + } + }, []) + return ( { - let row - let cue - let indenting - let indent - let text - let VTTCue = window.VTTCue || window.TextTrackCue - - const cues = [] - - for (let r = 0; r < captionScreen.rows.length; r++) { - row = captionScreen.rows[r] - indenting = true - indent = 0 - text = '' - - if (!row.isEmpty()) { - for (let c = 0; c < row.chars.length; c++) { - if (row.chars[c].uchar.match(/\s/) && indenting) { - indent++ - } else { - text += row.chars[c].uchar - indenting = false - } - } - // To be used for cleaning-up orphaned roll-up captions - row.cueStartTime = startTime - - // Give a slight bump to the endTime if it's equal to startTime to avoid a SyntaxError in IE - if (startTime === endTime) { - endTime += 0.0001 - } - - cue = new VTTCue(startTime, endTime, fixLineBreaks(text.trim())) - - if (indent >= 16) { - indent-- - } else { - indent++ - } - - // VTTCue.line get's flakey when using controls, so let's now include line 13&14 - // also, drop line 1 since it's to close to the top - if (navigator.userAgent.match(/Firefox\//)) { - cue.line = -2 - } else { - cue.line = r > 7 ? r - 2 : r + 1 - } - - cue.align = 'left' - // Clamp the position between 0 and 100 - if out of these bounds, Firefox throws an exception and captions break - cue.position = Math.max( - 0, - Math.min( - 100, - 100 * (indent / 32) + - (navigator.userAgent.match(/Firefox\//) ? 50 : 0), - ), - ) - // modified - they had a type error - cues.push(cue) - if (track) { - track.addCue(cue) - } - } - } - - return cues - }, -} - -function fixLineBreaks(input: string) { - return input.replace(//gi, '\n') -}