@stacksjs/video 0.75.683 → 0.75.687
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/browser.d.ts +10 -1
- package/dist/browser.js +1 -1
- package/package.json +1 -1
package/dist/browser.d.ts
CHANGED
|
@@ -33,15 +33,24 @@ export declare interface PrepareVideoUploadOptions {
|
|
|
33
33
|
maxOutputBytes?: number
|
|
34
34
|
signal?: AbortSignal
|
|
35
35
|
onProgress?: (ratio: number) => void
|
|
36
|
+
stallMs?: number
|
|
37
|
+
firstTickMs?: number
|
|
36
38
|
}
|
|
37
39
|
export declare interface PreparedVideoUpload {
|
|
38
40
|
video: Blob
|
|
39
41
|
poster?: Blob
|
|
40
42
|
converter: 'webcodecs' | null
|
|
41
|
-
reason?: 'unsupported' | 'already-ready' | 'too-large' | 'failed'
|
|
43
|
+
reason?: 'unsupported' | 'already-ready' | 'too-large' | 'failed' | 'stalled'
|
|
42
44
|
cause?: unknown
|
|
43
45
|
width?: number
|
|
44
46
|
height?: number
|
|
45
47
|
duration?: number
|
|
46
48
|
sourceBytes: number
|
|
47
49
|
}
|
|
50
|
+
/** A conversion that stopped moving, given up so the original can be sent. */
|
|
51
|
+
export declare class VideoPrepareStalledError extends Error {
|
|
52
|
+
/** @defaultValue 'VideoPrepareStalledError' */
|
|
53
|
+
name: string;
|
|
54
|
+
readonly stallMs: number;
|
|
55
|
+
constructor(stallMs: number);
|
|
56
|
+
}
|
package/dist/browser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export function canPrepareVideoUpload(){const scope=globalThis;return["VideoDecoder","VideoEncoder","EncodedVideoChunk","VideoFrame","OffscreenCanvas"].every((name)=>typeof scope[name]==="function")}const isAbort=(error,signal)=>Boolean(signal?.aborted)||error?.name==="AbortError";export async function prepareVideoUpload(file,options={}){const original=(reason,cause)=>({video:file,converter:null,reason,sourceBytes:file.size,...cause===undefined?{}:{cause}});if(!canPrepareVideoUpload())return original("unsupported");
|
|
1
|
+
export function canPrepareVideoUpload(){const scope=globalThis;return["VideoDecoder","VideoEncoder","EncodedVideoChunk","VideoFrame","OffscreenCanvas"].every((name)=>typeof scope[name]==="function")}const isHidden=()=>Boolean(globalThis.document?.hidden);const isAbort=(error,signal)=>Boolean(signal?.aborted)||error?.name==="AbortError";const STALL_MS=20000;const FIRST_TICK_MS=60000;const FIRST_TICK_MS_PER_MB=250;export class VideoPrepareStalledError extends Error{stallMs;name="VideoPrepareStalledError";constructor(stallMs){super(`The clip did not move for ${Math.round(stallMs/1000)} seconds while it was converted`);this.stallMs=stallMs}}export async function prepareVideoUpload(file,options={}){const original=(reason,cause)=>({video:file,converter:null,reason,sourceBytes:file.size,...cause===undefined?{}:{cause}});if(!canPrepareVideoUpload())return original("unsupported");if(options.signal?.aborted)throw options.signal.reason??new DOMException("The upload was cancelled","AbortError");const work=new AbortController;const stallMs=options.stallMs??STALL_MS;const firstTickMs=options.firstTickMs??FIRST_TICK_MS+Math.round(file.size/1048576*FIRST_TICK_MS_PER_MB);let ticked=false;let settled=false;let watchdog;let stop=()=>{};const halted=new Promise((resolve)=>{stop=resolve});const watch=()=>{clearTimeout(watchdog);if(!stallMs||settled||isHidden())return;const budget=ticked?stallMs:Math.max(stallMs,firstTickMs);watchdog=setTimeout(()=>stop({stalled:new VideoPrepareStalledError(budget)}),budget)};const cancel=()=>stop({cancelled:options.signal.reason});options.signal?.addEventListener("abort",cancel,{once:true});const page=globalThis.document;page?.addEventListener?.("visibilitychange",watch);watch();const converting=(async()=>{const{prepareVideoForUpload}=await import("ts-videos/native-transcode");return prepareVideoForUpload(file,{maxLongEdge:options.maxLongEdge,maxShortEdge:options.maxShortEdge,maxFrameRate:options.maxFrameRate,videoBitrate:options.videoBitrate,poster:options.poster??true,maxOutputBytes:options.maxOutputBytes,signal:work.signal,onProgress:(progress)=>{if(settled)return;ticked=true;watch();options.onProgress?.(progress.ratio)}})})();try{const outcome=await Promise.race([converting.then((result)=>({result})),halted]);if("cancelled"in outcome){work.abort(outcome.cancelled);throw outcome.cancelled??new DOMException("The upload was cancelled","AbortError")}if("stalled"in outcome){work.abort(outcome.stalled);return original("stalled",outcome.stalled)}const{result}=outcome;if(result.unchanged)return{...original("already-ready"),width:result.width,height:result.height,duration:result.duration};return{video:result.video,poster:result.poster,converter:"webcodecs",width:result.width,height:result.height,duration:result.duration,sourceBytes:file.size}}catch(error){if(isAbort(error,options.signal))throw error;if(error?.name==="UploadVideoTooLargeError")return original("too-large",error);return original("failed",error)}finally{settled=true;clearTimeout(watchdog);options.signal?.removeEventListener("abort",cancel);page?.removeEventListener?.("visibilitychange",watch);converting.catch(()=>{})}}
|