.opus file extension changes to .ogg in Opus file attachments

Aivaras

Well-known member
Affected version
v2.2.17, including the current version of xenforo.com
When I attach a file with the .opus extension to a post, the extension gets changed to .ogg during the process. If I then download the .ogg file, it still plays back as Opus. So it seems that XF automatically encapsulates Opus files in the OGG container. Why does this happen, and can it be avoided? We use Opus files as digital products, and changing their extensions is very misleading.

A possible cause of the problem may be 'opus' => 'audio/ogg' instead of 'opus' => 'audio/opus' in the snippet below:

 
So it seems that XF automatically encapsulates Opus files in the OGG container.
I dont think so, XenForo doesn't modify the file - it just changes the extension.

Why does this happen, and can it be avoided?

Opus packets are not self-delimiting, but are designed to be used inside a container of some sort which supplies the decoder with each packet's length. Opus was originally specified for encapsulation in Ogg containers, specified as audio/ogg; codecs=opus, and for Ogg Opus files the .opus filename extension is recommended.
So most likely your .opus files are Ogg containers that contain opus audio.
When XenForo detects an Ogg container, it automatically changes the fie extension to .ogg.
This isn't wrong technically as it is an Ogg container after all, but I understand why this may be confusing.

To avoid this you can add a little patch to XF/Http/Upload.php

Add
PHP:
// check if it is opus which has recommended file extension .opus and set this instead of .ogg
$first64 = substr($preamble, 0, 128);
if (strpos($first64, '4F70757348656164') !== false)
{
    $audioType = 'opus';
}
after
PHP:
$audioType = 'ogg';
and .opus files that are Ogg containers with opus audio should stay .opus (but .ogg files with opus audio will be changed to .opus).

A possible cause of the problem may be 'opus' => 'audio/ogg' instead of 'opus' => 'audio/opus' in the snippet below:
audio/ogg is the correct MIME type for an ogg container with opus audio.
 
Last edited:
Back
Top Bottom