Python 如何修复moviepy中to_soundarray中的越界错误?

Python 如何修复moviepy中to_soundarray中的越界错误?,python,ffmpeg,youtube-dl,moviepy,Python,Ffmpeg,Youtube Dl,Moviepy,代码使用默认的youtube_dl名称,但不使用我在ydl_opts中输入的outtmpl名称 这里给出的解决方案- 似乎不起作用 这是密码- import numpy as np import matplotlib.pyplot as plt %matplotlib inline import moviepy.editor as mpy !pip install youtube_dl import youtube_dl url = 'https://www.youtube.com/watc

代码使用默认的youtube_dl名称,但不使用我在ydl_opts中输入的outtmpl名称

这里给出的解决方案- 似乎不起作用

这是密码-

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import moviepy.editor as mpy
!pip install youtube_dl
import youtube_dl


url = 'https://www.youtube.com/watch?v=5pIpVK3Gecg' 
start_ts = 170
end_ts = 180

ydl_opts = {
  'format': 'bestaudio/best',
  'postprocessors': [{
      'key': 'FFmpegExtractAudio',
      'preferredcodec': 'wav',
      'preferredquality': '192',
  }],
  'outtmpl': 'original.wav',

}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  ydl.download([url])


from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

ffmpeg_extract_subclip('original.wav' ,170, 180, targetname='clipped.wav')

talk = mpy.AudioFileClip('clipped.wav')


plt.axis('off')

sample_rate = talk.fps

NFFT = sample_rate /25
audio_data = talk.to_soundarray()

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(2.44, 2.44), dpi=100.)

ax.axis('off')
spectrum, freqs, time, im = ax.specgram(audio_data.mean(axis=1), NFFT=NFFT, pad_to=4096, Fs=sample_rate, 
                                        noverlap=512, mode='magnitude', )
这里是错误-



[youtube] 5pIpVK3Gecg: Downloading webpage
[youtube] 5pIpVK3Gecg: Downloading video info webpage
[download] original.wav has already been downloaded
[download] 100% of 48.12MiB
[ffmpeg] Correcting container in "original.wav"
[ffmpeg] Post-process file original.wav exists, skipping

[MoviePy] Running:
>>> /root/.imageio/ffmpeg/ffmpeg-linux64-v3.3.1 -y -i original.wav -ss 170.00 -t 10.00 -vcodec copy -acodec copy clipped.wav
... command successful.

---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

/usr/local/lib/python3.6/dist-packages/moviepy/audio/io/readers.py in get_frame(self, tt)
    189                     indices = indices - (self.buffersize // 2 - len(self.buffer) + 1)
--> 190                 result[in_time] = self.buffer[indices]
    191                 return result

IndexError: index -59041 is out of bounds for axis 0 with size 40960


During handling of the above exception, another exception occurred:

IndexError                                Traceback (most recent call last)

10 frames

<ipython-input-86-63ba7dea8c67> in <module>()
     30 
     31 NFFT = sample_rate /25
---> 32 audio_data = talk.to_soundarray()
     33 
     34 fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(2.44, 2.44), dpi=100.)

</usr/local/lib/python3.6/dist-packages/decorator.py:decorator-gen-194> in to_soundarray(self, tt, fps, quantize, nbytes, buffersize)

/usr/local/lib/python3.6/dist-packages/moviepy/decorators.py in requires_duration(f, clip, *a, **k)
     52         raise ValueError("Attribute 'duration' not set")
     53     else:
---> 54         return f(clip, *a, **k)
     55 
     56 

/usr/local/lib/python3.6/dist-packages/moviepy/audio/AudioClip.py in to_soundarray(self, tt, fps, quantize, nbytes, buffersize)
    115             if self.duration > max_duration:
    116                 return stacker(self.iter_chunks(fps=fps, quantize=quantize,
--> 117                                                 nbytes=2, chunksize=buffersize))
    118             else:
    119                 tt = np.arange(0, self.duration, 1.0/fps)

<__array_function__ internals> in vstack(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/core/shape_base.py in vstack(tup)
    277         # raise warning if necessary
    278         _arrays_for_stack_dispatcher(tup, stacklevel=2)
--> 279     arrs = atleast_2d(*tup)
    280     if not isinstance(arrs, list):
    281         arrs = [arrs]

/usr/local/lib/python3.6/dist-packages/moviepy/audio/AudioClip.py in generator()
     82                 tt = (1.0/fps)*np.arange(pospos[i], pospos[i+1])
     83                 yield self.to_soundarray(tt, nbytes=nbytes, quantize=quantize,
---> 84                                          fps=fps, buffersize=chunksize)
     85 
     86         if progress_bar:

</usr/local/lib/python3.6/dist-packages/decorator.py:decorator-gen-194> in to_soundarray(self, tt, fps, quantize, nbytes, buffersize)

/usr/local/lib/python3.6/dist-packages/moviepy/decorators.py in requires_duration(f, clip, *a, **k)
     52         raise ValueError("Attribute 'duration' not set")
     53     else:
---> 54         return f(clip, *a, **k)
     55 
     56 

/usr/local/lib/python3.6/dist-packages/moviepy/audio/AudioClip.py in to_soundarray(self, tt, fps, quantize, nbytes, buffersize)
    128         #print tt.max() - tt.min(), tt.min(), tt.max()
    129 
--> 130         snd_array = self.get_frame(tt)
    131 
    132         if quantize:

</usr/local/lib/python3.6/dist-packages/decorator.py:decorator-gen-132> in get_frame(self, t)

/usr/local/lib/python3.6/dist-packages/moviepy/decorators.py in wrapper(f, *a, **kw)
     87         new_kw = {k: fun(v) if k in varnames else v
     88                  for (k,v) in kw.items()}
---> 89         return f(*new_a, **new_kw)
     90     return decorator.decorator(wrapper)
     91 

/usr/local/lib/python3.6/dist-packages/moviepy/Clip.py in get_frame(self, t)
     92                 return frame
     93         else:
---> 94             return self.make_frame(t)
     95 
     96     def fl(self, fun, apply_to=None, keep_duration=True):

/usr/local/lib/python3.6/dist-packages/moviepy/audio/io/AudioFileClip.py in <lambda>(t)
     76         self.buffersize = self.reader.buffersize
     77 
---> 78         self.make_frame = lambda t: self.reader.get_frame(t)
     79         self.nchannels = self.reader.nchannels
     80 

/usr/local/lib/python3.6/dist-packages/moviepy/audio/io/readers.py in get_frame(self, tt)
    200                 # repeat the last frame instead
    201                 indices[indices>=len(self.buffer)] = len(self.buffer) -1
--> 202                 result[in_time] = self.buffer[indices]
    203                 return result
    204 

IndexError: index -59041 is out of bounds for axis 0 with size 40960

import matplotlib.pyplot as plt
%matplotlib inline
import moviepy.editor as mpy
!pip install youtube_dl
import youtube_dl

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'wav',
        'preferredquality': '192',
    }],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=5pIpVK3Gecg'])

#Tyler, The Creator - 'IGOR,' Odd Future and Scoring a Number 1 Album _ Apple Music-5pIpVK3Gecg.wav is the name
#that youtube_dl auto assigns. I got it from the output of ydl.download command

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
ffmpeg_extract_subclip("Tyler, The Creator - 'IGOR,' Odd Future and Scoring a Number 1 Album _ Apple Music-5pIpVK3Gecg.wav", 170, 180, targetname="talk.wav")


talk = mpy.AudioFileClip('talk.wav')


sample_rate = talk.fps
NFFT = sample_rate /25
audio_data = talk.to_soundarray()