@web-media/ogg-polyfill
is a web infrastructure project that provides Ogg
format support for Safari.
Compared to other implementations, @web-media/ogg-polyfill
offers superior
performance thanks to our implementation approach. In fact, we don't perform
complex transcoding on media files; instead, we simply replace the audio
container with the CAF format.
By doing so, we only need to break down and reassemble the binary sequences of
the Ogg files, a process so fast that it's hardly noticeable to our users.
- To play an Ogg file from another origin, the server must respond with a CORS header in accordance with the same-origin policy for media files.
- Vorbis encoding is not supported since Safari does not provide Vorbis encoding support within the browser.
- Lossless encoding is not supported. Typically, you can use the FLAC container to achieve the playback. Thus, we haven't seen a need to implement this feature. If you have such a requirement, please let us know!
- Ogg files containing VBR Opus can be converted to a legal CAF, but the result is unplayable even with Safari 17. Use CBR (not VBR, not CVBR) only.
Currently, our implementation does not automatically replace all Ogg media requests. Please use this API to perform the operation.
const x = fetchOggOpusFile(oggExample);
const caf = oggOpusToCaf(x, true);
let done = false;
let data: Uint8Array[] = [];
while (!done) {
const { done: d, value: v } = await caf.next();
done = !!d;
if (v) {
data.push(v.raw);
}
}
const cafFileSize = data.reduce((acc, curr) => acc + curr.byteLength, 0);
const cafBinaryData = new Uint8Array(cafFileSize);
let offset = 0;
for (const chunk of data) {
cafBinaryData.set(chunk, offset);
offset += chunk.byteLength;
}
cafBinaryData
is the converted file. You can use the URL.createObjectURL
API to pass this file to an audio tag for playback, or you can directly pass
this binary sequence to the Audio Context API for playback or processing.
Ah, let's give a standing ovation to Apple for their splendidly dreadful browser implementation and their impeccable taste in technology. Without these awe-inspiring gems of incompetence, we wouldn't have been graced with the glorious existence of this marvelous tool 🙃.