1
|
|
|
""" |
2
|
|
|
ffmpeg_streaming.streams |
3
|
|
|
~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
Useful methods |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
:copyright: (c) 2020 by Amin Yazdanpanah. |
9
|
|
|
:website: https://www.aminyazdanpanah.com |
10
|
|
|
:email: [email protected] |
11
|
|
|
:license: MIT, see LICENSE for more details. |
12
|
|
|
""" |
13
|
|
|
import logging |
14
|
|
|
import os |
15
|
|
|
import re |
16
|
|
|
import time |
17
|
|
|
import warnings |
18
|
|
|
from sys import platform |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
def get_path_info(path): |
22
|
|
|
""" |
23
|
|
|
@TODO: add documentation |
24
|
|
|
""" |
25
|
|
|
dirname = os.path.dirname(path) |
26
|
|
|
name = str(os.path.basename(path).split('.')[0]) |
27
|
|
|
|
28
|
|
|
return dirname, name |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def mkdir(dirname: str) -> None: |
32
|
|
|
""" |
33
|
|
|
@TODO: add documentation |
34
|
|
|
""" |
35
|
|
|
try: |
36
|
|
|
os.makedirs(dirname) |
37
|
|
|
except OSError as exc: |
38
|
|
|
logging.info(exc) |
39
|
|
|
pass |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def rm(path: str) -> None: |
43
|
|
|
""" |
44
|
|
|
@TODO: add documentation |
45
|
|
|
""" |
46
|
|
|
try: |
47
|
|
|
os.remove(path) |
48
|
|
|
except OSError as exc: |
49
|
|
|
logging.info(exc) |
50
|
|
|
pass |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def clean_args(args: list) -> list: |
54
|
|
|
""" |
55
|
|
|
@TODO: add documentation |
56
|
|
|
""" |
57
|
|
|
clean_args_ = [] |
58
|
|
|
for arg in args: |
59
|
|
|
if " " in arg: |
60
|
|
|
arg = '"' + arg + '"' |
61
|
|
|
clean_args_.append(arg.replace("\\", "/").replace("__COLON__", ":")) |
62
|
|
|
|
63
|
|
|
return clean_args_ |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def convert_to_sec(time): |
67
|
|
|
""" |
68
|
|
|
@TODO: add documentation |
69
|
|
|
""" |
70
|
|
|
h, m, s = time.split(":") |
71
|
|
|
return int(h) * 3600 + int(m) * 60 + int(s) |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def get_time(key, string, default): |
75
|
|
|
""" |
76
|
|
|
@TODO: add documentation |
77
|
|
|
""" |
78
|
|
|
time = re.search('(?<={})\w+:\w+:\w+'.format(key), string) |
79
|
|
|
return convert_to_sec(time.group(0)) if time else default |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
def time_left(start_time, unit, total): |
83
|
|
|
""" |
84
|
|
|
@TODO: add documentation |
85
|
|
|
""" |
86
|
|
|
if unit != 0: |
87
|
|
|
diff_time = time.time() - start_time |
88
|
|
|
return total * diff_time / unit - diff_time |
89
|
|
|
else: |
90
|
|
|
return 0 |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
def deprecated(func): |
94
|
|
|
""" |
95
|
|
|
@TODO: add documentation |
96
|
|
|
""" |
97
|
|
|
def deprecated_fun(*args, **kwargs): |
98
|
|
|
warnings.warn('The {} method is deprecated and will be removed in a future release'.format(func.__name__) |
99
|
|
|
, DeprecationWarning, stacklevel=2) |
100
|
|
|
return func(*args, **kwargs) |
101
|
|
|
return deprecated_fun |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def get_os(): |
105
|
|
|
""" |
106
|
|
|
@TODO: add documentation |
107
|
|
|
""" |
108
|
|
|
if platform == "linux" or platform == "linux2": |
109
|
|
|
os_name = 'linux' |
110
|
|
|
elif platform == "darwin": |
111
|
|
|
os_name = 'os_x' |
112
|
|
|
elif platform == "win32" or platform == "Windows": |
113
|
|
|
os_name = 'windows' |
114
|
|
|
else: |
115
|
|
|
os_name = 'unknown' |
116
|
|
|
|
117
|
|
|
return os_name |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
def cnv_options_to_args(options: dict): |
121
|
|
|
""" |
122
|
|
|
@TODO: add documentation |
123
|
|
|
""" |
124
|
|
|
args = [] |
125
|
|
|
for k, v in options.items(): |
126
|
|
|
args.append('-{}'.format(k)) |
127
|
|
|
if v is not None: |
128
|
|
|
args.append('{}'.format(v)) |
129
|
|
|
|
130
|
|
|
return args |
131
|
|
|
|