3. Saving a multichannel audio to one channel per file

[1]:
import pathlib
import tempfile

from cutcutcodec.core.io import read, write
from cutcutcodec.core.filter.audio.equation import FilterAudioEquation
from cutcutcodec.utils import get_project_root
[2]:
input_file = get_project_root().parent / "media" / "cutcutcodec.ogg"  # your audio path
dst_folder = pathlib.Path(tempfile.gettempdir())  # the destination folder
[3]:
with read(input_file) as container:  # open the audio file
    stream = container.out_streams[0]  # get the audio stream
    for channel, name in stream.layout.channels:  # iterate on each channel
        stream_mono = FilterAudioEquation([stream], f"{channel}_0").out_streams[0]  # extract only one channel
        write([stream_mono], dst_folder / f"{name.replace(' ', '_')}.mp3", [{"encodec": "libmp3lame", "rate": stream.rate}])  # lossly
        # write([stream_mono], dst_folder / f"{name.replace(' ', '_')}.wav", [{"encodec": "pcm_f32le", "rate": stream.rate}])  # lossless
WARNING:root:unknowned extension .ogg, try several readers
WARNING:root:audio frame overlap of 0.051833 seconds detected                                  | 0.00s/1.99s [00:00<?]
WARNING:root:audio frame overlap of 0.006500 seconds detected
Encoding front_left.mp3: 100%|█████████████████████████████████████████████████████████████| 1.99s/1.99s [00:00<00:00]
WARNING:root:audio frame overlap of 0.051833 seconds detected                                  | 0.00s/1.99s [00:00<?]
Encoding front_right.mp3: 100%|████████████████████████████████████████████████████████████| 1.99s/1.99s [00:00<00:00]