finch11111
finch11111

Reputation: 11

AttributeError: 'str' object has no attribute 'duration'

I am trying to edit my video using moviepy. And when I want to cut a part of it I get error: AttributeError: 'str' object has no attribute 'duration' Why ?

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')

This is my Error traceback:

---------------------------------------------------------------------------
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'

Upvotes: 0

Views: 1991

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54718

The PROBLEM here, which would have been clear if you read the documentation, is that the set_audio function does not take a string. It takes an AudioFileClip object. moviepy is smart enough not to do the actual work at that point; it just remembers what you wanted for audio. Later, when you try to use that clip, it tries to look up the audio file's duration, and it finds a string where it expected an object.

clip1 = clip0.set_audio(AudioFileClip(audio))

Upvotes: 2

Related Questions