该代码可使用默认的youtube_dl名称,但不能使用我输入ydl_opts的outtmpl名称
此处给出的解决方案-https://www.reddit.com/r/moviepy/comments/dwc27w/to_sound_array_issue/ 似乎不起作用
这是代码-
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,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,**k)
55
56
/usr/local/lib/python3.6/dist-packages/moviepy/audio/AudioClip.py in to_soundarray(self,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,---> 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,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,**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
当我没有在ydl_opts中设置outtmpl选项并手动输入ydl自动分配给ffmpeg_extract_subclip的名称时,它似乎可以工作
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',}
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,' Odd Future and Scoring a Number 1 Album _ Apple Music-5pIpVK3Gecg.wav",targetname="talk.wav")
talk = mpy.AudioFileclip('talk.wav')
sample_rate = talk.fps
NFFT = sample_rate /25
audio_data = talk.to_soundarray()