How to use the pydub.AudioSegment.from_ogg function in pydub

To help you get started, we’ve selected a few pydub examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nyumaya / nyumaya_audio_recognition / python / test_accuracy.py View on Github external
def load_audio_file(filename,resize=False):
	sound = None
	try:
		if filename.endswith('.mp3') or filename.endswith('.MP3'):
			sound = AudioSegment.from_mp3(filename)
		elif filename.endswith('.wav') or filename.endswith('.WAV'):
			sound = AudioSegment.from_wav(filename)
		elif filename.endswith('.ogg'):
			sound = AudioSegment.from_ogg(filename)
		elif filename.endswith('.flac'):
			sound = AudioSegment.from_file(filename, "flac")
		elif filename.endswith('.3gp'):
			sound = AudioSegment.from_file(filename, "3gp")
		elif filename.endswith('.3g'):
			sound = AudioSegment.from_file(filename, "3gp")

		sound = sound.set_frame_rate(samplerate)
		sound = sound.set_channels(1)
		sound = sound.set_sample_width(2)
		duration = sound.duration_seconds
	except:
		print("Couldn't load file")
		return None,None
github gittb / audiosandbox / convert / audioutils.py View on Github external
def audio_to_arr(fname, newrate=44100):

    if '.mp3' in fname:
        raw = pydub.AudioSegment.from_mp3(fname)

    elif '.wav' in fname:
        raw = pydub.AudioSegment.from_wav(fname)

    elif '.ogg' in fname:
        raw = pydub.AudioSegment.from_ogg(fname)

    elif '.flv' in fname:
        raw = pydub.AudioSegment.from_flv(fname)
    else:
        print('File Format: ".' + str(fname.split('.')[-1]) + '" is unsupported.')

    arr = np.array(raw.get_array_of_samples())

    chans = raw.channels
    if chans > 1:
        arr = arr.reshape((-1, chans))
        arr = arr.sum(axis=1) / chans

    arr = hz_correct(arr, raw.frame_rate, newrate)
    lengths = len(arr) / rate
github KeelyHill / Prime-Number-Audio-Book / prime_audio.py View on Github external
def main():

    print("Gettings raw number sound bites.")

    # get each sound as pydub audio segment and add to list for easy access
    for i in range(10):
        number_sounds.append(AudioSegment.from_ogg("sound_bites/%i.ogg" % i))

    # load in the beast by the lines of the file
    lines = loadBigNumFileToList()

    print("Creating blank audio file in memory.")
    output = AudioSegment.silent(duration=500) # 'blank' slate to append to.

    job_server = pp.Server()

    print("Splitting labor, and starting")
    # Define jobs, cpu cores/2 in my case
    #                                               give range    and other params
    job1 = job_server.submit(processRangeForLines, (range(0,10), lines, number_sounds))
    job2 = job_server.submit(processRangeForLines, (range(10,20), lines, number_sounds))

    # execute and grab value
github edouardpoitras / eva / clients / headless.py View on Github external
def play(filepath, content_type='audio/wav'):
    """
    Will attempt to play various audio file types (wav, ogg, mp3).
    """
    if 'wav' in content_type:
        sound = AudioSegment.from_wav(filepath)
    elif 'ogg' in content_type or 'opus' in content_type:
        sound = AudioSegment.from_ogg(filepath)
    elif 'mp3' in content_type or 'mpeg' in content_type:
        sound = AudioSegment.from_mp3(filepath)
    pydub_play(sound)
github amsehili / auditok / auditok / cmdline.py View on Github external
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":