TeamPyOgg / PyOgg

Simple OGG Vorbis, Opus and FLAC bindings for Python
The Unlicense
63 stars 27 forks source link

OggOpusWriter: Incorrect duration when sample rate is not 48kHz. #93

Open ijc8 opened 2 years ago

ijc8 commented 2 years ago

For supported sample rates other than 48k (i.e. 8k, 12k, 16k, 24k), OggOpusWriter generates an incorrectly reported duration, which causes bizarre behaviors depending on the audio player.

To reproduce:

Initially, I thought this was because OggOpusWriter always writes a sample rate of 0 (unspecified) in the Opus header. However, specifying the original sample rate in the header does not correct this duration issue:

import pyogg

sr = 12000

encoder = pyogg.OpusBufferedEncoder()
encoder.set_application("audio")
encoder.set_sampling_frequency(sr)
encoder.set_channels(1)
encoder.set_frame_size(20)

writer = pyogg.OggOpusWriter("should_be_1s.opus", encoder)
# HACK: Get OggOpusWriter to specify the sample rate in the header.
pyogg.OggOpusWriter._make_identification_header.__defaults__ = (sr,)
bytes_per_sample = 2
samples = bytearray(sr * bytes_per_sample)
writer.write(samples)
writer.close()

Running opusinfo on should_be_1s.opus correctly reports "Original sample rate: 12000Hz", but still reports a playback length of .25 seconds. Any idea what's going wrong?