Video-Summary/Exporter.py

120 lines
4.4 KiB
Python
Raw Normal View History

import imageio
2020-09-25 17:52:41 +00:00
import imutils
import numpy as np
from Layer import Layer
2020-09-25 17:52:41 +00:00
import cv2
from VideoReader import VideoReader
2020-09-20 20:01:54 +00:00
class Exporter:
fps = 30
2020-09-24 13:47:49 +00:00
def __init__(self):
print("Exporter initiated")
2020-09-24 13:47:49 +00:00
def export(self, frames, outputPath):
fps = self.fps
writer = imageio.get_writer(outputPath, fps=fps)
2020-09-24 13:47:49 +00:00
for frame in frames:
writer.append_data(np.array(frame))
2020-09-24 13:47:49 +00:00
writer.close()
2020-09-25 17:52:41 +00:00
2020-10-11 12:13:27 +00:00
def exportLayers(self, layers, footagePath, outputPath, resizeWidth):
listOfFrames = self.makeListOfFrames(layers)
videoReader = VideoReader(footagePath, listOfFrames)
videoReader.fillBuffer()
maxLength = self.getMaxLengthOfLayers(layers)
underlay = cv2.VideoCapture(footagePath).read()[1]
2020-10-11 12:13:27 +00:00
underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB)
frames = [underlay]*maxLength
exportFrame = 0
2020-09-28 20:28:23 +00:00
fps = self.fps
writer = imageio.get_writer(outputPath, fps=fps)
2020-10-11 12:13:27 +00:00
while not videoReader.videoEnded():
frameCount, frame = videoReader.pop()
if frameCount % (60*self.fps) == 0:
print("Minutes processed: ", frameCount/(60*self.fps))
if frame is None:
print("ContourExtractor: frame was None")
2020-09-29 20:52:36 +00:00
continue
2020-10-11 12:13:27 +00:00
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
for layer in layers:
if layer.startFrame <= frameCount and layer.startFrame + len(layer.bounds) > frameCount:
(x, y, w, h) = layer.bounds[frameCount - layer.startFrame]
factor = videoReader.w / resizeWidth
x = int(x * factor)
y = int(y * factor)
w = int(w * factor)
h = int(h * factor)
# if exportFrame as index instead of frameCount - layer.startFrame then we have layer after layer
frame2 = underlay
frame2[y:y+h, x:x+w] = frame[y:y+h, x:x+w]
writer.append_data(frame2)
2020-10-11 12:13:27 +00:00
videoReader.thread.join()
2020-10-11 12:13:27 +00:00
def exportOverlayed(self, layers, footagePath, outputPath, resizeWidth):
listOfFrames = self.makeListOfFrames(layers)
videoReader = VideoReader(footagePath, listOfFrames)
videoReader.fillBuffer()
2020-09-30 17:22:10 +00:00
maxLength = self.getMaxLengthOfLayers(layers)
underlay = cv2.VideoCapture(footagePath).read()[1]
underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB)
frames = [underlay]*maxLength
exportFrame = 0
2020-09-30 17:22:10 +00:00
while not videoReader.videoEnded():
frameCount, frame = videoReader.pop()
if frameCount % (60*self.fps) == 0:
print("Minutes processed: ", frameCount/(60*self.fps))
if frame is None:
print("ContourExtractor: frame was None")
continue
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
2020-09-30 17:22:10 +00:00
for layer in layers:
if layer.startFrame <= frameCount and layer.startFrame + len(layer.bounds) > frameCount:
(x, y, w, h) = layer.bounds[frameCount - layer.startFrame]
factor = videoReader.w / resizeWidth
x = int(x * factor)
y = int(y * factor)
w = int(w * factor)
h = int(h * factor)
# if exportFrame as index instead of frameCount - layer.startFrame then we have layer after layer
frame2 = frames[frameCount - layer.startFrame]
frame2[y:y+h, x:x+w] = frame[y:y+h, x:x+w]
frames[frameCount - layer.startFrame] = np.copy(frame2)
videoReader.thread.join()
fps = self.fps
writer = imageio.get_writer(outputPath, fps=fps)
for frame in frames:
writer.append_data(frame)
writer.close()
2020-09-30 17:22:10 +00:00
def getMaxLengthOfLayers(self, layers):
maxLength = 0
for layer in layers:
if layer.getLength() > maxLength:
maxLength = layer.getLength()
return maxLength
def makeListOfFrames(self, layers):
'''Returns set of all Frames which are relavant to the Layers'''
frameNumbers = set()
for layer in layers:
frameNumbers.update(
list(range(layer.startFrame, layer.startFrame + len(layer.bounds))))
return list(frameNumbers)