-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_files.py
executable file
·149 lines (127 loc) · 5.35 KB
/
copy_files.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
# See the README.txt for install
# ShareableList values (index: (type) meaning)
# 0: (boolean) stop flag, changed by the main processed when the user stop the operation.
# 1: (boolean) finish flag, changed by this process when the operation finished.
# 2: (string) destination path where to copy the media files.
# 3: (string) media pool forder name where to search for files to copy
# 4: (int) number of clips relinked (to send to main process)
from multiprocessing.shared_memory import ShareableList
from multiprocessing import resource_tracker
import os
import shutil
import time
import datetime
def getMediaFolder(name, parent = None):
"""
Get media folder with "name" from the media pool root, or from parent, if given.
"""
mpRoot = mediaPool.GetRootFolder()
if parent:
mpRoot = parent
for folder in mpRoot.GetSubFolderList():
if folder.GetName() == name:
return folder
return None
def inspectSouces(clips:list):
totalSize= 0
filesCount=0
clipsFiles={}
for clip in clips:
filesCount+=1
thisFile = clip.GetClipProperty("File Path")
totalSize+=os.path.getsize(thisFile)
clipsFiles[clip.GetClipProperty("File Name")] = clip
return filesCount,totalSize,clipsFiles
def humanReadable(bytesNum,humanReadbleDivider=1024**2,humanReadbleName="MBytes/s"):
return "{:.3f}".format(bytesNum/humanReadbleDivider) + " " + humanReadbleName
try:
sl = ShareableList(name='ConformAllCopyMedia')
sl[0]=False
sl[4]=0
mediaPath = sl[2] #folder where to copy the files
mediaFolderName = sl[3] # bin folder name in the DR from where to read the clips. Current filepath is read from the clip properties.
print("Media Path:",mediaPath)
print("DR Media Folder:",mediaFolderName)
pm = resolve.GetProjectManager()
currentProject = pm.GetCurrentProject()
currentTimeline = currentProject.GetCurrentTimeline()
mediaPool = currentProject.GetMediaPool()
parentFolder = getMediaFolder(currentTimeline.GetName()) # we assume that the media folder bin is s child of the bin
mediaFolder = getMediaFolder(mediaFolderName,parentFolder)
#print(mediaFolder.GetName())
cancel = False
oldFiles = {} # Files already in the folder (recursive). Filename as key and filepath as value
for root, dirs, files in os.walk(mediaPath):
for name in files:
if not name.startswith("."):
oldFiles[name] = os.path.join(root,name)
clips = mediaFolder.GetClipList()
clipsFilesNum,totalSize,clipsFiles = inspectSouces(clips) # clipsFiles is a dicionary with file name as key (not file path) and the clip object as value
copyCount = 0
copySize = 0
relinkedCount = 0
meanArray = []
clips2Relink=[]
for key in clipsFiles.keys():
cancel=sl[0]
if cancel:
print("Copy Media process canceled...")
break
copyCount+=1
clip = clipsFiles.get(key)
filePath = clip.GetClipProperty("File Path")
fileSize = os.path.getsize(filePath)
copySize+=fileSize
print("Copying file",copyCount,"of",clipsFilesNum,end=". ")
msg = ""
if key in oldFiles: # test if the filename already exists in the mediaPath
msg += "File " + key + " already in the media path."
clips2Relink.append(clip)
else:
if os.path.exists(filePath):
ts = datetime.datetime.timestamp(datetime.datetime.now())
# TODO: block copy may be the way if we whant to return the copy progress to the user
shutil.copyfile(filePath,os.path.join(mediaPath,key),follow_symlinks=False)
dt = datetime.datetime.timestamp(datetime.datetime.now()) - ts
bps = fileSize/dt
copySizeRemaining = totalSize - copySize
meanArray.append(bps)
meanBps = sum(meanArray)/len(meanArray)
timeRemaining = copySizeRemaining/meanBps
convertedSeconds = str(datetime.timedelta(seconds = int(timeRemaining)))
msg += "File " + filePath + " copied (" + humanReadable(bps)+ ", " + convertedSeconds + " remaining)."
clips2Relink.append(clip)
#else:
# msg += "Erro copying the file " + filePath + ". (error: " + str(error) + ")"
else:
msg += "The file " + filePath + " does not exists."
#if mediaPool.RelinkClips([clip],mediaPath):
# relinkedCount+=1
# msg += " Clip relinked."
#else:
# msg += " Failed to relink the clip"
#if not oldFiles.pop(key,False):
# msg += " Can't pop media file value from dictionary."
print(msg)
"""
if not cancel:
print("Deleting",len(oldFiles),"unused files")
for file in oldFiles.values():
if os.path.exists(file):
os.remove(file)
"""
relinkedCount = len(clips2Relink)
sl[4]=relinkedCount
if relinkedCount > 0:
if mediaPool.RelinkClips(clips2Relink,mediaPath):
print(relinkedCount," of ",clipsFilesNum," relinked.")
else:
print("Error relinking clips.")
else:
print("There is no clips to relink.")
except Exception as e:
print(e)
finally:
sl[0]=False
sl[1]=True
sl.shm.close()