File size: 1,909 Bytes
6755a2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
import cv2

import logging

logger = logging.getLogger(__name__)  # pylint: disable=invalid-name


class VideoClipOperator(object):
    def __init__(self) -> None:
        pass

    def __call__(self, *args, **kwds):
        pass


class VSELength(VideoClipOperator):
    def __init__(self, time_start, duration, target, change_length_func) -> None:
        self.time_start = time_start
        self.duration = duration
        self.target = target
        self.change_length_func = change_length_func

    def __call__(self, *args, **kwds):
        return super().__call__(*args, **kwds)


class EditedVideoWriter(object):
    """do operators to videoclip

    Args:
        operators ([[VideoClipOperator,VideoClipOperator], [VideoClipOperator]]):
    """

    def __init__(self, operators):
        self.operators = operators

    def __call__(self, video, out):
        """
        1. open out path
        2. do operator to video, return edited video clip
        3. save

        Args:
            video (_type_): _description_
            out (_type_): _description_
        """
        cap = cv2.VideoCapture(video)
        # Check if camera opened successfully
        if cap.isOpened() == False:
            logger.error("Error opening video stream or file")

        out = cv2.VideoWriter(
            out,
            cv2.VideoWriter_fourcc("M", "J", "P", "G"),
            10,
            (self.width, self.height),
        )
        # float `width`
        for clip_operator in self.operators:
            frames = clip_operator(
                width=cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH),
                height=cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT),
                frame_count=cap.get(cv2.CAP_PROP_FRAME_COUNT),
                fps=cap.get(cv2.cv.CV_CAP_PROP_FPS),
            )
            for frame in frames:
                out.write(frame)
        out.release()