gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
ffmpeg python wav save one channel
- User
ffmpeg python wav save one channel
- 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
subprocessmodule 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
-arflag to match the original sample rate. - We import the