Gagravarr / VorbisJava

A library for working with Ogg Vorbis files
Apache License 2.0
126 stars 26 forks source link

Trim opus.ogg file without encoding/transcoding #45

Closed omkar-tenkale closed 2 years ago

omkar-tenkale commented 2 years ago

I/m having difficulting understanding the library and implement it for a file trim

Input inputfile.opus.ogg
startPositionMs: 10000 endPositionMs: 15000

Output trimmedInputFile.opus.ogg

Any help on how can I implement this with this library? @Gagravarr @nick-burch-flec @andrm

andrm commented 2 years ago

For this to work you need to understand the granule position of Opus. It tells the timestamp/play time of the file/stream. See https://datatracker.ietf.org/doc/html/rfc7845 Non-working pseudo code (I use the real one to read an opus file):

var fis = new BufferedInputStream(new FileInputStream(file),1024);
OggFile ogg = new OggFile(fis);
OpusFile opus = new OpusFile(ogg);
OpusAudioData p;
List<OpusAudioData> dataForOutput = new ArrayList();

while ((p = opus.getNextAudioPacket()) != null) {
  gp = p.getGranulePosition();
  -- calculate playtime with gp
  -- if within desired time window:
     dataForOutput.add(p);
}

 -- write data in list to new file
OggFile out_ogg;
OggPacketWriter writer;

out_ogg = new OggFile(new FileOutputStream(temp_file));
writer = out_ogg.getPacketWriter(some_stream_id);
// write info packet
// write tags
// write content: (not sure if this works, my code parses the packets into frames)
dataForOutput.forEach(packet -> {
OggPacket ogg = new OggPacket(packet.getData());
writer.buffer(ogg);
}

Sorry, I don't have more time.

andrm commented 2 years ago

@omkar-tenkale please close this issue if my answer has helped you.

omkar-tenkale commented 2 years ago

sure, thanks