栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将MP3音频文件读取到numpy数组中/如何将numpy数组保存到MP3?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将MP3音频文件读取到numpy数组中/如何将numpy数组保存到MP3?

正如许多关于阅读MP3的文章中所建议的那样,调用

ffmpeg
和手动解析它
stdout
是一项繁琐的任务(在许多特殊情况下,因为可能会使用不同数量的通道,等等),因此这是一个使用
pydub
(您需要
pipinstall pydub
首先使用)的可行解决方案。

此代码允许
使用与以下类似的API

scipy.io.wavfile.read/write
读取MP3到numpy数组/将numpy数组写入MP3文件:

import pydub import numpy as npdef read(f, normalized=False):    """MP3 to numpy array"""    a = pydub.AudioSegment.from_mp3(f)    y = np.array(a.get_array_of_samples())    if a.channels == 2:        y = y.reshape((-1, 2))    if normalized:        return a.frame_rate, np.float32(y) / 2**15    else:        return a.frame_rate, ydef write(f, sr, x, normalized=False):    """numpy array to MP3"""    channels = 2 if (x.ndim == 2 and x.shape[1] == 2) else 1    if normalized:  # normalized array - each item should be a float in [-1, 1)        y = np.int16(x * 2 ** 15)    else:        y = np.int16(x)    song = pydub.AudioSegment(y.tobytes(), frame_rate=sr, sample_width=2, channels=channels)    song.export(f, format="mp3", bitrate="320k")

笔记:

  • 目前仅适用于16位文件(即使24位WAV文件非常普遍,我也很少见过24位MP3文件…是否存在?)
  • normalized=True
    允许使用浮点数组([-1,1中的每个项目)

用法示例:

sr, x = read('test.mp3')print(x)#[[-225  707]# [-234  782]# [-205  755]# ..., # [ 303   89]# [ 337   69]# [ 274   89]]write('out2.mp3', sr, x)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/596596.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号