0

I hope the question is clear enough. Basically I have an array "output" containing all the folders I will utilize (as shown below):

output = ['folder1', 'folder2', 'folder3', 'folder4']

I want to create a video for all the subfolders. I can manage to do it if I just create a video with all the videos, but not when I want to create a video from the videos of each folder. the following code I used to compile all the .mp4 files into one video:

from moviepy.editor import *
import os
from natsort import natsorted

L =[]

for root, dirs, files in os.walk("/media/pi/videos"):

    #files.sort()
    files = natsorted(files)
    for file in files:
        if os.path.splitext(file)[1] == '.mp4':
            filePath = os.path.join(root, file)
            video = VideoFileClip(filePath)
            L.append(video)


final_clip = concatenate_videoclips(L)
final_clip.to_videofile("output.mp4", fps=24, remove_temp=False)

But when I try to do the same thing over a for loop I get errors and cannot manage to get my head around what is wrong. Any help is sincerely appreciated. This is the code I have been trying to get to work:

output.sort()

   for i in output:
    if not i == 'System Volume Information':
        path2 = '/media/pi/videos/%s' % i

        L =[]

           for root, dirs, files in os.walk(path2):

               #files.sort()
               files = natsorted(files)
               for file in files:
                   if os.path.splitext(file)[1] == '.mp4':
                       filePath = os.path.join(root, file)
                       video = VideoFileClip(filePath)
                       L.append(video)


           final_clip = concatenate_videoclips(L)
           final_clip.to_videofile("output.mp4", fps=24, remove_temp=False)

Basically What I am trying to do is combine a certain amount of videos. I have renamed all my .mp4 files to "1.mp4" "2.mp4" "3.mp4" etc, and I am am trying to make the code combine lets say 5 .mp4 files for 1 video and would go in a for loop until there are no more videos to combine. This is the only way I could think of doing it. I have made subfolders containing 5 .mp4 files and now I am trying to combine these videos for each folder. This, I believe, is not very efficient so any better advice would be helpful

George Lua
  • 17
  • 1
  • 5
  • Hello, I can see that you've fixed your problem. If you post the complete error trace here, then other people might be able to find your solution and you'll have an easier time getting help in the future. – Tom Burrows Apr 26 '20 at 21:24

2 Answers2

0

If I've understood what you're trying to achieve, maybe something like this?

from pathlib import Path
from moviepy.editor import VideoFileClip, concatenate_videoclips

for resource_path in Path("vids").glob("*"):
    if not resource_path.is_dir():
        continue
    directory_path = resource_path

    clip = concatenate_videoclips(list(map(VideoFileClip, (str(v) for v in directory_path.glob("*.mp4")))))
    clip.to_videofile(str(directory_path.joinpath("output.mp4")), fps=24, remove_temp=False)

Basically, it iterates over all directories in the "vids" directory. Then, it grabs all the mp4s inside the current directory and concatenates them together to create an "output.mp4" for that folder. It then moves on to the next folder and repeats.

Paul M.
  • 10,481
  • 2
  • 9
  • 15
  • Thank you for your reply. I have tried your code and it runs, but does not compile the videos in all these folders... I thought that maybe it is due to Path("videos"), but even when I changed it ti Path("/media/pi/videos") i still get an error, saying " valueError: max() arg is an empty sequence" – George Lua Apr 10 '20 at 10:05
  • @GeorgeLua Could you post the full stack-trace of the exception? On which line does it occur? – Paul M. Apr 10 '20 at 16:21
  • it was probably my own fault that I couldn't manage to get it to work, but I managed to figure it out my way. Anyways, thank you very much for your reply :) Have a great day! – George Lua Apr 11 '20 at 19:49
0

This the way I managed to solve my own problem. There are probably way better ways to do this, but this is what worked for me. I believe all is clear by some of the comments I put in there.

from moviepy.editor import *
import os
from natsort import natsorted

#directory of videos
source_dir = '/media/video'

for root, dirs, files in os.walk(source_dir):
    for i in dirs:
        #checks the subdirectories
        path2 = "/media/video/" + str(i)  
        L =[]

        #combines all the .mp4 files
        for root, dirs, files in os.walk(path22):
            files = natsorted(files)        
            for file in files:
                if os.path.splitext(file)[1] == '.mp4':
                    filePath = os.path.join(root, file)
                    video = VideoFileClip(filePath)
                    L.append(video)

        #creates the final video          
        final_clip = concatenate_videoclips(L)
        final_clip.to_videofile( "_output.mp4"  , fps=24, remove_temp=False) 
George Lua
  • 17
  • 1
  • 5