本文整理汇总了Python中pydub.AudioSegment.split_to_mono方法的典型用法代码示例。如果您正苦于以下问题:Python AudioSegment.split_to_mono方法的具体用法?Python AudioSegment.split_to_mono怎么用?Python AudioSegment.split_to_mono使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pydub.AudioSegment
的用法示例。
在下文中一共展示了AudioSegment.split_to_mono方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: file_to_audio_source
# 需要导入模块: from pydub import AudioSegment [as 别名]
# 或者: from pydub.AudioSegment import split_to_mono [as 别名]
def file_to_audio_source(filename, filetype=None, **kwargs):
lower_fname = filename.lower()
rawdata = False
if filetype is not None:
filetype = filetype.lower()
if filetype == "raw" or (filetype is None and lower_fname.endswith(".raw")):
srate = kwargs.pop("sampling_rate", None)
if srate is None:
srate = kwargs.pop("sr", None)
swidth = kwargs.pop("sample_width", None)
if swidth is None:
swidth = kwargs.pop("sw", None)
ch = kwargs.pop("channels", None)
if ch is None:
ch = kwargs.pop("ch", None)
if None in (swidth, srate, ch):
raise Exception("All audio parameters are required for raw data")
data = open(filename).read()
rawdata = True
# try first with pydub
if WITH_PYDUB:
use_channel = kwargs.pop("use_channel", None)
if use_channel is None:
use_channel = kwargs.pop("uc", None)
if use_channel is None:
use_channel = 1
else:
try:
use_channel = int(use_channel)
except ValueError:
pass
if not isinstance(use_channel, (int)) and not use_channel.lower() in ["left", "right", "mix"] :
raise ValueError("channel must be an integer or one of 'left', 'right' or 'mix'")
asegment = None
if rawdata:
asegment = AudioSegment(data, sample_width=swidth, frame_rate=srate, channels=ch)
if filetype in("wave", "wav") or (filetype is None and lower_fname.endswith(".wav")):
asegment = AudioSegment.from_wav(filename)
elif filetype == "mp3" or (filetype is None and lower_fname.endswith(".mp3")):
asegment = AudioSegment.from_mp3(filename)
elif filetype == "ogg" or (filetype is None and lower_fname.endswith(".ogg")):
asegment = AudioSegment.from_ogg(filename)
elif filetype == "flv" or (filetype is None and lower_fname.endswith(".flv")):
asegment = AudioSegment.from_flv(filename)
else:
asegment = AudioSegment.from_file(filename)
if asegment.channels > 1:
if isinstance(use_channel, int):
if use_channel > asegment.channels:
raise ValueError("Can not use channel '{0}', audio file has only {1} channels".format(use_channel, asegment.channels))
else:
asegment = asegment.split_to_mono()[use_channel - 1]
else:
ch_lower = use_channel.lower()
if ch_lower == "mix":
asegment = asegment.set_channels(1)
elif use_channel.lower() == "left":
asegment = asegment.split_to_mono()[0]
elif use_channel.lower() == "right":
asegment = asegment.split_to_mono()[1]
return BufferAudioSource(data_buffer = asegment._data,
sampling_rate = asegment.frame_rate,
sample_width = asegment.sample_width,
channels = asegment.channels)
# fall back to standard python
else:
if rawdata:
if ch != 1:
raise ValueError("Cannot handle multi-channel audio without pydub")
return BufferAudioSource(data, srate, swidth, ch)
if filetype in ("wav", "wave") or (filetype is None and lower_fname.endswith(".wav")):
wfp = wave.open(filename)
ch = wfp.getnchannels()
if ch != 1:
wfp.close()
raise ValueError("Cannot handle multi-channel audio without pydub")
#.........这里部分代码省略.........