TeamPyOgg / PyOgg

Simple OGG Vorbis, Opus and FLAC bindings for Python
The Unlicense
63 stars 27 forks source link

Single-responsibility OggOpusWriter class #62

Closed LecronRu closed 3 years ago

LecronRu commented 3 years ago

I wanted to write an existing opus stream to a new media file. For example from OpusFileStream. It is needed to merge multiple files. But OggOpusWriter.write only works with pcm stream. That is, it combines the functions of the writer and encoder.

mattgwwalker commented 3 years ago

Hi LecronRu,

I’m not sure I understand your issue here.

Let me repeat what I think you said:

What do you mean by “merge”?

Do you wish to concatenate the three files? If you did this then your final file would be three minutes long.

Another interpretation of “merge” would be to combine them as tracks, and then your final output would be one minute long.

Both options are easy to do with PyOgg (at least the as yet unreleased version in the git repository).

The concatenate version of merge would be achieved, for example, by reading the three files into three different OpusFileStreams. You would then open one OpusFileReader and pass the first file’s buffer to write(), then the second file‘s buffer to write() and then the third file’s buffer to write(). Then call close() and you should have a concatenated version.

If you want to treat the three source files as tracks then you will need to open them, convert the bytes to floats, add the three floats together, normalise the resulting audio, convert the floats back to bytes and pass them to write().

If you would like to share your code and specify which of the two options you’re interested in, I might be able to help you more.

Also the documentation might be a little out of date as I‘ve only recently pushed some changes to OpusWriter and OpusBufferedEncoder. Those classes are still a work in progress and their interfaces may still change. Have a look at the tests to see examples of how they’re used.

Regards,

Matthew

On Fri, 13 Nov 2020 at 23:19, LecronRu notifications@github.com wrote:

I wanted to write an existing opus stream to a new media file. For example from OpusFileStream. It is needed to merge multiple files. But OggOpusWriter.write only works with pcm stream. That is, it combines the functions of the writer and encoder.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/TeamPyOgg/PyOgg/issues/62, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA653XBFT6UPRINURZO7ZI3SPUBZXANCNFSM4TUM2S5A .

LecronRu commented 3 years ago

Thank you for your help Yes, three minutes long. Unexpectedly use OpusFileReader for writing, when OpusFileWriter is present.

Those classes are still a work in progress and their interfaces may still change.

I will assume that OpusFileReader, OpusFileStream and OpusEncoder, at the types level, must have a similar interface to return a stream. It doesn't matter where we get the data from, OpusFileWriter only writes the opus stream. Pipeline read/encode -> write

mattgwwalker commented 3 years ago

I have added an example demonstrating how two OggOpus files can be concatenated together. Please see https://github.com/TeamPyOgg/PyOgg/blob/master/examples/03b-concat-ogg-opus.py

I would recommend you clone the latest repository from Github. This will give you the most recent version of PyOgg (unreleased), plus the example code and audio files that it uses.

Again, these interfaces have not been finalized, so please expect breaking changes in the (fairly near) future. However I expect that none of the changes will be very difficult for you to adapt to.

Are you happy that I close this issue?

Cheers,

Matthew

mattgwwalker commented 3 years ago

Also, my apologies for any confusion my first reply caused: OpusFileReader does not exist :o) Consider instead OpusFileStream or OpusFile. In the example I used OpusFile.

LecronRu commented 3 years ago

Are you happy that I close this issue?

no problems. Good luck. And thanks again.