File size: 1,832 Bytes
4ae7d54
 
 
 
 
 
 
 
ea5b008
 
4ae7d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e14a56
 
 
 
4ae7d54
 
 
4e14a56
4ae7d54
 
 
 
 
 
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
import os
import datetime
import uuid
from typing import List

import supervision as sv


# MAX_VIDEO_LENGTH_SEC = 1
MAX_VIDEO_LENGTH_SEC = 2


def generate_file_name(extension="mp4"):
    current_datetime = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    unique_id = uuid.uuid4()
    return f"{current_datetime}_{unique_id}.{extension}"


def list_files_older_than(directory: str, diff_minutes: int) -> List[str]:
    diff_seconds = diff_minutes * 60
    now = datetime.datetime.now()
    older_files: List[str] = []

    for filename in os.listdir(directory):
        file_path = os.path.join(directory, filename)
        if os.path.isfile(file_path):
            file_mod_time = os.path.getmtime(file_path)
            file_mod_datetime = datetime.datetime.fromtimestamp(file_mod_time)
            time_diff = now - file_mod_datetime
            if time_diff.total_seconds() > diff_seconds:
                older_files.append(file_path)

    return older_files


def remove_files_older_than(directory: str, diff_minutes: int) -> None:
    older_files = list_files_older_than(directory, diff_minutes)
    file_count = len(older_files)

    for file_path in older_files:
        os.remove(file_path)

    now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(
        f"[{now}] Removed {file_count} files older than {diff_minutes} minutes from "
        f"'{directory}' directory."
    )


def calculate_end_frame_index(
    source_video_path: str,
    max_video_length_sec: int = MAX_VIDEO_LENGTH_SEC
) -> int:
    video_info = sv.VideoInfo.from_video_path(source_video_path)
    return min(
        video_info.total_frames,
        video_info.fps * max_video_length_sec
    )


def create_directory(directory_path: str) -> None:
    if not os.path.exists(directory_path):
        os.makedirs(directory_path)