0

Iam trying to put each clip inside a file into one clip using moviepy library

from moviepy.editor import VideoFileClip, concatenate_videoclips
import os
import cv2

clips = []
    for filename in os.listdir(r'clips//'):
        clips.append(filename)

print(clips)

finalVideo = concatenate_videoclips(f'clips//{clips}')
finalVideo.write_videofile('finalVideo.mp4')

However, i get this error:

File "C:/Users/myalt/OneDrive/Desktop/pythonProject2/main.py", line 11, in <module>
    finalVideo = concatenate_videoclips(f'clips//{clips}')
  File "C:\Users\myalt\OneDrive\Desktop\pythonProject2\venv\lib\site-packages\moviepy\video\compositing\concatenate.py", line 71, in concatenate_videoclips
    tt = np.cumsum([0] + [c.duration for c in clips])
  File "C:\Users\myalt\OneDrive\Desktop\pythonProject2\venv\lib\site-packages\moviepy\video\compositing\concatenate.py", line 71, in <listcomp>
    tt = np.cumsum([0] + [c.duration for c in clips])
AttributeError: 'str' object has no attribute 'duration'
Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43
8auuuuuer
  • 51
  • 5

1 Answers1

1

What about something like:

clips = []
clipPaths = [] #If you want to store the paths as well
for filename in os.listdir(r'clips//'):
    #clips.append(filename)
    clip = VideoFileClip(filename)
    clips.append(clip)
    clipPaths.append(filename)

https://zulko.github.io/moviepy/examples/quick_recipes.html

https://zulko.github.io/moviepy/getting_started/compositing.html

(May have to tweak the path)

Twenkid
  • 825
  • 7
  • 15
  • Thanks! works now, is there anyway to speed it up? its awfully slow – 8auuuuuer Dec 18 '20 at 01:04
  • 1
    You're welcome! I don't know how moviePy does it, I guess it renders it/reencodes the clips. If the clips have the same codecs and bitrates (e.g. from the same camera) and no trimming is needed, I'd use ffmpeg for that job, which can concatenate without reencoding. E.g. https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg I usually apply the method with the files: mylist.txt file '/path/to/file1' file '/path/to/file2' file '/path/to/file3' ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4 – Twenkid Dec 18 '20 at 01:14