Skip to content

Commit 4ac4243

Browse files
authored
Merge pull request #79 from ChetanXpro/fix/mislabeled-wav-handling
fix: handling of mislabeled WAV files with non-WAV content
2 parents 3b64cdd + 12d9ec8 commit 4ac4243

File tree

1 file changed

+55
-14
lines changed

1 file changed

+55
-14
lines changed

src/utils.ts

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,66 @@ export const checkIfFileExists = (filePath: string) => {
88
}
99
}
1010

11-
export const convertToWavType = async (inputFilePath: string, verbose: boolean) => {
12-
const fileExtension = path.extname(inputFilePath).toLowerCase()
11+
async function isValidWavHeader(filePath) {
12+
return new Promise((resolve, reject) => {
13+
const readable = fs.createReadStream(filePath, { end: 11 })
14+
let data = ''
1315

14-
if (fileExtension === '.wav') {
15-
return inputFilePath
16-
}
16+
readable.on('data', chunk => {
17+
data += chunk.toString('binary')
18+
})
1719

18-
const outputFilePath = path.join(path.dirname(inputFilePath), `${path.basename(inputFilePath, fileExtension)}.wav`)
20+
readable.on('end', () => {
21+
const isValid = data.startsWith('RIFF') || data.startsWith('RIFX')
22+
resolve(isValid)
23+
})
1924

20-
if (verbose) {
21-
console.log(`[Nodejs-whisper] Converting audio to WAV file type...\n`)
22-
}
25+
readable.on('error', err => {
26+
reject(err)
27+
})
28+
})
29+
}
2330

24-
const command = `ffmpeg -nostats -loglevel error -y -i "${inputFilePath}" -ar 16000 -ac 1 -c:a pcm_s16le "${outputFilePath}"`
25-
const result = shell.exec(command, { silent: !verbose })
31+
export const convertToWavType = async (inputFilePath, verbose) => {
32+
const fileExtension = path.extname(inputFilePath).toLowerCase()
2633

27-
if (result.code !== 0) {
28-
throw new Error(`[Nodejs-whisper] Failed to convert audio file: ${result.stderr}`)
34+
if (verbose) {
35+
console.log(`[Nodejs-whisper] Checking if the file is a valid WAV: ${inputFilePath}`)
2936
}
3037

31-
return outputFilePath
38+
if (fileExtension === '.wav') {
39+
const isWav = await isValidWavHeader(inputFilePath)
40+
if (isWav) {
41+
if (verbose) {
42+
console.log(`[Nodejs-whisper] File is a valid WAV file.`)
43+
}
44+
return inputFilePath
45+
} else {
46+
if (verbose) {
47+
console.log(`[Nodejs-whisper] File has a .wav extension but is not a valid WAV, overwriting...`)
48+
}
49+
// Overwrite the original WAV file
50+
const command = `ffmpeg -nostats -loglevel error -y -i "${inputFilePath}" -ar 16000 -ac 1 -c:a pcm_s16le "${inputFilePath}"`
51+
const result = shell.exec(command, { silent: !verbose })
52+
if (result.code !== 0) {
53+
throw new Error(`[Nodejs-whisper] Failed to convert audio file: ${result.stderr}`)
54+
}
55+
return inputFilePath
56+
}
57+
} else {
58+
// Convert to a new WAV file
59+
const outputFilePath = path.join(
60+
path.dirname(inputFilePath),
61+
`${path.basename(inputFilePath, fileExtension)}.wav`
62+
)
63+
if (verbose) {
64+
console.log(`[Nodejs-whisper] Converting to a new WAV file: ${outputFilePath}`)
65+
}
66+
const command = `ffmpeg -nostats -loglevel error -y -i "${inputFilePath}" -ar 16000 -ac 1 -c:a pcm_s16le "${outputFilePath}"`
67+
const result = shell.exec(command, { silent: !verbose })
68+
if (result.code !== 0) {
69+
throw new Error(`[Nodejs-whisper] Failed to convert audio file: ${result.stderr}`)
70+
}
71+
return outputFilePath
72+
}
3273
}

0 commit comments

Comments
 (0)