首页 > 解决方案 > 如何从 ogg/opus 文件中一一读取 OPUS 数据包

问题描述

我需要从 ogg/opus 文件中一一读取 OPUS 数据包,并以 OPUS 格式进一步发送它们,因此无需解码。我正在查看opusfile库,但 API 和示例相当复杂,并且更专注于解码文件并获得结果 PCM。有没有办法用这个库实现我想要的,以及如何实现?如果不是,我还有什么其他选择?

标签: codecoggopus

解决方案


我想我有点晚了。我有非常相似的用例,我想从 ogg opus 文件中提取 opus 数据包。我找到了这个答案

https://stackoverflow.com/a/26285277/10110652

以下代码将帮助您在 while 循环内的 'nextPacket' byteArray 中获取 opus 数据包(未解码为 pcm)

File audioFile = null; 
    try {
        audioFile = new File("xyz.ogg"); //input ogg opus file
        oggFile = new FileStream(new RandomAccessFile(audioFile, "r"));

        for (LogicalOggStream stream : (Collection<LogicalOggStream>) oggFile.getLogicalStreams()) {
            byte[] nextPacket = stream.getNextOggPacket();
            while (nextPacket != null) {
                    //nextPacket here contains opus packet
                    //do what ever you want to do with this packet
                 
                nextPacket = stream.getNextOggPacket();
            }
        }

    } catch (EndOfOggStreamException e) {
        System.out.println("End of File");
        //file usually get over by raising this exception while execution getNextOggPacket()
    }

相同的程序可用于提取 ogg 容器中的 vorbis 编码数据包,即 ogg vorbis 文件。

快乐编码!!!


推荐阅读