@twick/timeline 0.14.11 → 0.14.13

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/dist/index.mjs CHANGED
@@ -219,6 +219,12 @@ const extractAudio = async ({
219
219
  const isSafeUrl = /^(https?:|blob:|data:)/i.test(src);
220
220
  if (!isSafeUrl) throw new Error("Unsafe media source URL");
221
221
  const audioBuffer = await fetchAndDecodeAudio(src);
222
+ if (audioBuffer.duration === 0 || audioBuffer.length === 0) {
223
+ throw new Error("No audio track found in the media source");
224
+ }
225
+ if (isAudioSilent(audioBuffer)) {
226
+ throw new Error("Audio track is silent (no audio content detected)");
227
+ }
222
228
  const clampedStart = Math.max(0, start || 0);
223
229
  const fullDuration = audioBuffer.duration;
224
230
  const clampedEnd = Math.min(
@@ -260,13 +266,24 @@ const decodeAudioData = async (arrayBuffer) => {
260
266
  audioContext.decodeAudioData(
261
267
  arrayBuffer.slice(0),
262
268
  (buf) => resolve(buf),
263
- (err) => reject(err || new Error("Failed to decode audio"))
269
+ (err) => reject(err || new Error("Failed to decode audio: no audio track found or unsupported format"))
264
270
  );
265
271
  });
266
272
  } finally {
267
273
  audioContext.close();
268
274
  }
269
275
  };
276
+ const isAudioSilent = (buffer, threshold = 1e-3) => {
277
+ for (let channel = 0; channel < buffer.numberOfChannels; channel++) {
278
+ const channelData = buffer.getChannelData(channel);
279
+ for (let i2 = 0; i2 < channelData.length; i2 += 100) {
280
+ if (Math.abs(channelData[i2]) > threshold) {
281
+ return false;
282
+ }
283
+ }
284
+ }
285
+ return true;
286
+ };
270
287
  const renderAudioSegment = async (audioBuffer, start, end, playbackRate) => {
271
288
  const OfflineAudioContextCtor = window.OfflineAudioContext || window.webkitOfflineAudioContext;
272
289
  if (!OfflineAudioContextCtor) throw new Error("OfflineAudioContext not supported");