Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

ffmpeg python wav save one channel

  1. User

    ffmpeg python wav save one channel

  2. Assistant

    To save only one channel of a WAV file using ffmpeg in Python, you can use the following code:

    import subprocess
    
    input_file = "input.wav"
    output_file = "output.wav"
    channel = "1"  # set the channel number to save, starting from 1
    
    command = ["ffmpeg", 
               "-i", input_file, 
               "-map_channel", f"0.{channel}",  # select the specified channel
               "-ac", "1",  # set the output to mono
               "-ar", "44100",  # set the output to 44100 Hz, change as needed
               output_file]
    
    subprocess.run(command)
    

    Explanation:

    • We import the subprocess module to run the ffmpeg command.
    • We set the input and output filenames.
    • We set the channel number to save. Note that channels are numbered from 1.
    • We construct an array with the ffmpeg command and its arguments, using f-strings to insert the input file, output file, and channel number.
    • We run the command using subprocess.run().

    This code will create a new WAV file containing only the selected channel, downmixed to mono. Modify the -ar flag to match the original sample rate.