python - AttributeError:“str”对象没有属性“duration”
问题描述
我正在尝试使用 moviepy 编辑我的视频。当我想剪掉它的一部分时,我得到了错误:
AttributeError: 'str' object has no attribute 'duration'
为什么?
from moviepy.editor import *
clip0 = VideoFileClip('08.mkv')
clip0 = clip0.set_audio(f'../Rus_sound/08.mkv'[:-3] + 'mp3')
end = 0
start = 0
lista = [0.4,0.6]
movie1 = '08.mkv'
movie2 = '../Bubble_Background_Video3.mp4'
clip0 = VideoFileClip(movie1)
audio = f'../Rus_sound/{movie1}'[:-3] + 'mp3'
clip1 = clip0.set_audio(audio)
w = clip1.w
h = clip1.h
fps = clip1.fps
clip2 = VideoFileClip(movie2).resize(height=h, width=w).set_fps(fps)
durata = clip1.duration - end
lista = [start] + [i*durata for i in lista ] + [durata]
stocked = []
for i in range(1, len(lista)):
o = i-1
clip = clip1.subclip(lista[o], lista[i])
stocked.append(clip)
if i != len(lista)-1:
stocked.append(clip2)
clip = concatenate_videoclips(stocked, method='compose')
这是我的错误回溯:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-42faa818ba3e> in <module>
----> 1 clip = clip1.subclip(0, 449.241)
<decorator-gen-152> in subclip(self, t_start, t_end)
~/Anaconda3/lib/python3.8/site-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
<decorator-gen-151> in subclip(self, t_start, t_end)
~/Anaconda3/lib/python3.8/site-packages/moviepy/decorators.py in apply_to_mask(f, clip, *a, **k)
27 the clip created with f """
28
---> 29 newclip = f(clip, *a, **k)
30 if getattr(newclip, 'mask', None):
31 newclip.mask = f(newclip.mask, *a, **k)
<decorator-gen-150> in subclip(self, t_start, t_end)
~/Anaconda3/lib/python3.8/site-packages/moviepy/decorators.py in apply_to_audio(f, clip, *a, **k)
41 newclip = f(clip, *a, **k)
42 if getattr(newclip, 'audio', None):
---> 43 newclip.audio = f(newclip.audio, *a, **k)
44 return newclip
45
~/Anaconda3/lib/python3.8/site-packages/moviepy/Clip.py in subclip(self, t_start, t_end)
382 t_start = self.duration + t_start # Remember t_start is negative
383
--> 384 if (self.duration is not None) and (t_start > self.duration):
385 raise ValueError("t_start (%.02f) " % t_start +
386 "should be smaller than the clip's " +
AttributeError: 'str' object has no attribute 'duration'
解决方案
如果您阅读文档就会清楚这里的问题是该set_audio
函数不接受字符串。它需要一个AudioFileClip
对象。 moviepy
足够聪明,不会在那个时候做实际的工作;它只记得你想要的音频。稍后,当您尝试使用该剪辑时,它会尝试查找音频文件的持续时间,并在预期对象的位置找到一个字符串。
clip1 = clip0.set_audio(AudioFileClip(audio))
推荐阅读
- django - 使用 Django Rest Framework JSON Web Token 创建新用户
- php - 使用自定义字段值(ACF 字段)将过滤器添加到管理列
- python - 如何在 discord.py 中将 on_message 与 sqlite3 集成?
- php - Laravel 包的 composer require 在 composer.json laravel/ui[v2.2.0, ..., 2.x-dev] 上有问题
- javascript - 注销后如何显示烤面包机
- c# - 获取分配给 Windows 计算机上登录用户的 Azure AD 组
- python - AttributeError:“RMSProp”没有属性“名称”
- wpf - 如何将用户信息从 Google OAuth 传递到 WPF CefSharp 浏览器中的请求网站
- xamarin - 为什么我不能在 android 模拟器的旋转视图上获得我的图像 aspectfit
- html - 如何在 HTML 或 CSS 中的链接旁边添加文本