feat: add silent check
This commit is contained in:
		
							
								
								
									
										12
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								package.json
									
									
									
									
									
								
							@@ -1,14 +1,17 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "@kevisual/video",
 | 
			
		||||
  "version": "0.0.1",
 | 
			
		||||
  "version": "0.0.2",
 | 
			
		||||
  "description": "",
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "build": "tsup"
 | 
			
		||||
  },
 | 
			
		||||
  "files": [
 | 
			
		||||
    "dist"
 | 
			
		||||
  ],
 | 
			
		||||
  "keywords": [],
 | 
			
		||||
  "author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
 | 
			
		||||
  "license": "MIT",
 | 
			
		||||
  "packageManager": "pnpm@10.6.2",
 | 
			
		||||
  "packageManager": "pnpm@10.8.1",
 | 
			
		||||
  "type": "module",
 | 
			
		||||
  "devDependencies": {
 | 
			
		||||
    "@types/node": "^22.14.1",
 | 
			
		||||
@@ -16,7 +19,10 @@
 | 
			
		||||
    "typescript": "^5.8.3"
 | 
			
		||||
  },
 | 
			
		||||
  "exports": {
 | 
			
		||||
    ".": "./dist/index.mjs"
 | 
			
		||||
    ".": {
 | 
			
		||||
      "import": "./dist/index.mjs",
 | 
			
		||||
      "require": "./dist/index.js"
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  "publishConfig": {
 | 
			
		||||
    "access": "public"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,4 @@
 | 
			
		||||
import { encodeWav, decodeWav } from './wav-converter/index.ts';
 | 
			
		||||
import { isSilentNoLength, isChunkSilent } from './silent/index.ts';
 | 
			
		||||
 | 
			
		||||
export { encodeWav, decodeWav };
 | 
			
		||||
export { encodeWav, decodeWav, isSilentNoLength, isChunkSilent };
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										27
									
								
								src/silent/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/silent/index.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
/**
 | 
			
		||||
 * 判断是否为无声,
 | 
			
		||||
 * 方法 1:检查 Buffer 是否全 0(适用于原始 PCM 数据)
 | 
			
		||||
 * @param chunk
 | 
			
		||||
 * @returns
 | 
			
		||||
 */
 | 
			
		||||
export const isSilentNoLength = (chunk: Buffer) => {
 | 
			
		||||
  const isSilent = Buffer.compare(chunk, Buffer.alloc(chunk.length)) === 0;
 | 
			
		||||
  return isSilent;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 判断是否为无声, 统计振幅(更通用)
 | 
			
		||||
 * 方法 2:检查每个样本是否低于阈值
 | 
			
		||||
 * @param chunk
 | 
			
		||||
 * @param threshold
 | 
			
		||||
 * @returns
 | 
			
		||||
 */
 | 
			
		||||
export function isChunkSilent(chunk: Buffer, threshold = 0.01) {
 | 
			
		||||
  const floats = new Float32Array(chunk.buffer);
 | 
			
		||||
  for (let i = 0; i < floats.length; i++) {
 | 
			
		||||
    if (Math.abs(floats[i]) > threshold) {
 | 
			
		||||
      return false; // 发现非静音样本
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return true; // 所有样本均低于阈值
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user