I'm reading a video from YouTube(for testing purpose. real case would be from a video camera feed) and take each frame and inject a logo on it and create another video using those frames. Also I need to save these frames as images as well(which is already works for me). I tried the following,
img = cv2.imread('logo.png')
img_height, img_width, _ = img.shape
url = "https://www.youtube.com/watch?v=vDHtypVwbHQ"
video = pafy.new(url)
best = video.getbest(preftype="mp4")
frame_no = 1
width = 1280
hieght = 720
fps = 30
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
videoW = cv2.VideoWriter('image_to_video.mp4', fourcc, float(fps), (width, hieght))
while True:
_, frame = video.read()
frame[y:y + img_height , x:x + img_width ] = img
videoW.write(frame)
frame_no += 1
This writes a video but it says the video corrupted or incorrect extension. What would be the best way to write these frames into a new video in Python with OpenCV?
video = pafy.new(url), and then you assign the videowriter object to that same variable:video = cv2.VideoWriter(. On the other hand VideoWriter serves to write, and instead you try to read from that file. Finally, why do you use pafy if in the end "best" you don't use it at all?ret, frame = video.read()if ret:# foo process_, frame = video.read()should throw an exception to you, pointing out that you are using a non-existent method._, frame = video.read()is executed, the program breaks and there will only be an empty .mp4 so you cannot reproduce it.