0

Say I have multiple videos sorted out in a list that I'll call "sub_videos" based on its video length:

#  sub_videos ____________________________________________________________________________________
ten_second_or_below_videos = [ten_sec_video1, ten_sec_video2, ...]
twenty_second_or_below_videos = [twenty_sec_video10, twenty_sec_video12, ...]
thirty_second_or_below_videos = [thirty_sec_video10, thirty_sec_video12, ...]
forty_second_or_below_videos = [forty_sec_video15, forty_sec_video17, ...]
fifty_second_or_below_videos = [fifty_sec_video13, fifty_sec_video14, ...]
sixty_or_more_second_videos = [sixty_sec_video100, sixty_sec_video101, ...]
#  ^^^^^^^^^^^^^ everything above this will be called "sub_videos" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The ten_second_or_below_videos variable contains a list of videos that are 10 seconds or less long, the twenty_second_or_below_videos has a list that contains videos that are 20 seconds or less long, but not below or equal 10 seconds, the thirty_second_or_below_videos contains videos that are 30 seconds or less long, but not below or equal 20 seconds and so on...

And say I have another list of videos that have varying video lengths but are unsorted, which I'll call "main_videos":

main_videos = [ten_second_video1, twenty_second_video2, ten_second_video2, ...]

I want to be able to splice together one of the videos in "main_videos" with any of the videos on "sub_videos" with moviepy, but the final output should be just below or equal a desired time limit, which for my case; is 60 seconds. I have no clue on how to do it, so please help. The output should look like this somehow:

FINAL_OUTPUT = [ten_second_main_video, ten_second_sub_video,ten_second_sub_video,
                ten_second_sub_video, ten_second_sub_video, ten_second_sub_video]

or

FINAL_OUTPUT = [EXACTLY_twenty_second_main_video, EXACTLY_twenty_second_sub_video, EXACTLY_twenty_second_sub_video]  
#  the capitalized EXACTLY is used to indicate that the video is exactly twenty second

or

FINAL_OUTPUT = [ten_second_main_video, EXACTLY_thirty_second_sub_video,
                ten_second_sub_video, EXACTLY_ten_second_sub_video]

etc...


concatenate_videoclips(FINAL_OUTPUT).write_videofile('video.mp4') # the saved file should be just below or equal 60 seconds
AlexK
  • 2,855
  • 9
  • 16
  • 27
HELPME
  • 1

1 Answers1

0

There may be better solutions but this is what I came up with. I don't have the variety of video clips to test it but it should work in theory adjust it as needed. Note that you don't seem to have a 50-60 video clip list but this may account for it. Hope this helps!

subclips = [ten_second_or_below_videos, twenty_second_or_below_videos, thirty_second_or_below_videos, forty_second_or_below_videos, fifty_second_or_below_videos, sixty_or_more_second_videos]
for i in range(len(main_videos)):
    FINAL_OUTPUT = [main_videos[i]]
    duration = main_videos[i].duration
    max_added_lengths = duration - 60
    FINAL_OUTPUT = getSubclips(max_added_lengths, subclips, FINAL_OUTPUT)
    concatenate_videoclips(FINAL_OUTPUT).write_videofile('video.mp4')
    
def getIndex(max_added_lengths):    
    if max_added_lengths <= 10:
        return 0
    elif max_added_lengths <= 20:
        return 1
    elif max_added_lengths <= 30:
        return 2
    elif max_added_lengths <= 40:
        return 3
    elif max_added_lengths <= 50:
        return 4
    else:
        return 5
        
def getSubclips(max_added_lengths, subclips, FINAL_OUTPUT):
    index = getIndex(max_added_lengths)
    clips = subclips[index]
    max, index_of_max = checkClipDuration(max_added_lengths, clips)
    if max == -1 and index != 0:
        max, index_of_max = checkClipDuration(max_added_lengths, subclips[index-1])
        FINAL_OUTPUT.append(clips(index_of_max))
        max_added_lengths += max
        getSubclips(max_added_lengths, subclips, FINAL_OUTPUT)
    elif max == -1 and index == 0:
        return FINAL_OUTPUT
    else:
        FINAL_OUTPUT.append(clips(index_of_max))
        max_added_lengths += max
        getSubclips(max_added_lengths, subclips, FINAL_OUTPUT)
    
def checkClipDuration(max_added_lengths, clips):    
    max = -1
    index_of_max = -1
    for k in range(len(clips)):
        if(clips[k].duration > max and (max_added_lengths - clips[k].duration) >= 0):
            max = clips[k].duration
            index_of_max = k
    return max, index_of_max
        
kraken1989
  • 23
  • 4