🎥 Make videos programmatically with React
choose best byte
Jonny Burgerfe6841a87e8a
diff --git a/packages/media-parser/src/containers/mp3/get-seeking-byte.ts b/packages/media-parser/src/containers/mp3/get-seeking-byte.tsindex b6fcfd0a57..8f0a5e44a1 100644--- a/packages/media-parser/src/containers/mp3/get-seeking-byte.ts+++ b/packages/media-parser/src/containers/mp3/get-seeking-byte.ts@@ -1,6 +1,6 @@-import type {AudioSampleOffset} from '../../state/audio-sample-map';import type {SeekResolution} from '../../work-on-seek-request';import {getApproximateByteFromBitrate} from './seek/get-approximate-byte-from-bitrate';+import {getByteFromObservedSamples} from './seek/get-byte-from-observed-samples';import type {Mp3SeekingHints} from './seeking-hints';export const getSeekingByteForMp3 = ({@@ -10,8 +10,6 @@ export const getSeekingByteForMp3 = ({time: number;info: Mp3SeekingHints;}): SeekResolution => {- let bestAudioSample: AudioSampleOffset | undefined;-if (info.mp3BitrateInfo === null ||info.mp3Info === null ||@@ -29,46 +27,22 @@ export const getSeekingByteForMp3 = ({mediaSection: info.mediaSection,contentLength: info.contentLength,});- if (approximateByte) {- return {- type: 'do-seek',- byte: approximateByte,- };- }-- for (const hint of info.audioSampleMap) {- if (hint.timeInSeconds > time) {- continue;- }-- // Everything is a mp3 in flac, so if this sample does not cover the time, it's not a good candidate.- // Let's go to the next one. Exception: If we already saw the last sample, we use it so we find can at least- // find the closest one.- if (- hint.timeInSeconds + hint.durationInSeconds < time &&- !info.lastSampleObserved- ) {- continue;- }-- if (!bestAudioSample) {- bestAudioSample = hint;- continue;- }-- if (bestAudioSample.timeInSeconds < hint.timeInSeconds) {- bestAudioSample = hint;- }- }+ const bestAudioSample = getByteFromObservedSamples({+ info,+ timeInSeconds: time,+ });- if (bestAudioSample) {+ const candidates = [approximateByte, bestAudioSample?.offset ?? null].filter(+ (b) => b !== null,+ );+ if (candidates.length === 0) {return {- type: 'do-seek',- byte: bestAudioSample.offset,+ type: 'valid-but-must-wait',};}return {- type: 'valid-but-must-wait',+ type: 'do-seek',+ byte: Math.max(...candidates),};};diff --git a/packages/media-parser/src/containers/mp3/seek/get-byte-from-observed-samples.ts b/packages/media-parser/src/containers/mp3/seek/get-byte-from-observed-samples.tsnew file mode 100644index 0000000000..cac93e2d0f--- /dev/null+++ b/packages/media-parser/src/containers/mp3/seek/get-byte-from-observed-samples.ts@@ -0,0 +1,39 @@+import type {AudioSampleOffset} from '../../../state/audio-sample-map';+import type {Mp3SeekingHints} from '../seeking-hints';++export const getByteFromObservedSamples = ({+ info,+ timeInSeconds,+}: {+ info: Mp3SeekingHints;+ timeInSeconds: number;+}) => {+ let bestAudioSample: AudioSampleOffset | undefined;++ for (const hint of info.audioSampleMap) {+ if (hint.timeInSeconds > timeInSeconds) {+ continue;+ }++ // Everything is a keyframe in mp3, so if this sample does not cover the time, it's not a good candidate.+ // Let's go to the next one. Exception: If we already saw the last sample, we use it so we find can at least+ // find the closest one.+ if (+ hint.timeInSeconds + hint.durationInSeconds < timeInSeconds &&+ !info.lastSampleObserved+ ) {+ continue;+ }++ if (!bestAudioSample) {+ bestAudioSample = hint;+ continue;+ }++ if (bestAudioSample.timeInSeconds < hint.timeInSeconds) {+ bestAudioSample = hint;+ }+ }++ return bestAudioSample;+};