devowlio / node-lame

LAME is an open-source encoder that encodes and decodes audio to the MP3 file format. For all MP3 needs a Node.js wrapper of the full LAME command line.
Other
81 stars 9 forks source link

Error encoding Buffer to Buffer #26

Closed jakobsuckow closed 3 years ago

jakobsuckow commented 3 years ago

How to reproduce

Code: `@Injectable() export class AudioService { lame: Lame; constructor( @InjectRepository(AudioEntity) private audioRepository: Repository, ) { this.lame = new Lame({ output: "buffer", bitrate: 128 }); }

async create(file: CreateFileDto) { this.lame.setBuffer(file.buffer); this.lame.encode().then(() => { const buffer = this.lame.getBuffer(); console.log(buffer); }) } }`

Error: ERROR: ENOENT: no such file or directory, unlink '/usr/src/app/node_modules/node-lame/lib/build/../../temp/encoded/tsYL33Yk2swI83......

Package.json

{ "scripts": { "prebuild": "rimraf dist", "build": "nest build", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "nest start", "start:dev": "nest start --watch", "start:debug": "nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "test": "jest", "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { "@azure/storage-blob": "^12.7.0", "@nestjs/common": "^7.0.0", "@nestjs/config": "^1.0.1", "@nestjs/core": "^7.0.0", "@nestjs/mapped-types": "*", "@nestjs/platform-express": "^7.0.0", "@nestjs/typeorm": "^8.0.2", "class-transformer": "^0.4.0", "class-validator": "^0.13.1", "ffmpeg": "^0.0.4", "fluent-ffmpeg": "^2.1.2", "get-audio-duration": "^3.0.0", "ibm-watson": "^6.1.1", "node-lame": "^1.3.1", "pg": "^8.7.1", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7", "typeorm": "^0.2.36", "winston": "^3.3.3" }, "devDependencies": { "@nestjs/cli": "^7.0.0", "@nestjs/schematics": "^7.0.0", "@nestjs/testing": "^7.0.0", "@types/express": "^4.17.3", "@types/ffmpeg": "^1.0.4", "@types/fluent-ffmpeg": "^2.1.18", "@types/jest": "26.0.10", "@types/node": "^13.9.1", "@types/supertest": "^2.0.8", "@typescript-eslint/eslint-plugin": "3.9.1", "@typescript-eslint/parser": "3.9.1", "eslint": "7.7.0", "eslint-config-prettier": "^6.10.0", "eslint-plugin-import": "^2.20.1", "jest": "26.4.2", "prettier": "^1.19.1", "supertest": "^4.0.2", "ts-jest": "26.2.0", "ts-loader": "^6.2.1", "ts-node": "9.0.0", "tsconfig-paths": "^3.9.0", "typescript": "^3.7.4" }, "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".spec.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, "coverageDirectory": "../coverage", "testEnvironment": "node" } }

What is working: The following code works this.lame.setFile("./voice.mp3"); this.lame.encode().then(async () => { const buffer = this.lame.getBuffer(); const res = await this.azureBlobService.upload({ buffer: buffer, mimetype: "audio/mp3", originalname: "test" }); console.log(res); });

jankarres commented 3 years ago

Please take a look at https://github.com/devowlio/node-lame/issues/1 (includes a demo) and https://github.com/devowlio/node-lame/issues/10. Do these issues include the solution for you?

zouchengzhuo commented 3 years ago

@jankarres After two hours debug, I found a stupid problem! the real error is "Error: lame: Warning: unsupported audio format", but what i got is "RROR: ENOENT: no such file or directory, unlink ..."

.catch((error: Error) => {
      this.removeTempFilesOnError();
      throw error;
});

and in removeTempFilesOnError

removeTempFilesOnError() {
    if (this.fileBufferTempFilePath != undefined) {
        fs_1.unlinkSync(this.fileBufferTempFilePath);
    }
    if (this.progressedBufferTempFilePath != undefined) {
        fs_1.unlinkSync(this.progressedBufferTempFilePath);
    }
}

when error occurs, removeTempFilesOnError will always throw an error of "RROR: ENOENT: no such file or directory, unlink ...", the developers can not get the true reason!!

the true reason is i passed in raw pcm but not specified sampling rate and mono/stereo/jstereo.

jankarres commented 3 years ago

Thanks for pointing this bug out, @zouchengzhuo. I have fixed it and released node-lame@1.3.2.