-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutoUpload.py
480 lines (376 loc) · 16.1 KB
/
AutoUpload.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import sys
import os
import time
import tempfile
import subprocess
import json
import pdb
import logging
import shutil
import natsort
#todo fix uploading #todo carriage return frame status
#todo watch frame location indefinitely if the supplied location is empty
#todo avoid upscaling video if supplied resolution is smaller than the resolution in the config
#todo copy frames as they being made
#todo email after the video is uploaded
#todo move log to output folder
#todo
# frames/video -> video -> youtube -> email
"""
normal process
normal process without upload
quick convert existing video
"""
class FFmpegObject:
fullBatchPath = ''
videoFramerate = ''
parameter1 = ''
inputFile = ''
outputResolution = ''
outputFileDir = ''
outputFileName = ''
outputFile = ''
def createBatchFile(self):
# Create temporary batch file to call ffmpeg
tempBatFile = tempfile.NamedTemporaryFile(suffix='.bat', delete=False)
#todo figure out why it freaks out on frame mode if you put param1 after inputfile
if gArgs.inputArgIsDir:
tempBatFile.write(self.fullBatchPath + self.videoFramerate + self.parameter1 + self.inputFile + self.outputResolution + '"'+self.outputFile+'"')
else:
tempBatFile.write(self.fullBatchPath + self.videoFramerate + self.inputFile + self.parameter1 + self.outputResolution + '"'+self.outputFile+'"')
tempBatFile.close()
log('Batch arguments: ' + self.fullBatchPath + self.videoFramerate + self.parameter1 + self.inputFile + self.outputResolution + self.outputFile)
log('Batch file created.')
print(tempBatFile.name)
subprocess.call(tempBatFile.name)
log('Batch program returned')
#remove temp batch file
os.remove(tempBatFile.name)
#copy all frames to a temp location
#determine the filetype of the frames
#remove any files that aren't of that type
#sort the frames into alphabetical order
#top and tail the frames
#rename the frames into an ordered sequence
class FramePrep:
inputDirectory = ''
tempInputDirectory = ''
filePrefix = ''
fileNumberinglength = 0
fileSuffix = ''
def __init__(self, frameDir):
self.inputDirectory = frameDir
self.copyTempFrames()
self.determineFrameAttributes()
self.removeNonFrameObjects()
self.getSortedFrameList()
self.topAndTail()
self.renameFramesToSortedList()
#copy only image files to temp directory
def copyTempFrames(self):
suffixes = ('.png', '.jpg', '.jpeg', '.tga', '.tiff')
self.tempInputDirectory = tempfile.mkdtemp(dir=os.path.dirname(self.inputDirectory))
self.tempInputDirectory += '\\'
#make temp directory if it doesn't already exist. If it does, clear the directory before we copy anything to it
'''
if not os.path.isdir(os.path.dirname(gInputPath) + "\\temp\\"):
os.mkdir(os.path.dirname(gInputPath) + "\\temp\\")
else:
for existingFile in os.listdir(self.tempInputDirectory):
os.remove(self.tempInputDirectory + '\\' + existingFile)
'''
for file in os.listdir(self.inputDirectory):
if file.endswith(suffixes):
shutil.copy(self.inputDirectory + '\\' + file, self.tempInputDirectory + file)
else:
print('File ' + file + ' was not copied')
return
def getFileExtension(self, filename):
fileExtension = ''
#get the file extension
characterIndex = len(filename) - 1
while True:
if filename[characterIndex] == '.':
break
characterIndex = characterIndex - 1
fileExtension = str((filename[characterIndex:]))
return fileExtension
def determineFrameAttributes(self):
tempFilename = os.listdir(self.tempInputDirectory)[0]
#print (os.listdir(self.tempInputDirectory)[0])
#get the file extension
self.fileSuffix = self.getFileExtension(tempFilename)
#print('file extension: {}').format(self.fileSuffix)
tempFilename = tempFilename.replace(self.fileSuffix, '')
#print('filename without extension: {}').format(tempFilename)
tempFileNumberingLength = 0
for file in os.listdir(self.tempInputDirectory):
tempPrefix = file.replace(self.fileSuffix, '')
#find how many sequence numbers exist
characterIndex = len(tempPrefix) - 1
numeralDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
while True:
if not tempPrefix[characterIndex] in numeralDigits:
break
characterIndex -= 1
if (len(tempPrefix) - 1) - characterIndex > self.fileNumberinglength:
self.fileNumberinglength = (len(tempPrefix) - 1) - characterIndex
#print('length: {}').format(self.fileNumberinglength)
self.filePrefix = str((tempFilename[:characterIndex + 1]))
#print('numbering length: ' + str(self.fileNumberinglength))
#print(tempPrefix)
#FirstPersonExampleMap.0001.jpg
return
def removeNonFrameObjects(self):
return
def getSortedFrameList(self):
return
def topAndTail(self):
topAmount = gConfig.getValue('Properties', 'NumStartingFramesToSkip')
tailAmount = gConfig.getValue('Properties', 'NumEndingFramesToSkip')
currentTrimAmount = 0
currentCheckIndex = 0
while True:
if currentTrimAmount == int(topAmount):
break
sortedFileList = natsort.natsorted(os.listdir(self.tempInputDirectory))
currentFile = sortedFileList[currentCheckIndex]
if currentFile.startswith(self.filePrefix):
os.remove(self.tempInputDirectory + "\\" + currentFile)
currentTrimAmount = currentTrimAmount + 1
else:
currentCheckIndex = currentCheckIndex + 1
currentTrimAmount = 0
currentCheckIndex = 0
while True:
if currentTrimAmount == int(tailAmount):
break
sortedFileList = natsort.natsorted(os.listdir(self.tempInputDirectory))
lastIndex = len(sortedFileList) - 1
currentFile = sortedFileList[lastIndex - currentCheckIndex]
if currentFile.startswith(self.filePrefix):
os.remove(self.tempInputDirectory + "\\" + currentFile)
currentTrimAmount = currentTrimAmount + 1
else:
currentCheckIndex = currentCheckIndex + 1
return
def renameFramesToSortedList(self):
count = 0
fileList = os.listdir(self.tempInputDirectory)
fileList = natsort.natsorted(fileList)
for file in fileList:
#print(file)
if file.startswith(self.filePrefix):
os.rename(self.tempInputDirectory + '\\' + file, self.tempInputDirectory + '\\' + self.filePrefix + str(count).zfill(self.fileNumberinglength) + self.fileSuffix)
count += 1
return
def removeTempFrames(self):
shutil.rmtree(self.tempInputDirectory)
#
class Args:
args = ''
programDirectory = ''
inputArg = ''
inputArgIsFile = False
inputArgIsDir = False
argUpload = False
argFilename = ''
def __init__(self):
self.args = sys.argv
if len(self.args) < 2:
log('No frame directory supplied. Drag frame folder or movie file onto program.')
shutdown()
self.findArguments()
self.determineInputType()
def findArguments(self):
self.programDirectory = os.path.dirname(sys.argv[0])
self.inputArg = str(sys.argv[1])
if '-upload' in self.args:
self.argUpload = True
for arg in sys.argv:
if arg.startswith('-VideoTitle='):
self.argFilename = arg[12:]
#figure out if the input passed in is a directory or a file
def determineInputType(self):
if os.path.isdir(self.inputArg):
self.inputArgIsDir = True
elif os.path.isfile(self.inputArg):
self.inputArgIsFile = True
else:
log('Supplied input directory is empty. Retrying...')
time.sleep(3)
self.determineInputType()
#todo handle missing config file
class JsonReader:
data = ''
def __init__(self):
with open(gProgramDirectory + '\Config.json') as data_file:
self.data = json.load(data_file)
pass
def getValue(self, category, value):
return self.data[category][value]
pass
def convertFramesToVideo(ffmpegCall):
framesDirectory = gFramePrepObject.tempInputDirectory
ffmpegCall.fullBatchPath = gProgramDirectory + '\\' + 'ffmpeg.exe '
ffmpegCall.videoFramerate = '-r ' + gConfig.getValue('Properties', 'Framerate') + ' '
ffmpegCall.parameter1 = '-f image2 '
padding = r'.%%0' + str(gFramePrepObject.fileNumberinglength) + 'd'
ffmpegCall.inputFile = '-i ' + '"' + framesDirectory + '\\' + getFilePrefix(os.listdir(framesDirectory + '\\')[0]) + padding + getFileType() + '" '
ffmpegCall.outputResolution = '-s ' + gConfig.getValue('Properties', 'OutputWidth') + 'x' + gConfig.getValue('Properties', 'OutputHeight') + ' '
ffmpegCall.outputFileDir = tempfile.gettempdir()+ '\\'
ffmpegCall.outputFileName = gVideoTitle +'.mp4'
ffmpegCall.outputFile = ffmpegCall.outputFileDir + ffmpegCall.outputFileName
ffmpegCall.createBatchFile()
return ffmpegCall
def convertVideo():
ffmpegCall.fullBatchPath = gProgramDirectory + '\\' + 'ffmpeg.exe '
ffmpegCall.videoFramerate = ' '
ffmpegCall.parameter1 = '-c:v libx264 '
ffmpegCall.inputFile = '-i ' + '"' + gInputPath + '" '
ffmpegCall.outputResolution = '-s ' + gConfig.getValue('Properties', 'OutputWidth') + 'x' + gConfig.getValue('Properties', 'OutputHeight') + ' '
ffmpegCall.outputFileDir = tempfile.gettempdir()+ '\\'
ffmpegCall.outputFileName = gVideoTitle +'.mp4'
ffmpegCall.outputFile = ffmpegCall.outputFileDir + ffmpegCall.outputFileName
ffmpegCall.createBatchFile()
return ffmpegCall
def getFrameCount(_frameDir):
return len(os.listdir(_frameDir))
def getByteCount(_videoFile):
return os.path.getsize(_videoFile)
#todo rename or refactor this - doing more than one task
def getFileType():
suffixes = ('.png', '.jpg', '.jpeg', '.tga', '.tiff')
currentIndex = 0
while True:
filename = os.listdir(gInputPath + '\\')[currentIndex]
characterIndex = len(filename) - 1
while True:
if filename[characterIndex] == '.':
break
characterIndex = characterIndex - 1
fileExtension = str((filename[characterIndex:]))
if fileExtension in suffixes:
return str((filename[characterIndex:]))
else:
currentIndex += 1
#todo generic way to get the file extension of any passed in path with an optional ability to filter entries
def getFileExtension(fileName, filter = []):
while True:
characterIndex = len(filename) - 1
while True:
if filename[characterIndex] == '.':
break
characterIndex -= 1
fileExtension = str((filename[characterIndex:]))
if fileExtension in suffixes:
return str((filename[characterIndex:]))
else:
currentIndex += 1
def getFilePrefix(filename):
# filename = os.listdir(framesDirectory + '\\')[0]
characterIndex = 0
while True:
if filename[characterIndex] == '.':
break
characterIndex = characterIndex + 1
return str(filename[:characterIndex])
#todo handle mismatch with only 2 files (could cause infinite loop)
def getLastFrameName():
dirList = os.listdir(gInputPath)
dirList.sort()
currentLastFileSearchIndex = 1
while True:
lastFrameName = dirList[len(dirList) - currentLastFileSearchIndex]
if getFilePrefix(lastFrameName) == getFilePrefix(os.listdir(gInputPath + '\\')[0]): #if the prefixes of the first and last file match
return lastFrameName
currentLastFileSearchIndex = currentLastFileSearchIndex + 1
def watchDirectoryForFrames(_currentFrameCount):
while True:
_lastframeCount = _currentFrameCount
_currentFrameCount = getFrameCount(gInputPath)
if (_lastframeCount == _currentFrameCount):
break
#print('Last frame count: ' + str(_lastframeCount))
print('Current frame count: ' + str(_currentFrameCount))
sleepInterval = float(gConfig.getValue('Properties', 'FrameDirectoryWatchInterval'))
time.sleep(sleepInterval)
return _currentFrameCount
def watchVideoFile(_currentByteCount):
while True:
_lastByteCount = _currentByteCount
_currentByteCount = getByteCount(gInputPath)
if (_lastByteCount == _currentByteCount):
break
#print('Last byte count: ' + str(_lastByteCount))
print('Current byte count: ' + str(_currentByteCount))
sleepInterval = float(gConfig.getValue('Properties', 'FrameDirectoryWatchInterval'))
time.sleep(sleepInterval)
return _currentByteCount
def countFrames():
# watch directory for frames
currentFrameCount = 0
currentFrameCount = watchDirectoryForFrames(currentFrameCount)
if currentFrameCount < int(gConfig.getValue('Properties', 'MinimumFrameCount')):
log('Error: Supplied frame directory has fewer than MinimumFrameCount files after waiting for the FrameDirectoryWatchInterval in config.json. Either select the correct directory or increase FrameDirectoryWatchInterval time')
shutdown()
log('Found ' + str(currentFrameCount) + ' frames in directory. Starting sequence creation...')
def countBytes():
# watch directory for frames
currentByteCount = 0
currentByteCount = watchVideoFile(currentByteCount)
log('Found ' + str(currentByteCount) + ' bytes in file. Starting conversion...')
def uploadToYoutube():
fullBatchPath = gProgramDirectory + '\\Python27\\python.exe ' + gProgramDirectory + '\\upload_video.py --file '
videoPath = '"' + os.path.dirname(gInputPath) + '\\' + ffmpegCall.outputFileName + '" '
videoTitleParam = ' --title "' + gVideoTitle + '"'
tempBatFile = tempfile.NamedTemporaryFile(suffix='.bat', delete=False)
tempBatFile.write(fullBatchPath + videoPath + videoTitleParam)
tempBatFile.close()
log('Batch file created.')
print(tempBatFile.name)
subprocess.call(tempBatFile.name)
log('Batch program returned')
#remove temp batch file
os.remove(tempBatFile.name)
def log(logMessage):
logging.debug(time.strftime("%H%M%S", time.localtime()) + ': ' + logMessage)
print(logMessage)
def shutdown():
exit('Program ended. Press any key to close window.')
#process input arguments
gArgs = Args()
gProgramDirectory = gArgs.programDirectory
# Setup log file for each session
logging.basicConfig(filename=gProgramDirectory + '\log-' + time.strftime("%H%M%S%d%m%y", time.localtime()) + '.log',level=logging.DEBUG)
if gArgs.argFilename is '':
gVideoTitle = raw_input('Enter video title: ')
else:
gVideoTitle = gArgs.argFilename
gInputPath = str(sys.argv[1])
log('Frame directory: ' + gInputPath)
#read config file
gConfig = JsonReader()
if gArgs.inputArgIsDir:
countFrames()
else:
countBytes()
gFramePrepObject = ''
if gArgs.inputArgIsDir:
# top and tail frames and rename them into an ordered sequence
gFramePrepObject = FramePrep(gInputPath)
# when frames are no longer being created, convert
ffmpegCall = FFmpegObject()
if gArgs.inputArgIsDir:
convertFramesToVideo(ffmpegCall)
gFramePrepObject.removeTempFrames()
else:
convertVideo()
# move video out of temp directory into the directory of the script
shutil.move(ffmpegCall.outputFile, os.path.dirname(gInputPath) + '\\' + ffmpegCall.outputFileName)
log('Output video moved to ' + gInputPath + '\\' + ffmpegCall.outputFileName)
if gArgs.argUpload == True:
uploadToYoutube()
else:
print("No -upload parameter passed. Skipping upload")
# send email notification