forked from saeidEmadi/DCProj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoController.py
166 lines (142 loc) · 6.49 KB
/
videoController.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import os
import argparse
import shutil
class VideoController():
""" this class use for control video versions
if videos folder is exists :
True : change video names and move in to
else : make video directory """
def __init__(self, source : str, dest : str = '.', formats : list = ['.mp4','.mkv']):
""" initial class constructor """
# check video path folder
self.source = source
self.dest = dest
self.formats = formats
self.__lastIndex = self.__getLastIndex()
try :
if 'videos' not in os.listdir(os.path.abspath(dest)):
os.mkdir(os.path.abspath(os.path.dirname(dest))+"\\videos\\")
print(f"video folder created in : {os.path.abspath(os.path.dirname(dest))}\\videos\\")
else :
# order video names
self.__lastIndex = -1
self.__ordering()
except :
raise FileNotFoundError('The directory name is invalid')
def __fileMover(self, source : str, dest : str) -> bool :
try :
shutil.move(source, dest)
return True
except :
return False
def videoAdder(self) -> None :
""" add video from source to destination Path """
if os.path.isfile(self.__source) :
file_name, extension = os.path.splitext(self.__source)
file_name = self.__getNemName(extension)
if self.__fileMover(os.path.realpath(self.__source), \
os.path.abspath(self.__dest)+"\\videos\\"+file_name) :
print(f"mv {os.path.realpath(self.__source)} to {os.path.abspath(self.__dest)}\\videos\\{file_name}")
else :
print(f"can\'t mv {os.path.realpath(self.__source)} to {os.path.abspath(self.__dest)}\\videos\\{file_name}")
elif os.path.isdir(self.__source) :
for video in self.__fetchVideos(self.source) :
file_name, extension = os.path.splitext(video)
file_name = self.__getNemName(extension)
if self.__fileMover(os.path.realpath(self.__source)+"\\"+video, \
os.path.abspath(self.__dest)+"\\videos\\"+file_name) :
print(f"mv {os.path.realpath(self.__source)}\\{video} to {os.path.abspath(self.__dest)}\\videos\\{file_name}")
else :
print(f"can\'t mv {os.path.realpath(self.__source)}\\{video} to {os.path.abspath(self.__dest)}\\videos\\{file_name}")
def getCameraOfflineVideos(self, numOfClients : int) -> list :
""" check pair camera's and Client's """
j = self.__lastIndex
lVideos = self.__fetchVideos(self.__source + "\\videos\\")
videos = list()
for _ in range(numOfClients):
if j % self.__lastIndex == 0 :
j -= self.__lastIndex
videos.append(lVideos[j])
j += 1
return videos
def __getLastIndex(self) -> int :
""" find last index for set new name indexing """
if 'videos' in os.listdir(os.path.abspath(self.__dest)):
videos = self.__fetchVideos(self.__dest+"\\videos")
if len(videos) > 0 :
file_name, _ = os.path.splitext(videos[-1])
return int(''.join(_ for _ in file_name if _.isdigit()))
return -1
def __getNemName(self, videoFormat) -> str :
""" create new video name
pattern : video{index}[.format]"""
self.__lastIndex += 1
return f"video{self.__lastIndex}{videoFormat}"
def __rename(self, old, new) -> bool :
""" rename file """
try :
os.rename(old, new)
return True
except :
return False
def __ordering(self) -> None :
""" ordering file's """
videos = self.__fetchVideos(self.__dest+"\\videos")
for video in videos :
file_name, extension = os.path.splitext(video)
if not self.__rename(self.__dest + "\\videos\\"+video, self.__dest +"\\videos\\"+self.__getNemName(extension)) :
print("cant ordering files in videos folder")
def __fetchVideos(self, addr : str) -> list :
""" fetch vide files and directory """
if os.path.isfile(addr) :
addr = os.path.dirname(os.path.realpath(addr))
direList = os.listdir(addr)
videos = list()
for _ in direList :
file_name, extension = os.path.splitext(_)
if extension in self.__formats :
videos.append(_)
return videos
@property
def source(self) -> str :
return self.__source
@source.setter
def source(self, sourceAddress) -> None :
""" check source address valid """
if os.path.isdir(sourceAddress) \
or os.path.isfile(sourceAddress) :
self.__source = sourceAddress
else :
raise FileExistsError('source Path is not exists.')
@property
def dest(self) -> str :
return self.__dest
@dest.setter
def dest(self, destAddress) -> None :
""" check destination address valid """
if os.path.isdir(destAddress) :
self.__dest = destAddress
else :
raise FileExistsError('source Path is not exists.')
@property
def formats(self) -> list :
return self.__formats
@formats.setter
def formats(self, formats) -> None :
self.__formats = formats
if __name__ == "__main__" :
""" Script mode runner """
print("[ video controller Running : CLI Mode]")
# argument parser
argparser = argparse.ArgumentParser(
description = "video controller runner Script | run script for input controlling video for camera's",\
prog = 'videoController')
argparser.add_argument('source_address', metavar = 'source address', type = str, nargs = 1, \
help = "video source address (folder or video name)")
argparser.add_argument('--dest', '-d', metavar = 'destination address', type = str, nargs = 1, \
help = "video destination address folder (video folder)", default = list(['.'],))
argparser.add_argument('--format', '-f', metavar = 'format', type = str, nargs = '+', \
required = True ,help = "find this formats in directory [.mp4|.mkv|etc]")
args = argparser.parse_args()
c = VideoController(source = args.source_address[0], dest = args.dest[0])
c.videoAdder()