""" Copyright 2024 RobotsMali AI4D Lab. Licensed under the Creative Commons Attribution 4.0 International License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://creativecommons.org/licenses/by/4.0/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ from pydub import AudioSegment import sys import json def convert_to_mono(file_path): """Convert an audio file to mono if it has multiple channels.""" audio = AudioSegment.from_file(file_path) if audio.channels > 1: audio = audio.set_channels(1) audio.export(file_path, format="wav") print(f"Converted {file_path} to mono.") def check_and_convert_audio_channels(manifest_path): """Check the number of channels in audio files and convert to mono if necessary.""" with open(manifest_path, 'r') as f: lines = f.readlines() for line in lines: audio_path = json.loads(line)['audio_filepath'] audio = AudioSegment.from_file(audio_path) if audio.channels > 1: print(f"{audio_path} is not mono. Converting...") convert_to_mono(audio_path) else: print(f"{audio_path} is already mono.") if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: python script.py ") sys.exit(1) manifest_path = sys.argv[1] check_and_convert_audio_channels(manifest_path=manifest_path)