🎥 Make videos programmatically with React
implement player in / out range
Jonny Burger0b3c69a0b050
diff --git a/packages/player-example/src/App.tsx b/packages/player-example/src/App.tsxindex a27972ad87..ca6c25f3c4 100644--- a/packages/player-example/src/App.tsx+++ b/packages/player-example/src/App.tsx@@ -56,6 +56,11 @@ const ControlsOnly: React.FC<{setShowPosterWhenEnded: React.Dispatch<React.SetStateAction<boolean>>;showPosterWhenPaused: boolean;setShowPosterWhenPaused: React.Dispatch<React.SetStateAction<boolean>>;+ inFrame: number | null;+ setInFrame: React.Dispatch<React.SetStateAction<number | null>>;+ outFrame: number | null;+ setOutFrame: React.Dispatch<React.SetStateAction<number | null>>;+ durationInFrames: number;}> = ({playerRef: ref,color,@@ -81,6 +86,11 @@ const ControlsOnly: React.FC<{showPosterWhenUnplayed,showPosterWhenEnded,showPosterWhenPaused,+ inFrame,+ outFrame,+ setInFrame,+ setOutFrame,+ durationInFrames,}) => {const [logs, setLogs] = useState<string[]>(() => []);@@ -167,7 +177,6 @@ const ControlsOnly: React.FC<{}}/></div>-<div style={{paddingTop: '0.5rem'}}><div>Select Text Color{' '}@@ -186,7 +195,6 @@ const ControlsOnly: React.FC<{/></div></div>-<br /><button type="button" onClick={(e) => ref.current?.play(e)}>▶️ Play@@ -211,7 +219,6 @@ const ControlsOnly: React.FC<{>5 seconds forward</button>-<br /><buttontype="button"@@ -238,7 +245,6 @@ const ControlsOnly: React.FC<{>pause and seek</button>-<br /><buttontype="button"@@ -272,7 +278,6 @@ const ControlsOnly: React.FC<{>-1x speed</button>-<br /><button type="button" onClick={() => ref.current?.mute()}>🔇 Mute@@ -290,7 +295,6 @@ const ControlsOnly: React.FC<{set volume to 1</button><br />-<button type="button" onClick={() => setLoop((l) => !l)}>loop = {String(loop)}</button>@@ -320,7 +324,6 @@ const ControlsOnly: React.FC<{showPosterWhenPaused = {String(showPosterWhenPaused)}</button><br />-<buttontype="button"onClick={() => setMoveToBeginningWhenEnded((l) => !l)}@@ -338,7 +341,6 @@ const ControlsOnly: React.FC<{>log currentFrame</button>-<buttontype="button"onClick={() =>@@ -363,7 +365,48 @@ const ControlsOnly: React.FC<{>trigger error</button>-+ <br />+ <label>+ <input+ type="checkbox"+ onChange={(e) => {+ setInFrame(e.target.checked ? 0 : null);+ }}+ />+ Enable inFrame+ </label>{' '}+ {inFrame === null ? null : (+ <input+ type="range"+ min={0}+ max={durationInFrames}+ step={1}+ onChange={(e) => {+ setInFrame(Number(e.target.value));+ }}+ />+ )}+ <br />+ <label>+ <input+ type="checkbox"+ onChange={(e) => {+ setOutFrame(e.target.checked ? 0 : null);+ }}+ />+ Enable outFrame+ </label>{' '}+ {outFrame === null ? null : (+ <input+ type="range"+ min={0}+ max={durationInFrames}+ step={1}+ onChange={(e) => {+ setOutFrame(Number(e.target.value));+ }}+ />+ )}<br /><br />{logs@@ -392,6 +435,8 @@ const PlayerOnly: React.FC<showPosterWhenPaused: boolean;showPosterWhenEnded: boolean;showPosterWhenUnplayed: boolean;+ inFrame: number | null;+ outFrame: number | null;} & CompProps<any>> = ({playerRef,@@ -406,6 +451,8 @@ const PlayerOnly: React.FC<showPosterWhenPaused,showPosterWhenEnded,showPosterWhenUnplayed,+ inFrame,+ outFrame,...props}) => {const renderLoading: RenderLoading = useCallback(() => {@@ -462,6 +509,8 @@ const PlayerOnly: React.FC<showPosterWhenUnplayed={showPosterWhenUnplayed}showPosterWhenEnded={showPosterWhenEnded}showPosterWhenPaused={showPosterWhenPaused}+ inFrame={inFrame}+ outFrame={outFrame}/>);};@@ -485,6 +534,8 @@ export default ({const [showPosterWhenUnplayed, setshowPosterWhenUnplayed] = useState(true);const [showPosterWhenEnded, setShowPosterWhenEnded] = useState(true);const [showPosterWhenPaused, setShowPosterWhenPaused] = useState(true);+ const [inFrame, setInFrame] = useState<number | null>(null);+ const [outFrame, setOutFrame] = useState<number | null>(null);const ref = useRef<PlayerRef>(null);@@ -512,6 +563,8 @@ export default ({showPosterWhenEnded={showPosterWhenEnded}showPosterWhenPaused={showPosterWhenPaused}showPosterWhenUnplayed={showPosterWhenUnplayed}+ inFrame={inFrame}+ outFrame={outFrame}/><ControlsOnlybgColor={bgColor}@@ -538,6 +591,11 @@ export default ({showPosterWhenUnplayed={showPosterWhenUnplayed}showPosterWhenEnded={showPosterWhenEnded}showPosterWhenPaused={showPosterWhenPaused}+ setInFrame={setInFrame}+ setOutFrame={setOutFrame}+ inFrame={inFrame}+ outFrame={outFrame}+ durationInFrames={durationInFrames}/></div>);diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsxindex e14f377c4e..f129056592 100644--- a/packages/player/src/Player.tsx+++ b/packages/player/src/Player.tsx@@ -25,6 +25,7 @@ import {PLAYER_CSS_CLASSNAME} from './player-css-classname';import type {PlayerRef} from './player-methods';import type {RenderLoading, RenderPoster} from './PlayerUI';import PlayerUI from './PlayerUI';+import {validateInOutFrames} from './utils/validate-in-out-frame';import {validateInitialFrame} from './utils/validate-initial-frame';import {validatePlaybackRate} from './utils/validate-playbackrate';import {getPreferredVolume, persistVolume} from './volume-persistance';@@ -64,6 +65,8 @@ export type PlayerProps<T> = {showPosterWhenPaused?: boolean;showPosterWhenEnded?: boolean;showPosterWhenUnplayed?: boolean;+ inFrame?: number | null;+ outFrame?: number | null;} & PropsIfHasProps<T> &CompProps<T>;@@ -104,6 +107,8 @@ export const PlayerFn = <T,>(showPosterWhenPaused,initialFrame,renderPoster,+ inFrame,+ outFrame,...componentProps}: PlayerProps<T>,ref: MutableRefObject<PlayerRef>@@ -142,6 +147,7 @@ export const PlayerFn = <T,>(const component = Internals.useLazyComponent(componentProps);validateInitialFrame({initialFrame, durationInFrames});+const [frame, setFrame] = useState(() => initialFrame ?? 0);const [playing, setPlaying] = useState<boolean>(false);const [rootId] = useState<string>('player-comp');@@ -180,6 +186,12 @@ export const PlayerFn = <T,>();Internals.validateFps(fps, 'as a prop of the <Player/> component', false);+ validateInOutFrames({+ durationInFrames,+ inFrame,+ outFrame,+ });+if (typeof controls !== 'boolean' && typeof controls !== 'undefined') {throw new TypeError(`'controls' must be a boolean or undefined but got '${typeof controls}' instead`@@ -411,6 +423,8 @@ export const PlayerFn = <T,>(showPosterWhenEnded={Boolean(showPosterWhenEnded)}showPosterWhenPaused={Boolean(showPosterWhenPaused)}renderPoster={renderPoster}+ inFrame={inFrame ?? null}+ outFrame={outFrame ?? null}/></PlayerEventEmitterContext.Provider></Internals.SharedAudioContextProvider>diff --git a/packages/player/src/PlayerUI.tsx b/packages/player/src/PlayerUI.tsxindex 47cfc62457..9266f88ba9 100644--- a/packages/player/src/PlayerUI.tsx+++ b/packages/player/src/PlayerUI.tsx@@ -65,6 +65,8 @@ const PlayerUI: React.ForwardRefRenderFunction<showPosterWhenPaused: boolean;showPosterWhenEnded: boolean;showPosterWhenUnplayed: boolean;+ inFrame: number | null;+ outFrame: number | null;}> = ({@@ -91,6 +93,8 @@ const PlayerUI: React.ForwardRefRenderFunction<showPosterWhenUnplayed,showPosterWhenEnded,showPosterWhenPaused,+ inFrame,+ outFrame,},ref) => {@@ -112,8 +116,8 @@ const PlayerUI: React.ForwardRefRenderFunction<loop,playbackRate,moveToBeginningWhenEnded,- inFrame: null,- outFrame: null,+ inFrame,+ outFrame,});const player = usePlayer();diff --git a/packages/player/src/test/validate-in-out-frames.test.ts b/packages/player/src/test/validate-in-out-frames.test.tsnew file mode 100644index 0000000000..f85491d150--- /dev/null+++ b/packages/player/src/test/validate-in-out-frames.test.ts@@ -0,0 +1,81 @@+import {validateInOutFrames} from '../utils/validate-in-out-frame';++test('Validate in out frames', () => {+ expect(() =>+ validateInOutFrames({+ durationInFrames: 200,+ inFrame: 201,+ outFrame: undefined,+ })+ ).toThrow(/inFrame must be less than \(durationInFrames - 1\)/);++ expect(() =>+ validateInOutFrames({+ durationInFrames: 200,+ inFrame: 199,+ outFrame: 201,+ })+ ).toThrow(/outFrame must be less than \(durationInFrames - 1\)/);++ expect(() =>+ validateInOutFrames({+ durationInFrames: 200,+ inFrame: -10,+ outFrame: null,+ })+ ).toThrow(/inFrame must be greater than 0, but is -10/);++ expect(() =>+ validateInOutFrames({+ durationInFrames: 200,+ inFrame: null,+ outFrame: -10,+ })+ ).toThrow(/outFrame must be greater than 0, but is -10/);++ expect(() =>+ validateInOutFrames({+ durationInFrames: 200,+ inFrame: 1.5,+ outFrame: null,+ })+ ).toThrow(/"inFrame" must be an integer, but is 1.5/);++ expect(() =>+ validateInOutFrames({+ durationInFrames: 200,+ inFrame: 20,+ outFrame: 20,+ })+ ).toThrow(/outFrame must be greater than inFrame, but is 20/);++ expect(() =>+ validateInOutFrames({+ durationInFrames: 200,+ inFrame: 21,+ outFrame: 20,+ })+ ).toThrow(/outFrame must be greater than inFrame, but is 20 <= 21/);++ expect(() =>+ validateInOutFrames({+ durationInFrames: 200,+ inFrame: null,+ outFrame: 20,+ })+ ).not.toThrow();+ expect(() =>+ validateInOutFrames({+ durationInFrames: 200,+ inFrame: null,+ outFrame: null,+ })+ ).not.toThrow();+ expect(() =>+ validateInOutFrames({+ durationInFrames: 200,+ inFrame: 10,+ outFrame: 20,+ })+ ).not.toThrow();+});diff --git a/packages/player/src/utils/validate-in-out-frame.ts b/packages/player/src/utils/validate-in-out-frame.tsnew file mode 100644index 0000000000..4b0c5201ea--- /dev/null+++ b/packages/player/src/utils/validate-in-out-frame.ts@@ -0,0 +1,91 @@+export const validateSingleFrameFrame = (+ frame: unknown,+ variableName: string+): number | null => {+ if (typeof frame === 'undefined' || frame === null) {+ return frame ?? null;+ }++ if (typeof frame !== 'number') {+ throw new TypeError(+ `"${variableName}" must be a number, but is ${JSON.stringify(frame)}`+ );+ }++ if (Number.isNaN(frame)) {+ throw new TypeError(+ `"${variableName}" must not be NaN, but is ${JSON.stringify(frame)}`+ );+ }++ if (!Number.isFinite(frame)) {+ throw new TypeError(+ `"${variableName}" must be finite, but is ${JSON.stringify(frame)}`+ );+ }++ if (frame % 1 !== 0) {+ throw new TypeError(+ `"${variableName}" must be an integer, but is ${JSON.stringify(frame)}`+ );+ }++ return frame;+};++export const validateInOutFrames = ({+ inFrame,+ durationInFrames,+ outFrame,+}: {+ inFrame: unknown;+ outFrame: unknown;+ durationInFrames: number;+}) => {+ const validatedInFrame = validateSingleFrameFrame(inFrame, 'inFrame');+ const validateOutFrame = validateSingleFrameFrame(outFrame, 'outFrame');+ if (validatedInFrame === null && validateOutFrame === null) {+ return;+ }++ // Must not be over the duration+ if (validatedInFrame !== null && validatedInFrame > durationInFrames - 1) {+ throw new Error(+ 'inFrame must be less than (durationInFrames - 1), but is ' ++ validatedInFrame+ );+ }++ if (validateOutFrame !== null && validateOutFrame > durationInFrames - 1) {+ throw new Error(+ 'outFrame must be less than (durationInFrames - 1), but is ' ++ validatedInFrame+ );+ }++ // Must not be under 0+ if (validatedInFrame !== null && validatedInFrame < 0) {+ throw new Error(+ 'inFrame must be greater than 0, but is ' + validatedInFrame+ );+ }++ if (validateOutFrame !== null && validateOutFrame < 0) {+ throw new Error(+ 'outFrame must be greater than 0, but is ' + validateOutFrame+ );+ }++ if (+ validateOutFrame !== null &&+ validatedInFrame !== null &&+ validateOutFrame <= validatedInFrame+ ) {+ throw new Error(+ 'outFrame must be greater than inFrame, but is ' ++ validateOutFrame ++ ' <= ' ++ validatedInFrame+ );+ }+};