Reputation: 75
Say, I have a video and I have divided it in 7 parts and saved each of these parts separately and its length is 20 seconds.
clip1= VideoFileClip("first.mp4")
clip2= VideoFileClip("second.mp4")
clip3= VideoFileClip("third.mp4")
clip5= VideoFileClip("Fourth.mp4")
clip4= VideoFileClip("Fifth.mp4")
clip7= VideoFileClip("sixth.mp4")
clip6= VideoFileClip("seventh.mp4")
final_clip=concatenate_videoclips([clip1,clip2,clip3,clip4,clip5,clip6,clip7])
final_clip.write_videofile("new1.mp4")
Now what I want to do is, I want to create a program to extend this video to a specified time by randomly repeating these clips.
For instance say I want to make this 20 second long clip to 1 hour long by repeating these 7 clips randomly in python. So video generated will be 1 hour long but will contain random arrangement and repetition of these 7 clips.
Can anybody please tell me how to do this?
Upvotes: 1
Views: 703
Reputation: 1055
If you assign an id to each clip, say 0-6 for simplicity, why not just randomly generate a list containing the clips you'll need to reach 1 hour :
import random
# 1 hour is 3600 seconds
total_duration = 3600
clip_duration = 20
# 180 clips are needed to get an hour-long video
num_clips_required = int(total_duration / clip_duration)
# perform random sampling with replacement
list_of_clips = random.choices(range(7), k=num_clips_required)
You could use moviepy
to construct your final clip :
from moviepy.editor import VideoFileClip, concatenate_videoclips
# construct some dictionary containing ids as keys, and VideoFileClip objects as values
D = {0:VideoFileClip("path_to_clip_0.mp4"),
1:VideoFileClip("path_to_clip_1.mp4"),
# etc...
}
Then, write the final clip by doing :
combined = concatenate_videoclips([D[id] for id in list_of_clips])
combined.write_videofile("combined.mp4")
Upvotes: 1
Reputation: 103
import numpy as np
import random
import cv2
import os
videolist = os.listdir("path to video dir")
while True:
cap = cv2.VideoCapture("path to video dir/{}.webm".format(random.choice(videolist)))
while cap.isOpened():
ret, frame = cap.read()
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
if ret:
cv2.imshow("Image", frame)
else:
print("no video")
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
Upvotes: 1