5. Saving a multichannel audio to one channel per file

[1]:
import pathlib
import tempfile

from cutcutcodec.core.io import read, write
from cutcutcodec.utils import get_project_root
[2]:
input_file = get_project_root().parent / "media" / "audio" / "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
       mono = container.apply_audio_equation(f"{channel}_0")   # extract only one channel
       write(mono.out_streams, dst_folder / f"{name.replace(' ', '_')}.mp3", [{"encodec": "libmp3lame", "rate": stream.rate}])  # lossly
       # write(mono.out_streams, dst_folder / f"{name.replace(' ', '_')}.wav", [{"encodec": "pcm_f32le", "rate": stream.rate}])  # lossless
WARNING:root:audio frame overlap of 0.051833 seconds detected                                                                                                    | 0.00s/1.99s [00:00<?]
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]