第一步:先下載ffmpeg--》下載鏈接
下載好解壓到某個文件夾,並將該文件夾中的bin目錄添加到系統path。
第二步:安裝pydub
pip3 install pydub
# -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/5/18 15:13 from pydub import AudioSegment sound=AudioSegment.from_file("aaa.mp3","mp3") sound2=AudioSegment.from_file('bbb.mp3','mp3') # 把一個多聲道音頻分解成兩個單聲道 # index[0]為左聲道 # index[1]為右聲道 # sounds=sound.split_to_mono() # print(sounds) # 將兩個單聲道合並成多聲道 # stereo_sound = AudioSegment.from_mono_audiosegments(sounds[0], sounds[1]) # # 取得音頻的分貝數 # loudness = sound.dBFS # print(loudness) # # 獲取音頻音量大小,該值通常用來計算分貝數(dB= 20×lgX) # loudness = sound.rms # print(loudness) # # 取得音頻的聲道數 # channel_count = sound.channels # print(channel_count) # # 取得音頻文件采樣寬度 # bytes_per_sample = sound.sample_width # print(bytes_per_sample) # # # 取得音頻文件采樣頻率 # frames_per_second = sound.frame_rate # print(frames_per_second) # #取得音頻文件幀寬度 # bytes_per_frame = sound.frame_width # print(bytes_per_frame) # # #取得音頻中的最大振幅 # normalized_sound = sound.apply_gain(-sound.max_dBFS) # print(normalized_sound) # #取得音頻的持續時間,同 len() # print(sound.duration_seconds) # print((len(sound) / 1000.0)) # #取得音頻數據 # raw_audio_data = sound.raw_data # # print(raw_audio_data) # #取得音頻的frame數量 # number_of_frames_in_sound = sound.frame_count() # number_of_frames_in_200ms_of_sound = sound.frame_count(ms=200) # print(number_of_frames_in_sound) # print(number_of_frames_in_200ms_of_sound) # 拼接sound1與sound2,返回一個新的AudioSegment實例 # cossfade:交叉漸變間隔 ms # no_crossfade1 = sound.append(sound2, crossfade=5000) # print(no_crossfade1) # no_crossfade1.export(r'cc.wav',format='wav') # 輸出 # 把sound2覆蓋在sound1上,兩個音頻文件會疊加,如果sound2較長,則會被截斷。 # 參數: # position:覆蓋起始位置(毫秒) # loop:是否循環覆蓋(true/false) # times:重復覆蓋次數(默認1) # gain_during_overlay:調整被覆蓋音頻的音量(eg,-6.0) # played_togther = sound.overlay(sound2) # # sound2_starts_after_delay = sound.overlay(sound2, position=5000) # # volume_of_sound1_reduced_during_overlay = sound.overlay(sound2, gain_during_overlay=-8) # # sound2_repeats_until_sound1_ends = sound.overlay(sound2, loop=True) # # sound2_plays_twice = sound.overlay(sound2, times=2) # played_togther.export(r'dd.wav',format='wav') # 輸出 #調整音量大小 # louder_via_method = sound.apply_gain(+3.5) # 提高 # quieter_via_method = sound.apply_gain(-5.7) # 減小 #淡出 # 參數: # to_gain:淡出結束時音頻音量下降到的分貝數 # from_gain:設置淡出前的所有音頻分貝數 # start:淡出的起始位置 # end:淡出的結束位置 # duration:淡出持續時間 # fade_in_the_hard_way = sound.fade(from_gain=-120.0, start=0, duration=5000) # fade_out_the_hard_way = sound.fade(to_gain=-120.0, end=0, duration=5000) # 反向輸出 # sound.reverse().export(r'ee.wav',format='wav') # 輸出 # 調整多聲道音頻的左右聲道音量 # 如果單聲道音頻調用此方法,它將先被轉換為多聲道 # stereo_balance_adjusted = sound.apply_gain_stereo(-6, +2) # # #左右聲道平衡,按百分比增大一邊,減小另一邊 # # pan the sound 15% to the right # panned_right = sound.pan(+0.15) # # pan the sound 50% to the left # panned_left = sound.pan(-0.50) # # # # 基於DSP的渲染 # # 產生一個反向信號的副本,來消除反相位波,或者降低噪音 # sound.invert_phase()