-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovideos.py
104 lines (83 loc) · 3.42 KB
/
provideos.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
import os
import sys
import stat
from send2trash import send2trash
from Modules import tools
from Modules import convert
from Modules import crunch
from Modules import optimise
__author__ = 'Gus'
def validateArguments(arguments):
if len(arguments) < 3:
print 'Please provide a folder to process, and a target folder.'
exit(0)
if len(arguments) > 3:
print 'Please only provide a folder to process, and a target folder.'
print 'e.g. python processvideosrelocate.py inputfolder outputfolder'
exit(0)
if not os.path.exists(arguments[1]):
print 'Input folder does not exist'
exit(0)
if not os.path.exists(arguments[2]):
print 'Output folder does not exist'
exit(0)
if __name__ == "__main__":
paths = sys.argv
validateArguments(paths)
sourcePaths = paths[1]
targetPath = paths[2]
videofiles = tools.getFileNamesRecursive(sourcePaths, "*.mkv")
videofiles.extend(tools.getFileNamesRecursive(sourcePaths, "*.mov"))
videofiles.extend(tools.getFileNamesRecursive(sourcePaths, "*.m4a"))
videofiles.extend(tools.getFileNamesRecursive(sourcePaths, "*.mp4"))
videofiles.extend(tools.getFileNamesRecursive(sourcePaths, "*.avi"))
print 'Found videos:'
print "\n".join(videofiles)
for i in range (0, len(videofiles)):
needsConvert = False
needsCompress = False
print 'Processing new video - ' + os.path.basename(videofiles[i])
finalFilename = ''
tempfilename = tools.getTempFilePath(videofiles[i], '.mp4')
finalFilename = tools.getOriginalFilenameFromTemp(tempfilename)
os.chmod(videofiles[i], stat.S_IRWXU)
needsConvert = True
if tools.getFileSize(videofiles[i]) > tools.gigabytes(2):
needsCompress = True
else:
print 'Video too small to compress.'
if needsConvert:
print 'Attempting to convert to MP4 using libx264, aac...'
if convert.toMP4(videofiles[i]) == 0:
send2trash(videofiles[i])
tools.rename(tempfilename, finalFilename)
print 'Success!'
else:
finalFilename = videofiles[i]
if os.path.exists(tempfilename):
send2trash(tempfilename)
print 'Failed!'
if needsCompress:
print 'Attempting to compress video and optimise for streaming...'
if crunch.crfandstream(finalFilename, "21") == 0:
send2trash(finalFilename)
tools.rename(tempfilename, finalFilename)
print 'Success!'
else:
if os.path.exists(tempfilename):
send2trash(tempfilename)
print 'Failed!'
else:
print 'Attempting to optimise for streaming...'
if optimise.forStreaming(videofiles[i].replace(tools.getExtension(videofiles[i]), ".mp4")) == 0:
send2trash(videofiles[i].replace(tools.getExtension(videofiles[i]), ".mp4"))
tools.rename(tempfilename, finalFilename)
print 'Success!'
else:
finalFilename = videofiles[i]
if os.path.exists(tempfilename):
send2trash(tempfilename)
print 'Failed!'
print 'Moving to target location...'
tools.move(finalFilename, targetPath)
print 'Complete!'