moviepy音视频剪辑:AudioClip帧处理时报TypeError: only size-1 arrays can be converted to Python scalar错
程序员文章站
2022-03-10 19:48:50
☞ ░ 前往老猿Python博文目录 ░一、环境操作系统:win7 64位moviepy:1.0.3numpy:1.19.0Python:3.7.2二、应用代码及报错信息程序代码if __name__ == '__main__': import numpy as np make_frame = lambda t: 2*[t**((int(100000*t))%3)*0.99] clip = AudioClip(make_frame, duration=5...
☞ ░ 前往老猿Python博文目录 ░
一、环境
操作系统:win7 64位
moviepy:1.0.3
numpy:1.19.0
Python:3.7.2
二、应用代码及报错信息
程序代码
if __name__ == '__main__':
import numpy as np
make_frame = lambda t: 2*[t**((int(100000*t))%3)*0.99]
clip = AudioClip(make_frame, duration=5,fps=24000)
v = clip.max_volume()
print("maxvolumn=",v)
clip.write_audiofile(r"f:\video\audio.mp3")
以上代码是根据时间值来变更音频帧数据的输出,但执行时报错。
报错信息
"C:\Program Files\Python37\python.exe" F:/study/python/project/moviepyTest/moviepyTest.py F:\video\zbl1.mp4 50 0
Traceback (most recent call last):
File "F:/study/python/project/moviepyTest/moviepyTest.py", line 31, in <module>
v = clip.max_volume()
File "C:\Program Files\Python37\lib\site-packages\moviepy\audio\AudioClip.py", line 142, in max_volume
for chunk in self.iter_chunks(chunksize=chunksize,logger=logger):
File "C:\Program Files\Python37\lib\site-packages\moviepy\audio\AudioClip.py", line 86, in iter_chunks
fps=fps, buffersize=chunksize)
File "<decorator-gen-44>", line 2, in to_soundarray
File "C:\Program Files\Python37\lib\site-packages\moviepy\decorators.py", line 54, in requires_duration
return f(clip, *a, **k)
File "C:\Program Files\Python37\lib\site-packages\moviepy\audio\AudioClip.py", line 128, in to_soundarray
snd_array = self.get_frame(tt)
File "<decorator-gen-11>", line 2, in get_frame
File "C:\Program Files\Python37\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "C:\Program Files\Python37\lib\site-packages\moviepy\Clip.py", line 93, in get_frame
return self.make_frame(t)
File "F:/study/python/project/moviepyTest/moviepyTest.py", line 28, in <lambda>
make_frame = lambda t: 2*[t**((int(100000*t))%3)*0.99]
TypeError: only size-1 arrays can be converted to Python scalars
Process finished with exit code 1
这个报错原因老猿分析了好久,最后才发现这个错误是因为将int作用到了一个numpy数组导致的,因为音频的帧数据获取的参数t不只是浮点数,也可能是一个numpy数组,不能使用简单数字的操作方法。去掉int转换代码改成如下即可:
if __name__ == '__main__':
import numpy as np
#make_frame = lambda t: 2*[t**((int(100000*t))%3)*0.99]
make_frame = lambda t: 2 * [t ** ((100000 * t) % 3) * 0.99]
clip = AudioClip(make_frame, duration=5,fps=24000)
v = clip.max_volume()
print("maxvolumn=",v)
clip.write_audiofile(r"f:\video\audio.mp3")
在音频帧处理中,这是个容易犯错的地方,包括这样写代码:
make_frame = lambda t: 2 * [t*(-1) if t<0 else t]
这个处理音频时会报错:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
这是因为音频的帧获取的参数t有可能是numpy数组,只能使用numpy数组和数字都能操作的代码才能进行帧处理。
更多moviepy的介绍请参考《PyQt+moviepy音视频剪辑实战文章目录》或《moviepy音视频开发专栏》。
关于收费专栏
老猿的付费专栏《使用PyQt开发图形界面Python应用》专门介绍基于Python的PyQt图形界面开发基础教程,付费专栏《moviepy音视频开发专栏》详细介绍moviepy音视频剪辑合成处理的类相关方法及使用相关方法进行相关剪辑合成场景的处理,两个专栏加起来只需要19.9元,都适合有一定Python基础但无相关专利知识的小白读者学习。这2个收费专栏都有对应免费专栏,只是收费专栏的文章介绍更具体、内容更深入、案例更多。
对于缺乏Python基础的同仁,可以通过老猿的免费专栏《专栏:Python基础教程目录》从零开始学习Python。
如果有兴趣也愿意支持老猿的读者,欢迎购买付费专栏。
跟老猿学Python、学5G!
☞ ░ 前往老猿Python博文目录 ░
本文地址:https://blog.csdn.net/LaoYuanPython/article/details/107648390