File size: 2,503 Bytes
6ed89af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from PIL import Image, ImageFilter
import random
import os
from pathlib import Path
import cv2
import numpy as np
from modules.utils import image_frame_generator

class FrameVideoCreator:
    def __init__(self, fps: int = 30):
        """
        FrameVideoCreator ใ‚ฏใƒฉใ‚นใฎๅˆๆœŸๅŒ–้–ขๆ•ฐ
        
        ๅผ•ๆ•ฐ:
        - fps: ๅ‹•็”ปใฎใƒ•ใƒฌใƒผใƒ ใƒฌใƒผใƒˆ (ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฏ30FPS)
        """
        self.fps = fps

        # ๆ˜‡้ †๏ผˆไธŠใ‹ใ‚‰ไธ‹๏ผ‰ใฎใ‚จใƒ•ใ‚งใ‚ฏใƒˆใ‚’ๅˆใ‚ใซ้ฉ็”จ
        self.ascending_order = True
    
    def create_video_from_frames(self, frames: list, output_path: str):
        """
        ไธŽใˆใ‚‰ใ‚ŒใŸ็”ปๅƒใƒ•ใƒฌใƒผใƒ ใฎใƒชใ‚นใƒˆใ‹ใ‚‰ๅ‹•็”ปใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ๅ‹•็”ปใ”ใจใซๆ˜‡้ †ใจ้™้ †ใฎใ‚จใƒ•ใ‚งใ‚ฏใƒˆใ‚’ไบคไบ’ใซ้ฉ็”จใ—ใพใ™ใ€‚
        
        ๅผ•ๆ•ฐ:
        - frames: ็”ปๅƒใƒ•ใƒฌใƒผใƒ ใฎใƒชใ‚นใƒˆ
        - output_path: ไฟๅญ˜ใ™ใ‚‹ๅ‹•็”ปใฎใƒ‘ใ‚น
        """
        frames_bgr = [cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR) for frame in frames]
        
        # ๆ˜‡้ †ใ‹้™้ †ใฎใ‚จใƒ•ใ‚งใ‚ฏใƒˆใ‚’้ฉ็”จใ™ใ‚‹ใ‹ใซๅŸบใฅใ„ใฆใ€ใƒ•ใƒฌใƒผใƒ ใฎ้ †็•ชใ‚’ๅค‰ๆ›ด
        if not self.ascending_order:
            frames_bgr = list(reversed(frames_bgr))
        
        # ๆฌกๅ›žใฎๅ‹•็”ป็”ŸๆˆใฎใŸใ‚ใซใ‚จใƒ•ใ‚งใ‚ฏใƒˆใฎ้ †็•ชใ‚’ๅˆ‡ใ‚Šๆ›ฟใˆ
        self.ascending_order = not self.ascending_order
        
        height, width, layers = frames_bgr[0].shape
        size = (width, height)
        out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'MP4V'), self.fps, size)
        
        for frame in frames_bgr:
            out.write(frame)
        
        out.release()

    def process_folder(self, input_folder: str):
        """
        ๆŒ‡ๅฎšใ—ใŸใƒ•ใ‚ฉใƒซใƒ€ๅ†…ใฎ็”ปๅƒใ‚’ๅ‡ฆ็†ใ—ใ€ๅ‹•็”ปใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚
        
        ๅผ•ๆ•ฐ:
        - input_folder: ็”ปๅƒใŒไฟๅญ˜ใ•ใ‚Œใฆใ„ใ‚‹ใƒ•ใ‚ฉใƒซใƒ€ใฎใƒ‘ใ‚น
        """
        output_folder = f"{input_folder}_mov"
        Path(output_folder).mkdir(parents=True, exist_ok=True)

        for image_file in Path(input_folder).glob("*.png"):
            frames = image_frame_generator.generate_centered_moving_frames(str(image_file))
            output_video_path = Path(output_folder) / f"{image_file.stem}.mp4"
            self.create_video_from_frames(frames, str(output_video_path))

if __name__ == '__main__':   
    # ใ‚ฏใƒฉใ‚นใฎไฝฟ็”จไพ‹
    video_creator = FrameVideoCreator()
    video_creator.process_folder(r"image/Echoes-of-Creation_Blurred")