音频帧未转换为narray



我正在尝试运行一个协作文件训练openAI的点唱机,但是当我尝试运行加载音频的函数代码时,我得到一个错误:

File "/content/jukebox/jukebox/data/files_dataset.py",第82行,在get_song_chunk数据,sr= load_audio(文件名,sr=self。Sr, offset=offset, duration=self.sample_length)文件"/content/jukebox/jukebox/utils/io.py",第48行,在load_audioframe = frame. to_narray (format='fltp') #转换为浮点数而不是int16AttributeError: 'list'对象没有' to_narray '属性

它似乎将框架输入解释为一个列表,打印出来时看起来像这样:

[& lt; av。AudioFrame 0, pts=None, 778个采样,22050Hz,立体声,fltp0 x7fd03dd64150>]

当我尝试更改为frame = resampler.resample(frame)时,我得到这个错误:

TypeError: 'av.audio.frame.AudioFrame'对象不能被解释为一个整数

我不太了解音频文件,所以我不知道如何调试,希望这里的帮助。

加载音频的完整代码如下。

def load_audio(file, sr, offset, duration, resample=True, approx=False, time_base='samples', check_duration=True):
if time_base == 'sec':
offset = offset * sr
duration = duration * sr
# Loads at target sr, stereo channels, seeks from offset, and stops after duration
container = av.open(file)
audio = container.streams.get(audio=0)[0] # Only first audio stream
audio_duration = audio.duration * float(audio.time_base)
if approx:
if offset + duration > audio_duration*sr:
# Move back one window. Cap at audio_duration
offset = np.min(audio_duration*sr - duration, offset - duration)
else:
if check_duration:
assert offset + duration <= audio_duration*sr, f'End {offset + duration} beyond duration {audio_duration*sr}'
if resample:
resampler = av.AudioResampler(format='fltp',layout='stereo', rate=sr)
else:
assert sr == audio.sample_rate
offset = int(offset / sr / float(audio.time_base)) #int(offset / float(audio.time_base)) # Use units of time_base for seeking
duration = int(duration) #duration = int(duration * sr) # Use units of time_out ie 1/sr for returning
sig = np.zeros((2, duration), dtype=np.float32)
container.seek(offset, stream=audio)
total_read = 0
for frame in container.decode(audio=0): # Only first audio stream
if resample:
frame.pts = None
frame = resampler.resample(frame)
frame = frame.to_ndarray(format='fltp') # Convert to floats and not int16
read = frame.shape[-1]
if total_read + read > duration:
read = duration - total_read
sig[:, total_read:total_read + read] = frame[:, :read]
total_read += read
if total_read == duration:
break
assert total_read <= duration, f'Expected {duration} frames, got {total_read}'
return sig, sr

如果您的变量frame被解释为列表,您可以将frame = resampler.resample(frame)替换为frame = resampler.resample(frame)[0]。当我做了这个编辑后,你的代码没有错误地运行。

尝试用直接赋值变量frame来替换frame = frame.to_ndarray(format='fltp'):

import numpy as np
#frame = frame.to_ndarray(format='fltp') #Original line
frame = np.ndarray(frame)

如果你希望它是一个特定的数据类型,你可以改变ndarray函数的dtype参数:

frame = np.ndarray(frame, dtype=np.float32)

Try:frame = frame[0].to_ndarray(format='fltp')

最新更新