python_数据_pydub

pydub

基于ffmpeg

start

import numpy as np
import pandas as pd
from scipy import integrate
import matplotlib.pyplot as plt
import pydub

使用pydub 读取MP3格式的音乐文件,进行剪切组合

hard = pydub.AudioSegment.from_file('难念的经.mp3')
gui = pydub.AudioSegment.from_file('鬼迷心窍.mp3')
mix = hard[:30*1000] - 20 + gui[:30 * 1000] +10   # 切片单位为毫秒;-20,+10 用于调整声音大小
pydub.AudioSegment.export(mix,out_f='mix.mp3')
  • 组合音乐并导出

查看MP3的数据

hard.duration_seconds   # 音乐时长
hard.frame_rate     # 帧率 44100
data = hard.get_array_of_samples()    # array.array
nd = np.array(data.tolist())    # array.array --->list ----> ndarray 
nd1 = nd.reshape((-1,2))
nd1.shape    # (23779584,) ---- > (11889792, 2)   # 转成二维双通道
两种方式(wavfile,AudioSegment)的区别
1.wavefile 只读取wav格式的文件,AudioSegment类中针对不同格式的音频文件封装有函数
2.读取后
from scipy.io import wavfile
like = wavfile.read('./邓紫棋-喜欢你.wav')   # 双通道
# like   # 展示相关信息 44100 和数据
love = pydub.AudioSegment.from_wav('./邓紫棋-喜欢你.wav')
# love   # 可以直接加载到页面,进行播放
love = np.array(love.get_array_of_samples().tolist())
love.shape    # 单通道
3.逆序与存储,其他

对于wavfile打开的文件

wavfile.write('./like.wav',44100,like[1][::-1])   # 反着播放音乐

对于AudioSegment打开的文件

hard[::-1]   # <generator object AudioSegment.__getitem__.<locals>.<genexpr> at 0x000001E287EF85E8>
hard1 = pydub.AudioSegment.reverse(hard)
pydub.AudioSegment.export(hard1,'./hard1.mp3')

猜你喜欢

转载自blog.csdn.net/sinat_39045958/article/details/86531162
今日推荐