ishvn
ishvn

Reputation: 61

How do I convert a 4 channel stereo file to mono in Python

How do I convert a 4 channel stereo file to mono in Python, preferably using pydub? Thanks in advance

Upvotes: 4

Views: 3098

Answers (2)

Anil_M
Anil_M

Reputation: 11453

In addition to @Patrice answer, as per this discussion on github pydub repo, export returns without executing ffmpeg with parameters if format is wav.

You may need to modify pydub.AudioSegment as below in order for ffmpeg to actually execute with optional parameters provided in export.

Changing line 600

if format == "wav":
    data = out_f
else:
    data = NamedTemporaryFile(mode="wb", delete=False)

to:

if format == "wav" and parameters is None:
        data = out_f
    else:
        data = NamedTemporaryFile(mode="wb", delete=False)

And line 616:

 if format == 'wav':
            return out_f

to:

 if format == 'wav' and parameters is None:
            return out_f

Then ffmpeg accepts parameters and processes accordingly .

I don't have a "quadrophonic" audio sample to test this "theory", if you can upload to your github issue tracker , above can be tested. Let me know.

Upvotes: 1

PatriceG
PatriceG

Reputation: 4063

From the pydub documentation, I suppose you can try something like that:

from pydub import AudioSegment 

song = AudioSegment.from_wav("example.wav") #read wav
song.export("out.wav", parameters=["-ac", "1"])             # export wav (1 channel)

As I understand the documentation, the values of "parameters" is based on the ffmpeg documentation (audio options). So you can use all parameters from ffmpeg. Here, I just set the number of audio channels to 1.

Upvotes: 5

Related Questions