temp
This commit is contained in:
commit
2d29bcc489
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
node_modules
|
||||
dist
|
||||
.DS_Store
|
||||
|
||||
|
||||
.env*
|
||||
!.env*example
|
2
.npmrc
Normal file
2
.npmrc
Normal file
@ -0,0 +1,2 @@
|
||||
//npm.xiongxiao.me/:_authToken=${ME_NPM_TOKEN}
|
||||
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
|
2
demos/test-convert/.gitignore
vendored
Normal file
2
demos/test-convert/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
files/*
|
||||
!files/test.pcm
|
BIN
demos/test-convert/files/test.pcm
Normal file
BIN
demos/test-convert/files/test.pcm
Normal file
Binary file not shown.
12
demos/test-convert/index.ts
Normal file
12
demos/test-convert/index.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { encodeWav, decodeWav } from '../../src/wav-converter';
|
||||
import fs from 'fs';
|
||||
const pcm = fs.readFileSync('./files/test.pcm');
|
||||
|
||||
const wav = encodeWav(pcm, {
|
||||
sampleRate: 16000,
|
||||
numChannels: 1,
|
||||
byteRate: 16,
|
||||
});
|
||||
console.log(wav);
|
||||
|
||||
fs.writeFileSync('./files/test.wav', wav);
|
24
package.json
Normal file
24
package.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "@kevisual/video",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"build": "tsup"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
||||
"license": "MIT",
|
||||
"packageManager": "pnpm@10.6.2",
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.14.1",
|
||||
"tsup": "^8.4.0",
|
||||
"typescript": "^5.8.3"
|
||||
},
|
||||
"exports": {
|
||||
".": "./dist/index.mjs"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
1102
pnpm-lock.yaml
generated
Normal file
1102
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
3
src/index.ts
Normal file
3
src/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { encodeWav, decodeWav } from './wav-converter/index.ts';
|
||||
|
||||
export { encodeWav, decodeWav };
|
69
src/wav-converter/index.ts
Normal file
69
src/wav-converter/index.ts
Normal file
@ -0,0 +1,69 @@
|
||||
type EncodeWavOptions = {
|
||||
numChannels?: number;
|
||||
sampleRate?: number;
|
||||
byteRate?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* 编码pcm文件为wav文件
|
||||
* @param rawPCM
|
||||
* @param options
|
||||
* @returns
|
||||
*/
|
||||
export function encodeWav(rawPCM: Buffer | string, options?: EncodeWavOptions) {
|
||||
if (typeof rawPCM === 'string') {
|
||||
rawPCM = Buffer.from(rawPCM, 'binary');
|
||||
}
|
||||
|
||||
if (!Buffer.isBuffer(rawPCM)) {
|
||||
throw new TypeError('pcm data must be Buffer or string');
|
||||
}
|
||||
const opt = options || {};
|
||||
const sampleRate = opt.sampleRate || 16000;
|
||||
const numChannels = opt.numChannels || 1;
|
||||
const byteRate = opt.byteRate || 16;
|
||||
|
||||
const buf = rawPCM;
|
||||
const header = Buffer.alloc(44);
|
||||
|
||||
header.write('RIFF', 0);
|
||||
header.writeUInt32LE(buf.length, 4);
|
||||
header.write('WAVE', 8);
|
||||
header.write('fmt ', 12);
|
||||
header.writeUInt8(16, 16);
|
||||
header.writeUInt8(1, 20);
|
||||
header.writeUInt8(numChannels, 22);
|
||||
header.writeUInt32LE(sampleRate, 24);
|
||||
header.writeUInt32LE(byteRate, 28);
|
||||
header.writeUInt8(4, 32);
|
||||
header.writeUInt8(16, 34);
|
||||
header.write('data', 36);
|
||||
header.writeUInt32LE(buf.length + 44 - 8, 40);
|
||||
|
||||
return Buffer.concat([header, buf]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码wav文件
|
||||
* @param rawWav
|
||||
* @returns
|
||||
*/
|
||||
export function decodeWav(rawWav: Buffer | string) {
|
||||
if (typeof rawWav === 'string') {
|
||||
rawWav = Buffer.from(rawWav, 'binary');
|
||||
}
|
||||
|
||||
if (!Buffer.isBuffer(rawWav)) {
|
||||
throw new TypeError('pcm data must be Buffer or string');
|
||||
}
|
||||
|
||||
// remove the header of pcm format
|
||||
rawWav = rawWav.subarray(44);
|
||||
|
||||
return rawWav;
|
||||
}
|
||||
|
||||
export const converter = {
|
||||
encodeWav,
|
||||
decodeWav,
|
||||
};
|
34
tsconfig.json
Normal file
34
tsconfig.json
Normal file
@ -0,0 +1,34 @@
|
||||
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "NodeNext",
|
||||
"target": "esnext",
|
||||
"noImplicitAny": false,
|
||||
"outDir": "./dist",
|
||||
"sourceMap": false,
|
||||
"allowJs": true,
|
||||
"newLine": "LF",
|
||||
"baseUrl": "./",
|
||||
"typeRoots": [
|
||||
"node_modules/@types",
|
||||
],
|
||||
"declaration": false,
|
||||
"noEmit": true,
|
||||
"allowImportingTsExtensions": true,
|
||||
"moduleResolution": "NodeNext",
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"esModuleInterop": true,
|
||||
"paths": {
|
||||
"@/*": [
|
||||
"src/*"
|
||||
],
|
||||
}
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
]
|
||||
}
|
20
tsup.config.ts
Normal file
20
tsup.config.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { defineConfig } from 'tsup';
|
||||
// import glob from 'fast-glob';
|
||||
// const services = glob.sync('src/services/*.ts');
|
||||
|
||||
const entrys = ['src/index.ts'];
|
||||
|
||||
export default defineConfig({
|
||||
entry: entrys,
|
||||
outExtension: ({ format }) => ({
|
||||
js: format === 'esm' ? '.mjs' : '.js',
|
||||
}),
|
||||
splitting: false,
|
||||
sourcemap: false,
|
||||
clean: true,
|
||||
format: 'esm',
|
||||
external: ['dotenv'],
|
||||
dts: true,
|
||||
outDir: 'dist',
|
||||
tsconfig: 'tsconfig.json',
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user