简体   繁体   中英

Pass Python list to concatenate_videoclips function in Moviepy module

Purpose:

  1. Build a python list of video files in a directory
  2. Open video clips (Understood requirement of moviepy)
  3. Call combine_clips function and pass 'clips' list (Error occurs here.)
  4. Write combined clips to single video file

Error:

"concatenate.py", line 71, in concatenate_videoclips
tt = np.cumsum([0] + [c.duration for c in clips])
AttributeError: 'str' object has no attribute 'duration'"

Script:

import os
from moviepy.editor import *

def clips_list():
    clips = []
    for file in os.listdir('.'):
        if file.endswith(".mov"):
            clips.append(file)
    open_clips(clips)

def open_clips(clips):
    for clip in clips:
        VideoFileClip(clip)
    combine_clips(clips)

def combine_clips(clips):
    video = concatenate_videoclips(clips, method='compose')

def output_video(video):
    video.write_videofile('ct_01.mp4')

clips_list()

Question: Why does "concatenate_videoclips()" accept a hardcoded list but fail when I pass it a list.

You create VideoFileClip(clip) but you don't add to list so you send list with filenames to concatenate_videoclips

You need new_list in

def open_clips(clips):

    new_list = []

    for clip in clips:
        new_list.append(VideoFileClip(clip))

    combine_clips(new_list)

EDIT: shorter:

import os
from moviepy.editor import *

clips = []

for filename in os.listdir('.'):
    if filename.endswith(".mov"):
        clips.append(VideoFileClip(filename))

video = concatenate_videoclips(clips, method='compose')
video.write_videofile('ct_01.mp4')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM