41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
const AUDIO_MIME_CANDIDATES = [
|
|
"audio/webm;codecs=opus",
|
|
"audio/webm",
|
|
"audio/ogg;codecs=opus",
|
|
"audio/ogg",
|
|
"audio/mp4",
|
|
"audio/mpeg",
|
|
"audio/wav",
|
|
]
|
|
|
|
const AUDIO_MIME_EXTENSION_MAP: Record<string, string> = {
|
|
"audio/webm": "webm",
|
|
"audio/ogg": "ogg",
|
|
"audio/mp4": "m4a",
|
|
"audio/mpeg": "mp3",
|
|
"audio/wav": "wav",
|
|
}
|
|
|
|
export function normalizeMimeType(mimeType: string) {
|
|
return mimeType.split(";")[0].trim().toLowerCase()
|
|
}
|
|
|
|
export function pickSupportedMimeType(isTypeSupported?: (mimeType: string) => boolean) {
|
|
const checker = isTypeSupported ?? (
|
|
typeof MediaRecorder === "undefined" ? undefined : MediaRecorder.isTypeSupported.bind(MediaRecorder)
|
|
)
|
|
|
|
if (!checker) return ""
|
|
|
|
for (const candidate of AUDIO_MIME_CANDIDATES) {
|
|
if (checker(candidate)) return candidate
|
|
}
|
|
return ""
|
|
}
|
|
|
|
export function buildAudioFileName(mimeType: string, now: Date = new Date()) {
|
|
const normalized = normalizeMimeType(mimeType)
|
|
const ext = AUDIO_MIME_EXTENSION_MAP[normalized] ?? "webm"
|
|
const timestamp = now.toISOString().replace(/[:.]/g, "-")
|
|
return `audio-${timestamp}.${ext}`
|
|
}
|