How to cut, edit and merge OGG files in C#?
Asked Answered
G

2

5

I have an ogg vorbis file and I have to do two operations with it:

  1. Cutting a part of a file from one position to another
  2. Merging another file with existing one

How can I do these two operations in C#?

Georama answered 16/11, 2011 at 14:0 Comment(3)
I tried to read official Xiph.org documentation but I haven't found any useful methods for my purposes.Georama
What do you mean by "merge"? Simple concatenation?Scaphoid
Yes, I want to simply append new file to the existing oneGeorama
S
5

I'd look into the c documentation for libogg, and figure out how to do this with c. And then write almost the same code in C# using a wrapper over libogg.

I've created a low level wrapper over libogg and libvorbis using the interop assistant: https://github.com/CodesInChaos/Xiph/blob/master/LowLevel.cs

That project also contains some higher level constructs, but I don't think they'll be useful for what you're doing.

BTW if the stream IDs between the files differ, you can simply append a file to another creating a valid file that plays both streams in sequence.

You probably need to read the input files packet wise using the decoding API, and then write the combined data out packet wise. Possibly replacing the stream ID and granulepos in between.

StreamID is an integer that identifies substreams in an ogg file. To append multiple such substreams you can simply ensure that they have a different ID and then write the data.

Splitting is a bit more annoying, since granulepos is a codec dependent timestamp, and I don't remember how it is defined for vorbis. Another problem here is that you can't simply split in the middle of a packet without reencoding.

Scaphoid answered 16/11, 2011 at 14:9 Comment(1)
I'm using Xiph Interop Library (oggvorbisinterop.codeplex.com). Which methods I have to use for my purposes?Georama
S
6

You can do this with libzplay http://libzplay.sourceforge.net/ The steps needed to do what is being asked about:

  1. OpenFile
  2. Seek
  3. SetWaveOutFile(this supports .ogg exporting as well as other formats)
  4. StartPlayback
  5. StopPlayback(at time needed)

Everything is extremely well documented on the linked site for multiple languages, including c#.
This answer is for all the other people that spent hours searching and weren't helped by the previous answers. This isn't a very efficient solution to the problem here, but while searching this question came up many times, and this might be helpful to others. :)

Subsumption answered 2/2, 2012 at 2:32 Comment(2)
God Bless you ntw1103. This is exactly what I needed. I don't know how it didn't come up when I spent like a week Googling .NET and ogg vorbis.Vasomotor
How does the license (GNU GPL) affect distribution and sales?Cattery
S
5

I'd look into the c documentation for libogg, and figure out how to do this with c. And then write almost the same code in C# using a wrapper over libogg.

I've created a low level wrapper over libogg and libvorbis using the interop assistant: https://github.com/CodesInChaos/Xiph/blob/master/LowLevel.cs

That project also contains some higher level constructs, but I don't think they'll be useful for what you're doing.

BTW if the stream IDs between the files differ, you can simply append a file to another creating a valid file that plays both streams in sequence.

You probably need to read the input files packet wise using the decoding API, and then write the combined data out packet wise. Possibly replacing the stream ID and granulepos in between.

StreamID is an integer that identifies substreams in an ogg file. To append multiple such substreams you can simply ensure that they have a different ID and then write the data.

Splitting is a bit more annoying, since granulepos is a codec dependent timestamp, and I don't remember how it is defined for vorbis. Another problem here is that you can't simply split in the middle of a packet without reencoding.

Scaphoid answered 16/11, 2011 at 14:9 Comment(1)
I'm using Xiph Interop Library (oggvorbisinterop.codeplex.com). Which methods I have to use for my purposes?Georama

© 2022 - 2024 — McMap. All rights reserved.