question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not an issue - Looking for an example of writing encoded Opus data to a file

See original GitHub issue

I have opus encoded bytes and I’d like to write them to an opus file; since the only example I found uses an existing opus file to read from then write to a new file, it doesn’t fit my need. I don’t have a source file, I am taking pcm samples and encoding them with an Opus encoder, so I tried to do this and it didn’t work:

    OggPacket pkt = new OggPacket((byte[]) outBuf.getData());
    OpusAudioData oad = new OpusAudioData(pkt);
    opusFile.writeAudioData(oad);

As well as this

    OpusAudioData oad = new OpusAudioData((byte[]) outBuf.getData());
    opusFile.writeAudioData(oad);

When I tried using the OggPacket, I get this exception:

java.lang.NullPointerException: null
	at org.gagravarr.ogg.OggPacket.getGranulePosition(OggPacket.java:74)
	at org.gagravarr.ogg.OggStreamAudioVisualData.<init>(OggStreamAudioVisualData.java:28)
	at org.gagravarr.ogg.OggStreamAudioData.<init>(OggStreamAudioData.java:21)
	at org.gagravarr.opus.OpusAudioData.<init>(OpusAudioData.java:29)

My file creation looks like so:

        Path opus = Paths.get("target/test-classes/out.opus");
        // clean up existing output files
        try {
            Files.deleteIfExists(opus);
        } catch (IOException e1) {
        }
        OpusFile opusFile = null;
        try {
            // set opus out
            OpusTags ot = new OpusTags();
            OpusInfo oi = new OpusInfo();
            oi.setSampleRate(48000);
            oi.setNumChannels(2);
            //oi.preSkip = 3840
            opusFile = new OpusFile(new FileOutputStream(opus.toFile()), oi, ot);

Any examples or clues as to how to accomplish what I need would be greatly appretiated.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
mondaincommented, Oct 30, 2020

@andrm I pulled the PR https://github.com/Gagravarr/VorbisJava/pull/33 into my fork and the granule warnings disappeared and the duration is exactly correct now in ffprobe and opustool Playback length: 00:01:00.00

1reaction
andrmcommented, Oct 29, 2020

@mondain Please see https://tools.ietf.org/html/rfc7845.html on how to calculate granule position and pre-skip.

core/src/main/java/org/gagravarr/opus/OpusStatistics.java has also instructive code, you see where the errors are coming from.

Read more comments on GitHub >

github_iconTop Results From Across the Web

avformat_open_input cannot open a file with raw opus audio ...
What do you mean by raw audio data in opus format? Opus is not self-delimiting, it needs a container (OGG, Matroska, ISO BMFF...
Read more >
RFC 7845
This allows data encoded in the Opus format to be stored in an Ogg logical bitstream. Status of This Memo This is an...
Read more >
News Archive – Opus Codec
The opusfile library provides seeking, decode, and playback of Opus streams in the Ogg container (.opus files) including over http(s) on posix and...
Read more >
opus - Go Packages
Decode encoded Opus data into the supplied buffer. On success, returns the number of samples correctly written to the target buffer. func (* ......
Read more >
Supported media formats - Android Developers
Video formats; Video encoding recommendations ... device might support additional formats or file types that are not listed in these tables.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found