将不同长度的视频拼接在一起,生成不超过60秒的视频



假设我有多个视频在一个列表中排序,我将称之为"sub_ videos">基于其视频长度:

#  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" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ten_second_or_below_videos变量包含10秒或更短的视频列表,twenty_second_or_below _videos包含20秒或更短但不低于或等于10秒的视频列表;thirty_cond_or_below_video包含30秒或更短,但不低于和等于20秒的视频,依此类推…

然后说我有另一个视频列表,这些视频长度不同,但未排序,我称之为"main_ videos">

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

我希望能够将"中的一个视频拼接在一起;main_ videos">上的任何视频";sub_ videos">使用moviepy,但最终输出应该刚好低于或等于所需的时间限制,在我的情况下;是60秒。我不知道怎么做,所以请帮忙。输出应该是这样的:

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

可能有更好的解决方案,但这就是我想到的。我没有各种各样的视频剪辑来测试它,但理论上应该可以根据需要进行调整。请注意,你似乎没有50-60的视频剪辑列表,但这可能是原因。希望这能有所帮助!

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

最新更新