lostromb / concentus

Pure Portable C# and Java implementations of the Opus audio codec
Other
246 stars 49 forks source link

Crop Opus #52

Open BasselAshi opened 5 hours ago

BasselAshi commented 5 hours ago

I am receiving a byte array of webm/opus from the frontend in chunks and would like to decode it to be able to crop the audio on the go. The only decoding I see converts it to pcm. Will I be able to do such thing with this library?

lostromb commented 3 hours ago

uhhhh.....you'll have to be a bit more specific, what do you mean by "crop"? Taking a long opus stream and pulling out a subsection of the audio without re-encoding? I assume since you don't want to decode to PCM that you don't want to decode the audio, take the time slice of only what you want, and then re-encode that to opus again because of computational / quality loss limitations.

I think you're basically describing remuxing, which should actually work mostly fine with the way Opus is designed. If you have a series of Opus packets, you can remove any number of packets from the beginning or end and get a trimmed audio file with only minor changes to the output (mainly, that the first half-second or so of the trimmed audio will have a sort of "fade-in" from silence, even if the original audio was constant volume).

To do this you don't actually need any tools specific to the Opus codec itself. WebM is a variant of the Matroska container format, so if you had some code that could read+write that format then you could just identify the opus stream, pull the raw packets for that stream out of the container, and then put them into a new container (....also WebM, I assume?). Maybe this library or a similar one would help.