File size: 3,422 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from functools import partial
import os
from typing import Tuple, Union

from ...utils.path_util import get_dir_file_map, get_path_dct
from ...utils.signature import get_md5sum


def get_video_signature(
    path: str,
    rename: bool = False,
    length: int = None,
    signature: str = None,
    sep: str = "@",
) -> Union[str, Tuple[str, str]]:
    """视频文件hash值特殊保存方法,name@signature,若hash值与目标长度不符或者不存在,则重新计算,并根据需要是否修改名字

    Args:
        path (str): 视频源路径
        rename (bool, optional): 是否对视频源文件重命名. Defaults to False.
        length (int, optional): hash值签名长度. Defaults to 8.
        signature (str, optional): full signature of path, to avoid recalculate signature. Defaults to `None`.
        sep (str, optional): 将hash值嵌入命名的分隔符. Defaults to "@".

    Returns:
        str: 视频文件hash值
        Tuple[str, str]: 对应的hash签名, 新的视频文件地址,
    """
    dirname = os.path.dirname(path)
    basename = os.path.basename(path)
    filename, ext = os.path.splitext(basename)
    file_signature = None
    if "@" in filename:
        file_signature = filename.split(sep)[-1]
        if length is not None and len(file_signature) != length:
            file_signature = None
    if file_signature is None:
        if signature is None:
            signature = get_md5sum(path, length=length)
            file_signature = signature
        if rename:
            dst_path = os.path.join(
                dirname, "{}@{}{}".format(filename.split(sep)[0], signature, ext)
            )
            os.rename(path, dst_path)
            path = dst_path
    if rename:
        return file_signature, path
    else:
        return file_signature


def split_names_with_signature(basename: str, sep: str = "@") -> str:
    """从videoinfo中分离 video的名字,videoinfo的名字是videoname_hash.json

    Args:
        basename (str): like name_hash
        sep (str): like sep

    Returns:
        str: 视频文件名
    """
    filename = ".".join(basename.split(".")[:-1])
    ext = basename.split(".")[-1]
    if sep in filename:
        filename = sep.join(filename.split(sep)[:-1])
    return filename, ext


def get_video_map_path_dct(
    path: str, mode: int = 1, sep: str = "@", exts=["json"]
) -> dict:
    """遍历目标文件夹及子文件夹下所有视频谱面文件,生成字典。"""
    dct = get_path_dct(
        path=path,
        mode=mode,
        sep=sep,
        split_func=partial(split_names_with_signature, sep=sep),
        exts=exts,
    )
    return dct


def get_video_path_dct(
    path, mode: int = 1, sep: str = "@", exts=["mp4", "mkv", "ts", "rmvb", "mov"]
) -> dict:
    """遍历目标文件夹及子文件夹下所有视频文件,生成字典。"""
    dct = get_path_dct(
        path=path,
        mode=mode,
        sep=sep,
        split_func=partial(split_names_with_signature, sep=sep),
        exts=exts,
    )
    return dct


def get_video_emd_path_dct(path, mode: int = 1, sep: str = "@", exts=["hdf5"]) -> dict:
    """遍历目标文件夹及子文件夹下所有视频文件,生成字典。"""
    dct = get_path_dct(
        path=path,
        mode=mode,
        sep=sep,
        split_func=partial(split_names_with_signature, sep=sep),
        exts=exts,
    )
    return dct