将视频帧写入另一个视频Python OpenCV



我正在阅读YouTube上的一个视频(用于测试目的)。真实的情况是来自视频摄像机馈送),并采取每一帧,并注入一个标志,并使用这些帧创建另一个视频。此外,我需要保存这些帧的图像以及(这是已经为我工作)。我尝试了以下命令

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

这写一个视频,但它说视频损坏或不正确的扩展名。在Python中使用OpenCV将这些帧写入新视频的最佳方式是什么?

实现中有很多不合逻辑的东西,比如"video"是一个聚会。没有read方法的流

你应该做的是使用流url和videoccapture来获取帧,复制徽标的像素,然后用VideoWriter来写。

import cv2
import pafy

def apply_logo(url, logo, filename):
video = pafy.new(url)
best = video.getbest(preftype="mp4")
reader = cv2.VideoCapture(best.url)
if not reader.isOpened():
return False
fourcc = cv2.VideoWriter_fourcc(*"MP4V")
writer = cv2.VideoWriter(filename, fourcc, 60.0, best.dimensions)
logo_width, logo_height = [min(x, y) for x, y in zip(best.dimensions, logo.shape)]
i = 0
while True:
ret, frame = reader.read()
if ret:
frame[:logo_width, :logo_height] = logo[:logo_width, :logo_height]
writer.write(frame)
i += 1
print(i)
else:
break
reader.release()
writer.release()
return True
url = "https://www.youtube.com/watch?v=vDHtypVwbHQ"
logo = cv2.imread("logo.png")
apply_logo(url, logo, "output.mp4")

相关内容

  • 没有找到相关文章

最新更新