当前位置: 首页>>代码示例>>Python>>正文


Python AudioSegment.set_channels方法代码示例

本文整理汇总了Python中pydub.AudioSegment.set_channels方法的典型用法代码示例。如果您正苦于以下问题:Python AudioSegment.set_channels方法的具体用法?Python AudioSegment.set_channels怎么用?Python AudioSegment.set_channels使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pydub.AudioSegment的用法示例。


在下文中一共展示了AudioSegment.set_channels方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: file_to_audio_source

# 需要导入模块: from pydub import AudioSegment [as 别名]
# 或者: from pydub.AudioSegment import set_channels [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")
           
#.........这里部分代码省略.........
开发者ID:amsehili,项目名称:auditok,代码行数:103,代码来源:cmdline.py


注:本文中的pydub.AudioSegment.set_channels方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。