-
Notifications
You must be signed in to change notification settings - Fork 1
/
trim.py
executable file
·132 lines (115 loc) · 4.17 KB
/
trim.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
import sys
import os
import time
from os.path import expanduser, join
file_name = ''
def lazy_name(task, variables, folder_path):
'''
task is the acronym for task should be less than 3 letters
## DONT FORGET TO ADD UNDERSORCE AT THE END OF TASK NAME ##
P_or_T is pool or transdeck should be strictly 'P' or 'T'
F_or_B is front or bottom camera should be strictly 'F' or 'B'
'''
TASKNAME_LIST = [
('br' , 'buoy_red_'),
('bg' , 'buoy_green_'),
('by' , 'buoy_yellow_'),
('bir' , 'bin_red_'),
('big' , 'bin_green_'),
('e' , 'empty_'),
('g' , 'gate_'),
('m' , 'multi_'),
('p' , 'parking_'),
('mr' , 'maker_'),
('t' , 'torpedo'),
]
task_found = False
for task_map in TASKNAME_LIST:
if task == task_map[0]:
task = task_map[1]
task_found = True
break
folder_list = folder_path.split('/')
if folder_list[-1] == '':
folder_list.pop(-1)
short_folder_path = folder_list[-2]
if 'P' in variables:
P_or_T = 'pool'
elif 'T' in variables:
P_or_T = 'transdeck'
else:
P_or_T = ''
if 'L' in variables:
L_or_D = 'light_'
elif 'D' in variables:
L_or_D = 'dark_'
else:
L_or_D = ''
if 'F' in variables:
F_or_N = 'far_'
elif 'N' in variables:
F_or_N = 'near_'
else:
F_or_N = ''
task_name = task + L_or_D + F_or_N + P_or_T
return task_name, short_folder_path
def trim_in_folder(folder_path):
global file_name
with open(join(folder_path, 'trim.txt'), 'r') as rf:
ls = rf.readlines()
home = expanduser("~")
print(ls)
for wrds in ls:
try:
l = wrds.strip('\n').split(" ")
if l[0].find('.avi') == -1:
if file_name.find('.avi') == -1:
raise AttributeError
l = [file_name] + l
file_name = join(folder_path, l[0])
task_name, short_folder_path = lazy_name(l[3], l[4], folder_path)
try:
txtF = os.listdir(home + '/AUV_DataBase/' + task_name)
except OSError:
os.system('mkdir -p ' + home + '/AUV_DataBase/' + task_name)
txtF = os.listdir(home + '/AUV_DataBase/' + task_name)
num = txtF
t1 = l[1].split(':')
t2 = l[2].split(':')
try:
int(t1[2])
except IndexError:
t1 = [0] + t1
t2 = [0] + t2
tdf = (int(t2[0]) - int(t1[0])) * 3600 + (int(t2[1]) - int(t1[1])) *60 + (int(t2[2]) - int(t1[2]))
t3 = time.strftime('%H:%M:%S', time.gmtime(tdf))
os.system("ffmpeg -ss "+l[1]+" -i "+ file_name +" -t "+t3+" -c copy " + home+'/AUV_DataBase/'+task_name+"/"+str(len(num))+'_'+short_folder_path+'.avi')
except Exception as e:
print (e)
def trim_recursive_folder(folder_path):
for root, subdirs, files in os.walk(folder_path):
for file in files:
if file == 'trim.txt':
print("--------------------------------------------------------------")
print("--------------------------------------------------------------")
print("--------------------------------------------------------------")
print("--------------------------------------------------------------")
print('Starting trim in folder ' + root)
trim_in_folder(root)
def total_video_count(folder_path):
COUNTER = 0
for root, subdirs, files in os.walk(folder_path):
for file in files:
if file == 'trim.txt':
f = open(join(root,'trim.txt'), 'r')
l = f.readlines()
for line in l:
if line != '\n':
COUNTER += 1
print COUNTER
if __name__ == '__main__':
try:
root_folder_path = sys.argv[1]
except IndexError:
root_folder_path = '.'
trim_recursive_folder(expanduser(root_folder_path))