-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
77 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { fileTypeFromBuffer } from "file-type" | ||
import { PassThrough } from "stream" | ||
import { logVerbose } from "./util" | ||
import { writeFile } from "fs/promises" | ||
import { Readable } from "stream" | ||
|
||
async function importFfmpeg() { | ||
const ffmpeg = await import("fluent-ffmpeg") | ||
return ffmpeg.default | ||
} | ||
|
||
export async function convertToAudioBlob(data: Buffer): Promise<Blob> { | ||
const mime = await fileTypeFromBuffer(data) | ||
if (/^audio\//.test(mime?.mime)) | ||
return new Blob([data], { type: mime.mime }) | ||
|
||
logVerbose(`ffmpeg: extracting audio from video...`) | ||
return new Promise<Blob>(async (resolve, reject) => { | ||
const inputStream = new PassThrough() | ||
inputStream.end(data) | ||
await writeFile("input.mp4", data) | ||
|
||
console.log(await fileTypeFromBuffer(data)) | ||
const outputStream = new PassThrough() | ||
const chunks: Buffer[] = [] | ||
|
||
outputStream.on("data", (chunk) => chunks.push(chunk)) | ||
outputStream.on("end", async () => { | ||
const buffer = Buffer.concat(chunks) | ||
const mime = await fileTypeFromBuffer(buffer) | ||
await writeFile("audio.wav", buffer) | ||
resolve(new Blob([buffer], { type: mime.mime })) | ||
}) | ||
outputStream.on("error", reject) | ||
|
||
const ffmpeg = await importFfmpeg() | ||
ffmpeg(inputStream) | ||
.toFormat("wav") | ||
.audioBitrate("16k") | ||
.on("error", reject) | ||
.pipe(outputStream) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters