如何将只有音频的 mp4 转换为 numpy 数组

How to turn mp4 with only audio into numpy array

我想:

Download audio files from Youtube

我用 pytube 完成了,但是,即使我将 only_audio 设置为 True,它的格式也是 mp4 .

then turn the audio files to numpy arrays

有些库适用于 mp3,例如 pydub,但不适用于 mp4。当我尝试 moviepy 时,它失败了,因为没有视频,因此没有帧率。我不想下载视频,因为它需要更长的时间。

请注意,我要的是音频,而不是视频。

怎样才能:

download audio from youtube, and turn it into numpy arrays?

感谢您的帮助:)


编辑

感谢评论,我已经成功地使用 ffmpeg

将 mp4 转换为 mp3

但是,当我尝试使用 中的代码将其转换为 numpy 数组时,它看起来像这样:

def 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, y

它引发了这个错误:

    Traceback (most recent call last):
  File "C:\Users\myname\Google Drive\Python\Projects\Music\Downloads\Music Read.py", line 63, in <module>
    print(read(x,True))
  ......
  File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 1017, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

这很奇怪,因为如下所示,该路径应该可以正常工作

for f in os.listdir(path):
    if (f.endswith(".mp3")):
        print(f)
        x = 'C:/Users/myname/Google Drive/Python/Projects/Music/Downloads/{}'.format(f)
        print(os.path.exists(x))
        print(open(x))
        print(read(x,True))

输出:

test-Copy.mp3
True
c:/users/myname/google drive/python/projects/music/downloads/test-copy.mp3
<_io.TextIOWrapper name='c:/users/myname/google drive/python/projects/music/downloads/test-copy.mp3' mode='r' encoding='cp1252'>

此外,当我输入一个实际不存在的文件路径时,它会输出不同的错误:

......
File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\pydub\utils.py", line 57, in _fd_or_path_or_tempfile
fd = open(fd, mode=mode)
FileNotFoundError: [Errno 2] No such file or directory: 'c:/users/myname/google drive/python/projects/music/downloads/hi'

How can use the code from to turn the mp3 into numpy arrays, if I can't, how else?

顺便说一句,我 运行 在 Win10 上 python 3.6

我真的希望我已经说得够清楚了,再次提前感谢您的任何建议:)

回答我自己的问题很奇怪但是:

我使用以下代码解决了 pydub 问题:

def decode (fname):
    # If you are on Windows use full path to ffmpeg.exe
    cmd = ["C:/Users/allen/Google Drive/Python/Tools/ffmpeg-20190604-d3f236b-win64-static/bin/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)