@lumen5/framefusion 1.1.0 → 1.2.0
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/framefusion.cjs +1 -1
- package/dist/framefusion.cjs.map +1 -1
- package/dist/framefusion.es.js +121 -128
- package/dist/framefusion.es.js.map +1 -1
- package/dist/src/DownloadVideoURL.d.ts +4 -4
- package/dist/src/DownloadVideoURL.js +28 -39
- package/dist/src/DownloadVideoURL.js.map +1 -1
- package/package.json +1 -1
package/dist/framefusion.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var $=(a,s,t)=>{if(!s.has(a))throw TypeError("Cannot "+t)};var e=(a,s,t)=>($(a,s,"read from private field"),t?t.call(a):s.get(a)),c=(a,s,t)=>{if(s.has(a))throw TypeError("Cannot add the same private member more than once");s instanceof WeakSet?s.add(a):s.set(a,t)},o=(a,s,t,i)=>($(a,s,"write to private field"),i?i.call(a,t):s.set(a,t),t),v=(a,s,t,i)=>({set _(r){o(a,s,r,t)},get _(){return e(a,s,i)}}),V=(a,s,t)=>($(a,s,"access private method"),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("@lumen5/beamcoder"),O=require("path"),j=require("stream"),z=require("tmp"),G=require("fs-extra");class H{static async create(s){throw new Error("Not implemented")}async init({inputFileOrUrl:s,outputFile:t,threadCount:i=8,endTime:r,interpolateFps:h,interpolateMode:n}){throw new Error("Not implemented")}get duration(){throw new Error("Not implemented")}get width(){throw new Error("Not implemented")}get height(){throw new Error("Not implemented")}async seekToPTS(s){throw new Error("Not implemented")}async getFrameAtTime(s){throw new Error("Not implemented")}async getImageDataAtTime(s){throw new Error("Not implemented")}async getFrameAtPts(s){throw new Error("Not implemented")}async seekToTime(s){throw new Error("Not implemented")}ptsToTime(s){throw new Error("Not implemented")}async readFrames({onFrameAvailable:s,flush:t=!0}={flush:!0,onFrameAvailable:()=>!0}){throw new Error("Not implemented")}async dispose(){throw new Error("Not implemented")}}var A,D,F,_;class L{constructor(s){c(this,A,void 0);c(this,D,void 0);c(this,F,void 0);c(this,_,void 0);o(this,A,s);const t=O.extname(s);o(this,_,z.fileSync({postfix:t})),o(this,F,e(this,_).name)}get filepath(){return e(this,F)}async download(){const s=e(this,A),t=await fetch(s);if(!t.ok)throw new Error(`Failed to fetch ${s}, status: ${t.status}`);const i=t.headers.get("content-type");if(!i||!i.includes("video"))throw new Error(`Source ${s}, returned unsupported content type ${i}`);const r=G.createWriteStream(e(this,_).name),h=t.body;if(!h)throw new Error(`Response body is null for ${s}`);return new Promise((n,y)=>{h.pipeTo(j.Writable.toWeb(r)),r.on("finish",()=>{r.close(),o(this,F,e(this,_).name),n()}),r.on("error",u=>{y(u)})})}clear(){e(this,_)&&e(this,_).removeCallback(),e(this,A)&&o(this,A,void 0),e(this,D)&&o(this,D,null),e(this,F)&&o(this,F,void 0)}}A=new WeakMap,D=new WeakMap,F=new WeakMap,_=new WeakMap;const M=4,X=({demuxer:a,streamIndex:s,threadCount:t})=>{const i={width:a.streams[s].codecpar.width,height:a.streams[s].codecpar.height,pix_fmt:a.streams[s].codecpar.format,thread_count:t};return a.streams[s].codecpar.name==="vp8"?C.decoder({...i,name:"libvpx"}):a.streams[s].codecpar.name==="vp9"?C.decoder({...i,name:"libvpx-vp9"}):C.decoder({...i,demuxer:a,stream_index:s})},K=async({stream:a,outputPixelFormat:s,interpolateFps:t,interpolateMode:i="fast"})=>{if(!a.codecpar.format)return null;let r=[`[in0:v]format=${a.codecpar.format}`];if(t)if(i==="high-quality")r=[...r,`minterpolate=fps=${t}`];else if(i==="fast")r=[...r,`fps=${t}`];else throw new Error(`Unexpected interpolation mode: ${i}`);const h=r.join(", ")+"[out0:v]";return C.filterer({filterType:"video",inputParams:[{name:"in0:v",width:a.codecpar.width,height:a.codecpar.height,pixelFormat:a.codecpar.format,timeBase:a.time_base,pixelAspect:a.sample_aspect_ratio}],outputParams:[{name:"out0:v",pixelFormat:s}],filterSpec:h})},W="video",Y="rgba",Z=5;var l,d,x,p,g,E,T,R,f,b,N,S,B;const I=class extends H{constructor(){super(...arguments);c(this,S);c(this,l,null);c(this,d,null);c(this,x,null);c(this,p,[]);c(this,g,[]);c(this,E,null);c(this,T,null);c(this,R,8);c(this,f,0);c(this,b,0);c(this,N,0)}static async create(t){const i=new I;return await i.init(t),i}async init({inputFileOrUrl:t,threadCount:i=8}){if(o(this,R,i),t.startsWith("http")){const r=new L(t);await r.download(),t=r.filepath}if(t.startsWith("file:")||(t="file:"+t),o(this,l,await C.demuxer(t)),o(this,f,e(this,l).streams.findIndex(r=>r.codecpar.codec_type===W)),e(this,f)===-1)throw new Error(`File has no ${W} stream!`);o(this,x,await K({stream:e(this,l).streams[e(this,f)],outputPixelFormat:Y}))}get duration(){const t=e(this,l).streams[e(this,f)];return t.duration!==null?this.ptsToTime(t.duration):this.ptsToTime(e(this,l).duration)/1e3}get width(){return e(this,l).streams[e(this,f)].codecpar.width}get height(){return e(this,l).streams[e(this,f)].codecpar.height}async getFrameAtTime(t){const i=Math.round(this._timeToPTS(t));return this._getFrameAtPts(i)}async getImageDataAtTime(t,i){const r=Math.round(this._timeToPTS(t)),h=await this._getFrameAtPts(r);if(!h)return null;let n=i;return i||(n=new Uint8ClampedArray(h.width*h.height*M)),this._setFrameDataToImageData(h,n),{data:n,width:h.width,height:h.height}}_timeToPTS(t){const i=e(this,l).streams[e(this,f)].time_base;return t*i[1]/i[0]}ptsToTime(t){const i=e(this,l).streams[e(this,f)].time_base;return t*i[0]/i[1]}get packetReadCount(){return e(this,b)}async _getFrameAtPts(t,i=0){o(this,b,0);const r=3,h=e(this,p).flat().some(w=>this.ptsToTime(Math.abs(t-w.pts))<r);(e(this,T)===null||e(this,T)>t||!h)&&(await e(this,l).seek({stream_index:0,timestamp:t+i,any:!1}),await V(this,S,B).call(this),o(this,E,null),o(this,g,[]),o(this,T,t),o(this,p,[]));let n=null,y=-1,u=null;if(e(this,p).length>0){const w=e(this,p).flat().find(m=>m.pts<=t);if(w){const m=e(this,p).flat().find(k=>k.pts>w.pts);if(y=w.pts,u=w,m&&m.pts>t||y===t)return o(this,T,t),u}}for(!e(this,E)&&e(this,g).length===0&&({packet:v(this,E)._,frames:v(this,g)._}=await this._getNextPacketAndDecodeFrames(),v(this,b)._++);(e(this,E)||e(this,g).length!==0)&&y<t;){if(e(this,g).length!==0){n=(await e(this,x).filter([{name:"in0:v",frames:e(this,g)}])).flatMap(k=>k.frames);const m=e(this,b)===1&&n[0].pts>t?n[0]:n.reverse().find(k=>k.pts<=t);if(!m)return u;if(e(this,p).unshift(n),e(this,p).length>2&&e(this,p).pop(),y=m==null?void 0:m.pts,!u||y<=t)o(this,T,t),u=m;else break}({packet:v(this,E)._,frames:v(this,g)._}=await this._getNextPacketAndDecodeFrames()),v(this,b)._++}if(!u){if(Z<e(this,N))throw Error("No matching frame found");const w=.1,m=this._timeToPTS(w);v(this,N)._++,u=await this._getFrameAtPts(t,i-m),u&&o(this,N,0)}return u}async _getNextPacketAndDecodeFrames(){const t=await this._getNextVideoStreamPacket();let i=null;t!==null&&e(this,d)?i=await e(this,d).decode(t):e(this,d)&&(i=await e(this,d).flush(),o(this,d,null));let r=[];return i&&i.frames.length!==0&&(r=i.frames),{packet:t,frames:r}}async _getNextVideoStreamPacket(){let t=await e(this,l).read();for(;t&&t.stream_index!==e(this,f);)if(t=await e(this,l).read(),t===null)return null;return t}_setFrameDataToImageData(t,i){const r=t.linesize,h=t.data[0];for(let n=0;n<t.height;n++){const y=n*r,u=y+t.width*M,w=h.subarray(y,u),m=n*t.width*M;i.set(w,m)}}async dispose(){e(this,d)&&(await e(this,d).flush(),o(this,d,null)),e(this,l).forceClose(),o(this,x,null),o(this,p,void 0),o(this,g,[]),o(this,E,null),o(this,T,null),o(this,f,0)}};let q=I;l=new WeakMap,d=new WeakMap,x=new WeakMap,p=new WeakMap,g=new WeakMap,E=new WeakMap,T=new WeakMap,R=new WeakMap,f=new WeakMap,b=new WeakMap,N=new WeakMap,S=new WeakSet,B=async function(){e(this,d)&&(await e(this,d).flush(),o(this,d,null)),o(this,d,X({demuxer:e(this,l),streamIndex:e(this,f),threadCount:e(this,R)}))};var P;class J{constructor(){c(this,P,new Map)}get(s){const t=this;let i;return{url:s,get filepath(){return i},async download(){let r=e(t,P).get(s);if(r)r.refCount+=1,r.downloadPromise&&await r.downloadPromise;else{const h=new L(s),n=h.download();r={downloader:h,refCount:1,downloadPromise:n},e(t,P).set(s,r);try{await n}finally{r.downloadPromise=void 0}}i=e(t,P).get(s).downloader.filepath},destroy(){const r=e(t,P).get(s);r&&(r.refCount-=1,r.refCount<=0&&(r.downloader.clear(),e(t,P).delete(s)),i=void 0)}}}}P=new WeakMap;exports.BeamcoderExtractor=q;exports.CachedVideoDownloader=J;
|
|
2
2
|
//# sourceMappingURL=framefusion.cjs.map
|
package/dist/framefusion.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"framefusion.cjs","sources":["../src/BaseExtractor.ts","../src/DownloadVideoURL.ts","../src/backends/beamcoder.ts","../src/cachedVideoDownloader.ts"],"sourcesContent":["import type {\n ExtractorArgs,\n Frame,\n Extractor\n} from '../framefusion';\nimport type { ImageData } from './types';\n\n\nexport class BaseExtractor implements Extractor {\n static async create(args: ExtractorArgs): Promise<Extractor> {\n throw new Error('Not implemented');\n }\n\n async init({\n inputFileOrUrl,\n outputFile,\n threadCount = 8,\n endTime,\n interpolateFps,\n interpolateMode,\n }: ExtractorArgs): Promise<void> {\n throw new Error('Not implemented');\n }\n\n get duration(): number {\n throw new Error('Not implemented');\n }\n\n get width(): number {\n throw new Error('Not implemented');\n }\n\n get height(): number {\n throw new Error('Not implemented');\n }\n\n async seekToPTS(targetPts: number) {\n throw new Error('Not implemented');\n }\n\n async getFrameAtTime(targetTime: number): Promise<Frame> {\n throw new Error('Not implemented');\n }\n\n async getImageDataAtTime(targetTime: number): Promise<ImageData> {\n throw new Error('Not implemented');\n }\n\n async getFrameAtPts(targetPts: number): Promise<Frame> {\n throw new Error('Not implemented');\n }\n\n async seekToTime(targetTime: number) {\n throw new Error('Not implemented');\n }\n\n /**\n * Convert a PTS (based on timebase) to PTS (in seconds)\n */\n ptsToTime(pts: number) {\n throw new Error('Not implemented');\n }\n\n async readFrames({\n onFrameAvailable,\n flush = true,\n }: {\n /**\n * Return true if we need to read more frames.\n */\n onFrameAvailable?: (frame: Frame) => Promise<boolean> | boolean;\n flush?: boolean;\n } = {\n flush: true,\n onFrameAvailable: () => true,\n }) {\n throw new Error('Not implemented');\n }\n\n async dispose() {\n throw new Error('Not implemented');\n }\n}\n","import path from 'path';\nimport https from 'node:https';\nimport type { ClientRequest } from 'http';\nimport http from 'http';\nimport tmp from 'tmp';\nimport fs from 'fs-extra';\n\nclass CancelRequestError extends Error { }\n\n/**\n * Downloads a video file from a given URL as a temporary file. When the object is cleared, the temporary file is\n * deleted.\n */\nexport class DownloadVideoURL {\n #url: string | undefined;\n #httpRequest: ClientRequest | undefined = undefined;\n #filepath: string;\n #tmpObj: tmp.FileResult | undefined = undefined;\n\n constructor(url: string) {\n this.#url = url;\n\n const extension = path.extname(url);\n this.#tmpObj = tmp.fileSync({ postfix: extension });\n this.#filepath = this.#tmpObj.name;\n }\n\n /**\n * returns the filepath of the downloaded file. If the file has not been downloaded yet, it will be undefined\n */\n get filepath() {\n return this.#filepath;\n }\n\n /**\n * Downloads the file from the given URL. The file will be downloaded to a temporary file.\n */\n async download() {\n await new Promise<void>((resolve, reject) => {\n const source = this.#url;\n try {\n const connectionHandler = source.startsWith('https://') ? https : http;\n this.#httpRequest = connectionHandler.get(source, (res) => {\n const contentType = res.headers['content-type'];\n if (!contentType.includes('video')) {\n const err = new Error(`Source ${source}, returned unsupported content type ${contentType}`);\n reject(err);\n return;\n }\n const writeStream = fs.createWriteStream(this.#tmpObj.name);\n res.pipe(writeStream);\n writeStream.on('finish', () => {\n writeStream.close();\n this.#filepath = this.#tmpObj.name;\n resolve();\n });\n writeStream.on('error', (e) => {\n reject(e);\n });\n });\n this.#httpRequest.on('error', (e) => {\n if (e instanceof CancelRequestError) {\n return;\n }\n reject(e);\n });\n }\n catch (e) {\n reject(e);\n }\n });\n }\n\n clear() {\n if (this.#tmpObj) this.#tmpObj.removeCallback();\n if (this.#url) this.#url = undefined;\n if (this.#httpRequest) this.#httpRequest = null;\n if (this.#filepath) this.#filepath = undefined;\n }\n}\n","import type {\n Packet,\n Demuxer,\n Decoder,\n Filterer,\n Frame\n} from '@lumen5/beamcoder';\nimport beamcoder from '@lumen5/beamcoder';\nimport type { ImageData } from '../types';\nimport { BaseExtractor } from '../BaseExtractor';\nimport type { Extractor, ExtractorArgs, InterpolateMode } from '../../framefusion';\nimport { DownloadVideoURL } from '../DownloadVideoURL';\n\nconst VERBOSE = false;\n\n/**\n * RGBA format need one byte for every components: r, g, b and a\n */\nconst RGBA_PIXEL_SIZE = 4;\n\nconst createDecoder = ({\n demuxer,\n streamIndex,\n threadCount,\n}: {\n demuxer: Demuxer;\n streamIndex: number;\n threadCount: number;\n}): Decoder => {\n const commonParams = {\n width: demuxer.streams[streamIndex].codecpar.width,\n height: demuxer.streams[streamIndex].codecpar.height,\n pix_fmt: demuxer.streams[streamIndex].codecpar.format,\n thread_count: threadCount,\n };\n\n if (demuxer.streams[streamIndex].codecpar.name === 'vp8') {\n return beamcoder.decoder({\n ...commonParams,\n name: 'libvpx',\n });\n }\n\n if (demuxer.streams[streamIndex].codecpar.name === 'vp9') {\n return beamcoder.decoder({\n ...commonParams,\n name: 'libvpx-vp9',\n });\n }\n\n return beamcoder.decoder({\n ...commonParams,\n demuxer: demuxer,\n stream_index: streamIndex,\n });\n};\n\n/**\n * A filter to convert between color spaces.\n * An example would be YUV to RGB, for mp4 to png conversion.\n */\nconst createFilter = async({\n stream,\n outputPixelFormat,\n interpolateFps,\n interpolateMode = 'fast',\n}: {\n stream: beamcoder.Stream;\n outputPixelFormat: string;\n interpolateFps?: number;\n interpolateMode?: InterpolateMode;\n}): Promise<beamcoder.Filterer> => {\n if (!stream.codecpar.format) {\n return null;\n }\n\n let filterSpec = [`[in0:v]format=${stream.codecpar.format}`];\n\n if (interpolateFps) {\n if (interpolateMode === 'high-quality') {\n filterSpec = [...filterSpec, `minterpolate=fps=${interpolateFps}`];\n }\n else if (interpolateMode === 'fast') {\n filterSpec = [...filterSpec, `fps=${interpolateFps}`];\n }\n else {\n throw new Error(`Unexpected interpolation mode: ${interpolateMode}`);\n }\n }\n\n const filterSpecStr = filterSpec.join(', ') + '[out0:v]';\n\n VERBOSE && console.log(`filterSpec: ${filterSpecStr}`);\n\n return beamcoder.filterer({\n filterType: 'video',\n inputParams: [\n {\n name: 'in0:v',\n width: stream.codecpar.width,\n height: stream.codecpar.height,\n pixelFormat: stream.codecpar.format,\n timeBase: stream.time_base,\n pixelAspect: stream.sample_aspect_ratio,\n },\n ],\n outputParams: [\n {\n name: 'out0:v',\n pixelFormat: outputPixelFormat,\n },\n ],\n filterSpec: filterSpecStr,\n });\n};\n\nconst STREAM_TYPE_VIDEO = 'video';\nconst COLORSPACE_RGBA = 'rgba';\nconst MAX_RECURSION = 5;\n\n/**\n * A simple extractor that uses beamcoder to extract frames from a video file.\n */\nexport class BeamcoderExtractor extends BaseExtractor implements Extractor {\n /**\n * The demuxer reads the file and outputs packet streams\n */\n #demuxer: Demuxer = null;\n\n /**\n * The decoder reads packets and can output raw frame data\n */\n #decoder: Decoder = null;\n\n /**\n * Packets can be filtered to change colorspace, fps and add various effects. If there are no colorspace changes or\n * filters, filter might not be necessary.\n */\n #filterer: Filterer = null;\n\n /**\n * This is where we store filtered frames from each previously processed packet.\n * We keep these in chronological order. We hang on to them for two reasons:\n * 1. so we can return them if we get a request for the same time again\n * 2. so we can return frames close the end of the stream. When such a frame is requested we have to flush (destroy)\n * the encoder to get the last few frames. This avoids having to re-create an encoder.\n */\n #filteredFramesPacket: undefined[] | Array<Array<Frame>> = [];\n\n /**\n * This contains the last raw frames we read from the demuxer. We use it as a starting point for each new query. We\n * do this ensure we don't skip any frames.\n */\n #frames = [];\n\n /**\n * This contains the last packet we read from the demuxer. We use it as a starting point for each new query. We do\n * this ensure we don't skip any frames.\n */\n #packet: null | Packet = null;\n\n /**\n * The last target presentation timestamp (PTS) we requested. If we never requested a time(stamp) then this\n * value is null\n */\n #previousTargetPTS: null | number = null;\n\n /**\n * The number of threads to use for decoding\n */\n #threadCount = 8;\n\n /**\n * The index of the video stream in the demuxer\n */\n #streamIndex = 0;\n\n /**\n * The number of packets we've read from the demuxer to complete the frame query\n * @private\n */\n #packetReadCount = 0;\n\n /**\n * The number of times we've recursively read packets from the demuxer to complete the frame query\n * @private\n */\n #recursiveReadCount = 0;\n\n /**\n * Encoder/Decoder construction is async, so it can't be put in a regular constructor.\n * Use and await this method to generate an extractor.\n */\n static async create(args: ExtractorArgs): Promise<BeamcoderExtractor> {\n const extractor = new BeamcoderExtractor();\n await extractor.init(args);\n return extractor;\n }\n\n async init({\n inputFileOrUrl,\n threadCount = 8,\n }: ExtractorArgs): Promise<void> {\n this.#threadCount = threadCount;\n if (inputFileOrUrl.startsWith('http')) {\n VERBOSE && console.log('downloading url', inputFileOrUrl);\n const downloadUrl = new DownloadVideoURL(inputFileOrUrl);\n await downloadUrl.download();\n inputFileOrUrl = downloadUrl.filepath;\n VERBOSE && console.log('finished downloading');\n }\n // Assume file url at this point\n if (!inputFileOrUrl.startsWith('file:')) {\n inputFileOrUrl = 'file:' + inputFileOrUrl;\n }\n this.#demuxer = await beamcoder.demuxer(inputFileOrUrl);\n this.#streamIndex = this.#demuxer.streams.findIndex(stream => stream.codecpar.codec_type === STREAM_TYPE_VIDEO);\n\n if (this.#streamIndex === -1) {\n throw new Error(`File has no ${STREAM_TYPE_VIDEO} stream!`);\n }\n this.#filterer = await createFilter({\n stream: this.#demuxer.streams[this.#streamIndex],\n outputPixelFormat: COLORSPACE_RGBA,\n });\n }\n\n async #createDecoder() {\n // It's possible that we need to create decoder multiple times during the lifecycle of this extractor so we\n // need to make sure we destroy the old one first if it exists\n if (this.#decoder) {\n await this.#decoder.flush();\n this.#decoder = null;\n }\n this.#decoder = createDecoder({\n demuxer: this.#demuxer as Demuxer,\n streamIndex: this.#streamIndex,\n threadCount: this.#threadCount,\n });\n }\n\n /**\n * This is the duration of the first video stream in the file expressed in seconds.\n */\n get duration(): number {\n const stream = this.#demuxer.streams[this.#streamIndex];\n if (stream.duration !== null) {\n return this.ptsToTime(stream.duration);\n }\n return this.ptsToTime(this.#demuxer.duration) / 1000;\n }\n\n /**\n * Width in pixels\n */\n get width(): number {\n return this.#demuxer.streams[this.#streamIndex].codecpar.width;\n }\n\n /**\n * Height in pixels\n */\n get height(): number {\n return this.#demuxer.streams[this.#streamIndex].codecpar.height;\n }\n\n /**\n * Get the beamcoder Frame for a given time in seconds\n * @param targetTime\n */\n async getFrameAtTime(targetTime: number): Promise<beamcoder.Frame> {\n VERBOSE && console.log(`getFrameAtTime time(s)=${targetTime}`);\n const targetPts = Math.round(this._timeToPTS(targetTime));\n return this._getFrameAtPts(targetPts);\n }\n\n /**\n * Get imageData for a given time in seconds\n * @param targetTime\n */\n async getImageDataAtTime(targetTime: number, target?: Uint8ClampedArray): Promise<ImageData> {\n const targetPts = Math.round(this._timeToPTS(targetTime));\n VERBOSE && console.log('targetTime', targetTime, '-> targetPts', targetPts);\n const frame = await this._getFrameAtPts(targetPts);\n if (!frame) {\n VERBOSE && console.log('no frame found');\n return null;\n }\n\n let rawData = target;\n\n if (!target) {\n rawData = new Uint8ClampedArray(frame.width * frame.height * RGBA_PIXEL_SIZE);\n }\n\n this._setFrameDataToImageData(frame, rawData);\n\n return {\n data: rawData,\n width: frame.width,\n height: frame.height,\n };\n }\n\n /**\n * Get the presentation timestamp (PTS) for a given time in seconds\n */\n _timeToPTS(time: number) {\n const time_base = this.#demuxer.streams[this.#streamIndex].time_base;\n return time * time_base[1] / time_base[0];\n }\n\n /**\n * Get the time in seconds from a given presentation timestamp (PTS)\n */\n ptsToTime(pts: number) {\n const time_base = this.#demuxer.streams[this.#streamIndex].time_base;\n return pts * time_base[0] / time_base[1];\n }\n\n get packetReadCount() {\n return this.#packetReadCount;\n }\n\n /**\n * Get the frame at the given presentation timestamp (PTS)\n * @param targetPTS - the target presentation timestamp (PTS) we want to retrieve\n * @param SeekPTSOffset - the offset to use when seeking to the targetPTS. This is used when we have trouble finding\n * the targetPTS. We use it to further move away from the requested PTS to find a frame. The allows use to read\n * additional packets and find a frame that is closer to the targetPTS.\n */\n async _getFrameAtPts(targetPTS: number, SeekPTSOffset = 0): Promise<beamcoder.Frame> {\n VERBOSE && console.log('_getFrameAtPts', targetPTS, 'seekPTSOffset', SeekPTSOffset, 'duration', this.duration);\n this.#packetReadCount = 0;\n\n // seek and create a decoder when retrieving a frame for the first time or when seeking backwards\n // we have to create a new decoder when seeking backwards as the decoder can only process frames in\n // chronological order.\n // RE_SEEK_DELTA: sometimes, we are looking for a frame so far ahead that it's better to drop everything and seek.\n // Example: when we got a frame a 0 and request a frame at t = 30s just after, we don't want to start reading all packets\n // until 30s.\n const RE_SEEK_THRESHOLD = 3; // 3 seconds - typically we have keyframes at shorter intervals\n const hasFrameWithinThreshold = this.#filteredFramesPacket.flat().some(frame => {\n return this.ptsToTime(Math.abs(targetPTS - (frame as Frame).pts)) < RE_SEEK_THRESHOLD;\n });\n VERBOSE && console.log('hasPreviousTargetPTS:', this.#previousTargetPTS === null, ', targetPTS is smaller:', this.#previousTargetPTS > targetPTS, ', has frame within threshold:', hasFrameWithinThreshold);\n if (this.#previousTargetPTS === null || this.#previousTargetPTS > targetPTS || !hasFrameWithinThreshold) {\n VERBOSE && console.log(`Seeking to ${targetPTS + SeekPTSOffset}`);\n\n await this.#demuxer.seek({\n stream_index: 0, // even though we specify the stream index, it still seeks all streams\n timestamp: targetPTS + SeekPTSOffset,\n any: false,\n });\n await this.#createDecoder();\n this.#packet = null;\n this.#frames = [];\n this.#previousTargetPTS = targetPTS;\n this.#filteredFramesPacket = [];\n }\n\n let filteredFrames = null;\n let closestFramePTS = -1;\n let outputFrame = null;\n\n // If we have previously filtered frames, get the frame closest to our targetPTS\n if (this.#filteredFramesPacket.length > 0) {\n const closestFrame = this.#filteredFramesPacket\n .flat()\n .find(f => (f as Frame).pts <= targetPTS) as Frame;\n\n if (closestFrame) {\n const nextFrame = this.#filteredFramesPacket\n .flat()\n .find(f => (f as Frame).pts > closestFrame.pts) as Frame;\n\n VERBOSE && console.log('returning previously filtered frame with pts', (closestFrame as Frame).pts);\n closestFramePTS = (closestFrame as Frame).pts;\n outputFrame = closestFrame;\n\n if ((nextFrame && nextFrame.pts > targetPTS) || (closestFramePTS === targetPTS)) {\n // We have a next frame, so we know the frame being displayed at targetPTS is the previous one,\n // which corresponds to outputFrame.\n this.#previousTargetPTS = targetPTS;\n return outputFrame;\n }\n }\n }\n\n // This is the first time we're decoding frames. Get the first packet and decode it.\n if (!this.#packet && this.#frames.length === 0) {\n ({ packet: this.#packet, frames: this.#frames } = await this._getNextPacketAndDecodeFrames());\n this.#packetReadCount++;\n }\n // Read packets until we have a frame which is closest to targetPTS\n while ((this.#packet || this.#frames.length !== 0) && closestFramePTS < targetPTS) {\n VERBOSE && console.log('packet si:', this.#packet?.stream_index, 'pts:', this.#packet?.pts, 'frames:', this.#frames?.length);\n VERBOSE && console.log('frames', this.#frames?.length, 'frames.pts:', JSON.stringify(this.#frames?.map(f => f.pts)), '-> target.pts:', targetPTS);\n\n // packet contains frames\n if (this.#frames.length !== 0) {\n // filter the frames\n const filteredResult = await this.#filterer.filter([{ name: 'in0:v', frames: this.#frames }]);\n filteredFrames = filteredResult.flatMap(r => r.frames);\n VERBOSE && console.log('filteredFrames', filteredFrames.length, 'filteredFrames.pts:', JSON.stringify(filteredFrames.map(f => f.pts)), '-> target.pts:', targetPTS);\n\n // get the closest frame to our target presentation timestamp (PTS)\n // Beamcoder returns decoded packet frames as follows: [1000, 2000, 3000, 4000]\n // If we're looking for a frame at 0, we want to return the frame at 1000\n // If we're looking for a frame at 2500, we want to return the frame at 2000\n const closestFrame = (this.#packetReadCount === 1 && filteredFrames[0].pts > targetPTS)\n ? filteredFrames[0]\n : filteredFrames.reverse().find(f => f.pts <= targetPTS);\n\n // The packet contains frames, but all of them have PTS larger than our a targetPTS (we looked too far)\n if (!closestFrame) {\n return outputFrame;\n }\n\n // store the filtered packet frames for later reuse\n this.#filteredFramesPacket.unshift(filteredFrames);\n if (this.#filteredFramesPacket.length > 2) {\n this.#filteredFramesPacket.pop();\n }\n\n closestFramePTS = closestFrame?.pts;\n VERBOSE && console.log('closestFramePTS', closestFramePTS, 'targetPTS', targetPTS);\n if (!outputFrame || closestFramePTS <= targetPTS) {\n VERBOSE && console.log('assigning outputFrame', closestFrame?.pts);\n this.#previousTargetPTS = targetPTS;\n outputFrame = closestFrame;\n }\n else {\n // break out of the loop if we've found the closest frame (and ensure we don't move to the next\n // packet by calling _getNextPacketAndDecodeFrames again) as this risks us getting a frame that is\n // after our targetPTS\n VERBOSE && console.log('breaking');\n break;\n }\n }\n // get the next packet and frames\n ({ packet: this.#packet, frames: this.#frames } = await this._getNextPacketAndDecodeFrames());\n\n // keep track of how many packets we've read\n this.#packetReadCount++;\n }\n\n // we read through all the available packets and frames, but we still don't have a frame. This can happen\n // when our targetPTS is to close to the end of the video. In this case, we'll try to seek further away from\n // the end of the video and try again. We've set up a MAX_RECURSION to prevent an infinite loop.\n if (!outputFrame) {\n if (MAX_RECURSION < this.#recursiveReadCount) {\n throw Error('No matching frame found');\n }\n const TIME_OFFSET = 0.1; // time offset in seconds\n const PTSOffset = this._timeToPTS(TIME_OFFSET);\n this.#recursiveReadCount++;\n outputFrame = await this._getFrameAtPts(targetPTS, SeekPTSOffset - PTSOffset);\n if (outputFrame) {\n this.#recursiveReadCount = 0;\n }\n }\n VERBOSE && console.log('read', this.packetReadCount, 'packets');\n\n return outputFrame;\n }\n\n /**\n * Get the next packet from the video stream and decode it into frames. Each frame has a presentation time stamp\n * (PTS). If we've reached the end of the stream and no more packets are available, we'll extract the last frames\n * from the decoder and destroy it.\n */\n async _getNextPacketAndDecodeFrames() {\n const packet = await this._getNextVideoStreamPacket();\n VERBOSE && console.log('packet pts:', packet?.pts);\n\n // extract frames from the packet\n let decodedFrames = null;\n if (packet !== null && this.#decoder) {\n decodedFrames = await this.#decoder.decode(packet as Packet);\n VERBOSE && console.log('decodedFrames', decodedFrames.frames.length, decodedFrames.frames.map(f => f.pts));\n }\n // we've reached the end of the stream\n else {\n if (this.#decoder) {\n VERBOSE && console.log('getting the last frames from the decoder');\n // flush the decoder -- this will return the last frames and destroy the decoder\n decodedFrames = await this.#decoder.flush();\n this.#decoder = null;\n }\n else {\n // we don't have a decoder, so we can't decode any more frames\n VERBOSE && console.log('no more frames to decode');\n }\n }\n\n let frames = [];\n if (decodedFrames && decodedFrames.frames.length !== 0) {\n frames = decodedFrames.frames;\n }\n VERBOSE && console.log(`returning ${frames.length} decoded frames`);\n\n return { packet, frames };\n }\n\n async _getNextVideoStreamPacket(): Promise<null | Packet> {\n VERBOSE && console.log('_getNextVideoStreamPacket');\n\n let packet = await this.#demuxer.read();\n while (packet && packet.stream_index !== this.#streamIndex) {\n packet = await this.#demuxer.read();\n if (packet === null) {\n VERBOSE && console.log('no more packets');\n return null;\n }\n }\n VERBOSE && console.log('returning packet', !!packet, 'pts', packet?.pts, 'si', packet?.stream_index);\n return packet as Packet;\n }\n\n _setFrameDataToImageData(frame: beamcoder.Frame, target: Uint8ClampedArray) {\n const sourceLineSize = frame.linesize as unknown as number;\n // frame.data can contain multiple \"planes\" in other colorspaces, but in rgba, there is just one \"plane\", so\n // our data is in frame.data[0]\n const pixels = frame.data[0] as Uint8Array;\n\n // libav creates larger buffers because it makes their internal code simpler.\n // we have to trim a part at the right of each pixel row.\n\n for (let i = 0; i < frame.height; i++) {\n const sourceStart = i * sourceLineSize;\n const sourceEnd = sourceStart + frame.width * RGBA_PIXEL_SIZE;\n const sourceData = pixels.subarray(sourceStart, sourceEnd);\n const targetOffset = i * frame.width * RGBA_PIXEL_SIZE;\n target.set(sourceData, targetOffset);\n }\n }\n\n async dispose() {\n if (this.#decoder) {\n await this.#decoder.flush();\n this.#decoder = null;\n }\n this.#demuxer.forceClose();\n this.#filterer = null;\n this.#filteredFramesPacket = undefined;\n this.#frames = [];\n this.#packet = null;\n this.#previousTargetPTS = null;\n this.#streamIndex = 0;\n }\n}\n","import { DownloadVideoURL } from './DownloadVideoURL';\n\ninterface CachedResource {\n url: string;\n filepath: string | undefined;\n download(): Promise<void>;\n destroy(): void;\n}\n\ninterface CacheEntry {\n downloader: DownloadVideoURL;\n refCount: number;\n downloadPromise?: Promise<void>;\n}\n\nexport class CachedVideoDownloader {\n #cache: Map<string, CacheEntry> = new Map();\n\n get(url: string): CachedResource {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n let filepath: string | undefined;\n\n return {\n url,\n get filepath() {\n return filepath;\n },\n\n async download() {\n let entry = self.#cache.get(url);\n\n if (entry) {\n entry.refCount += 1;\n\n // Wait for in-progress download if exists\n if (entry.downloadPromise) {\n await entry.downloadPromise;\n }\n }\n else {\n const downloader = new DownloadVideoURL(url);\n\n const promise = downloader.download();\n entry = {\n downloader,\n refCount: 1,\n downloadPromise: promise,\n };\n self.#cache.set(url, entry);\n\n try {\n await promise;\n }\n finally {\n entry.downloadPromise = undefined; // Clear after completion\n }\n }\n\n filepath = self.#cache.get(url).downloader.filepath;\n },\n\n destroy() {\n const entry = self.#cache.get(url);\n if (!entry) return;\n\n entry.refCount -= 1;\n\n if (entry.refCount <= 0) {\n entry.downloader.clear();\n self.#cache.delete(url);\n }\n\n filepath = undefined;\n },\n };\n }\n}\n"],"names":["BaseExtractor","args","inputFileOrUrl","outputFile","threadCount","endTime","interpolateFps","interpolateMode","targetPts","targetTime","pts","onFrameAvailable","flush","CancelRequestError","DownloadVideoURL","url","__privateAdd","_url","_httpRequest","_filepath","_tmpObj","__privateSet","extension","path","tmp","__privateGet","resolve","reject","source","connectionHandler","https","http","res","contentType","err","writeStream","fs","e","RGBA_PIXEL_SIZE","createDecoder","demuxer","streamIndex","commonParams","beamcoder","createFilter","stream","outputPixelFormat","filterSpec","filterSpecStr","STREAM_TYPE_VIDEO","COLORSPACE_RGBA","MAX_RECURSION","_BeamcoderExtractor","_createDecoder","_demuxer","_decoder","_filterer","_filteredFramesPacket","_frames","_packet","_previousTargetPTS","_threadCount","_streamIndex","_packetReadCount","_recursiveReadCount","extractor","downloadUrl","target","frame","rawData","time","time_base","targetPTS","SeekPTSOffset","RE_SEEK_THRESHOLD","hasFrameWithinThreshold","__privateMethod","createDecoder_fn","filteredFrames","closestFramePTS","outputFrame","closestFrame","nextFrame","f","__privateWrapper","r","TIME_OFFSET","PTSOffset","packet","decodedFrames","frames","sourceLineSize","pixels","i","sourceStart","sourceEnd","sourceData","targetOffset","BeamcoderExtractor","CachedVideoDownloader","_cache","self","filepath","entry","downloader","promise"],"mappings":"wpBAQO,MAAMA,CAAmC,CAC5C,aAAa,OAAOC,EAAyC,CACnD,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,KAAK,CACP,eAAAC,EACA,WAAAC,EACA,YAAAC,EAAc,EACd,QAAAC,EACA,eAAAC,EACA,gBAAAC,CAAA,EAC6B,CACvB,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,IAAI,UAAmB,CACb,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,IAAI,OAAgB,CACV,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,IAAI,QAAiB,CACX,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,UAAUC,EAAmB,CACzB,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,eAAeC,EAAoC,CAC/C,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,mBAAmBA,EAAwC,CACvD,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,cAAcD,EAAmC,CAC7C,MAAA,IAAI,MAAM,iBAAiB,CACrC,CA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,EAAM,iBACN,MAAMA,EAAM,oBAGf,CACK,MAAAC,EAAa,IAAI1F,EAAiBC,CAAG,EAErC0F,EAAUD,EAAW,WACnBD,EAAA,CACJ,WAAAC,EACA,SAAU,EACV,gBAAiBC,CAAA,EAEhBhF,EAAA4E,EAAAD,GAAO,IAAIrF,EAAKwF,CAAK,EAEtB,GAAA,CACM,MAAAE,CAAA,QAEV,CACIF,EAAM,gBAAkB,MAC5B,EAGJD,EAAW7E,EAAA4E,EAAKD,GAAO,IAAIrF,CAAG,EAAE,WAAW,QAC/C,EAEA,SAAU,CACN,MAAMwF,EAAQ9E,EAAA4E,EAAKD,GAAO,IAAIrF,CAAG,EAC5BwF,IAELA,EAAM,UAAY,EAEdA,EAAM,UAAY,IAClBA,EAAM,WAAW,QACZ9E,EAAA4E,EAAAD,GAAO,OAAOrF,CAAG,GAGfuF,EAAA,OACf,CAAA,CAER,CACJ,CA9DIF,EAAA"}
|
|
1
|
+
{"version":3,"file":"framefusion.cjs","sources":["../src/BaseExtractor.ts","../src/DownloadVideoURL.ts","../src/backends/beamcoder.ts","../src/cachedVideoDownloader.ts"],"sourcesContent":["import type {\n ExtractorArgs,\n Frame,\n Extractor\n} from '../framefusion';\nimport type { ImageData } from './types';\n\n\nexport class BaseExtractor implements Extractor {\n static async create(args: ExtractorArgs): Promise<Extractor> {\n throw new Error('Not implemented');\n }\n\n async init({\n inputFileOrUrl,\n outputFile,\n threadCount = 8,\n endTime,\n interpolateFps,\n interpolateMode,\n }: ExtractorArgs): Promise<void> {\n throw new Error('Not implemented');\n }\n\n get duration(): number {\n throw new Error('Not implemented');\n }\n\n get width(): number {\n throw new Error('Not implemented');\n }\n\n get height(): number {\n throw new Error('Not implemented');\n }\n\n async seekToPTS(targetPts: number) {\n throw new Error('Not implemented');\n }\n\n async getFrameAtTime(targetTime: number): Promise<Frame> {\n throw new Error('Not implemented');\n }\n\n async getImageDataAtTime(targetTime: number): Promise<ImageData> {\n throw new Error('Not implemented');\n }\n\n async getFrameAtPts(targetPts: number): Promise<Frame> {\n throw new Error('Not implemented');\n }\n\n async seekToTime(targetTime: number) {\n throw new Error('Not implemented');\n }\n\n /**\n * Convert a PTS (based on timebase) to PTS (in seconds)\n */\n ptsToTime(pts: number) {\n throw new Error('Not implemented');\n }\n\n async readFrames({\n onFrameAvailable,\n flush = true,\n }: {\n /**\n * Return true if we need to read more frames.\n */\n onFrameAvailable?: (frame: Frame) => Promise<boolean> | boolean;\n flush?: boolean;\n } = {\n flush: true,\n onFrameAvailable: () => true,\n }) {\n throw new Error('Not implemented');\n }\n\n async dispose() {\n throw new Error('Not implemented');\n }\n}\n","import path from 'path';\nimport type { ClientRequest } from 'http';\n\nimport { Writable } from 'stream';\nimport tmp from 'tmp';\nimport fs from 'fs-extra';\n\n/**\n * Downloads a video file from a given URL as a temporary file. When the object is cleared, the temporary file is\n * deleted.\n */\nexport class DownloadVideoURL {\n #url: string | undefined;\n #httpRequest: ClientRequest | undefined = undefined;\n #filepath: string;\n #tmpObj: tmp.FileResult | undefined = undefined;\n\n constructor(url: string) {\n this.#url = url;\n\n const extension = path.extname(url);\n this.#tmpObj = tmp.fileSync({ postfix: extension });\n this.#filepath = this.#tmpObj.name;\n }\n\n /**\n * returns the filepath of the downloaded file. If the file has not been downloaded yet, it will be undefined\n */\n get filepath() {\n return this.#filepath;\n }\n\n /**\n * Downloads the file from the given URL. The file will be downloaded to a temporary file.\n */\n async download() {\n const source = this.#url;\n\n const response = await fetch(source);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch ${source}, status: ${response.status}`\n );\n }\n\n const contentType = response.headers.get('content-type');\n if (!contentType || !contentType.includes('video')) {\n throw new Error(\n `Source ${source}, returned unsupported content type ${contentType}`\n );\n }\n\n const writeStream = fs.createWriteStream(this.#tmpObj.name);\n const readableStream = response.body;\n\n if (!readableStream) {\n throw new Error(`Response body is null for ${source}`);\n }\n\n return new Promise<void>((resolve, reject) => {\n readableStream.pipeTo(Writable.toWeb(writeStream));\n writeStream.on('finish', () => {\n writeStream.close();\n this.#filepath = this.#tmpObj.name;\n resolve();\n });\n writeStream.on('error', (e) => {\n reject(e);\n });\n });\n }\n\n clear() {\n if (this.#tmpObj) this.#tmpObj.removeCallback();\n if (this.#url) this.#url = undefined;\n if (this.#httpRequest) this.#httpRequest = null;\n if (this.#filepath) this.#filepath = undefined;\n }\n}\n","import type {\n Packet,\n Demuxer,\n Decoder,\n Filterer,\n Frame\n} from '@lumen5/beamcoder';\nimport beamcoder from '@lumen5/beamcoder';\nimport type { ImageData } from '../types';\nimport { BaseExtractor } from '../BaseExtractor';\nimport type { Extractor, ExtractorArgs, InterpolateMode } from '../../framefusion';\nimport { DownloadVideoURL } from '../DownloadVideoURL';\n\nconst VERBOSE = false;\n\n/**\n * RGBA format need one byte for every components: r, g, b and a\n */\nconst RGBA_PIXEL_SIZE = 4;\n\nconst createDecoder = ({\n demuxer,\n streamIndex,\n threadCount,\n}: {\n demuxer: Demuxer;\n streamIndex: number;\n threadCount: number;\n}): Decoder => {\n const commonParams = {\n width: demuxer.streams[streamIndex].codecpar.width,\n height: demuxer.streams[streamIndex].codecpar.height,\n pix_fmt: demuxer.streams[streamIndex].codecpar.format,\n thread_count: threadCount,\n };\n\n if (demuxer.streams[streamIndex].codecpar.name === 'vp8') {\n return beamcoder.decoder({\n ...commonParams,\n name: 'libvpx',\n });\n }\n\n if (demuxer.streams[streamIndex].codecpar.name === 'vp9') {\n return beamcoder.decoder({\n ...commonParams,\n name: 'libvpx-vp9',\n });\n }\n\n return beamcoder.decoder({\n ...commonParams,\n demuxer: demuxer,\n stream_index: streamIndex,\n });\n};\n\n/**\n * A filter to convert between color spaces.\n * An example would be YUV to RGB, for mp4 to png conversion.\n */\nconst createFilter = async({\n stream,\n outputPixelFormat,\n interpolateFps,\n interpolateMode = 'fast',\n}: {\n stream: beamcoder.Stream;\n outputPixelFormat: string;\n interpolateFps?: number;\n interpolateMode?: InterpolateMode;\n}): Promise<beamcoder.Filterer> => {\n if (!stream.codecpar.format) {\n return null;\n }\n\n let filterSpec = [`[in0:v]format=${stream.codecpar.format}`];\n\n if (interpolateFps) {\n if (interpolateMode === 'high-quality') {\n filterSpec = [...filterSpec, `minterpolate=fps=${interpolateFps}`];\n }\n else if (interpolateMode === 'fast') {\n filterSpec = [...filterSpec, `fps=${interpolateFps}`];\n }\n else {\n throw new Error(`Unexpected interpolation mode: ${interpolateMode}`);\n }\n }\n\n const filterSpecStr = filterSpec.join(', ') + '[out0:v]';\n\n VERBOSE && console.log(`filterSpec: ${filterSpecStr}`);\n\n return beamcoder.filterer({\n filterType: 'video',\n inputParams: [\n {\n name: 'in0:v',\n width: stream.codecpar.width,\n height: stream.codecpar.height,\n pixelFormat: stream.codecpar.format,\n timeBase: stream.time_base,\n pixelAspect: stream.sample_aspect_ratio,\n },\n ],\n outputParams: [\n {\n name: 'out0:v',\n pixelFormat: outputPixelFormat,\n },\n ],\n filterSpec: filterSpecStr,\n });\n};\n\nconst STREAM_TYPE_VIDEO = 'video';\nconst COLORSPACE_RGBA = 'rgba';\nconst MAX_RECURSION = 5;\n\n/**\n * A simple extractor that uses beamcoder to extract frames from a video file.\n */\nexport class BeamcoderExtractor extends BaseExtractor implements Extractor {\n /**\n * The demuxer reads the file and outputs packet streams\n */\n #demuxer: Demuxer = null;\n\n /**\n * The decoder reads packets and can output raw frame data\n */\n #decoder: Decoder = null;\n\n /**\n * Packets can be filtered to change colorspace, fps and add various effects. If there are no colorspace changes or\n * filters, filter might not be necessary.\n */\n #filterer: Filterer = null;\n\n /**\n * This is where we store filtered frames from each previously processed packet.\n * We keep these in chronological order. We hang on to them for two reasons:\n * 1. so we can return them if we get a request for the same time again\n * 2. so we can return frames close the end of the stream. When such a frame is requested we have to flush (destroy)\n * the encoder to get the last few frames. This avoids having to re-create an encoder.\n */\n #filteredFramesPacket: undefined[] | Array<Array<Frame>> = [];\n\n /**\n * This contains the last raw frames we read from the demuxer. We use it as a starting point for each new query. We\n * do this ensure we don't skip any frames.\n */\n #frames = [];\n\n /**\n * This contains the last packet we read from the demuxer. We use it as a starting point for each new query. We do\n * this ensure we don't skip any frames.\n */\n #packet: null | Packet = null;\n\n /**\n * The last target presentation timestamp (PTS) we requested. If we never requested a time(stamp) then this\n * value is null\n */\n #previousTargetPTS: null | number = null;\n\n /**\n * The number of threads to use for decoding\n */\n #threadCount = 8;\n\n /**\n * The index of the video stream in the demuxer\n */\n #streamIndex = 0;\n\n /**\n * The number of packets we've read from the demuxer to complete the frame query\n * @private\n */\n #packetReadCount = 0;\n\n /**\n * The number of times we've recursively read packets from the demuxer to complete the frame query\n * @private\n */\n #recursiveReadCount = 0;\n\n /**\n * Encoder/Decoder construction is async, so it can't be put in a regular constructor.\n * Use and await this method to generate an extractor.\n */\n static async create(args: ExtractorArgs): Promise<BeamcoderExtractor> {\n const extractor = new BeamcoderExtractor();\n await extractor.init(args);\n return extractor;\n }\n\n async init({\n inputFileOrUrl,\n threadCount = 8,\n }: ExtractorArgs): Promise<void> {\n this.#threadCount = threadCount;\n if (inputFileOrUrl.startsWith('http')) {\n VERBOSE && console.log('downloading url', inputFileOrUrl);\n const downloadUrl = new DownloadVideoURL(inputFileOrUrl);\n await downloadUrl.download();\n inputFileOrUrl = downloadUrl.filepath;\n VERBOSE && console.log('finished downloading');\n }\n // Assume file url at this point\n if (!inputFileOrUrl.startsWith('file:')) {\n inputFileOrUrl = 'file:' + inputFileOrUrl;\n }\n this.#demuxer = await beamcoder.demuxer(inputFileOrUrl);\n this.#streamIndex = this.#demuxer.streams.findIndex(stream => stream.codecpar.codec_type === STREAM_TYPE_VIDEO);\n\n if (this.#streamIndex === -1) {\n throw new Error(`File has no ${STREAM_TYPE_VIDEO} stream!`);\n }\n this.#filterer = await createFilter({\n stream: this.#demuxer.streams[this.#streamIndex],\n outputPixelFormat: COLORSPACE_RGBA,\n });\n }\n\n async #createDecoder() {\n // It's possible that we need to create decoder multiple times during the lifecycle of this extractor so we\n // need to make sure we destroy the old one first if it exists\n if (this.#decoder) {\n await this.#decoder.flush();\n this.#decoder = null;\n }\n this.#decoder = createDecoder({\n demuxer: this.#demuxer as Demuxer,\n streamIndex: this.#streamIndex,\n threadCount: this.#threadCount,\n });\n }\n\n /**\n * This is the duration of the first video stream in the file expressed in seconds.\n */\n get duration(): number {\n const stream = this.#demuxer.streams[this.#streamIndex];\n if (stream.duration !== null) {\n return this.ptsToTime(stream.duration);\n }\n return this.ptsToTime(this.#demuxer.duration) / 1000;\n }\n\n /**\n * Width in pixels\n */\n get width(): number {\n return this.#demuxer.streams[this.#streamIndex].codecpar.width;\n }\n\n /**\n * Height in pixels\n */\n get height(): number {\n return this.#demuxer.streams[this.#streamIndex].codecpar.height;\n }\n\n /**\n * Get the beamcoder Frame for a given time in seconds\n * @param targetTime\n */\n async getFrameAtTime(targetTime: number): Promise<beamcoder.Frame> {\n VERBOSE && console.log(`getFrameAtTime time(s)=${targetTime}`);\n const targetPts = Math.round(this._timeToPTS(targetTime));\n return this._getFrameAtPts(targetPts);\n }\n\n /**\n * Get imageData for a given time in seconds\n * @param targetTime\n */\n async getImageDataAtTime(targetTime: number, target?: Uint8ClampedArray): Promise<ImageData> {\n const targetPts = Math.round(this._timeToPTS(targetTime));\n VERBOSE && console.log('targetTime', targetTime, '-> targetPts', targetPts);\n const frame = await this._getFrameAtPts(targetPts);\n if (!frame) {\n VERBOSE && console.log('no frame found');\n return null;\n }\n\n let rawData = target;\n\n if (!target) {\n rawData = new Uint8ClampedArray(frame.width * frame.height * RGBA_PIXEL_SIZE);\n }\n\n this._setFrameDataToImageData(frame, rawData);\n\n return {\n data: rawData,\n width: frame.width,\n height: frame.height,\n };\n }\n\n /**\n * Get the presentation timestamp (PTS) for a given time in seconds\n */\n _timeToPTS(time: number) {\n const time_base = this.#demuxer.streams[this.#streamIndex].time_base;\n return time * time_base[1] / time_base[0];\n }\n\n /**\n * Get the time in seconds from a given presentation timestamp (PTS)\n */\n ptsToTime(pts: number) {\n const time_base = this.#demuxer.streams[this.#streamIndex].time_base;\n return pts * time_base[0] / time_base[1];\n }\n\n get packetReadCount() {\n return this.#packetReadCount;\n }\n\n /**\n * Get the frame at the given presentation timestamp (PTS)\n * @param targetPTS - the target presentation timestamp (PTS) we want to retrieve\n * @param SeekPTSOffset - the offset to use when seeking to the targetPTS. This is used when we have trouble finding\n * the targetPTS. We use it to further move away from the requested PTS to find a frame. The allows use to read\n * additional packets and find a frame that is closer to the targetPTS.\n */\n async _getFrameAtPts(targetPTS: number, SeekPTSOffset = 0): Promise<beamcoder.Frame> {\n VERBOSE && console.log('_getFrameAtPts', targetPTS, 'seekPTSOffset', SeekPTSOffset, 'duration', this.duration);\n this.#packetReadCount = 0;\n\n // seek and create a decoder when retrieving a frame for the first time or when seeking backwards\n // we have to create a new decoder when seeking backwards as the decoder can only process frames in\n // chronological order.\n // RE_SEEK_DELTA: sometimes, we are looking for a frame so far ahead that it's better to drop everything and seek.\n // Example: when we got a frame a 0 and request a frame at t = 30s just after, we don't want to start reading all packets\n // until 30s.\n const RE_SEEK_THRESHOLD = 3; // 3 seconds - typically we have keyframes at shorter intervals\n const hasFrameWithinThreshold = this.#filteredFramesPacket.flat().some(frame => {\n return this.ptsToTime(Math.abs(targetPTS - (frame as Frame).pts)) < RE_SEEK_THRESHOLD;\n });\n VERBOSE && console.log('hasPreviousTargetPTS:', this.#previousTargetPTS === null, ', targetPTS is smaller:', this.#previousTargetPTS > targetPTS, ', has frame within threshold:', hasFrameWithinThreshold);\n if (this.#previousTargetPTS === null || this.#previousTargetPTS > targetPTS || !hasFrameWithinThreshold) {\n VERBOSE && console.log(`Seeking to ${targetPTS + SeekPTSOffset}`);\n\n await this.#demuxer.seek({\n stream_index: 0, // even though we specify the stream index, it still seeks all streams\n timestamp: targetPTS + SeekPTSOffset,\n any: false,\n });\n await this.#createDecoder();\n this.#packet = null;\n this.#frames = [];\n this.#previousTargetPTS = targetPTS;\n this.#filteredFramesPacket = [];\n }\n\n let filteredFrames = null;\n let closestFramePTS = -1;\n let outputFrame = null;\n\n // If we have previously filtered frames, get the frame closest to our targetPTS\n if (this.#filteredFramesPacket.length > 0) {\n const closestFrame = this.#filteredFramesPacket\n .flat()\n .find(f => (f as Frame).pts <= targetPTS) as Frame;\n\n if (closestFrame) {\n const nextFrame = this.#filteredFramesPacket\n .flat()\n .find(f => (f as Frame).pts > closestFrame.pts) as Frame;\n\n VERBOSE && console.log('returning previously filtered frame with pts', (closestFrame as Frame).pts);\n closestFramePTS = (closestFrame as Frame).pts;\n outputFrame = closestFrame;\n\n if ((nextFrame && nextFrame.pts > targetPTS) || (closestFramePTS === targetPTS)) {\n // We have a next frame, so we know the frame being displayed at targetPTS is the previous one,\n // which corresponds to outputFrame.\n this.#previousTargetPTS = targetPTS;\n return outputFrame;\n }\n }\n }\n\n // This is the first time we're decoding frames. Get the first packet and decode it.\n if (!this.#packet && this.#frames.length === 0) {\n ({ packet: this.#packet, frames: this.#frames } = await this._getNextPacketAndDecodeFrames());\n this.#packetReadCount++;\n }\n // Read packets until we have a frame which is closest to targetPTS\n while ((this.#packet || this.#frames.length !== 0) && closestFramePTS < targetPTS) {\n VERBOSE && console.log('packet si:', this.#packet?.stream_index, 'pts:', this.#packet?.pts, 'frames:', this.#frames?.length);\n VERBOSE && console.log('frames', this.#frames?.length, 'frames.pts:', JSON.stringify(this.#frames?.map(f => f.pts)), '-> target.pts:', targetPTS);\n\n // packet contains frames\n if (this.#frames.length !== 0) {\n // filter the frames\n const filteredResult = await this.#filterer.filter([{ name: 'in0:v', frames: this.#frames }]);\n filteredFrames = filteredResult.flatMap(r => r.frames);\n VERBOSE && console.log('filteredFrames', filteredFrames.length, 'filteredFrames.pts:', JSON.stringify(filteredFrames.map(f => f.pts)), '-> target.pts:', targetPTS);\n\n // get the closest frame to our target presentation timestamp (PTS)\n // Beamcoder returns decoded packet frames as follows: [1000, 2000, 3000, 4000]\n // If we're looking for a frame at 0, we want to return the frame at 1000\n // If we're looking for a frame at 2500, we want to return the frame at 2000\n const closestFrame = (this.#packetReadCount === 1 && filteredFrames[0].pts > targetPTS)\n ? filteredFrames[0]\n : filteredFrames.reverse().find(f => f.pts <= targetPTS);\n\n // The packet contains frames, but all of them have PTS larger than our a targetPTS (we looked too far)\n if (!closestFrame) {\n return outputFrame;\n }\n\n // store the filtered packet frames for later reuse\n this.#filteredFramesPacket.unshift(filteredFrames);\n if (this.#filteredFramesPacket.length > 2) {\n this.#filteredFramesPacket.pop();\n }\n\n closestFramePTS = closestFrame?.pts;\n VERBOSE && console.log('closestFramePTS', closestFramePTS, 'targetPTS', targetPTS);\n if (!outputFrame || closestFramePTS <= targetPTS) {\n VERBOSE && console.log('assigning outputFrame', closestFrame?.pts);\n this.#previousTargetPTS = targetPTS;\n outputFrame = closestFrame;\n }\n else {\n // break out of the loop if we've found the closest frame (and ensure we don't move to the next\n // packet by calling _getNextPacketAndDecodeFrames again) as this risks us getting a frame that is\n // after our targetPTS\n VERBOSE && console.log('breaking');\n break;\n }\n }\n // get the next packet and frames\n ({ packet: this.#packet, frames: this.#frames } = await this._getNextPacketAndDecodeFrames());\n\n // keep track of how many packets we've read\n this.#packetReadCount++;\n }\n\n // we read through all the available packets and frames, but we still don't have a frame. This can happen\n // when our targetPTS is to close to the end of the video. In this case, we'll try to seek further away from\n // the end of the video and try again. We've set up a MAX_RECURSION to prevent an infinite loop.\n if (!outputFrame) {\n if (MAX_RECURSION < this.#recursiveReadCount) {\n throw Error('No matching frame found');\n }\n const TIME_OFFSET = 0.1; // time offset in seconds\n const PTSOffset = this._timeToPTS(TIME_OFFSET);\n this.#recursiveReadCount++;\n outputFrame = await this._getFrameAtPts(targetPTS, SeekPTSOffset - PTSOffset);\n if (outputFrame) {\n this.#recursiveReadCount = 0;\n }\n }\n VERBOSE && console.log('read', this.packetReadCount, 'packets');\n\n return outputFrame;\n }\n\n /**\n * Get the next packet from the video stream and decode it into frames. Each frame has a presentation time stamp\n * (PTS). If we've reached the end of the stream and no more packets are available, we'll extract the last frames\n * from the decoder and destroy it.\n */\n async _getNextPacketAndDecodeFrames() {\n const packet = await this._getNextVideoStreamPacket();\n VERBOSE && console.log('packet pts:', packet?.pts);\n\n // extract frames from the packet\n let decodedFrames = null;\n if (packet !== null && this.#decoder) {\n decodedFrames = await this.#decoder.decode(packet as Packet);\n VERBOSE && console.log('decodedFrames', decodedFrames.frames.length, decodedFrames.frames.map(f => f.pts));\n }\n // we've reached the end of the stream\n else {\n if (this.#decoder) {\n VERBOSE && console.log('getting the last frames from the decoder');\n // flush the decoder -- this will return the last frames and destroy the decoder\n decodedFrames = await this.#decoder.flush();\n this.#decoder = null;\n }\n else {\n // we don't have a decoder, so we can't decode any more frames\n VERBOSE && console.log('no more frames to decode');\n }\n }\n\n let frames = [];\n if (decodedFrames && decodedFrames.frames.length !== 0) {\n frames = decodedFrames.frames;\n }\n VERBOSE && console.log(`returning ${frames.length} decoded frames`);\n\n return { packet, frames };\n }\n\n async _getNextVideoStreamPacket(): Promise<null | Packet> {\n VERBOSE && console.log('_getNextVideoStreamPacket');\n\n let packet = await this.#demuxer.read();\n while (packet && packet.stream_index !== this.#streamIndex) {\n packet = await this.#demuxer.read();\n if (packet === null) {\n VERBOSE && console.log('no more packets');\n return null;\n }\n }\n VERBOSE && console.log('returning packet', !!packet, 'pts', packet?.pts, 'si', packet?.stream_index);\n return packet as Packet;\n }\n\n _setFrameDataToImageData(frame: beamcoder.Frame, target: Uint8ClampedArray) {\n const sourceLineSize = frame.linesize as unknown as number;\n // frame.data can contain multiple \"planes\" in other colorspaces, but in rgba, there is just one \"plane\", so\n // our data is in frame.data[0]\n const pixels = frame.data[0] as Uint8Array;\n\n // libav creates larger buffers because it makes their internal code simpler.\n // we have to trim a part at the right of each pixel row.\n\n for (let i = 0; i < frame.height; i++) {\n const sourceStart = i * sourceLineSize;\n const sourceEnd = sourceStart + frame.width * RGBA_PIXEL_SIZE;\n const sourceData = pixels.subarray(sourceStart, sourceEnd);\n const targetOffset = i * frame.width * RGBA_PIXEL_SIZE;\n target.set(sourceData, targetOffset);\n }\n }\n\n async dispose() {\n if (this.#decoder) {\n await this.#decoder.flush();\n this.#decoder = null;\n }\n this.#demuxer.forceClose();\n this.#filterer = null;\n this.#filteredFramesPacket = undefined;\n this.#frames = [];\n this.#packet = null;\n this.#previousTargetPTS = null;\n this.#streamIndex = 0;\n }\n}\n","import { DownloadVideoURL } from './DownloadVideoURL';\n\ninterface CachedResource {\n url: string;\n filepath: string | undefined;\n download(): Promise<void>;\n destroy(): void;\n}\n\ninterface CacheEntry {\n downloader: DownloadVideoURL;\n refCount: number;\n downloadPromise?: Promise<void>;\n}\n\nexport class CachedVideoDownloader {\n #cache: Map<string, CacheEntry> = new Map();\n\n get(url: string): CachedResource {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n let filepath: string | undefined;\n\n return {\n url,\n get filepath() {\n return filepath;\n },\n\n async download() {\n let entry = self.#cache.get(url);\n\n if (entry) {\n entry.refCount += 1;\n\n // Wait for in-progress download if exists\n if (entry.downloadPromise) {\n await entry.downloadPromise;\n }\n }\n else {\n const downloader = new DownloadVideoURL(url);\n\n const promise = downloader.download();\n entry = {\n downloader,\n refCount: 1,\n downloadPromise: promise,\n };\n self.#cache.set(url, entry);\n\n try {\n await promise;\n }\n finally {\n entry.downloadPromise = undefined; // Clear after completion\n }\n }\n\n filepath = self.#cache.get(url).downloader.filepath;\n },\n\n destroy() {\n const entry = self.#cache.get(url);\n if (!entry) return;\n\n entry.refCount -= 1;\n\n if (entry.refCount <= 0) {\n entry.downloader.clear();\n self.#cache.delete(url);\n }\n\n filepath = undefined;\n },\n };\n }\n}\n"],"names":["BaseExtractor","args","inputFileOrUrl","outputFile","threadCount","endTime","interpolateFps","interpolateMode","targetPts","targetTime","pts","onFrameAvailable","flush","DownloadVideoURL","url","__privateAdd","_url","_httpRequest","_filepath","_tmpObj","__privateSet","extension","path","tmp","__privateGet","source","response","contentType","writeStream","fs","readableStream","resolve","reject","Writable","e","RGBA_PIXEL_SIZE","createDecoder","demuxer","streamIndex","commonParams","beamcoder","createFilter","stream","outputPixelFormat","filterSpec","filterSpecStr","STREAM_TYPE_VIDEO","COLORSPACE_RGBA","MAX_RECURSION","_BeamcoderExtractor","_createDecoder","_demuxer","_decoder","_filterer","_filteredFramesPacket","_frames","_packet","_previousTargetPTS","_threadCount","_streamIndex","_packetReadCount","_recursiveReadCount","extractor","downloadUrl","target","frame","rawData","time","time_base","targetPTS","SeekPTSOffset","RE_SEEK_THRESHOLD","hasFrameWithinThreshold","__privateMethod","createDecoder_fn","filteredFrames","closestFramePTS","outputFrame","closestFrame","f","nextFrame","__privateWrapper","r","TIME_OFFSET","PTSOffset","packet","decodedFrames","frames","sourceLineSize","pixels","i","sourceStart","sourceEnd","sourceData","targetOffset","BeamcoderExtractor","CachedVideoDownloader","_cache","self","filepath","entry","downloader","promise"],"mappings":"koBAQO,MAAMA,CAAmC,CAC5C,aAAa,OAAOC,EAAyC,CACnD,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,KAAK,CACP,eAAAC,EACA,WAAAC,EACA,YAAAC,EAAc,EACd,QAAAC,EACA,eAAAC,EACA,gBAAAC,CAAA,EAC6B,CACvB,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,IAAI,UAAmB,CACb,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,IAAI,OAAgB,CACV,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,IAAI,QAAiB,CACX,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,UAAUC,EAAmB,CACzB,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,eAAeC,EAAoC,CAC/C,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,mBAAmBA,EAAwC,CACvD,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,cAAcD,EAAmC,CAC7C,MAAA,IAAI,MAAM,iBAAiB,CACrC,CAEA,MAAM,WAAWC,EAAoB,CAC3B,MAAA,IAAI,MAAM,iBAAiB,CACrC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E,EAAA0E,EAAAD,GAAO,IAAInF,EAAKsF,CAAK,EAEtB,GAAA,CACM,MAAAE,CAAA,QAEV,CACIF,EAAM,gBAAkB,MAC5B,EAGJD,EAAW3E,EAAA0E,EAAKD,GAAO,IAAInF,CAAG,EAAE,WAAW,QAC/C,EAEA,SAAU,CACN,MAAMsF,EAAQ5E,EAAA0E,EAAKD,GAAO,IAAInF,CAAG,EAC5BsF,IAELA,EAAM,UAAY,EAEdA,EAAM,UAAY,IAClBA,EAAM,WAAW,QACZ5E,EAAA0E,EAAAD,GAAO,OAAOnF,CAAG,GAGfqF,EAAA,OACf,CAAA,CAER,CACJ,CA9DIF,EAAA"}
|
package/dist/framefusion.es.js
CHANGED
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
var
|
|
1
|
+
var $ = (a, i, t) => {
|
|
2
2
|
if (!i.has(a))
|
|
3
3
|
throw TypeError("Cannot " + t);
|
|
4
4
|
};
|
|
5
|
-
var e = (a, i, t) => (
|
|
5
|
+
var e = (a, i, t) => ($(a, i, "read from private field"), t ? t.call(a) : i.get(a)), c = (a, i, t) => {
|
|
6
6
|
if (i.has(a))
|
|
7
7
|
throw TypeError("Cannot add the same private member more than once");
|
|
8
8
|
i instanceof WeakSet ? i.add(a) : i.set(a, t);
|
|
9
|
-
}, o = (a, i, t, s) => (
|
|
9
|
+
}, o = (a, i, t, s) => ($(a, i, "write to private field"), s ? s.call(a, t) : i.set(a, t), t), v = (a, i, t, s) => ({
|
|
10
10
|
set _(r) {
|
|
11
11
|
o(a, i, r, t);
|
|
12
12
|
},
|
|
13
13
|
get _() {
|
|
14
14
|
return e(a, i, s);
|
|
15
15
|
}
|
|
16
|
-
}),
|
|
17
|
-
import
|
|
18
|
-
import
|
|
19
|
-
import
|
|
20
|
-
import z from "
|
|
21
|
-
import G from "
|
|
22
|
-
|
|
23
|
-
class K {
|
|
16
|
+
}), W = (a, i, t) => ($(a, i, "access private method"), t);
|
|
17
|
+
import C from "@lumen5/beamcoder";
|
|
18
|
+
import j from "path";
|
|
19
|
+
import { Writable as q } from "stream";
|
|
20
|
+
import z from "tmp";
|
|
21
|
+
import G from "fs-extra";
|
|
22
|
+
class H {
|
|
24
23
|
static async create(i) {
|
|
25
24
|
throw new Error("Not implemented");
|
|
26
25
|
}
|
|
@@ -29,8 +28,8 @@ class K {
|
|
|
29
28
|
outputFile: t,
|
|
30
29
|
threadCount: s = 8,
|
|
31
30
|
endTime: r,
|
|
32
|
-
interpolateFps:
|
|
33
|
-
interpolateMode:
|
|
31
|
+
interpolateFps: h,
|
|
32
|
+
interpolateMode: n
|
|
34
33
|
}) {
|
|
35
34
|
throw new Error("Not implemented");
|
|
36
35
|
}
|
|
@@ -77,60 +76,54 @@ class K {
|
|
|
77
76
|
throw new Error("Not implemented");
|
|
78
77
|
}
|
|
79
78
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
var P, N, F, _;
|
|
83
|
-
class q {
|
|
79
|
+
var P, D, F, _;
|
|
80
|
+
class B {
|
|
84
81
|
constructor(i) {
|
|
85
82
|
c(this, P, void 0);
|
|
86
|
-
c(this,
|
|
83
|
+
c(this, D, void 0);
|
|
87
84
|
c(this, F, void 0);
|
|
88
85
|
c(this, _, void 0);
|
|
89
86
|
o(this, P, i);
|
|
90
|
-
const t =
|
|
91
|
-
o(this, _,
|
|
87
|
+
const t = j.extname(i);
|
|
88
|
+
o(this, _, z.fileSync({ postfix: t })), o(this, F, e(this, _).name);
|
|
92
89
|
}
|
|
93
90
|
/**
|
|
94
|
-
|
|
95
|
-
|
|
91
|
+
* returns the filepath of the downloaded file. If the file has not been downloaded yet, it will be undefined
|
|
92
|
+
*/
|
|
96
93
|
get filepath() {
|
|
97
94
|
return e(this, F);
|
|
98
95
|
}
|
|
99
96
|
/**
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
* Downloads the file from the given URL. The file will be downloaded to a temporary file.
|
|
98
|
+
*/
|
|
102
99
|
async download() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
});
|
|
123
|
-
} catch (r) {
|
|
124
|
-
t(r);
|
|
125
|
-
}
|
|
100
|
+
const i = e(this, P), t = await fetch(i);
|
|
101
|
+
if (!t.ok)
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Failed to fetch ${i}, status: ${t.status}`
|
|
104
|
+
);
|
|
105
|
+
const s = t.headers.get("content-type");
|
|
106
|
+
if (!s || !s.includes("video"))
|
|
107
|
+
throw new Error(
|
|
108
|
+
`Source ${i}, returned unsupported content type ${s}`
|
|
109
|
+
);
|
|
110
|
+
const r = G.createWriteStream(e(this, _).name), h = t.body;
|
|
111
|
+
if (!h)
|
|
112
|
+
throw new Error(`Response body is null for ${i}`);
|
|
113
|
+
return new Promise((n, y) => {
|
|
114
|
+
h.pipeTo(q.toWeb(r)), r.on("finish", () => {
|
|
115
|
+
r.close(), o(this, F, e(this, _).name), n();
|
|
116
|
+
}), r.on("error", (p) => {
|
|
117
|
+
y(p);
|
|
118
|
+
});
|
|
126
119
|
});
|
|
127
120
|
}
|
|
128
121
|
clear() {
|
|
129
|
-
e(this, _) && e(this, _).removeCallback(), e(this, P) && o(this, P, void 0), e(this,
|
|
122
|
+
e(this, _) && e(this, _).removeCallback(), e(this, P) && o(this, P, void 0), e(this, D) && o(this, D, null), e(this, F) && o(this, F, void 0);
|
|
130
123
|
}
|
|
131
124
|
}
|
|
132
|
-
P = new WeakMap(),
|
|
133
|
-
const
|
|
125
|
+
P = new WeakMap(), D = new WeakMap(), F = new WeakMap(), _ = new WeakMap();
|
|
126
|
+
const I = 4, X = ({
|
|
134
127
|
demuxer: a,
|
|
135
128
|
streamIndex: i,
|
|
136
129
|
threadCount: t
|
|
@@ -141,18 +134,18 @@ const M = 4, Z = ({
|
|
|
141
134
|
pix_fmt: a.streams[i].codecpar.format,
|
|
142
135
|
thread_count: t
|
|
143
136
|
};
|
|
144
|
-
return a.streams[i].codecpar.name === "vp8" ?
|
|
137
|
+
return a.streams[i].codecpar.name === "vp8" ? C.decoder({
|
|
145
138
|
...s,
|
|
146
139
|
name: "libvpx"
|
|
147
|
-
}) : a.streams[i].codecpar.name === "vp9" ?
|
|
140
|
+
}) : a.streams[i].codecpar.name === "vp9" ? C.decoder({
|
|
148
141
|
...s,
|
|
149
142
|
name: "libvpx-vp9"
|
|
150
|
-
}) :
|
|
143
|
+
}) : C.decoder({
|
|
151
144
|
...s,
|
|
152
145
|
demuxer: a,
|
|
153
146
|
stream_index: i
|
|
154
147
|
});
|
|
155
|
-
},
|
|
148
|
+
}, K = async ({
|
|
156
149
|
stream: a,
|
|
157
150
|
outputPixelFormat: i,
|
|
158
151
|
interpolateFps: t,
|
|
@@ -168,8 +161,8 @@ const M = 4, Z = ({
|
|
|
168
161
|
r = [...r, `fps=${t}`];
|
|
169
162
|
else
|
|
170
163
|
throw new Error(`Unexpected interpolation mode: ${s}`);
|
|
171
|
-
const
|
|
172
|
-
return
|
|
164
|
+
const h = r.join(", ") + "[out0:v]";
|
|
165
|
+
return C.filterer({
|
|
173
166
|
filterType: "video",
|
|
174
167
|
inputParams: [
|
|
175
168
|
{
|
|
@@ -187,11 +180,11 @@ const M = 4, Z = ({
|
|
|
187
180
|
pixelFormat: i
|
|
188
181
|
}
|
|
189
182
|
],
|
|
190
|
-
filterSpec:
|
|
183
|
+
filterSpec: h
|
|
191
184
|
});
|
|
192
|
-
},
|
|
193
|
-
var l, d,
|
|
194
|
-
const
|
|
185
|
+
}, L = "video", Y = "rgba", Z = 5;
|
|
186
|
+
var l, d, x, u, g, E, T, R, f, b, N, S, O;
|
|
187
|
+
const M = class extends H {
|
|
195
188
|
constructor() {
|
|
196
189
|
super(...arguments);
|
|
197
190
|
c(this, S);
|
|
@@ -207,7 +200,7 @@ const $ = class extends K {
|
|
|
207
200
|
* Packets can be filtered to change colorspace, fps and add various effects. If there are no colorspace changes or
|
|
208
201
|
* filters, filter might not be necessary.
|
|
209
202
|
*/
|
|
210
|
-
c(this,
|
|
203
|
+
c(this, x, null);
|
|
211
204
|
/**
|
|
212
205
|
* This is where we store filtered frames from each previously processed packet.
|
|
213
206
|
* We keep these in chronological order. We hang on to them for two reasons:
|
|
@@ -215,12 +208,12 @@ const $ = class extends K {
|
|
|
215
208
|
* 2. so we can return frames close the end of the stream. When such a frame is requested we have to flush (destroy)
|
|
216
209
|
* the encoder to get the last few frames. This avoids having to re-create an encoder.
|
|
217
210
|
*/
|
|
218
|
-
c(this,
|
|
211
|
+
c(this, u, []);
|
|
219
212
|
/**
|
|
220
213
|
* This contains the last raw frames we read from the demuxer. We use it as a starting point for each new query. We
|
|
221
214
|
* do this ensure we don't skip any frames.
|
|
222
215
|
*/
|
|
223
|
-
c(this,
|
|
216
|
+
c(this, g, []);
|
|
224
217
|
/**
|
|
225
218
|
* This contains the last packet we read from the demuxer. We use it as a starting point for each new query. We do
|
|
226
219
|
* this ensure we don't skip any frames.
|
|
@@ -238,24 +231,24 @@ const $ = class extends K {
|
|
|
238
231
|
/**
|
|
239
232
|
* The index of the video stream in the demuxer
|
|
240
233
|
*/
|
|
241
|
-
c(this,
|
|
234
|
+
c(this, f, 0);
|
|
242
235
|
/**
|
|
243
236
|
* The number of packets we've read from the demuxer to complete the frame query
|
|
244
237
|
* @private
|
|
245
238
|
*/
|
|
246
|
-
c(this,
|
|
239
|
+
c(this, b, 0);
|
|
247
240
|
/**
|
|
248
241
|
* The number of times we've recursively read packets from the demuxer to complete the frame query
|
|
249
242
|
* @private
|
|
250
243
|
*/
|
|
251
|
-
c(this,
|
|
244
|
+
c(this, N, 0);
|
|
252
245
|
}
|
|
253
246
|
/**
|
|
254
247
|
* Encoder/Decoder construction is async, so it can't be put in a regular constructor.
|
|
255
248
|
* Use and await this method to generate an extractor.
|
|
256
249
|
*/
|
|
257
250
|
static async create(t) {
|
|
258
|
-
const s = new
|
|
251
|
+
const s = new M();
|
|
259
252
|
return await s.init(t), s;
|
|
260
253
|
}
|
|
261
254
|
async init({
|
|
@@ -263,34 +256,34 @@ const $ = class extends K {
|
|
|
263
256
|
threadCount: s = 8
|
|
264
257
|
}) {
|
|
265
258
|
if (o(this, R, s), t.startsWith("http")) {
|
|
266
|
-
const r = new
|
|
259
|
+
const r = new B(t);
|
|
267
260
|
await r.download(), t = r.filepath;
|
|
268
261
|
}
|
|
269
|
-
if (t.startsWith("file:") || (t = "file:" + t), o(this, l, await
|
|
270
|
-
throw new Error(`File has no ${
|
|
271
|
-
o(this,
|
|
272
|
-
stream: e(this, l).streams[e(this,
|
|
273
|
-
outputPixelFormat:
|
|
262
|
+
if (t.startsWith("file:") || (t = "file:" + t), o(this, l, await C.demuxer(t)), o(this, f, e(this, l).streams.findIndex((r) => r.codecpar.codec_type === L)), e(this, f) === -1)
|
|
263
|
+
throw new Error(`File has no ${L} stream!`);
|
|
264
|
+
o(this, x, await K({
|
|
265
|
+
stream: e(this, l).streams[e(this, f)],
|
|
266
|
+
outputPixelFormat: Y
|
|
274
267
|
}));
|
|
275
268
|
}
|
|
276
269
|
/**
|
|
277
270
|
* This is the duration of the first video stream in the file expressed in seconds.
|
|
278
271
|
*/
|
|
279
272
|
get duration() {
|
|
280
|
-
const t = e(this, l).streams[e(this,
|
|
273
|
+
const t = e(this, l).streams[e(this, f)];
|
|
281
274
|
return t.duration !== null ? this.ptsToTime(t.duration) : this.ptsToTime(e(this, l).duration) / 1e3;
|
|
282
275
|
}
|
|
283
276
|
/**
|
|
284
277
|
* Width in pixels
|
|
285
278
|
*/
|
|
286
279
|
get width() {
|
|
287
|
-
return e(this, l).streams[e(this,
|
|
280
|
+
return e(this, l).streams[e(this, f)].codecpar.width;
|
|
288
281
|
}
|
|
289
282
|
/**
|
|
290
283
|
* Height in pixels
|
|
291
284
|
*/
|
|
292
285
|
get height() {
|
|
293
|
-
return e(this, l).streams[e(this,
|
|
286
|
+
return e(this, l).streams[e(this, f)].codecpar.height;
|
|
294
287
|
}
|
|
295
288
|
/**
|
|
296
289
|
* Get the beamcoder Frame for a given time in seconds
|
|
@@ -305,32 +298,32 @@ const $ = class extends K {
|
|
|
305
298
|
* @param targetTime
|
|
306
299
|
*/
|
|
307
300
|
async getImageDataAtTime(t, s) {
|
|
308
|
-
const r = Math.round(this._timeToPTS(t)),
|
|
309
|
-
if (!
|
|
301
|
+
const r = Math.round(this._timeToPTS(t)), h = await this._getFrameAtPts(r);
|
|
302
|
+
if (!h)
|
|
310
303
|
return null;
|
|
311
|
-
let
|
|
312
|
-
return s || (
|
|
313
|
-
data:
|
|
314
|
-
width:
|
|
315
|
-
height:
|
|
304
|
+
let n = s;
|
|
305
|
+
return s || (n = new Uint8ClampedArray(h.width * h.height * I)), this._setFrameDataToImageData(h, n), {
|
|
306
|
+
data: n,
|
|
307
|
+
width: h.width,
|
|
308
|
+
height: h.height
|
|
316
309
|
};
|
|
317
310
|
}
|
|
318
311
|
/**
|
|
319
312
|
* Get the presentation timestamp (PTS) for a given time in seconds
|
|
320
313
|
*/
|
|
321
314
|
_timeToPTS(t) {
|
|
322
|
-
const s = e(this, l).streams[e(this,
|
|
315
|
+
const s = e(this, l).streams[e(this, f)].time_base;
|
|
323
316
|
return t * s[1] / s[0];
|
|
324
317
|
}
|
|
325
318
|
/**
|
|
326
319
|
* Get the time in seconds from a given presentation timestamp (PTS)
|
|
327
320
|
*/
|
|
328
321
|
ptsToTime(t) {
|
|
329
|
-
const s = e(this, l).streams[e(this,
|
|
322
|
+
const s = e(this, l).streams[e(this, f)].time_base;
|
|
330
323
|
return t * s[0] / s[1];
|
|
331
324
|
}
|
|
332
325
|
get packetReadCount() {
|
|
333
|
-
return e(this,
|
|
326
|
+
return e(this, b);
|
|
334
327
|
}
|
|
335
328
|
/**
|
|
336
329
|
* Get the frame at the given presentation timestamp (PTS)
|
|
@@ -340,43 +333,43 @@ const $ = class extends K {
|
|
|
340
333
|
* additional packets and find a frame that is closer to the targetPTS.
|
|
341
334
|
*/
|
|
342
335
|
async _getFrameAtPts(t, s = 0) {
|
|
343
|
-
o(this,
|
|
344
|
-
const r = 3,
|
|
345
|
-
(e(this, T) === null || e(this, T) > t || !
|
|
336
|
+
o(this, b, 0);
|
|
337
|
+
const r = 3, h = e(this, u).flat().some((w) => this.ptsToTime(Math.abs(t - w.pts)) < r);
|
|
338
|
+
(e(this, T) === null || e(this, T) > t || !h) && (await e(this, l).seek({
|
|
346
339
|
stream_index: 0,
|
|
347
340
|
// even though we specify the stream index, it still seeks all streams
|
|
348
341
|
timestamp: t + s,
|
|
349
342
|
any: !1
|
|
350
|
-
}), await
|
|
351
|
-
let
|
|
352
|
-
if (e(this,
|
|
353
|
-
const
|
|
354
|
-
if (
|
|
355
|
-
const
|
|
356
|
-
if (
|
|
357
|
-
return o(this, T, t),
|
|
343
|
+
}), await W(this, S, O).call(this), o(this, E, null), o(this, g, []), o(this, T, t), o(this, u, []));
|
|
344
|
+
let n = null, y = -1, p = null;
|
|
345
|
+
if (e(this, u).length > 0) {
|
|
346
|
+
const w = e(this, u).flat().find((m) => m.pts <= t);
|
|
347
|
+
if (w) {
|
|
348
|
+
const m = e(this, u).flat().find((k) => k.pts > w.pts);
|
|
349
|
+
if (y = w.pts, p = w, m && m.pts > t || y === t)
|
|
350
|
+
return o(this, T, t), p;
|
|
358
351
|
}
|
|
359
352
|
}
|
|
360
|
-
for (!e(this, E) && e(this,
|
|
361
|
-
if (e(this,
|
|
362
|
-
|
|
363
|
-
const
|
|
364
|
-
if (!
|
|
365
|
-
return
|
|
366
|
-
if (e(this,
|
|
367
|
-
o(this, T, t),
|
|
353
|
+
for (!e(this, E) && e(this, g).length === 0 && ({ packet: v(this, E)._, frames: v(this, g)._ } = await this._getNextPacketAndDecodeFrames(), v(this, b)._++); (e(this, E) || e(this, g).length !== 0) && y < t; ) {
|
|
354
|
+
if (e(this, g).length !== 0) {
|
|
355
|
+
n = (await e(this, x).filter([{ name: "in0:v", frames: e(this, g) }])).flatMap((k) => k.frames);
|
|
356
|
+
const m = e(this, b) === 1 && n[0].pts > t ? n[0] : n.reverse().find((k) => k.pts <= t);
|
|
357
|
+
if (!m)
|
|
358
|
+
return p;
|
|
359
|
+
if (e(this, u).unshift(n), e(this, u).length > 2 && e(this, u).pop(), y = m == null ? void 0 : m.pts, !p || y <= t)
|
|
360
|
+
o(this, T, t), p = m;
|
|
368
361
|
else
|
|
369
362
|
break;
|
|
370
363
|
}
|
|
371
|
-
({ packet: v(this, E)._, frames: v(this,
|
|
364
|
+
({ packet: v(this, E)._, frames: v(this, g)._ } = await this._getNextPacketAndDecodeFrames()), v(this, b)._++;
|
|
372
365
|
}
|
|
373
|
-
if (!
|
|
374
|
-
if (
|
|
366
|
+
if (!p) {
|
|
367
|
+
if (Z < e(this, N))
|
|
375
368
|
throw Error("No matching frame found");
|
|
376
|
-
const
|
|
377
|
-
v(this,
|
|
369
|
+
const w = 0.1, m = this._timeToPTS(w);
|
|
370
|
+
v(this, N)._++, p = await this._getFrameAtPts(t, s - m), p && o(this, N, 0);
|
|
378
371
|
}
|
|
379
|
-
return
|
|
372
|
+
return p;
|
|
380
373
|
}
|
|
381
374
|
/**
|
|
382
375
|
* Get the next packet from the video stream and decode it into frames. Each frame has a presentation time stamp
|
|
@@ -392,32 +385,32 @@ const $ = class extends K {
|
|
|
392
385
|
}
|
|
393
386
|
async _getNextVideoStreamPacket() {
|
|
394
387
|
let t = await e(this, l).read();
|
|
395
|
-
for (; t && t.stream_index !== e(this,
|
|
388
|
+
for (; t && t.stream_index !== e(this, f); )
|
|
396
389
|
if (t = await e(this, l).read(), t === null)
|
|
397
390
|
return null;
|
|
398
391
|
return t;
|
|
399
392
|
}
|
|
400
393
|
_setFrameDataToImageData(t, s) {
|
|
401
|
-
const r = t.linesize,
|
|
402
|
-
for (let
|
|
403
|
-
const
|
|
404
|
-
s.set(
|
|
394
|
+
const r = t.linesize, h = t.data[0];
|
|
395
|
+
for (let n = 0; n < t.height; n++) {
|
|
396
|
+
const y = n * r, p = y + t.width * I, w = h.subarray(y, p), m = n * t.width * I;
|
|
397
|
+
s.set(w, m);
|
|
405
398
|
}
|
|
406
399
|
}
|
|
407
400
|
async dispose() {
|
|
408
|
-
e(this, d) && (await e(this, d).flush(), o(this, d, null)), e(this, l).forceClose(), o(this,
|
|
401
|
+
e(this, d) && (await e(this, d).flush(), o(this, d, null)), e(this, l).forceClose(), o(this, x, null), o(this, u, void 0), o(this, g, []), o(this, E, null), o(this, T, null), o(this, f, 0);
|
|
409
402
|
}
|
|
410
403
|
};
|
|
411
|
-
let
|
|
412
|
-
l = new WeakMap(), d = new WeakMap(),
|
|
413
|
-
e(this, d) && (await e(this, d).flush(), o(this, d, null)), o(this, d,
|
|
404
|
+
let V = M;
|
|
405
|
+
l = new WeakMap(), d = new WeakMap(), x = new WeakMap(), u = new WeakMap(), g = new WeakMap(), E = new WeakMap(), T = new WeakMap(), R = new WeakMap(), f = new WeakMap(), b = new WeakMap(), N = new WeakMap(), S = new WeakSet(), O = async function() {
|
|
406
|
+
e(this, d) && (await e(this, d).flush(), o(this, d, null)), o(this, d, X({
|
|
414
407
|
demuxer: e(this, l),
|
|
415
|
-
streamIndex: e(this,
|
|
408
|
+
streamIndex: e(this, f),
|
|
416
409
|
threadCount: e(this, R)
|
|
417
410
|
}));
|
|
418
411
|
};
|
|
419
412
|
var A;
|
|
420
|
-
class
|
|
413
|
+
class st {
|
|
421
414
|
constructor() {
|
|
422
415
|
c(this, A, /* @__PURE__ */ new Map());
|
|
423
416
|
}
|
|
@@ -434,14 +427,14 @@ class ot {
|
|
|
434
427
|
if (r)
|
|
435
428
|
r.refCount += 1, r.downloadPromise && await r.downloadPromise;
|
|
436
429
|
else {
|
|
437
|
-
const
|
|
430
|
+
const h = new B(i), n = h.download();
|
|
438
431
|
r = {
|
|
439
|
-
downloader:
|
|
432
|
+
downloader: h,
|
|
440
433
|
refCount: 1,
|
|
441
|
-
downloadPromise:
|
|
434
|
+
downloadPromise: n
|
|
442
435
|
}, e(t, A).set(i, r);
|
|
443
436
|
try {
|
|
444
|
-
await
|
|
437
|
+
await n;
|
|
445
438
|
} finally {
|
|
446
439
|
r.downloadPromise = void 0;
|
|
447
440
|
}
|
|
@@ -457,7 +450,7 @@ class ot {
|
|
|
457
450
|
}
|
|
458
451
|
A = new WeakMap();
|
|
459
452
|
export {
|
|
460
|
-
|
|
461
|
-
|
|
453
|
+
V as BeamcoderExtractor,
|
|
454
|
+
st as CachedVideoDownloader
|
|
462
455
|
};
|
|
463
456
|
//# sourceMappingURL=framefusion.es.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"framefusion.es.js","sources":["../src/BaseExtractor.ts","../src/DownloadVideoURL.ts","../src/backends/beamcoder.ts","../src/cachedVideoDownloader.ts"],"sourcesContent":["import type {\n ExtractorArgs,\n Frame,\n Extractor\n} from '../framefusion';\nimport type { ImageData } from './types';\n\n\nexport class BaseExtractor implements Extractor {\n static async create(args: ExtractorArgs): Promise<Extractor> {\n throw new Error('Not implemented');\n }\n\n async init({\n inputFileOrUrl,\n outputFile,\n threadCount = 8,\n endTime,\n interpolateFps,\n interpolateMode,\n }: ExtractorArgs): Promise<void> {\n throw new Error('Not implemented');\n }\n\n get duration(): number {\n throw new Error('Not implemented');\n }\n\n get width(): number {\n throw new Error('Not implemented');\n }\n\n get height(): number {\n throw new Error('Not implemented');\n }\n\n async seekToPTS(targetPts: number) {\n throw new Error('Not implemented');\n }\n\n async getFrameAtTime(targetTime: number): Promise<Frame> {\n throw new Error('Not implemented');\n }\n\n async getImageDataAtTime(targetTime: number): Promise<ImageData> {\n throw new Error('Not implemented');\n }\n\n async getFrameAtPts(targetPts: number): Promise<Frame> {\n throw new Error('Not implemented');\n }\n\n async seekToTime(targetTime: number) {\n throw new Error('Not implemented');\n }\n\n /**\n * Convert a PTS (based on timebase) to PTS (in seconds)\n */\n ptsToTime(pts: number) {\n throw new Error('Not implemented');\n }\n\n async readFrames({\n onFrameAvailable,\n flush = true,\n }: {\n /**\n * Return true if we need to read more frames.\n */\n onFrameAvailable?: (frame: Frame) => Promise<boolean> | boolean;\n flush?: boolean;\n } = {\n flush: true,\n onFrameAvailable: () => true,\n }) {\n throw new Error('Not implemented');\n }\n\n async dispose() {\n throw new Error('Not implemented');\n }\n}\n","import path from 'path';\nimport https from 'node:https';\nimport type { ClientRequest } from 'http';\nimport http from 'http';\nimport tmp from 'tmp';\nimport fs from 'fs-extra';\n\nclass CancelRequestError extends Error { }\n\n/**\n * Downloads a video file from a given URL as a temporary file. When the object is cleared, the temporary file is\n * deleted.\n */\nexport class DownloadVideoURL {\n #url: string | undefined;\n #httpRequest: ClientRequest | undefined = undefined;\n #filepath: string;\n #tmpObj: tmp.FileResult | undefined = undefined;\n\n constructor(url: string) {\n this.#url = url;\n\n const extension = path.extname(url);\n this.#tmpObj = tmp.fileSync({ postfix: extension });\n this.#filepath = this.#tmpObj.name;\n }\n\n /**\n * returns the filepath of the downloaded file. If the file has not been downloaded yet, it will be undefined\n */\n get filepath() {\n return this.#filepath;\n }\n\n /**\n * Downloads the file from the given URL. The file will be downloaded to a temporary file.\n */\n async download() {\n await new Promise<void>((resolve, reject) => {\n const source = this.#url;\n try {\n const connectionHandler = source.startsWith('https://') ? https : http;\n this.#httpRequest = connectionHandler.get(source, (res) => {\n const contentType = res.headers['content-type'];\n if (!contentType.includes('video')) {\n const err = new Error(`Source ${source}, returned unsupported content type ${contentType}`);\n reject(err);\n return;\n }\n const writeStream = fs.createWriteStream(this.#tmpObj.name);\n res.pipe(writeStream);\n writeStream.on('finish', () => {\n writeStream.close();\n this.#filepath = this.#tmpObj.name;\n resolve();\n });\n writeStream.on('error', (e) => {\n reject(e);\n });\n });\n this.#httpRequest.on('error', (e) => {\n if (e instanceof CancelRequestError) {\n return;\n }\n reject(e);\n });\n }\n catch (e) {\n reject(e);\n }\n });\n }\n\n clear() {\n if (this.#tmpObj) this.#tmpObj.removeCallback();\n if (this.#url) this.#url = undefined;\n if (this.#httpRequest) this.#httpRequest = null;\n if (this.#filepath) this.#filepath = undefined;\n }\n}\n","import type {\n Packet,\n Demuxer,\n Decoder,\n Filterer,\n Frame\n} from '@lumen5/beamcoder';\nimport beamcoder from '@lumen5/beamcoder';\nimport type { ImageData } from '../types';\nimport { BaseExtractor } from '../BaseExtractor';\nimport type { Extractor, ExtractorArgs, InterpolateMode } from '../../framefusion';\nimport { DownloadVideoURL } from '../DownloadVideoURL';\n\nconst VERBOSE = false;\n\n/**\n * RGBA format need one byte for every components: r, g, b and a\n */\nconst RGBA_PIXEL_SIZE = 4;\n\nconst createDecoder = ({\n demuxer,\n streamIndex,\n threadCount,\n}: {\n demuxer: Demuxer;\n streamIndex: number;\n threadCount: number;\n}): Decoder => {\n const commonParams = {\n width: demuxer.streams[streamIndex].codecpar.width,\n height: demuxer.streams[streamIndex].codecpar.height,\n pix_fmt: demuxer.streams[streamIndex].codecpar.format,\n thread_count: threadCount,\n };\n\n if (demuxer.streams[streamIndex].codecpar.name === 'vp8') {\n return beamcoder.decoder({\n ...commonParams,\n name: 'libvpx',\n });\n }\n\n if (demuxer.streams[streamIndex].codecpar.name === 'vp9') {\n return beamcoder.decoder({\n ...commonParams,\n name: 'libvpx-vp9',\n });\n }\n\n return beamcoder.decoder({\n ...commonParams,\n demuxer: demuxer,\n stream_index: streamIndex,\n });\n};\n\n/**\n * A filter to convert between color spaces.\n * An example would be YUV to RGB, for mp4 to png conversion.\n */\nconst createFilter = async({\n stream,\n outputPixelFormat,\n interpolateFps,\n interpolateMode = 'fast',\n}: {\n stream: beamcoder.Stream;\n outputPixelFormat: string;\n interpolateFps?: number;\n interpolateMode?: InterpolateMode;\n}): Promise<beamcoder.Filterer> => {\n if (!stream.codecpar.format) {\n return null;\n }\n\n let filterSpec = [`[in0:v]format=${stream.codecpar.format}`];\n\n if (interpolateFps) {\n if (interpolateMode === 'high-quality') {\n filterSpec = [...filterSpec, `minterpolate=fps=${interpolateFps}`];\n }\n else if (interpolateMode === 'fast') {\n filterSpec = [...filterSpec, `fps=${interpolateFps}`];\n }\n else {\n throw new Error(`Unexpected interpolation mode: ${interpolateMode}`);\n }\n }\n\n const filterSpecStr = filterSpec.join(', ') + '[out0:v]';\n\n VERBOSE && console.log(`filterSpec: ${filterSpecStr}`);\n\n return beamcoder.filterer({\n filterType: 'video',\n inputParams: [\n {\n name: 'in0:v',\n width: stream.codecpar.width,\n height: stream.codecpar.height,\n pixelFormat: stream.codecpar.format,\n timeBase: stream.time_base,\n pixelAspect: stream.sample_aspect_ratio,\n },\n ],\n outputParams: [\n {\n name: 'out0:v',\n pixelFormat: outputPixelFormat,\n },\n ],\n filterSpec: filterSpecStr,\n });\n};\n\nconst STREAM_TYPE_VIDEO = 'video';\nconst COLORSPACE_RGBA = 'rgba';\nconst MAX_RECURSION = 5;\n\n/**\n * A simple extractor that uses beamcoder to extract frames from a video file.\n */\nexport class BeamcoderExtractor extends BaseExtractor implements Extractor {\n /**\n * The demuxer reads the file and outputs packet streams\n */\n #demuxer: Demuxer = null;\n\n /**\n * The decoder reads packets and can output raw frame data\n */\n #decoder: Decoder = null;\n\n /**\n * Packets can be filtered to change colorspace, fps and add various effects. If there are no colorspace changes or\n * filters, filter might not be necessary.\n */\n #filterer: Filterer = null;\n\n /**\n * This is where we store filtered frames from each previously processed packet.\n * We keep these in chronological order. We hang on to them for two reasons:\n * 1. so we can return them if we get a request for the same time again\n * 2. so we can return frames close the end of the stream. When such a frame is requested we have to flush (destroy)\n * the encoder to get the last few frames. This avoids having to re-create an encoder.\n */\n #filteredFramesPacket: undefined[] | Array<Array<Frame>> = [];\n\n /**\n * This contains the last raw frames we read from the demuxer. We use it as a starting point for each new query. We\n * do this ensure we don't skip any frames.\n */\n #frames = [];\n\n /**\n * This contains the last packet we read from the demuxer. We use it as a starting point for each new query. We do\n * this ensure we don't skip any frames.\n */\n #packet: null | Packet = null;\n\n /**\n * The last target presentation timestamp (PTS) we requested. If we never requested a time(stamp) then this\n * value is null\n */\n #previousTargetPTS: null | number = null;\n\n /**\n * The number of threads to use for decoding\n */\n #threadCount = 8;\n\n /**\n * The index of the video stream in the demuxer\n */\n #streamIndex = 0;\n\n /**\n * The number of packets we've read from the demuxer to complete the frame query\n * @private\n */\n #packetReadCount = 0;\n\n /**\n * The number of times we've recursively read packets from the demuxer to complete the frame query\n * @private\n */\n #recursiveReadCount = 0;\n\n /**\n * Encoder/Decoder construction is async, so it can't be put in a regular constructor.\n * Use and await this method to generate an extractor.\n */\n static async create(args: ExtractorArgs): Promise<BeamcoderExtractor> {\n const extractor = new BeamcoderExtractor();\n await extractor.init(args);\n return extractor;\n }\n\n async init({\n inputFileOrUrl,\n threadCount = 8,\n }: ExtractorArgs): Promise<void> {\n this.#threadCount = threadCount;\n if (inputFileOrUrl.startsWith('http')) {\n VERBOSE && console.log('downloading url', inputFileOrUrl);\n const downloadUrl = new DownloadVideoURL(inputFileOrUrl);\n await downloadUrl.download();\n inputFileOrUrl = downloadUrl.filepath;\n VERBOSE && console.log('finished downloading');\n }\n // Assume file url at this point\n if (!inputFileOrUrl.startsWith('file:')) {\n inputFileOrUrl = 'file:' + inputFileOrUrl;\n }\n this.#demuxer = await beamcoder.demuxer(inputFileOrUrl);\n this.#streamIndex = this.#demuxer.streams.findIndex(stream => stream.codecpar.codec_type === STREAM_TYPE_VIDEO);\n\n if (this.#streamIndex === -1) {\n throw new Error(`File has no ${STREAM_TYPE_VIDEO} stream!`);\n }\n this.#filterer = await createFilter({\n stream: this.#demuxer.streams[this.#streamIndex],\n outputPixelFormat: COLORSPACE_RGBA,\n });\n }\n\n async #createDecoder() {\n // It's possible that we need to create decoder multiple times during the lifecycle of this extractor so we\n // need to make sure we destroy the old one first if it exists\n if (this.#decoder) {\n await this.#decoder.flush();\n this.#decoder = null;\n }\n this.#decoder = createDecoder({\n demuxer: this.#demuxer as Demuxer,\n streamIndex: this.#streamIndex,\n threadCount: this.#threadCount,\n });\n }\n\n /**\n * This is the duration of the first video stream in the file expressed in seconds.\n */\n get duration(): number {\n const stream = this.#demuxer.streams[this.#streamIndex];\n if (stream.duration !== null) {\n return this.ptsToTime(stream.duration);\n }\n return this.ptsToTime(this.#demuxer.duration) / 1000;\n }\n\n /**\n * Width in pixels\n */\n get width(): number {\n return this.#demuxer.streams[this.#streamIndex].codecpar.width;\n }\n\n /**\n * Height in pixels\n */\n get height(): number {\n return this.#demuxer.streams[this.#streamIndex].codecpar.height;\n }\n\n /**\n * Get the beamcoder Frame for a given time in seconds\n * @param targetTime\n */\n async getFrameAtTime(targetTime: number): Promise<beamcoder.Frame> {\n VERBOSE && console.log(`getFrameAtTime time(s)=${targetTime}`);\n const targetPts = Math.round(this._timeToPTS(targetTime));\n return this._getFrameAtPts(targetPts);\n }\n\n /**\n * Get imageData for a given time in seconds\n * @param targetTime\n */\n async getImageDataAtTime(targetTime: number, target?: Uint8ClampedArray): Promise<ImageData> {\n const targetPts = Math.round(this._timeToPTS(targetTime));\n VERBOSE && console.log('targetTime', targetTime, '-> targetPts', targetPts);\n const frame = await this._getFrameAtPts(targetPts);\n if (!frame) {\n VERBOSE && console.log('no frame found');\n return null;\n }\n\n let rawData = target;\n\n if (!target) {\n rawData = new Uint8ClampedArray(frame.width * frame.height * RGBA_PIXEL_SIZE);\n }\n\n this._setFrameDataToImageData(frame, rawData);\n\n return {\n data: rawData,\n width: frame.width,\n height: frame.height,\n };\n }\n\n /**\n * Get the presentation timestamp (PTS) for a given time in seconds\n */\n _timeToPTS(time: number) {\n const time_base = this.#demuxer.streams[this.#streamIndex].time_base;\n return time * time_base[1] / time_base[0];\n }\n\n /**\n * Get the time in seconds from a given presentation timestamp (PTS)\n */\n ptsToTime(pts: number) {\n const time_base = this.#demuxer.streams[this.#streamIndex].time_base;\n return pts * time_base[0] / time_base[1];\n }\n\n get packetReadCount() {\n return this.#packetReadCount;\n }\n\n /**\n * Get the frame at the given presentation timestamp (PTS)\n * @param targetPTS - the target presentation timestamp (PTS) we want to retrieve\n * @param SeekPTSOffset - the offset to use when seeking to the targetPTS. This is used when we have trouble finding\n * the targetPTS. We use it to further move away from the requested PTS to find a frame. The allows use to read\n * additional packets and find a frame that is closer to the targetPTS.\n */\n async _getFrameAtPts(targetPTS: number, SeekPTSOffset = 0): Promise<beamcoder.Frame> {\n VERBOSE && console.log('_getFrameAtPts', targetPTS, 'seekPTSOffset', SeekPTSOffset, 'duration', this.duration);\n this.#packetReadCount = 0;\n\n // seek and create a decoder when retrieving a frame for the first time or when seeking backwards\n // we have to create a new decoder when seeking backwards as the decoder can only process frames in\n // chronological order.\n // RE_SEEK_DELTA: sometimes, we are looking for a frame so far ahead that it's better to drop everything and seek.\n // Example: when we got a frame a 0 and request a frame at t = 30s just after, we don't want to start reading all packets\n // until 30s.\n const RE_SEEK_THRESHOLD = 3; // 3 seconds - typically we have keyframes at shorter intervals\n const hasFrameWithinThreshold = this.#filteredFramesPacket.flat().some(frame => {\n return this.ptsToTime(Math.abs(targetPTS - (frame as Frame).pts)) < RE_SEEK_THRESHOLD;\n });\n VERBOSE && console.log('hasPreviousTargetPTS:', this.#previousTargetPTS === null, ', targetPTS is smaller:', this.#previousTargetPTS > targetPTS, ', has frame within threshold:', hasFrameWithinThreshold);\n if (this.#previousTargetPTS === null || this.#previousTargetPTS > targetPTS || !hasFrameWithinThreshold) {\n VERBOSE && console.log(`Seeking to ${targetPTS + SeekPTSOffset}`);\n\n await this.#demuxer.seek({\n stream_index: 0, // even though we specify the stream index, it still seeks all streams\n timestamp: targetPTS + SeekPTSOffset,\n any: false,\n });\n await this.#createDecoder();\n this.#packet = null;\n this.#frames = [];\n this.#previousTargetPTS = targetPTS;\n this.#filteredFramesPacket = [];\n }\n\n let filteredFrames = null;\n let closestFramePTS = -1;\n let outputFrame = null;\n\n // If we have previously filtered frames, get the frame closest to our targetPTS\n if (this.#filteredFramesPacket.length > 0) {\n const closestFrame = this.#filteredFramesPacket\n .flat()\n .find(f => (f as Frame).pts <= targetPTS) as Frame;\n\n if (closestFrame) {\n const nextFrame = this.#filteredFramesPacket\n .flat()\n .find(f => (f as Frame).pts > closestFrame.pts) as Frame;\n\n VERBOSE && console.log('returning previously filtered frame with pts', (closestFrame as Frame).pts);\n closestFramePTS = (closestFrame as Frame).pts;\n outputFrame = closestFrame;\n\n if ((nextFrame && nextFrame.pts > targetPTS) || (closestFramePTS === targetPTS)) {\n // We have a next frame, so we know the frame being displayed at targetPTS is the previous one,\n // which corresponds to outputFrame.\n this.#previousTargetPTS = targetPTS;\n return outputFrame;\n }\n }\n }\n\n // This is the first time we're decoding frames. Get the first packet and decode it.\n if (!this.#packet && this.#frames.length === 0) {\n ({ packet: this.#packet, frames: this.#frames } = await this._getNextPacketAndDecodeFrames());\n this.#packetReadCount++;\n }\n // Read packets until we have a frame which is closest to targetPTS\n while ((this.#packet || this.#frames.length !== 0) && closestFramePTS < targetPTS) {\n VERBOSE && console.log('packet si:', this.#packet?.stream_index, 'pts:', this.#packet?.pts, 'frames:', this.#frames?.length);\n VERBOSE && console.log('frames', this.#frames?.length, 'frames.pts:', JSON.stringify(this.#frames?.map(f => f.pts)), '-> target.pts:', targetPTS);\n\n // packet contains frames\n if (this.#frames.length !== 0) {\n // filter the frames\n const filteredResult = await this.#filterer.filter([{ name: 'in0:v', frames: this.#frames }]);\n filteredFrames = filteredResult.flatMap(r => r.frames);\n VERBOSE && console.log('filteredFrames', filteredFrames.length, 'filteredFrames.pts:', JSON.stringify(filteredFrames.map(f => f.pts)), '-> target.pts:', targetPTS);\n\n // get the closest frame to our target presentation timestamp (PTS)\n // Beamcoder returns decoded packet frames as follows: [1000, 2000, 3000, 4000]\n // If we're looking for a frame at 0, we want to return the frame at 1000\n // If we're looking for a frame at 2500, we want to return the frame at 2000\n const closestFrame = (this.#packetReadCount === 1 && filteredFrames[0].pts > targetPTS)\n ? filteredFrames[0]\n : filteredFrames.reverse().find(f => f.pts <= targetPTS);\n\n // The packet contains frames, but all of them have PTS larger than our a targetPTS (we looked too far)\n if (!closestFrame) {\n return outputFrame;\n }\n\n // store the filtered packet frames for later reuse\n this.#filteredFramesPacket.unshift(filteredFrames);\n if (this.#filteredFramesPacket.length > 2) {\n this.#filteredFramesPacket.pop();\n }\n\n closestFramePTS = closestFrame?.pts;\n VERBOSE && console.log('closestFramePTS', closestFramePTS, 'targetPTS', targetPTS);\n if (!outputFrame || closestFramePTS <= targetPTS) {\n VERBOSE && console.log('assigning outputFrame', closestFrame?.pts);\n this.#previousTargetPTS = targetPTS;\n outputFrame = closestFrame;\n }\n else {\n // break out of the loop if we've found the closest frame (and ensure we don't move to the next\n // packet by calling _getNextPacketAndDecodeFrames again) as this risks us getting a frame that is\n // after our targetPTS\n VERBOSE && console.log('breaking');\n break;\n }\n }\n // get the next packet and frames\n ({ packet: this.#packet, frames: this.#frames } = await this._getNextPacketAndDecodeFrames());\n\n // keep track of how many packets we've read\n this.#packetReadCount++;\n }\n\n // we read through all the available packets and frames, but we still don't have a frame. This can happen\n // when our targetPTS is to close to the end of the video. In this case, we'll try to seek further away from\n // the end of the video and try again. We've set up a MAX_RECURSION to prevent an infinite loop.\n if (!outputFrame) {\n if (MAX_RECURSION < this.#recursiveReadCount) {\n throw Error('No matching frame found');\n }\n const TIME_OFFSET = 0.1; // time offset in seconds\n const PTSOffset = this._timeToPTS(TIME_OFFSET);\n this.#recursiveReadCount++;\n outputFrame = await this._getFrameAtPts(targetPTS, SeekPTSOffset - PTSOffset);\n if (outputFrame) {\n this.#recursiveReadCount = 0;\n }\n }\n VERBOSE && console.log('read', this.packetReadCount, 'packets');\n\n return outputFrame;\n }\n\n /**\n * Get the next packet from the video stream and decode it into frames. Each frame has a presentation time stamp\n * (PTS). If we've reached the end of the stream and no more packets are available, we'll extract the last frames\n * from the decoder and destroy it.\n */\n async _getNextPacketAndDecodeFrames() {\n const packet = await this._getNextVideoStreamPacket();\n VERBOSE && console.log('packet pts:', packet?.pts);\n\n // extract frames from the packet\n let decodedFrames = null;\n if (packet !== null && this.#decoder) {\n decodedFrames = await this.#decoder.decode(packet as Packet);\n VERBOSE && console.log('decodedFrames', decodedFrames.frames.length, decodedFrames.frames.map(f => f.pts));\n }\n // we've reached the end of the stream\n else {\n if (this.#decoder) {\n VERBOSE && console.log('getting the last frames from the decoder');\n // flush the decoder -- this will return the last frames and destroy the decoder\n decodedFrames = await this.#decoder.flush();\n this.#decoder = null;\n }\n else {\n // we don't have a decoder, so we can't decode any more frames\n VERBOSE && console.log('no more frames to decode');\n }\n }\n\n let frames = [];\n if (decodedFrames && decodedFrames.frames.length !== 0) {\n frames = decodedFrames.frames;\n }\n VERBOSE && console.log(`returning ${frames.length} decoded frames`);\n\n return { packet, frames };\n }\n\n async _getNextVideoStreamPacket(): Promise<null | Packet> {\n VERBOSE && console.log('_getNextVideoStreamPacket');\n\n let packet = await this.#demuxer.read();\n while (packet && packet.stream_index !== this.#streamIndex) {\n packet = await this.#demuxer.read();\n if (packet === null) {\n VERBOSE && console.log('no more packets');\n return null;\n }\n }\n VERBOSE && console.log('returning packet', !!packet, 'pts', packet?.pts, 'si', packet?.stream_index);\n return packet as Packet;\n }\n\n _setFrameDataToImageData(frame: beamcoder.Frame, target: Uint8ClampedArray) {\n const sourceLineSize = frame.linesize as unknown as number;\n // frame.data can contain multiple \"planes\" in other colorspaces, but in rgba, there is just one \"plane\", so\n // our data is in frame.data[0]\n const pixels = frame.data[0] as Uint8Array;\n\n // libav creates larger buffers because it makes their internal code simpler.\n // we have to trim a part at the right of each pixel row.\n\n for (let i = 0; i < frame.height; i++) {\n const sourceStart = i * sourceLineSize;\n const sourceEnd = sourceStart + frame.width * RGBA_PIXEL_SIZE;\n const sourceData = pixels.subarray(sourceStart, sourceEnd);\n const targetOffset = i * frame.width * RGBA_PIXEL_SIZE;\n target.set(sourceData, targetOffset);\n }\n }\n\n async dispose() {\n if (this.#decoder) {\n await this.#decoder.flush();\n this.#decoder = null;\n }\n this.#demuxer.forceClose();\n this.#filterer = null;\n this.#filteredFramesPacket = undefined;\n this.#frames = [];\n this.#packet = null;\n this.#previousTargetPTS = null;\n this.#streamIndex = 0;\n }\n}\n","import { DownloadVideoURL } from './DownloadVideoURL';\n\ninterface CachedResource {\n url: string;\n filepath: string | undefined;\n download(): Promise<void>;\n destroy(): void;\n}\n\ninterface CacheEntry {\n downloader: DownloadVideoURL;\n refCount: number;\n downloadPromise?: Promise<void>;\n}\n\nexport class CachedVideoDownloader {\n #cache: Map<string, CacheEntry> = new Map();\n\n get(url: string): CachedResource {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n let filepath: string | undefined;\n\n return {\n url,\n get filepath() {\n return filepath;\n },\n\n async download() {\n let entry = self.#cache.get(url);\n\n if (entry) {\n entry.refCount += 1;\n\n // Wait for in-progress download if exists\n if (entry.downloadPromise) {\n await entry.downloadPromise;\n }\n }\n else {\n const downloader = new DownloadVideoURL(url);\n\n const promise = downloader.download();\n entry = {\n downloader,\n refCount: 1,\n downloadPromise: promise,\n };\n self.#cache.set(url, entry);\n\n try {\n await promise;\n }\n finally {\n entry.downloadPromise = undefined; // Clear after completion\n }\n }\n\n filepath = self.#cache.get(url).downloader.filepath;\n },\n\n destroy() {\n const entry = self.#cache.get(url);\n if (!entry) return;\n\n entry.refCount -= 1;\n\n if (entry.refCount <= 0) {\n entry.downloader.clear();\n self.#cache.delete(url);\n }\n\n filepath = undefined;\n },\n };\n }\n}\n"],"names":["BaseExtractor","args","inputFileOrUrl","outputFile","threadCount","endTime","interpolateFps","interpolateMode","targetPts","targetTime","pts","onFrameAvailable","flush","CancelRequestError","DownloadVideoURL","url","__privateAdd","_url","_httpRequest","_filepath","_tmpObj","__privateSet","extension","path","tmp","__privateGet","resolve","reject","source","connectionHandler","https","http","res","contentType","err","writeStream","fs","e","RGBA_PIXEL_SIZE","createDecoder","demuxer","streamIndex","commonParams","beamcoder","createFilter","stream","outputPixelFormat","filterSpec","filterSpecStr","STREAM_TYPE_VIDEO","COLORSPACE_RGBA","MAX_RECURSION","_BeamcoderExtractor","_createDecoder","_demuxer","_decoder","_filterer","_filteredFramesPacket","_frames","_packet","_previousTargetPTS","_threadCount","_streamIndex","_packetReadCount","_recursiveReadCount","extractor","downloadUrl","target","frame","rawData","time","time_base","targetPTS","SeekPTSOffset","RE_SEEK_THRESHOLD","hasFrameWithinThreshold","__privateMethod","createDecoder_fn","filteredFrames","closestFramePTS","outputFrame","closestFrame","nextFrame","f","__privateWrapper","r","TIME_OFFSET","PTSOffset","packet","decodedFrames","frames","sourceLineSize","pixels","i","sourceStart","sourceEnd","sourceData","targetOffset","BeamcoderExtractor","CachedVideoDownloader","_cache","self","filepath","entry","downloader","promise"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAQO,MAAMA,EAAmC;AAAA,EAC5C,aAAa,OAAOC,GAAyC;AACnD,UAAA,IAAI,MAAM,iBAAiB;AAAA,EACrC;AAAA,EAEA,MAAM,KAAK;AAAA,IACP,gBAAAC;AAAA,IACA,YAAAC;AAAA,IACA,aAAAC,IAAc;AAAA,IACd,SAAAC;AAAA,IACA,gBAAAC;AAAA,IACA,iBAAAC;AAAA,EAAA,GAC6B;AACvB,UAAA,IAAI,MAAM,iBAAiB;AAAA,EACrC;AAAA,EAEA,IAAI,WAAmB;AACb,UAAA,IAAI,MAAM,iBAAiB;AAAA,EACrC;AAAA,EAEA,IAAI,QAAgB;AACV,UAAA,IAAI,MAAM,iBAAiB;AAAA,EACrC;AAAA,EAEA,IAAI,SAAiB;AACX,UAAA,IAAI,MAAM,iBAAiB;AAAA,EACrC;AAAA,EAEA,MAAM,UAAUC,GAAmB;AACzB,UAAA,IAAI,MAAM,iBAAiB;AAAA,EACrC;AAAA,EAEA,MAAM,eAAeC,GAAoC;AAC/C,UAAA,IAAI,MAAM,iBAAiB;AAAA,EACrC;AAAA,EAEA,MAAM,mBAAmBA,GAAwC;AACvD,UAAA,IAAI,MAAM,iBAAiB;AAAA,EACrC;AAAA,EAEA,MAAM,cAAcD,GAAmC;AAC7C,UAAA,IAAI,M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src/backends/beamcoder.ts","../src/cachedVideoDownloader.ts"],"sourcesContent":["import type {\n ExtractorArgs,\n Frame,\n Extractor\n} from '../framefusion';\nimport type { ImageData } from './types';\n\n\nexport class BaseExtractor implements Extractor {\n static async create(args: ExtractorArgs): Promise<Extractor> {\n throw new Error('Not implemented');\n }\n\n async init({\n inputFileOrUrl,\n outputFile,\n threadCount = 8,\n endTime,\n interpolateFps,\n interpolateMode,\n }: ExtractorArgs): Promise<void> {\n throw new Error('Not implemented');\n }\n\n get duration(): number {\n throw new Error('Not implemented');\n }\n\n get width(): number {\n throw new Error('Not implemented');\n }\n\n get height(): number {\n throw new Error('Not implemented');\n }\n\n async seekToPTS(targetPts: number) {\n throw new Error('Not implemented');\n }\n\n async getFrameAtTime(targetTime: number): Promise<Frame> {\n throw new Error('Not implemented');\n }\n\n async getImageDataAtTime(targetTime: number): Promise<ImageData> {\n throw new Error('Not implemented');\n }\n\n async getFrameAtPts(targetPts: number): Promise<Frame> {\n throw new Error('Not implemented');\n }\n\n async seekToTime(targetTime: number) {\n throw new Error('Not implemented');\n }\n\n /**\n * Convert a PTS (based on timebase) to PTS (in seconds)\n */\n ptsToTime(pts: number) {\n throw new Error('Not implemented');\n }\n\n async readFrames({\n onFrameAvailable,\n flush = true,\n }: {\n /**\n * Return true if we need to read more frames.\n */\n onFrameAvailable?: (frame: Frame) => Promise<boolean> | boolean;\n flush?: boolean;\n } = {\n flush: true,\n onFrameAvailable: () => true,\n }) {\n throw new Error('Not implemented');\n }\n\n async dispose() {\n throw new Error('Not implemented');\n }\n}\n","import path from 'path';\nimport type { ClientRequest } from 'http';\n\nimport { Writable } from 'stream';\nimport tmp from 'tmp';\nimport fs from 'fs-extra';\n\n/**\n * Downloads a video file from a given URL as a temporary file. When the object is cleared, the temporary file is\n * deleted.\n */\nexport class DownloadVideoURL {\n #url: string | undefined;\n #httpRequest: ClientRequest | undefined = undefined;\n #filepath: string;\n #tmpObj: tmp.FileResult | undefined = undefined;\n\n constructor(url: string) {\n this.#url = url;\n\n const extension = path.extname(url);\n this.#tmpObj = tmp.fileSync({ postfix: extension });\n this.#filepath = this.#tmpObj.name;\n }\n\n /**\n * returns the filepath of the downloaded file. If the file has not been downloaded yet, it will be undefined\n */\n get filepath() {\n return this.#filepath;\n }\n\n /**\n * Downloads the file from the given URL. The file will be downloaded to a temporary file.\n */\n async download() {\n const source = this.#url;\n\n const response = await fetch(source);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch ${source}, status: ${response.status}`\n );\n }\n\n const contentType = response.headers.get('content-type');\n if (!contentType || !contentType.includes('video')) {\n throw new Error(\n `Source ${source}, returned unsupported content type ${contentType}`\n );\n }\n\n const writeStream = fs.createWriteStream(this.#tmpObj.name);\n const readableStream = response.body;\n\n if (!readableStream) {\n throw new Error(`Response body is null for ${source}`);\n }\n\n return new Promise<void>((resolve, reject) => {\n readableStream.pipeTo(Writable.toWeb(writeStream));\n writeStream.on('finish', () => {\n writeStream.close();\n this.#filepath = this.#tmpObj.name;\n resolve();\n });\n writeStream.on('error', (e) => {\n reject(e);\n });\n });\n }\n\n clear() {\n if (this.#tmpObj) this.#tmpObj.removeCallback();\n if (this.#url) this.#url = undefined;\n if (this.#httpRequest) this.#httpRequest = null;\n if (this.#filepath) this.#filepath = undefined;\n }\n}\n","import type {\n Packet,\n Demuxer,\n Decoder,\n Filterer,\n Frame\n} from '@lumen5/beamcoder';\nimport beamcoder from '@lumen5/beamcoder';\nimport type { ImageData } from '../types';\nimport { BaseExtractor } from '../BaseExtractor';\nimport type { Extractor, ExtractorArgs, InterpolateMode } from '../../framefusion';\nimport { DownloadVideoURL } from '../DownloadVideoURL';\n\nconst VERBOSE = false;\n\n/**\n * RGBA format need one byte for every components: r, g, b and a\n */\nconst RGBA_PIXEL_SIZE = 4;\n\nconst createDecoder = ({\n demuxer,\n streamIndex,\n threadCount,\n}: {\n demuxer: Demuxer;\n streamIndex: number;\n threadCount: number;\n}): Decoder => {\n const commonParams = {\n width: demuxer.streams[streamIndex].codecpar.width,\n height: demuxer.streams[streamIndex].codecpar.height,\n pix_fmt: demuxer.streams[streamIndex].codecpar.format,\n thread_count: threadCount,\n };\n\n if (demuxer.streams[streamIndex].codecpar.name === 'vp8') {\n return beamcoder.decoder({\n ...commonParams,\n name: 'libvpx',\n });\n }\n\n if (demuxer.streams[streamIndex].codecpar.name === 'vp9') {\n return beamcoder.decoder({\n ...commonParams,\n name: 'libvpx-vp9',\n });\n }\n\n return beamcoder.decoder({\n ...commonParams,\n demuxer: demuxer,\n stream_index: streamIndex,\n });\n};\n\n/**\n * A filter to convert between color spaces.\n * An example would be YUV to RGB, for mp4 to png conversion.\n */\nconst createFilter = async({\n stream,\n outputPixelFormat,\n interpolateFps,\n interpolateMode = 'fast',\n}: {\n stream: beamcoder.Stream;\n outputPixelFormat: string;\n interpolateFps?: number;\n interpolateMode?: InterpolateMode;\n}): Promise<beamcoder.Filterer> => {\n if (!stream.codecpar.format) {\n return null;\n }\n\n let filterSpec = [`[in0:v]format=${stream.codecpar.format}`];\n\n if (interpolateFps) {\n if (interpolateMode === 'high-quality') {\n filterSpec = [...filterSpec, `minterpolate=fps=${interpolateFps}`];\n }\n else if (interpolateMode === 'fast') {\n filterSpec = [...filterSpec, `fps=${interpolateFps}`];\n }\n else {\n throw new Error(`Unexpected interpolation mode: ${interpolateMode}`);\n }\n }\n\n const filterSpecStr = filterSpec.join(', ') + '[out0:v]';\n\n VERBOSE && console.log(`filterSpec: ${filterSpecStr}`);\n\n return beamcoder.filterer({\n filterType: 'video',\n inputParams: [\n {\n name: 'in0:v',\n width: stream.codecpar.width,\n height: stream.codecpar.height,\n pixelFormat: stream.codecpar.format,\n timeBase: stream.time_base,\n pixelAspect: stream.sample_aspect_ratio,\n },\n ],\n outputParams: [\n {\n name: 'out0:v',\n pixelFormat: outputPixelFormat,\n },\n ],\n filterSpec: filterSpecStr,\n });\n};\n\nconst STREAM_TYPE_VIDEO = 'video';\nconst COLORSPACE_RGBA = 'rgba';\nconst MAX_RECURSION = 5;\n\n/**\n * A simple extractor that uses beamcoder to extract frames from a video file.\n */\nexport class BeamcoderExtractor extends BaseExtractor implements Extractor {\n /**\n * The demuxer reads the file and outputs packet streams\n */\n #demuxer: Demuxer = null;\n\n /**\n * The decoder reads packets and can output raw frame data\n */\n #decoder: Decoder = null;\n\n /**\n * Packets can be filtered to change colorspace, fps and add various effects. If there are no colorspace changes or\n * filters, filter might not be necessary.\n */\n #filterer: Filterer = null;\n\n /**\n * This is where we store filtered frames from each previously processed packet.\n * We keep these in chronological order. We hang on to them for two reasons:\n * 1. so we can return them if we get a request for the same time again\n * 2. so we can return frames close the end of the stream. When such a frame is requested we have to flush (destroy)\n * the encoder to get the last few frames. This avoids having to re-create an encoder.\n */\n #filteredFramesPacket: undefined[] | Array<Array<Frame>> = [];\n\n /**\n * This contains the last raw frames we read from the demuxer. We use it as a starting point for each new query. We\n * do this ensure we don't skip any frames.\n */\n #frames = [];\n\n /**\n * This contains the last packet we read from the demuxer. We use it as a starting point for each new query. We do\n * this ensure we don't skip any frames.\n */\n #packet: null | Packet = null;\n\n /**\n * The last target presentation timestamp (PTS) we requested. If we never requested a time(stamp) then this\n * value is null\n */\n #previousTargetPTS: null | number = null;\n\n /**\n * The number of threads to use for decoding\n */\n #threadCount = 8;\n\n /**\n * The index of the video stream in the demuxer\n */\n #streamIndex = 0;\n\n /**\n * The number of packets we've read from the demuxer to complete the frame query\n * @private\n */\n #packetReadCount = 0;\n\n /**\n * The number of times we've recursively read packets from the demuxer to complete the frame query\n * @private\n */\n #recursiveReadCount = 0;\n\n /**\n * Encoder/Decoder construction is async, so it can't be put in a regular constructor.\n * Use and await this method to generate an extractor.\n */\n static async create(args: ExtractorArgs): Promise<BeamcoderExtractor> {\n const extractor = new BeamcoderExtractor();\n await extractor.init(args);\n return extractor;\n }\n\n async init({\n inputFileOrUrl,\n threadCount = 8,\n }: ExtractorArgs): Promise<void> {\n this.#threadCount = threadCount;\n if (inputFileOrUrl.startsWith('http')) {\n VERBOSE && console.log('downloading url', inputFileOrUrl);\n const downloadUrl = new DownloadVideoURL(inputFileOrUrl);\n await downloadUrl.download();\n inputFileOrUrl = downloadUrl.filepath;\n VERBOSE && console.log('finished downloading');\n }\n // Assume file url at this point\n if (!inputFileOrUrl.startsWith('file:')) {\n inputFileOrUrl = 'file:' + inputFileOrUrl;\n }\n this.#demuxer = await beamcoder.demuxer(inputFileOrUrl);\n this.#streamIndex = this.#demuxer.streams.findIndex(stream => stream.codecpar.codec_type === STREAM_TYPE_VIDEO);\n\n if (this.#streamIndex === -1) {\n throw new Error(`File has no ${STREAM_TYPE_VIDEO} stream!`);\n }\n this.#filterer = await createFilter({\n stream: this.#demuxer.streams[this.#streamIndex],\n outputPixelFormat: COLORSPACE_RGBA,\n });\n }\n\n async #createDecoder() {\n // It's possible that we need to create decoder multiple times during the lifecycle of this extractor so we\n // need to make sure we destroy the old one first if it exists\n if (this.#decoder) {\n await this.#decoder.flush();\n this.#decoder = null;\n }\n this.#decoder = createDecoder({\n demuxer: this.#demuxer as Demuxer,\n streamIndex: this.#streamIndex,\n threadCount: this.#threadCount,\n });\n }\n\n /**\n * This is the duration of the first video stream in the file expressed in seconds.\n */\n get duration(): number {\n const stream = this.#demuxer.streams[this.#streamIndex];\n if (stream.duration !== null) {\n return this.ptsToTime(stream.duration);\n }\n return this.ptsToTime(this.#demuxer.duration) / 1000;\n }\n\n /**\n * Width in pixels\n */\n get width(): number {\n return this.#demuxer.streams[this.#streamIndex].codecpar.width;\n }\n\n /**\n * Height in pixels\n */\n get height(): number {\n return this.#demuxer.streams[this.#streamIndex].codecpar.height;\n }\n\n /**\n * Get the beamcoder Frame for a given time in seconds\n * @param targetTime\n */\n async getFrameAtTime(targetTime: number): Promise<beamcoder.Frame> {\n VERBOSE && console.log(`getFrameAtTime time(s)=${targetTime}`);\n const targetPts = Math.round(this._timeToPTS(targetTime));\n return this._getFrameAtPts(targetPts);\n }\n\n /**\n * Get imageData for a given time in seconds\n * @param targetTime\n */\n async getImageDataAtTime(targetTime: number, target?: Uint8ClampedArray): Promise<ImageData> {\n const targetPts = Math.round(this._timeToPTS(targetTime));\n VERBOSE && console.log('targetTime', targetTime, '-> targetPts', targetPts);\n const frame = await this._getFrameAtPts(targetPts);\n if (!frame) {\n VERBOSE && console.log('no frame found');\n return null;\n }\n\n let rawData = target;\n\n if (!target) {\n rawData = new Uint8ClampedArray(frame.width * frame.height * RGBA_PIXEL_SIZE);\n }\n\n this._setFrameDataToImageData(frame, rawData);\n\n return {\n data: rawData,\n width: frame.width,\n height: frame.height,\n };\n }\n\n /**\n * Get the presentation timestamp (PTS) for a given time in seconds\n */\n _timeToPTS(time: number) {\n const time_base = this.#demuxer.streams[this.#streamIndex].time_base;\n return time * time_base[1] / time_base[0];\n }\n\n /**\n * Get the time in seconds from a given presentation timestamp (PTS)\n */\n ptsToTime(pts: number) {\n const time_base = this.#demuxer.streams[this.#streamIndex].time_base;\n return pts * time_base[0] / time_base[1];\n }\n\n get packetReadCount() {\n return this.#packetReadCount;\n }\n\n /**\n * Get the frame at the given presentation timestamp (PTS)\n * @param targetPTS - the target presentation timestamp (PTS) we want to retrieve\n * @param SeekPTSOffset - the offset to use when seeking to the targetPTS. This is used when we have trouble finding\n * the targetPTS. We use it to further move away from the requested PTS to find a frame. The allows use to read\n * additional packets and find a frame that is closer to the targetPTS.\n */\n async _getFrameAtPts(targetPTS: number, SeekPTSOffset = 0): Promise<beamcoder.Frame> {\n VERBOSE && console.log('_getFrameAtPts', targetPTS, 'seekPTSOffset', SeekPTSOffset, 'duration', this.duration);\n this.#packetReadCount = 0;\n\n // seek and create a decoder when retrieving a frame for the first time or when seeking backwards\n // we have to create a new decoder when seeking backwards as the decoder can only process frames in\n // chronological order.\n // RE_SEEK_DELTA: sometimes, we are looking for a frame so far ahead that it's better to drop everything and seek.\n // Example: when we got a frame a 0 and request a frame at t = 30s just after, we don't want to start reading all packets\n // until 30s.\n const RE_SEEK_THRESHOLD = 3; // 3 seconds - typically we have keyframes at shorter intervals\n const hasFrameWithinThreshold = this.#filteredFramesPacket.flat().some(frame => {\n return this.ptsToTime(Math.abs(targetPTS - (frame as Frame).pts)) < RE_SEEK_THRESHOLD;\n });\n VERBOSE && console.log('hasPreviousTargetPTS:', this.#previousTargetPTS === null, ', targetPTS is smaller:', this.#previousTargetPTS > targetPTS, ', has frame within threshold:', hasFrameWithinThreshold);\n if (this.#previousTargetPTS === null || this.#previousTargetPTS > targetPTS || !hasFrameWithinThreshold) {\n VERBOSE && console.log(`Seeking to ${targetPTS + SeekPTSOffset}`);\n\n await this.#demuxer.seek({\n stream_index: 0, // even though we specify the stream index, it still seeks all streams\n timestamp: targetPTS + SeekPTSOffset,\n any: false,\n });\n await this.#createDecoder();\n this.#packet = null;\n this.#frames = [];\n this.#previousTargetPTS = targetPTS;\n this.#filteredFramesPacket = [];\n }\n\n let filteredFrames = null;\n let closestFramePTS = -1;\n let outputFrame = null;\n\n // If we have previously filtered frames, get the frame closest to our targetPTS\n if (this.#filteredFramesPacket.length > 0) {\n const closestFrame = this.#filteredFramesPacket\n .flat()\n .find(f => (f as Frame).pts <= targetPTS) as Frame;\n\n if (closestFrame) {\n const nextFrame = this.#filteredFramesPacket\n .flat()\n .find(f => (f as Frame).pts > closestFrame.pts) as Frame;\n\n VERBOSE && console.log('returning previously filtered frame with pts', (closestFrame as Frame).pts);\n closestFramePTS = (closestFrame as Frame).pts;\n outputFrame = closestFrame;\n\n if ((nextFrame && nextFrame.pts > targetPTS) || (closestFramePTS === targetPTS)) {\n // We have a next frame, so we know the frame being displayed at targetPTS is the previous one,\n // which corresponds to outputFrame.\n this.#previousTargetPTS = targetPTS;\n return outputFrame;\n }\n }\n }\n\n // This is the first time we're decoding frames. Get the first packet and decode it.\n if (!this.#packet && this.#frames.length === 0) {\n ({ packet: this.#packet, frames: this.#frames } = await this._getNextPacketAndDecodeFrames());\n this.#packetReadCount++;\n }\n // Read packets until we have a frame which is closest to targetPTS\n while ((this.#packet || this.#frames.length !== 0) && closestFramePTS < targetPTS) {\n VERBOSE && console.log('packet si:', this.#packet?.stream_index, 'pts:', this.#packet?.pts, 'frames:', this.#frames?.length);\n VERBOSE && console.log('frames', this.#frames?.length, 'frames.pts:', JSON.stringify(this.#frames?.map(f => f.pts)), '-> target.pts:', targetPTS);\n\n // packet contains frames\n if (this.#frames.length !== 0) {\n // filter the frames\n const filteredResult = await this.#filterer.filter([{ name: 'in0:v', frames: this.#frames }]);\n filteredFrames = filteredResult.flatMap(r => r.frames);\n VERBOSE && console.log('filteredFrames', filteredFrames.length, 'filteredFrames.pts:', JSON.stringify(filteredFrames.map(f => f.pts)), '-> target.pts:', targetPTS);\n\n // get the closest frame to our target presentation timestamp (PTS)\n // Beamcoder returns decoded packet frames as follows: [1000, 2000, 3000, 4000]\n // If we're looking for a frame at 0, we want to return the frame at 1000\n // If we're looking for a frame at 2500, we want to return the frame at 2000\n const closestFrame = (this.#packetReadCount === 1 && filteredFrames[0].pts > targetPTS)\n ? filteredFrames[0]\n : filteredFrames.reverse().find(f => f.pts <= targetPTS);\n\n // The packet contains frames, but all of them have PTS larger than our a targetPTS (we looked too far)\n if (!closestFrame) {\n return outputFrame;\n }\n\n // store the filtered packet frames for later reuse\n this.#filteredFramesPacket.unshift(filteredFrames);\n if (this.#filteredFramesPacket.length > 2) {\n this.#filteredFramesPacket.pop();\n }\n\n closestFramePTS = closestFrame?.pts;\n VERBOSE && console.log('closestFramePTS', closestFramePTS, 'targetPTS', targetPTS);\n if (!outputFrame || closestFramePTS <= targetPTS) {\n VERBOSE && console.log('assigning outputFrame', closestFrame?.pts);\n this.#previousTargetPTS = targetPTS;\n outputFrame = closestFrame;\n }\n else {\n // break out of the loop if we've found the closest frame (and ensure we don't move to the next\n // packet by calling _getNextPacketAndDecodeFrames again) as this risks us getting a frame that is\n // after our targetPTS\n VERBOSE && console.log('breaking');\n break;\n }\n }\n // get the next packet and frames\n ({ packet: this.#packet, frames: this.#frames } = await this._getNextPacketAndDecodeFrames());\n\n // keep track of how many packets we've read\n this.#packetReadCount++;\n }\n\n // we read through all the available packets and frames, but we still don't have a frame. This can happen\n // when our targetPTS is to close to the end of the video. In this case, we'll try to seek further away from\n // the end of the video and try again. We've set up a MAX_RECURSION to prevent an infinite loop.\n if (!outputFrame) {\n if (MAX_RECURSION < this.#recursiveReadCount) {\n throw Error('No matching frame found');\n }\n const TIME_OFFSET = 0.1; // time offset in seconds\n const PTSOffset = this._timeToPTS(TIME_OFFSET);\n this.#recursiveReadCount++;\n outputFrame = await this._getFrameAtPts(targetPTS, SeekPTSOffset - PTSOffset);\n if (outputFrame) {\n this.#recursiveReadCount = 0;\n }\n }\n VERBOSE && console.log('read', this.packetReadCount, 'packets');\n\n return outputFrame;\n }\n\n /**\n * Get the next packet from the video stream and decode it into frames. Each frame has a presentation time stamp\n * (PTS). If we've reached the end of the stream and no more packets are available, we'll extract the last frames\n * from the decoder and destroy it.\n */\n async _getNextPacketAndDecodeFrames() {\n const packet = await this._getNextVideoStreamPacket();\n VERBOSE && console.log('packet pts:', packet?.pts);\n\n // extract frames from the packet\n let decodedFrames = null;\n if (packet !== null && this.#decoder) {\n decodedFrames = await this.#decoder.decode(packet as Packet);\n VERBOSE && console.log('decodedFrames', decodedFrames.frames.length, decodedFrames.frames.map(f => f.pts));\n }\n // we've reached the end of the stream\n else {\n if (this.#decoder) {\n VERBOSE && console.log('getting the last frames from the decoder');\n // flush the decoder -- this will return the last frames and destroy the decoder\n decodedFrames = await this.#decoder.flush();\n this.#decoder = null;\n }\n else {\n // we don't have a decoder, so we can't decode any more frames\n VERBOSE && console.log('no more frames to decode');\n }\n }\n\n let frames = [];\n if (decodedFrames && decodedFrames.frames.length !== 0) {\n frames = decodedFrames.frames;\n }\n VERBOSE && console.log(`returning ${frames.length} decoded frames`);\n\n return { packet, frames };\n }\n\n async _getNextVideoStreamPacket(): Promise<null | Packet> {\n VERBOSE && console.log('_getNextVideoStreamPacket');\n\n let packet = await this.#demuxer.read();\n while (packet && packet.stream_index !== this.#streamIndex) {\n packet = await this.#demuxer.read();\n if (packet === null) {\n VERBOSE && console.log('no more packets');\n return null;\n }\n }\n VERBOSE && console.log('returning packet', !!packet, 'pts', packet?.pts, 'si', packet?.stream_index);\n return packet as Packet;\n }\n\n _setFrameDataToImageData(frame: beamcoder.Frame, target: Uint8ClampedArray) {\n const sourceLineSize = frame.linesize as unknown as number;\n // frame.data can contain multiple \"planes\" in other colorspaces, but in rgba, there is just one \"plane\", so\n // our data is in frame.data[0]\n const pixels = frame.data[0] as Uint8Array;\n\n // libav creates larger buffers because it makes their internal code simpler.\n // we have to trim a part at the right of each pixel row.\n\n for (let i = 0; i < frame.height; i++) {\n const sourceStart = i * sourceLineSize;\n const sourceEnd = sourceStart + frame.width * RGBA_PIXEL_SIZE;\n const sourceData = pixels.subarray(sourceStart, sourceEnd);\n const targetOffset = i * frame.width * RGBA_PIXEL_SIZE;\n target.set(sourceData, targetOffset);\n }\n }\n\n async dispose() {\n if (this.#decoder) {\n await this.#decoder.flush();\n this.#decoder = null;\n }\n this.#demuxer.forceClose();\n this.#filterer = null;\n this.#filteredFramesPacket = undefined;\n this.#frames = [];\n this.#packet = null;\n this.#previousTargetPTS = null;\n this.#streamIndex = 0;\n }\n}\n","import { DownloadVideoURL } from './DownloadVideoURL';\n\ninterface CachedResource {\n url: string;\n filepath: string | undefined;\n download(): Promise<void>;\n destroy(): void;\n}\n\ninterface CacheEntry {\n downloader: DownloadVideoURL;\n refCount: number;\n downloadPromise?: Promise<void>;\n}\n\nexport class CachedVideoDownloader {\n #cache: Map<string, CacheEntry> = new Map();\n\n get(url: string): CachedResource {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n let filepath: string | undefined;\n\n return {\n url,\n get filepath() {\n return filepath;\n },\n\n async download() {\n let entry = self.#cache.get(url);\n\n if (entry) {\n entry.refCount += 1;\n\n // Wait for in-progress download if exists\n if (entry.downloadPromise) {\n await entry.downloadPromise;\n }\n }\n else {\n const downloader = new DownloadVideoURL(url);\n\n const promise = downloader.download();\n entry = {\n downloader,\n refCount: 1,\n downloadPromise: promise,\n };\n self.#cache.set(url, entry);\n\n try {\n await promise;\n }\n finally {\n entry.downloadPromise = undefined; // Clear after completion\n }\n }\n\n filepath = self.#cache.get(url).downloader.filepath;\n },\n\n destroy() {\n const entry = self.#cache.get(url);\n if (!entry) return;\n\n entry.refCount -= 1;\n\n if (entry.refCount <= 0) {\n entry.downloader.clear();\n self.#cache.delete(url);\n }\n\n filepath = undefined;\n },\n };\n }\n}\n"],"names":["BaseExtractor","args","inputFileOrUrl","outputFile","threadCount","endTime","interpolateFps","interpolateMode","targetPts","targetTime","pts","onFrameAvailable","flush","DownloadVideoURL","url","__privateAdd","_url","_httpRequest","_filepath","_tmpObj","__privateSet","extension","path","tmp","__privateGet","source","response","contentType","writeStream","fs","readableStream","resolve","reject","Writable","e","RGBA_PIXEL_SIZE","createDecoder","demuxer","streamIndex","commonParams","beamcoder","createFilter","stream","outputPixelFormat","filterSpec","filterSpecStr","STREAM_TYPE_VIDEO","COLORSPACE_RGBA","MAX_RECURSION","_BeamcoderExtractor","_createDecoder","_demuxer","_decoder","_filterer","_filteredFramesPacket","_frames","_packet","_previousTargetPTS","_threadCount","_streamIndex","_packetReadCount","_recursiveReadCount","extractor","downloadUrl","target","frame","rawData","time","time_base","targetPTS","SeekPTSOffset","RE_SEEK_THRESHOLD","hasFrameWithinThreshold","__privateMethod","createDecoder_fn","filteredFrames","closestFramePTS","outputFrame","closestFrame","f","nextFrame","__privateWrapper","r","TIME_OFFSET","PTSOffset","packet","decodedFrames","frames","sourceLineSize","pixels","i","sourceStart","sourceEnd","sourceData","targetOffset","BeamcoderExtractor","CachedVideoDownloader","_cache","self","filepath","entry","downloader","promise"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAQO,MAAMA,EAAmC;AAAA,EAC5C,aAAa,OAAOC,GAAyC;AACnD,UAAA,IAAI,MAAM,iBAAiB;AAAA,EACrC;AAAA,EAEA,MAAM,KAAK;AAAA,IACP,gB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,cAAA;AACM,kBAAAE;AAAA,UAAA,UAEV;AACI,YAAAF,EAAM,kBAAkB;AAAA,UAC5B;AAAA;AAGJ,QAAAD,IAAW3E,EAAA0E,GAAKD,GAAO,IAAInF,CAAG,EAAE,WAAW;AAAA,MAC/C;AAAA,MAEA,UAAU;AACN,cAAMsF,IAAQ5E,EAAA0E,GAAKD,GAAO,IAAInF,CAAG;AACjC,QAAKsF,MAELA,EAAM,YAAY,GAEdA,EAAM,YAAY,MAClBA,EAAM,WAAW,SACZ5E,EAAA0E,GAAAD,GAAO,OAAOnF,CAAG,IAGfqF,IAAA;AAAA,MACf;AAAA,IAAA;AAAA,EAER;AACJ;AA9DIF,IAAA;"}
|
|
@@ -6,12 +6,12 @@ export declare class DownloadVideoURL {
|
|
|
6
6
|
#private;
|
|
7
7
|
constructor(url: string);
|
|
8
8
|
/**
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
* returns the filepath of the downloaded file. If the file has not been downloaded yet, it will be undefined
|
|
10
|
+
*/
|
|
11
11
|
get filepath(): string;
|
|
12
12
|
/**
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
* Downloads the file from the given URL. The file will be downloaded to a temporary file.
|
|
14
|
+
*/
|
|
15
15
|
download(): Promise<void>;
|
|
16
16
|
clear(): void;
|
|
17
17
|
}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import
|
|
3
|
-
import http from 'http';
|
|
2
|
+
import { Writable } from 'stream';
|
|
4
3
|
import tmp from 'tmp';
|
|
5
4
|
import fs from 'fs-extra';
|
|
6
|
-
class CancelRequestError extends Error {
|
|
7
|
-
}
|
|
8
5
|
/**
|
|
9
6
|
* Downloads a video file from a given URL as a temporary file. When the object is cleared, the temporary file is
|
|
10
7
|
* deleted.
|
|
@@ -21,47 +18,39 @@ export class DownloadVideoURL {
|
|
|
21
18
|
this.#filepath = this.#tmpObj.name;
|
|
22
19
|
}
|
|
23
20
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
* returns the filepath of the downloaded file. If the file has not been downloaded yet, it will be undefined
|
|
22
|
+
*/
|
|
26
23
|
get filepath() {
|
|
27
24
|
return this.#filepath;
|
|
28
25
|
}
|
|
29
26
|
/**
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
* Downloads the file from the given URL. The file will be downloaded to a temporary file.
|
|
28
|
+
*/
|
|
32
29
|
async download() {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
this.#httpRequest.on('error', (e) => {
|
|
56
|
-
if (e instanceof CancelRequestError) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
reject(e);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
catch (e) {
|
|
30
|
+
const source = this.#url;
|
|
31
|
+
const response = await fetch(source);
|
|
32
|
+
if (!response.ok) {
|
|
33
|
+
throw new Error(`Failed to fetch ${source}, status: ${response.status}`);
|
|
34
|
+
}
|
|
35
|
+
const contentType = response.headers.get('content-type');
|
|
36
|
+
if (!contentType || !contentType.includes('video')) {
|
|
37
|
+
throw new Error(`Source ${source}, returned unsupported content type ${contentType}`);
|
|
38
|
+
}
|
|
39
|
+
const writeStream = fs.createWriteStream(this.#tmpObj.name);
|
|
40
|
+
const readableStream = response.body;
|
|
41
|
+
if (!readableStream) {
|
|
42
|
+
throw new Error(`Response body is null for ${source}`);
|
|
43
|
+
}
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
readableStream.pipeTo(Writable.toWeb(writeStream));
|
|
46
|
+
writeStream.on('finish', () => {
|
|
47
|
+
writeStream.close();
|
|
48
|
+
this.#filepath = this.#tmpObj.name;
|
|
49
|
+
resolve();
|
|
50
|
+
});
|
|
51
|
+
writeStream.on('error', (e) => {
|
|
63
52
|
reject(e);
|
|
64
|
-
}
|
|
53
|
+
});
|
|
65
54
|
});
|
|
66
55
|
}
|
|
67
56
|
clear() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DownloadVideoURL.js","sourceRoot":"","sources":["../../src/DownloadVideoURL.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"DownloadVideoURL.js","sourceRoot":"","sources":["../../src/DownloadVideoURL.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAGxB,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAE,MAAM,UAAU,CAAC;AAE1B;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IACzB,IAAI,CAAqB;IACzB,YAAY,GAA8B,SAAS,CAAC;IACpD,SAAS,CAAS;IAClB,OAAO,GAA+B,SAAS,CAAC;IAEhD,YAAY,GAAW;QACnB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAEhB,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACpC,IAAI,CAAC,OAAO,GAAG,GAAG,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC,CAAC;QACpD,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;IACvC,CAAC;IAED;;KAEC;IACD,IAAI,QAAQ;QACR,OAAO,IAAI,CAAC,SAAS,CAAC;IAC1B,CAAC;IAED;;KAEC;IACD,KAAK,CAAC,QAAQ;QACV,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC;QAEzB,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,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