Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ijc8 opened this issue Sep 19, 2021 · 0 comments
Open

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

ijc8 opened this issue Sep 19, 2021 · 0 comments

Comments

@ijc8
Copy link

ijc8 commented Sep 19, 2021

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:

  • Convert examples/left-right-demo-5s.wav to 12k (e.g. sox left-right-demo-5s.wav -r 12k 12k.wav; mv 12k.wav left-right-demo-5s.wav)
  • Run examples/03-write-ogg-opus.py
  • This generates left-right-demo-5s.wav.opus with an incorrect duration (ffmpeg reports 1.26s; opusinfo reports 2.149s), which will confuse various tools and audio players (e.g. VLC and Totem cut off early; pyogg throws an exception on open.)

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?

@ijc8 ijc8 changed the title OggOpusWriter: Write sample rate in Opus header. OggOpusWriter: Incorrect duration when sample rate is not 48kHz. Sep 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant