AttributeError:“str”对象没有属性“duration”

2024-05-20 13:36:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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'

Tags: inselfclipifdecoratorstartaudioend
1条回答
网友
1楼 · 发布于 2024-05-20 13:36:24

这里的问题是set_audio函数不接受字符串,如果您阅读文档,这一点就很清楚了。它接受一个AudioFileClip对象moviepy足够聪明,不会在那一点上做实际工作;它只记得你想要什么样的音频。稍后,当您尝试使用该剪辑时,它会尝试查找音频文件的持续时间,并在需要对象的位置找到字符串

clip1 = clip0.set_audio(AudioFileClip(audio))

相关问题 更多 >