pydub icon indicating copy to clipboard operation
pydub copied to clipboard

Support for mogg files

Open ddahan opened this issue 8 years ago • 12 comments

Mogg files are used to contain different tracks of a song. It could be very useful to be able to split these files in several ogg files.

ddahan avatar Jan 30 '17 15:01 ddahan

It seems that a mogg file is essentially a container format with multiple ogg streams.

Do you have an example mogg file?

I suspect with an extra parameter to ffmpeg it'll be possible to extract those streams individually.

jiaaro avatar Apr 11 '17 13:04 jiaaro

Hi @jiaaro Here is an example of .mogg file I uploaded: https://dl.dropboxusercontent.com/u/4703391/Avenged%20Sevenfold%20-%20Afterlife.mogg

ddahan avatar Apr 11 '17 18:04 ddahan

@ddahan sorry for the delay - I don't think that is a valid mogg file. I found this reddit thread linking to mogg files extracted from rock band and I was able to successfully open "A Hard Day's Night" (10 total audio channels) like so:

>>> from pydub import AudioSegment
>>> sound = AudioSegment.from_file("/Users/jiaaro/Downloads/A Hard Day's Night.mogg")
>>> print(sound.channels)
10

jiaaro avatar May 09 '17 13:05 jiaaro

Thanks for your answer. It seems you can load a mogg file indeed. But how would you extract some of the tracks to export them in a new file? For example, if I want to create a guitar backing-track (ie. removing guitar tracks from the original mogg), and export the file to mp3, how can I do this with pydub? Thanks.

ddahan avatar May 09 '17 22:05 ddahan

The latest release of pydub (v0.19.0 - available on pypi starting today :) ) supports using AudioSegment.split_to_mono() on audio segments with any number of channels. That's probably your best bet (though you'll need to recombine any stere pairs manually)

jiaaro avatar May 09 '17 23:05 jiaaro

Unfortunately, this returns a list of two AudioSegments. If there are 10 channels, I suppose there are at least 5 differents tracks in the song..

ddahan avatar May 09 '17 23:05 ddahan

It's important that you're on the very latest version of pydub (v0.19)

you can check your version with pip freeze, and you an upgrade with pip install -U pydub

jiaaro avatar May 10 '17 01:05 jiaaro

Excellent :) Thanks. One last question: currently, the overlay() method seems to accept one AudioSegment only. That means if my source has 10 channels, and my output need to have 8 channels, I need to loop on the overlay method to merge the chanels 1 by 1. (for example if I want to keep channel 1->8, I would use overlay to merge: 1 with 2, then 3 with [1+2], then 4 with [1+2+3], etc.) Is there a cleaner way?

Thanks again.

ddahan avatar May 10 '17 07:05 ddahan

I think that's what you'll have to do given the current AudioSegment methods. I could imagine a new class (StereoMixer maybe?) which allows you to set what audio segments start and end where and gain and panning, and at the end do a single pass to combine them all, but such a thing doesn't exist yet.

At a high level, such a thing would just keep a reference to all the audio segments in question and at the time of the "mix down" it would:

  1. Make sure all the audio segments have the same bit depth and frame rate
  2. Call sound.get_array_of_samples() on each one and sum all the signals - one sample at a time
  3. Generate a new output AudioSegment using the combined values

jiaaro avatar May 10 '17 14:05 jiaaro

That would be great 👍

ddahan avatar May 10 '17 14:05 ddahan

For anyone finding this thread in the future, I did prototype a Mixer class which may be a good starting point. Perhaps that's something to polish up a bit more and add to pydub?

jiaaro avatar Oct 12 '17 14:10 jiaaro

sorry to revive an old issue, is this already implemented?

can someone provide a working example of extracting mogg to wav?

thanks a lot in advance.

muzzol avatar Aug 08 '24 09:08 muzzol