@soga/encoder 0.3.0 → 1.0.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/{encoder.d.ts → main.d.mts} +20 -6
- package/dist/main.d.ts +41 -3
- package/dist/main.js +1 -1
- package/dist/main.mjs +1 -0
- package/package.json +21 -18
- package/dist/encoder.js +0 -1
- package/dist/types.d.ts +0 -12
- package/dist/types.js +0 -1
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
import { Repository } from 'typeorm';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { DataSource, Repository } from 'typeorm';
|
|
2
|
+
import { UploadFile, UploadRunning } from '@soga/entities';
|
|
3
|
+
|
|
4
|
+
type EncoderParams = {
|
|
5
|
+
ffmpegPath: string;
|
|
6
|
+
sdk_domain: string;
|
|
7
|
+
dataSource: DataSource;
|
|
8
|
+
threads?: number;
|
|
9
|
+
debug?: boolean;
|
|
10
|
+
onProgress?: (file: UploadFile) => Promise<void>;
|
|
11
|
+
onSuccess?: (file: UploadFile) => Promise<void>;
|
|
12
|
+
onError?: (file: UploadFile | null, error?: Error) => Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
declare class Encoder {
|
|
5
16
|
private piscina;
|
|
6
17
|
protected onProgress: (file: UploadFile) => Promise<void>;
|
|
7
18
|
protected onSuccess: (file: UploadFile) => Promise<void>;
|
|
8
19
|
protected onError: (file: UploadFile | null, error: Error) => Promise<void>;
|
|
9
20
|
protected fileRepository: Repository<UploadFile>;
|
|
21
|
+
protected runningRepository: Repository<UploadRunning>;
|
|
10
22
|
private isRunning;
|
|
11
23
|
private thread_count;
|
|
12
24
|
private maxThreads;
|
|
@@ -19,9 +31,11 @@ export declare class Encoder {
|
|
|
19
31
|
init(): Promise<void>;
|
|
20
32
|
start(): Promise<void>;
|
|
21
33
|
stopFiles(ids: number[]): Promise<void>;
|
|
22
|
-
deleteFiles(ids: number[]): Promise<void>;
|
|
23
34
|
stopAll(): Promise<void>;
|
|
24
35
|
private getThread;
|
|
25
|
-
private run;
|
|
26
36
|
private getUid;
|
|
27
37
|
}
|
|
38
|
+
|
|
39
|
+
declare const getEncoder: (params: EncoderParams) => Promise<Encoder>;
|
|
40
|
+
|
|
41
|
+
export { Encoder, getEncoder };
|
package/dist/main.d.ts
CHANGED
|
@@ -1,3 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { DataSource, Repository } from 'typeorm';
|
|
2
|
+
import { UploadFile, UploadRunning } from '@soga/entities';
|
|
3
|
+
|
|
4
|
+
type EncoderParams = {
|
|
5
|
+
ffmpegPath: string;
|
|
6
|
+
sdk_domain: string;
|
|
7
|
+
dataSource: DataSource;
|
|
8
|
+
threads?: number;
|
|
9
|
+
debug?: boolean;
|
|
10
|
+
onProgress?: (file: UploadFile) => Promise<void>;
|
|
11
|
+
onSuccess?: (file: UploadFile) => Promise<void>;
|
|
12
|
+
onError?: (file: UploadFile | null, error?: Error) => Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
declare class Encoder {
|
|
16
|
+
private piscina;
|
|
17
|
+
protected onProgress: (file: UploadFile) => Promise<void>;
|
|
18
|
+
protected onSuccess: (file: UploadFile) => Promise<void>;
|
|
19
|
+
protected onError: (file: UploadFile | null, error: Error) => Promise<void>;
|
|
20
|
+
protected fileRepository: Repository<UploadFile>;
|
|
21
|
+
protected runningRepository: Repository<UploadRunning>;
|
|
22
|
+
private isRunning;
|
|
23
|
+
private thread_count;
|
|
24
|
+
private maxThreads;
|
|
25
|
+
private threads;
|
|
26
|
+
params: EncoderParams;
|
|
27
|
+
private sdk;
|
|
28
|
+
protected getValidThreads(threads: number): number;
|
|
29
|
+
constructor(params: EncoderParams);
|
|
30
|
+
setThreads(threads: number): Promise<void>;
|
|
31
|
+
init(): Promise<void>;
|
|
32
|
+
start(): Promise<void>;
|
|
33
|
+
stopFiles(ids: number[]): Promise<void>;
|
|
34
|
+
stopAll(): Promise<void>;
|
|
35
|
+
private getThread;
|
|
36
|
+
private getUid;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare const getEncoder: (params: EncoderParams) => Promise<Encoder>;
|
|
40
|
+
|
|
41
|
+
export { Encoder, getEncoder };
|
package/dist/main.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"
|
|
1
|
+
Object.create;var e,t=Object.defineProperty,i=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,r=(Object.getPrototypeOf,Object.prototype.hasOwnProperty),a=(e,a,o,n)=>{if(a&&"object"==typeof a||"function"==typeof a)for(let d of s(a))r.call(e,d)||d===o||t(e,d,{get:()=>a[d],enumerable:!(n=i(a,d))||n.enumerable});return e},o={};((e,i)=>{for(var s in i)t(e,s,{get:i[s],enumerable:!0})})(o,{Encoder:()=>R,getEncoder:()=>m}),module.exports=(e=o,a(t({},"__esModule",{value:!0}),e));var n=require("@soga/sdk"),d=require("typeorm"),p=require("@soga/entities"),c=require("@soga/types"),u=require("piscina"),h=require("@soga/file-encoder"),l=require("@soga/utils"),g=require("@soga/sdk"),f=require("path"),y=require("worker_threads"),w=require("@soga/error"),R=class{piscina;onProgress=async()=>{};onSuccess=async()=>{};onError=async()=>{};fileRepository;runningRepository;isRunning=!1;thread_count=2;maxThreads=10;threads=[];params;sdk;getValidThreads(e){return Math.min(Math.max(e??0,0),this.maxThreads)}constructor(e){this.params=e;const{dataSource:t}=e;this.fileRepository=t.getRepository(p.UploadFile),this.runningRepository=t.getRepository(p.UploadRunning),this.thread_count=this.getValidThreads(e.threads??this.thread_count),this.sdk=(0,g.getSdk)(e.sdk_domain),e.onProgress&&(this.onProgress=e.onProgress.bind(this)),e.onSuccess&&(this.onSuccess=e.onSuccess.bind(this)),e.onError&&(this.onError=e.onError.bind(this));const i=new u.Piscina({filename:require.resolve("@soga/file-encoder")});this.piscina=i}async setThreads(e){const t=this.getValidThreads(e);this.thread_count=t,await this.start()}async init(){const e=await this.getUid();await this.fileRepository.update({uid:e,upload_status:c.UploadStatus.ENCODE_PROCESS},{upload_status:c.UploadStatus.NULL})}async start(){for(;this.isRunning;)await new Promise(e=>{setTimeout(e,100)});try{this.isRunning=!0;const e=await this.getUid(),t=this.threads.length,i=this.thread_count;if(t<i){const s=i-t;for(let t=0;t<s;t++){const t=await this.fileRepository.findOne({where:{uid:e,upload_status:c.UploadStatus.NULL,type:(0,d.Not)(c.RecordType.FOLDER),is_paused:!1},order:{created_at:"ASC"}});if(!t)break;const i=this.getThread(t);this.threads.push(i),await this.fileRepository.update(t.id,{upload_status:c.UploadStatus.ENCODE_PROCESS}),i.start().catch(e=>{})}}else if(t>i){const e=t-i,s=this.threads.slice(0,e);for(const e of s)await e.stop()}}finally{this.isRunning=!1}}async stopFiles(e){const t=this.threads.filter(t=>e.includes(t.id));await Promise.all(t.map(e=>e.stop())).catch(e=>{})}async stopAll(){await Promise.all(this.threads.map(e=>e.stop()))}getThread(e){const t=new AbortController,{port1:i,port2:s}=new y.MessageChannel;return{id:e.id,uid:e.uid,start:async()=>{s.on("message",async e=>{if("percent"==e.type){const{id:t,data:i}=e,s=await this.fileRepository.findOneBy({id:t});if(!s)return;const{percent:r,step:a}=i;s.progress[a].percent=r;const o=(0,l.getProgressPercent)(s.progress);await this.fileRepository.update(s.id,{progress:s.progress,percent:o}),await this.onProgress(s)}});try{const s=await this.fileRepository.findOneBy({id:e.id});if(!s)return;if(s.upload_status!=c.UploadStatus.ENCODE_PROCESS)return;await this.runningRepository.save(this.runningRepository.create({type:p.UploadRunningType.ENCODE,uid:e.uid,root_id:e.root_id,file_id:e.id}));const{id:r,uid:a,config:o,input:n,type:d,ftype:u,external_texts:l,is_attachment:g}=s,y=[];s.ali_host_id&&y.push(c.HostType.ALI),s.baidu_host_id&&y.push(c.HostType.BAIDU);const{encode_root:w}=await this.sdk.getSystemConfig(),R=(0,f.resolve)(w,s.is_attachment?"attachment":"file",s.id.toString());await this.fileRepository.update(e.id,{output_root:R});let _=!0,m=!0;g||(_=o.file_keeps==c.RecordFileKeep.PREVIEW||o.file_keeps==c.RecordFileKeep.BOTH,m=o.file_keeps==c.RecordFileKeep.SOURCE||o.file_keeps==c.RecordFileKeep.BOTH);const E={id:r,uid:a,keepPreview:_,keepSource:m,input:n,type:d,ftype:u,hosts:y,outputRoot:R,subtitles:l,ffmpegPath:this.params.ffmpegPath,debug:this.params.debug??!1,port:i};if(!Object.keys(s.progress??{}).length){const t=await(0,h.getPrepare)(E);await this.fileRepository.update(e.id,{progress:t.prepareResult})}const O=await this.piscina.run(E,{name:"encode",signal:t.signal,transferList:[i]});if(!O)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{upload_status:c.UploadStatus.ENCODE_SUCCESS,encoded:O}),await this.onSuccess(await this.fileRepository.findOneBy({id:r}))}catch(t){if("AbortError"==t.name)await this.fileRepository.update(e.id,{upload_status:c.UploadStatus.NULL});else{const i=(0,w.buildDError)(t,{message:"Error occurred during encoding",detail:`Error occurred during encoding: ${e.input.filepath}`});try{await this.fileRepository.update(e.id,{upload_status:c.UploadStatus.ENCODE_ERROR,error:{type:"encode",message:i.message,stack:i.stack,details:i.details,cause:i.cause}}),await this.onError(await this.fileRepository.findOneBy({id:e.id}),t)}catch(e){throw e}}}finally{this.threads=this.threads.filter(t=>t.id!==e.id),s.close(),await this.runningRepository.delete({type:p.UploadRunningType.ENCODE,file_id:e.id}),await this.start()}},stop:async()=>{s.close(),t.abort(),this.threads=this.threads.filter(t=>t.id==e.id)}}}async getUid(){return(await this.sdk.getUserInfo({refresh:!1})).id}},_=new Map,m=async e=>{const t=(0,n.getSdk)(e.sdk_domain),i=(await t.getUserInfo({refresh:!1})).id;for(;_.get(i)?.initing;)await new Promise(e=>setTimeout(e,100));if(_.get(i)?.encoder)return _.get(i).encoder;const s={encoder:new R(e),initing:!0};return _.set(i,s),await s.encoder.init(),s.initing=!1,_.set(i,s),s.encoder};
|
package/dist/main.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var t=(t=>"undefined"!=typeof require?require:"undefined"!=typeof Proxy?new Proxy(t,{get:(t,e)=>("undefined"!=typeof require?require:t)[e]}):t)(function(t){if("undefined"!=typeof require)return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')});import{getSdk as e}from"@soga/sdk";import{Not as i}from"typeorm";import{UploadFile as s,UploadRunning as r,UploadRunningType as o}from"@soga/entities";import{UploadStatus as a,HostType as n,RecordFileKeep as d,RecordType as p}from"@soga/types";import{Piscina as h}from"piscina";import{getPrepare as c}from"@soga/file-encoder";import{getProgressPercent as u}from"@soga/utils";import{getSdk as f}from"@soga/sdk";import{resolve as l}from"path";import{MessageChannel as g}from"worker_threads";import{buildDError as y}from"@soga/error";var m=class{piscina;onProgress=async()=>{};onSuccess=async()=>{};onError=async()=>{};fileRepository;runningRepository;isRunning=!1;thread_count=2;maxThreads=10;threads=[];params;sdk;getValidThreads(t){return Math.min(Math.max(t??0,0),this.maxThreads)}constructor(e){this.params=e;const{dataSource:i}=e;this.fileRepository=i.getRepository(s),this.runningRepository=i.getRepository(r),this.thread_count=this.getValidThreads(e.threads??this.thread_count),this.sdk=f(e.sdk_domain),e.onProgress&&(this.onProgress=e.onProgress.bind(this)),e.onSuccess&&(this.onSuccess=e.onSuccess.bind(this)),e.onError&&(this.onError=e.onError.bind(this));const o=new h({filename:t.resolve("@soga/file-encoder")});this.piscina=o}async setThreads(t){const e=this.getValidThreads(t);this.thread_count=e,await this.start()}async init(){const t=await this.getUid();await this.fileRepository.update({uid:t,upload_status:a.ENCODE_PROCESS},{upload_status:a.NULL})}async start(){for(;this.isRunning;)await new Promise(t=>{setTimeout(t,100)});try{this.isRunning=!0;const t=await this.getUid(),e=this.threads.length,s=this.thread_count;if(e<s){const r=s-e;for(let e=0;e<r;e++){const e=await this.fileRepository.findOne({where:{uid:t,upload_status:a.NULL,type:i(p.FOLDER),is_paused:!1},order:{created_at:"ASC"}});if(!e)break;const s=this.getThread(e);this.threads.push(s),await this.fileRepository.update(e.id,{upload_status:a.ENCODE_PROCESS}),s.start().catch(t=>{})}}else if(e>s){const t=e-s,i=this.threads.slice(0,t);for(const t of i)await t.stop()}}finally{this.isRunning=!1}}async stopFiles(t){const e=this.threads.filter(e=>t.includes(e.id));await Promise.all(e.map(t=>t.stop())).catch(t=>{})}async stopAll(){await Promise.all(this.threads.map(t=>t.stop()))}getThread(t){const e=new AbortController,{port1:i,port2:s}=new g;return{id:t.id,uid:t.uid,start:async()=>{s.on("message",async t=>{if("percent"==t.type){const{id:e,data:i}=t,s=await this.fileRepository.findOneBy({id:e});if(!s)return;const{percent:r,step:o}=i;s.progress[o].percent=r;const a=u(s.progress);await this.fileRepository.update(s.id,{progress:s.progress,percent:a}),await this.onProgress(s)}});try{const s=await this.fileRepository.findOneBy({id:t.id});if(!s)return;if(s.upload_status!=a.ENCODE_PROCESS)return;await this.runningRepository.save(this.runningRepository.create({type:o.ENCODE,uid:t.uid,root_id:t.root_id,file_id:t.id}));const{id:r,uid:p,config:h,input:u,type:f,ftype:g,external_texts:y,is_attachment:m}=s,w=[];s.ali_host_id&&w.push(n.ALI),s.baidu_host_id&&w.push(n.BAIDU);const{encode_root:_}=await this.sdk.getSystemConfig(),R=l(_,s.is_attachment?"attachment":"file",s.id.toString());await this.fileRepository.update(t.id,{output_root:R});let E=!0,k=!0;m||(E=h.file_keeps==d.PREVIEW||h.file_keeps==d.BOTH,k=h.file_keeps==d.SOURCE||h.file_keeps==d.BOTH);const O={id:r,uid:p,keepPreview:E,keepSource:k,input:u,type:f,ftype:g,hosts:w,outputRoot:R,subtitles:y,ffmpegPath:this.params.ffmpegPath,debug:this.params.debug??!1,port:i};if(!Object.keys(s.progress??{}).length){const e=await c(O);await this.fileRepository.update(t.id,{progress:e.prepareResult})}const S=await this.piscina.run(O,{name:"encode",signal:e.signal,transferList:[i]});if(!S)throw new Error("upload chunk failed!");await this.fileRepository.update(t.id,{upload_status:a.ENCODE_SUCCESS,encoded:S}),await this.onSuccess(await this.fileRepository.findOneBy({id:r}))}catch(e){if("AbortError"==e.name)await this.fileRepository.update(t.id,{upload_status:a.NULL});else{const i=y(e,{message:"Error occurred during encoding",detail:`Error occurred during encoding: ${t.input.filepath}`});try{await this.fileRepository.update(t.id,{upload_status:a.ENCODE_ERROR,error:{type:"encode",message:i.message,stack:i.stack,details:i.details,cause:i.cause}}),await this.onError(await this.fileRepository.findOneBy({id:t.id}),e)}catch(t){throw t}}}finally{this.threads=this.threads.filter(e=>e.id!==t.id),s.close(),await this.runningRepository.delete({type:o.ENCODE,file_id:t.id}),await this.start()}},stop:async()=>{s.close(),e.abort(),this.threads=this.threads.filter(e=>e.id==t.id)}}}async getUid(){return(await this.sdk.getUserInfo({refresh:!1})).id}},w=new Map,_=async t=>{const i=e(t.sdk_domain),s=(await i.getUserInfo({refresh:!1})).id;for(;w.get(s)?.initing;)await new Promise(t=>setTimeout(t,100));if(w.get(s)?.encoder)return w.get(s).encoder;const r={encoder:new m(t),initing:!0};return w.set(s,r),await r.encoder.init(),r.initing=!1,w.set(s,r),r.encoder};export{m as Encoder,_ as getEncoder};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soga/encoder",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -10,28 +10,31 @@
|
|
|
10
10
|
"files": [
|
|
11
11
|
"dist"
|
|
12
12
|
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "rimraf dist && tsup src/main.ts --format cjs,esm --dts --minify terser",
|
|
15
|
+
"prepublishOnly": "npm run build"
|
|
16
|
+
},
|
|
13
17
|
"keywords": [],
|
|
14
18
|
"author": "",
|
|
15
19
|
"license": "ISC",
|
|
16
20
|
"dependencies": {
|
|
17
|
-
"@soga/entities": "
|
|
18
|
-
"@soga/sdk": "
|
|
19
|
-
"@soga/
|
|
20
|
-
"@soga/
|
|
21
|
-
"@soga/
|
|
22
|
-
"
|
|
23
|
-
"
|
|
21
|
+
"@soga/entities": "latest",
|
|
22
|
+
"@soga/sdk": "latest",
|
|
23
|
+
"@soga/file-encoder": "latest",
|
|
24
|
+
"@soga/utils": "latest",
|
|
25
|
+
"@soga/error": "latest",
|
|
26
|
+
"@soga/node-types": "latest",
|
|
27
|
+
"@soga/types": "latest",
|
|
28
|
+
"fs-extra": "^11.3.2",
|
|
24
29
|
"piscina": "^5.0.0",
|
|
25
30
|
"typeorm": "^0.3.24"
|
|
26
31
|
},
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"dev": "ts-node ./src/main.ts",
|
|
35
|
-
"lint": "eslint . --ext .ts"
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@soga/typescript-config": "latest",
|
|
34
|
+
"@types/node": "^24.5.2",
|
|
35
|
+
"rimraf": "^6.0.1",
|
|
36
|
+
"terser": "^5.43.1",
|
|
37
|
+
"tsup": "^8.5.0",
|
|
38
|
+
"typescript": "^5.8.3"
|
|
36
39
|
}
|
|
37
|
-
}
|
|
40
|
+
}
|
package/dist/encoder.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Encoder=void 0;const typeorm_1=require("typeorm"),entities_1=require("@soga/entities"),types_1=require("@soga/types"),piscina_1=require("piscina"),single_encoder_1=require("@soga/single-encoder"),utils_1=require("@soga/utils"),sdk_1=require("@soga/sdk"),path_1=require("path");class Encoder{piscina;onProgress=async()=>{};onSuccess=async()=>{};onError=async()=>{};fileRepository;isRunning=!1;thread_count=3;maxThreads=10;threads=[];params;sdk;getValidThreads(e){return Math.min(Math.max(e??0,0),this.maxThreads)}constructor(e){this.params=e;const{dataSource:t}=e;this.fileRepository=t.getRepository(entities_1.UploadFile),this.thread_count=this.getValidThreads(e.threads??this.thread_count),this.sdk=(0,sdk_1.getSdk)(e.sdk_domain),e.onProgress&&(this.onProgress=e.onProgress.bind(this)),e.onSuccess&&(this.onSuccess=e.onSuccess.bind(this)),e.onError&&(this.onError=e.onError.bind(this));const s=new piscina_1.Piscina({filename:require.resolve("@soga/single-encoder")});this.piscina=s,s.on("message",(async e=>{if("percent"==e.type){const{id:t,data:s}=e,i=await this.fileRepository.findOneBy({id:t});if(!i)return;const{percent:a,step:r}=s;i.progress[r].percent=a;const o=(0,utils_1.getProgressPercent)(i.progress);await this.fileRepository.update(i.id,{progress:i.progress,percent:o}),this.onProgress(i)}})),s.on("error",(async e=>{await(this.params.onError?.(null,new utils_1.SimpleError({code:12200,message:e.message,stack:e.stack})))}))}async setThreads(e){const t=this.getValidThreads(e);this.thread_count=t,await this.run()}async init(){const e=await this.getUid();await this.fileRepository.update({uid:e,encode_status:types_1.EncodeStatus.PROCESS},{encode_status:types_1.EncodeStatus.NULL})}async start(){await this.run()}async stopFiles(e){const t=this.threads.filter((t=>e.includes(t.id)));await Promise.all(t.map((e=>e.stop()))),await this.run()}async deleteFiles(e){const t=this.threads.filter((t=>e.includes(t.id)));await Promise.all(t.map((e=>e.stop())))}async stopAll(){await Promise.all(this.threads.map((e=>e.stop())))}getThread(e){const t=new AbortController;return{id:e.id,uid:e.uid,start:async()=>{try{const s=await this.fileRepository.findOneBy({id:e.id});if(s.encode_status!=types_1.EncodeStatus.PROCESS)return;if(!s)return;const{id:i,uid:a,config:r,inputs:o,type:n,external_texts:d}=s,p=[];s.ali_host_id&&p.push(types_1.HostType.ALI),s.baidu_host_id&&p.push(types_1.HostType.BAIDU);const{encode_root:c}=await this.sdk.getSystemConfig(),h=(0,path_1.resolve)(c,s.id.toString());if(await this.fileRepository.update(e.id,{output_root:h}),s.type!=types_1.RecordType.AFFIX){const c={id:i,uid:a,keepPreview:r.file_keeps==types_1.RecordFileKeep.PREVIEW||r.file_keeps==types_1.RecordFileKeep.BOTH,keepSource:r.file_keeps==types_1.RecordFileKeep.SOURCE||r.file_keeps==types_1.RecordFileKeep.BOTH,input:o[0],type:n,hosts:p,outputRoot:h,texts:d,ffmpegPath:this.params.ffmpegPath,debug:this.params.debug};if(!Object.keys(s.progress??{}).length){const t=await(0,single_encoder_1.prepare)(c);await this.fileRepository.update(e.id,{progress:t.data})}const u=await this.piscina.run(c,{name:"encode",signal:t.signal});if(!u)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,encoded:u})}else{const a={id:i,inputs:s.inputs,outputRoot:h,hosts:p,debug:this.params.debug};if(!Object.keys(s.progress??{}).length){const t=await(0,single_encoder_1.prepareAffix)(a);await this.fileRepository.update(e.id,{progress:t})}const r=await this.piscina.run(a,{name:"encodeAffix",signal:t.signal});if(!r)throw new Error("upload chunk failed!");await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.SUCCESS,encoded:{affix:r}})}await this.onSuccess(await this.fileRepository.findOneBy({id:i}))}catch(t){"AbortError"==t.name?await this.fileRepository.update(e.id,{encode_status:types_1.EncodeStatus.NULL}):"SimpleError"==t.name?await this.onError(await this.fileRepository.findOneBy({id:e.id}),t):await this.onError(await this.fileRepository.findOneBy({id:e.id}),new utils_1.SimpleError({code:12100,message:t.message,stack:t.stack}))}finally{this.threads=this.threads.filter((t=>t.id!==e.id)),await this.run()}},stop:async()=>{t.abort()}}}async run(){for(;this.isRunning;)await new Promise((e=>{setTimeout(e,100)}));try{this.isRunning=!0;const e=await this.getUid(),t=this.threads.length,s=this.thread_count;if(t<s){const i=s-t;for(let t=0;t<i;t++){const t=await this.fileRepository.findOne({where:{uid:e,encode_status:types_1.EncodeStatus.NULL,type:(0,typeorm_1.Not)(types_1.RecordType.FOLDER),is_paused:!1},order:{created_at:"ASC"}});if(!t)break;const s=this.getThread(t);this.threads.push(s),await this.fileRepository.update(t.id,{encode_status:types_1.EncodeStatus.PROCESS}),s.start()}}else if(t>s){const e=t-s,i=this.threads.slice(0,e);for(const e of i)await e.stop()}}finally{this.isRunning=!1}}async getUid(){return(await this.sdk.getUserInfo({refresh:!1})).id}}exports.Encoder=Encoder;
|
package/dist/types.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { UploadFile } from '@soga/entities';
|
|
2
|
-
import { DataSource } from 'typeorm';
|
|
3
|
-
export type EncoderParams = {
|
|
4
|
-
ffmpegPath: string;
|
|
5
|
-
sdk_domain: string;
|
|
6
|
-
dataSource: DataSource;
|
|
7
|
-
threads?: number;
|
|
8
|
-
debug?: boolean;
|
|
9
|
-
onProgress?: (file: UploadFile) => Promise<void>;
|
|
10
|
-
onSuccess?: (file: UploadFile) => Promise<void>;
|
|
11
|
-
onError?: (file: UploadFile | null, error?: Error) => Promise<void>;
|
|
12
|
-
};
|
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});
|