我正在使用Scikit-learn研究音乐分类方法,该过程的第一步是将音乐文件转换为numpy数组。
在尝试从python脚本调用ffmpeg失败后,我决定直接通过管道将文件导入:
FFMPEG_BIN = "ffmpeg"
cwd = (os.getcwd())
dcwd = (cwd + "/temp")
if not os.path.exists(dcwd): os.makedirs(dcwd)
folder_path = sys.argv[1]
f = open("test.txt","a")
for f in glob.glob(os.path.join(folder_path, "*.mp3")):
ff = f.replace("./", "/")
print("Name: " + ff)
aa = (cwd + ff)
command = [ FFMPEG_BIN,
'-i', aa,
'-f', 's16le',
'-acodec', 'pcm_s16le',
'-ar', '22000', # ouput will have 44100 Hz
'-ac', '1', # stereo (set to '1' for mono)
'-']
pipe = sp.Popen(command, stdout=sp.PIPE, bufsize=10**8)
raw_audio = pipe.proc.stdout.read(88200*4)
audio_array = numpy.fromstring(raw_audio, dtype="int16")
print (str(audio_array))
f.write(audio_array + "n")
问题是,当我运行文件时,它启动ffmpeg,然后什么也不做:
[mp3 @ 0x1446540] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from '/home/don/Code/Projects/MC/Music/Spaz.mp3':
Metadata:
title : Spaz
album : Seeing souns
artist : N*E*R*D
genre : Hip-Hop
encoder : Audiograbber 1.83.01, LAME dll 3.96, 320 Kbit/s, Joint Stereo, Normal quality
track : 5/12
date : 2008
Duration: 00:03:50.58, start: 0.000000, bitrate: 320 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s
Output #0, s16le, to 'pipe:':
Metadata:
title : Spaz
album : Seeing souns
artist : N*E*R*D
genre : Hip-Hop
date : 2008
track : 5/12
encoder : Lavf56.4.101
Stream #0:0: Audio: pcm_s16le, 22000 Hz, mono, s16, 352 kb/s
Metadata:
encoder : Lavc56.1.100 pcm_s16le
Stream mapping:
Stream #0:0 -> #0:0 (mp3 (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
它只是坐在那里,悬着,比歌曲的时间长得多。我哪里做错了?
我推荐您使用pymedia或audioread或decoder.py。也有pyffmpeg和类似的模块可以做您想做的事情。看一下pypi.python.org。
当然,这些不能帮助您将数据转换为numpy数组。
无论如何,这就是如何使用管道来完成ffmpeg:
from subprocess import Popen, PIPE
import numpy as np
def decode (fname):
# If you are on Windows use full path to ffmpeg.exe
cmd = ["./ffmpeg.exe", "-i", fname, "-f", "wav", "-"]
# If you are on W add argument creationflags=0x8000000 to prevent another console window jumping out
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
data = p.communicate()[0]
return np.fromstring(data[data.find("data")+4:], np.int16)
基本使用时应该是这样的
它应该工作,因为ffmpeg的输出默认是16位音频。但是如果你搞混了,你应该知道numpy没有int24,所以你将被迫做一些位操作,并将24位音频表示为32位音频。只是,不用24位,世界就幸福了。: D
如果你需要更复杂的东西,我们可以讨论在注释中改进代码。
我使用的是:它使用pydub(它使用ffmpeg)和scipy。
完整设置(在Mac上,可能与其他系统不同):
pip install scipy
pip install pydub
brew install ffmpeg # Or probably "sudo apt-get install ffmpeg on linux"
然后读mp3:
import tempfile
import os
import pydub
import scipy
import scipy.io.wavfile
def read_mp3(file_path, as_float = False):
"""
Read an MP3 File into numpy data.
:param file_path: String path to a file
:param as_float: Cast data to float and normalize to [-1, 1]
:return: Tuple(rate, data), where
rate is an integer indicating samples/s
data is an ndarray(n_samples, 2)[int16] if as_float = False
otherwise ndarray(n_samples, 2)[float] in range [-1, 1]
"""
path, ext = os.path.splitext(file_path)
assert ext=='.mp3'
mp3 = pydub.AudioSegment.from_mp3(file_path)
_, path = tempfile.mkstemp()
mp3.export(path, format="wav")
rate, data = scipy.io.wavfile.read(path)
os.remove(path)
if as_float:
data = data/(2**15)
return rate, data
来源:James Thompson的博客