-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrate.py
320 lines (266 loc) · 10.7 KB
/
migrate.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import os
import json
import shutil
import subprocess
from pathlib import Path
import multiprocessing
from markkk.logger import logger
from typing import List, Dict, Tuple
def call_safe_copy(job: List):
src = job[0]
dest = job[1]
safe_copy(src, dest)
return dest
def safe_copy(src, dest):
if not Path(src).exists():
logger.error(f"safe_copy cannot be done, because source file {src} not found.")
return
if Path(dest).exists():
logger.error(
f"safe_copy cannot be done, because destination file {dest} already exist."
)
return
try:
# subprocess.run(["cp", str(src), str(dest)])
shutil.copy2(src, dest)
logger.debug(f"'{src}' has been copied to '{dest}'")
except Exception as err:
logger.error(f"Copy operation failed, reason: {err}")
def migrate_from_unlabelled_to_local():
base_path = Path("/home/UROP/data_urop/unlabelled")
destination_folder = Path("/home/UROP/data_urop/all_videos_local")
assert base_path.is_dir()
assert destination_folder.is_dir()
migration_list = []
for video in os.listdir(base_path):
video_path = base_path / video
if not video_path.is_file():
logger.info(f"Unexpected")
return
if str(video_path)[-4:] != ".mp4":
logger.info(f"Skip {video_path}")
continue
target_path = destination_folder / video
if target_path.is_file():
logger.info(f"{target_path} already exist")
else:
logger.debug(f"{video_path} -> {target_path}")
migration_list.append([str(video_path), str(target_path)])
logger.debug(f"Number of file to copy: {len(migration_list)}")
proceed = input("Proceed? (y/n)")
if proceed != "y":
logger.warning("Abort")
return
logger.debug(json.dumps(migration_list, indent=4))
proceed = input("Proceed? (y/n)")
if proceed != "y":
logger.warning("Abort")
return
pool = multiprocessing.Pool()
result = pool.map(call_safe_copy, migration_list)
def migrate_from_drive_to_local():
base_path = Path("/home/UROP/shared_drive/Video_Folders/Trimmed_All_Videos")
destination_folder = Path("/home/UROP/data_urop/all_videos_local")
assert base_path.is_dir()
assert destination_folder.is_dir()
migration_list = []
for folder in os.listdir(base_path):
folder_path = base_path / folder
if not folder_path.is_dir():
logger.info(f"Skip {folder_path}")
continue
logger.debug(folder)
if folder.startswith("bilibili_"):
# folder_num = int(folder[-3:])
# if folder_num <= 80:
# continue
folder_path = base_path / folder
assert folder_path.is_dir()
for file in os.listdir(folder_path):
if file[-4:] != ".mp4":
logger.info(f"Skip {file}")
continue
new_name = "b_" + file
video_filepath: Path = folder_path / file
dst_path: Path = destination_folder / new_name
logger.debug(f"{video_filepath} -> {dst_path}")
migration_list.append([str(video_filepath), str(dst_path)])
elif folder.startswith("youtube_"):
# folder_num = int(folder[-3:])
# if folder_num <= 10:
# continue
folder_path = base_path / folder
assert folder_path.is_dir()
for file in os.listdir(folder_path):
if file[-4:] != ".mp4":
logger.info(f"Skip {file}")
continue
new_name = "y_" + file
video_filepath: Path = folder_path / file
dst_path: Path = destination_folder / new_name
logger.debug(f"{video_filepath} -> {dst_path}")
migration_list.append([str(video_filepath), str(dst_path)])
elif folder.endswith("yutian"):
folder_path = base_path / folder
assert folder_path.is_dir()
for file in os.listdir(folder_path):
if file[-4:] != ".mp4":
logger.info(f"Skip {file}")
continue
new_name = ""
for i in file.lower():
if i == "(":
new_name += "_"
elif i == ")":
pass
else:
new_name += i
new_name = "c_" + new_name
video_filepath: Path = folder_path / file
dst_path: Path = destination_folder / new_name
logger.debug(f"{video_filepath} -> {dst_path}")
migration_list.append([str(video_filepath), str(dst_path)])
logger.debug(f"Number of file to copy: {len(migration_list)}")
proceed = input("Proceed? (y/n)")
if proceed != "y":
logger.warning("Abort")
return
logger.debug(json.dumps(migration_list, indent=4))
proceed = input("Proceed? (y/n)")
if proceed != "y":
logger.warning("Abort")
return
pool = multiprocessing.Pool()
result = pool.map(call_safe_copy, migration_list)
def migrate_1():
base_path = Path("/home/UROP/data_urop/Video_Folders_local/Trimmed_All_Videos")
destination_folder = Path("/home/UROP/data_urop/unlabelled")
assert base_path.is_dir()
assert destination_folder.is_dir()
for folder in os.listdir(base_path):
logger.debug(folder)
if folder.startswith("bilibili_"):
folder_num = int(folder[-3:])
if folder_num <= 80:
continue
folder_path = base_path / folder
assert folder_path.is_dir()
for file in os.listdir(folder_path):
if file[-4:] != ".mp4":
continue
new_name = "b_" + file
video_filepath: Path = folder_path / file
dst_path: Path = destination_folder / new_name
logger.debug(f"{video_filepath} -> {dst_path}")
shutil.move(video_filepath, dst_path)
elif folder.startswith("youtube_"):
folder_num = int(folder[-3:])
if folder_num <= 10:
continue
folder_path = base_path / folder
assert folder_path.is_dir()
for file in os.listdir(folder_path):
if file[-4:] != ".mp4":
continue
new_name = "y_" + file
video_filepath: Path = folder_path / file
dst_path: Path = destination_folder / new_name
logger.debug(f"{video_filepath} -> {dst_path}")
shutil.move(video_filepath, dst_path)
elif folder.endswith("yutian"):
folder_path = base_path / folder
assert folder_path.is_dir()
for file in os.listdir(folder_path):
if file[-4:] != ".mp4":
continue
new_name = ""
for i in file.lower():
if i == "(":
new_name += "_"
elif i == ")":
pass
else:
new_name += i
new_name = "c_" + new_name
video_filepath: Path = folder_path / file
dst_path: Path = destination_folder / new_name
logger.debug(f"{video_filepath} -> {dst_path}")
shutil.move(video_filepath, dst_path)
def convert_joe_vids_to_mp4():
joe_video_folder = Path(
"/home/UROP/data_urop/Video_Folders_local/joe_videos/traffic_videos"
)
destination_folder = Path("/home/UROP/data_urop/unlabelled")
assert joe_video_folder.is_dir()
for file in os.listdir(joe_video_folder):
if file[-4:] != ".mov":
continue
new_name = "j_" + file.lower()[4:-4] + ".mp4"
file_in = str(joe_video_folder / file)
file_out = str(destination_folder / new_name)
logger.debug(f"{file_in} -> {file_out}")
subprocess.run(
f'ffmpeg -loglevel warning -i "{file_in}" -vcodec copy -acodec copy "{file_out}"',
shell=True,
# stdout=subprocess.PIPE,
)
def migrate_2():
joe_video_folder = Path(
"/data/urop/gs-samill/Video_Folders/joe_videos/traffic_videos"
)
destination_folder = Path("/home/UROP/data_urop/unlabelled/joe")
assert joe_video_folder.is_dir()
assert destination_folder.is_dir()
for file in os.listdir(joe_video_folder):
if file[-4:] != ".mov":
continue
new_name = "j_" + file.lower()[4:]
video_filepath: Path = joe_video_folder / file
dst_path: Path = destination_folder / new_name
logger.debug(f"{video_filepath} -> {dst_path}")
shutil.copy2(video_filepath, dst_path)
def run_stats():
folder = Path("/home/UROP/data_urop/unlabelled")
from get_video_info import generate_video_list
generate_video_list(folder)
def migrate_3():
base_path = Path("/home/UROP/data_urop/Video_Folders_local/Trimmed_All_Videos")
destination_folder = Path("/home/UROP/data_urop/labelled")
assert base_path.is_dir()
assert destination_folder.is_dir()
for folder in os.listdir(base_path):
logger.debug(folder)
if folder.startswith("bilibili_"):
folder_num = int(folder[-3:])
if folder_num > 80:
continue
folder_path = base_path / folder
assert folder_path.is_dir()
for file in os.listdir(folder_path):
if file[-4:] != ".mp4":
continue
new_name = "b_" + file
video_filepath: Path = folder_path / file
dst_path: Path = destination_folder / new_name
logger.debug(f"{video_filepath} -> {dst_path}")
shutil.move(video_filepath, dst_path)
elif folder.startswith("youtube_"):
folder_num = int(folder[-3:])
if folder_num > 10:
continue
folder_path = base_path / folder
assert folder_path.is_dir()
for file in os.listdir(folder_path):
if file[-4:] != ".mp4":
continue
new_name = "y_" + file
video_filepath: Path = folder_path / file
dst_path: Path = destination_folder / new_name
logger.debug(f"{video_filepath} -> {dst_path}")
shutil.move(video_filepath, dst_path)
if __name__ == "__main__":
# convert_joe_vids_to_mp4()
# run_stats()
# migrate_3()
# migrate_from_drive_to_local()
migrate_from_unlabelled_to_local()