This commit is contained in:
2025-10-13 22:13:19 +08:00
parent 78cc6dcf55
commit 5603d09e80
10 changed files with 73 additions and 1265 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/video-tools",
"version": "0.0.4",
"version": "0.0.5",
"description": "",
"main": "index.js",
"basename": "/root/video-tools",
@@ -10,10 +10,7 @@
"type": "system-app"
},
"scripts": {
"watch": "rollup -c rollup.config.mjs -w",
"build": "rollup -c rollup.config.mjs",
"dev": "cross-env NODE_ENV=development nodemon --delay 2.5 -e js,cjs,mjs --exec node dist/app.mjs",
"dev:watch": "cross-env NODE_ENV=development concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ",
"dev:bun": "bun run src/dev.ts --watch",
"test": "tsx test/**/*.ts",
"clean": "rm -rf dist",
@@ -39,6 +36,7 @@
"@kevisual/use-config": "^1.0.17",
"@kevisual/video": "^0.0.2",
"cookie": "^1.0.2",
"crypto-js": "^4.2.0",
"dayjs": "^1.11.13",
"eventemitter3": "^5.0.1",
"formidable": "^3.5.4",
@@ -50,12 +48,6 @@
"@kevisual/logger": "^0.0.4",
"@kevisual/types": "^0.0.10",
"@kevisual/use-config": "^1.0.17",
"@rollup/plugin-alias": "^5.1.1",
"@rollup/plugin-commonjs": "^28.0.3",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-replace": "^6.0.2",
"@rollup/plugin-typescript": "^12.1.2",
"@types/crypto-js": "^4.2.2",
"@types/formidable": "^3.4.5",
"@types/lodash-es": "^4.17.12",
@@ -72,13 +64,8 @@
"pg": "^8.16.0",
"pm2": "^6.0.6",
"rimraf": "^6.0.1",
"rollup": "^4.41.1",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.2.1",
"rollup-plugin-esbuild": "^6.2.1",
"sequelize": "^6.37.7",
"tape": "^5.9.0",
"tsup": "^8.5.0",
"tsx": "^4.19.4",
"typescript": "^5.8.3",
"ws": "npm:@kevisual/ws"

1128
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,75 +0,0 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import path from 'path';
import esbuild from 'rollup-plugin-esbuild';
import alias from '@rollup/plugin-alias';
import replace from '@rollup/plugin-replace';
import pkgs from './package.json' with {type: 'json'};
const isDev = process.env.NODE_ENV === 'development';
const input = isDev ? './src/dev.ts' : './src/main.ts';
/**
* @type {import('rollup').RollupOptions}
*/
const config = {
input,
output: {
dir: './dist',
entryFileNames: 'app.mjs',
chunkFileNames: '[name]-[hash].mjs',
format: 'esm',
},
plugins: [
replace({
preventAssignment: true, // 防止意外赋值
DEV_SERVER: JSON.stringify(isDev), // 替换 process.env.NODE_ENV
APP_VERSION: JSON.stringify(pkgs.version),
}),
alias({
// only esbuild needs to be configured
entries: [
{ find: '@', replacement: path.resolve('src') }, // 配置 @ 为 src 目录
{ find: 'http', replacement: 'node:http' },
{ find: 'https', replacement: 'node:https' },
{ find: 'fs', replacement: 'node:fs' },
{ find: 'path', replacement: 'node:path' },
{ find: 'crypto', replacement: 'node:crypto' },
{ find: 'zlib', replacement: 'node:zlib' },
{ find: 'stream', replacement: 'node:stream' },
{ find: 'net', replacement: 'node:net' },
{ find: 'tty', replacement: 'node:tty' },
{ find: 'tls', replacement: 'node:tls' },
{ find: 'buffer', replacement: 'node:buffer' },
{ find: 'timers', replacement: 'node:timers' },
// { find: 'string_decoder', replacement: 'node:string_decoder' },
{ find: 'dns', replacement: 'node:dns' },
{ find: 'domain', replacement: 'node:domain' },
{ find: 'os', replacement: 'node:os' },
{ find: 'events', replacement: 'node:events' },
{ find: 'url', replacement: 'node:url' },
{ find: 'assert', replacement: 'node:assert' },
{ find: 'util', replacement: 'node:util' },
],
}),
resolve({
preferBuiltins: true, // 强制优先使用内置模块
}),
commonjs(),
esbuild({
target: 'node22', //
minify: false, // 启用代码压缩
tsconfig: 'tsconfig.json',
}),
json(),
],
external: [
/@kevisual\/router(\/.*)?/, //, // 路由
/@kevisual\/use-config(\/.*)?/, //
'sequelize', // 数据库 orm
'ioredis', // redis
'pg', // pg
],
};
export default config;

View File

@@ -1,5 +1,5 @@
import * as zlib from 'zlib';
import { promisify } from 'util';
import * as zlib from 'node:zlib';
import { promisify } from 'node:util';
import { nanoid } from 'nanoid';
import { VolcEngineBase, uuid } from './base.ts';
@@ -61,6 +61,39 @@ function generateBeforePayload(sequence: number): Buffer {
return beforePayload;
}
export type ParsedMessage = {
isLastPackage: boolean;
payloadSequence?: number;
payloadMsg?: {
audio_info?: {
duration: number;
};
result?: {
additions?: {
log_id?: string;
};
text?: string;
utterances?: Array<{
additions?: {
fixed_prefix_result?: string;
};
definite?: boolean;
end_time?: number;
start_time?: number;
text?: string;
words?: Array<{
end_time: number;
start_time: number;
text: string;
}>;
}>;
};
error?: any;
};
payloadSize?: number;
code?: number;
seq?: number;
};
/**
* Parse response from the WebSocket server
*/
@@ -393,10 +426,11 @@ export class AsrWsClient extends VolcEngineBase {
// Wait for response
await sendVoice(audioData, segmentSize);
}
async onMessage(event: MessageEvent) {
try {
const parsed = parseResponse(Buffer.from(event.data as ArrayBuffer));
console.log(`Seq ${parsed.payloadSequence} response:`, parsed);
// console.log(`Seq ${parsed.payloadSequence} response:`, parsed);
if (typeof event.data === 'string') {
throw new Error('event.data is string: ' + event.data);
}
@@ -405,10 +439,9 @@ export class AsrWsClient extends VolcEngineBase {
this.emitter.emit('error', parsed);
this.isError = true;
}
this.emitter.emit('message', parsed);
if (parsed.isLastPackage) {
this.emitter.emit('end', parsed);
} else {
this.emitter.emit('message', parsed);
}
} catch (error) {
console.error('Error processing response:', error);
@@ -440,6 +473,14 @@ export class AsrWsClient extends VolcEngineBase {
throw error;
}
}
async setIsEnd(isEnd: boolean) {
super.setIsEnd(isEnd);
if (isEnd) {
// 发送空白包
const emptyBuffer = Buffer.alloc(10000);
this.sendVoiceStream(emptyBuffer);
}
}
/**
* 发送语音流, 最小10000
* @param data

View File

@@ -238,7 +238,7 @@ interface AudioItem {
id: string | number;
path: string;
}
// 流式语音识别
export class AsrWsClient extends VolcEngineBase {
private audioPath: string;
private cluster: string;

View File

@@ -1,11 +1,11 @@
import crypto from "node:crypto"
import { nanoid } from "nanoid"
export const FlashURL = "https://openspeech.bytedance.com/api/v3/auc/bigmodel/recognize/flash"
export const AsrBaseURL = 'https://openspeech.bytedance.com/api/v3/auc/bigmodel/submit'
export const AsrBase = 'volc.bigasr.auc'
export const AsrTurbo = 'volc.bigasr.auc_turbo'
const uuid = () => crypto.randomUUID()
const uuid = () => nanoid()
type AsrOptions = {
url?: string

View File

@@ -1,4 +1,3 @@
import { initWs } from '../../../ws-adapter/index.ts';
import { WSServer } from '../../provider/ws-server.ts';
import { nanoid } from 'nanoid';

View File

@@ -7,15 +7,22 @@ import fs from 'fs';
const main = async () => {
const audioId = '123';
const asrClient = new AsrWsClient({
appid: config.APP_ID,
token: config.TOKEN,
appid: config.VOLCENGINE_ASR_MODEL_APPID,
token: config.VOLCENGINE_ASR_MODEL_TOKEN,
});
asrClient.emitter.on('message', (result) => {
console.log('识别结果', JSON.stringify(result, null, 2));
})
asrClient.emitter.on('end', (result) => {
console.log('识别结束', JSON.stringify(result, null, 2));
})
await new Promise((resolve) => setTimeout(resolve, 2000));
const data = fs.readFileSync(audioPath);
await asrClient.sendVoiceFile(data);
await asrClient.sendVoiceFile(fs.readFileSync(blankAudioPath));
// await asrClient.sendVoiceFile(fs.readFileSync(blankAudioPath));
asrClient.setIsEnd(true);
await asrClient.sendVoiceFile(fs.readFileSync(audioPath2));
// await asrClient.sendVoiceFile(fs.readFileSync(audioPath2));
};
main();

View File

@@ -1,4 +1,6 @@
const isBrowser = process?.env?.BROWSER === 'true' || (typeof window !== 'undefined' && typeof window.document !== 'undefined');
const isBrowser = (typeof process === 'undefined') ||
(typeof window !== 'undefined' && typeof window.document !== 'undefined') ||
(typeof process !== 'undefined' && process?.env?.BROWSER === 'true');
const chantHttpToWs = (url: string) => {
if (url.startsWith('http://')) {
return url.replace('http://', 'ws://');

View File

@@ -1,41 +0,0 @@
import { defineConfig } from 'tsup';
// import glob from 'fast-glob';
// const services = glob.sync('src/services/*.ts');
import fs from 'fs';
const clean = () => {
const distDir = 'dist';
if (fs.existsSync(distDir)) {
fs.rmSync(distDir, { recursive: true, force: true });
}
};
clean();
const entrys = ['src/index.ts'];
const nodeEntrys = ['src/dev.ts'];
const getCommonConfig = (opts = {}) => {
return {
entry: opts.entry,
outExtension: ({ format }) => ({
js: format === 'esm' ? '.mjs' : '.js',
}),
splitting: false,
sourcemap: false,
// clean: true,
format: 'esm',
external: ['dotenv'],
dts: true,
outDir: 'dist',
tsconfig: 'tsconfig.json',
...opts,
define: {
'process.env.IS_BROWSER': JSON.stringify(process.env.BROWSER || false),
...opts.define,
},
};
};
export default defineConfig([
// getCommonConfig({ entry: entrys, define: { 'process.env.IS_BROWSER': JSON.stringify(true) } }), // 浏览器
getCommonConfig({ entry: nodeEntrys, define: { 'process.env.IS_BROWSER': JSON.stringify(false) } }), // node
]);