-
Notifications
You must be signed in to change notification settings - Fork 2
/
transcoder.py
128 lines (110 loc) · 3.21 KB
/
transcoder.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
import os
import shutil
import time
import parser
import file_pparser
import ftp_connector
import s3_connector
import gst_transcoder
import t_logger
# main
def main() :
log = t_logger.setup('transcoder')
ftp=0
s3=0
bucketName=0
#
# parse arguments
#
params = parser.parse()
# parse file
if (params.file and params.extpath):
fileParams = file_pparser.readAndParseFile(params.extpath);
if (len(fileParams.keys()) >0):
params = parser.merge(fileParams,params)
if os.path.exists(params.input ) is False:
raise IOError (params.input,"does not exist")
#
# handle dest folder
#
if (params.inplace):
dest = params.input
else:
dest = getOutputFolder ( params.input, params.output )
try:
os.mkdir(dest)
except:
log.debug('%s folder already exists', dest)
#
# UPLOAD step configuration
#
if (params.upload):
if (params.via == "ftp"):
ftp = ftp_connector.connect(params)
if (params.via == "s3"):
bucketName = params.bucket if params.bucket else params.s3keyid.lower() + '_' +str(int(time.time()))
s3 = s3_connector.connect(params)
else:
log.info("Only FTP and S3 supported")
#
# SOURCEDIR recursive exploration
# + TRANSCODING
# + UPLOAD
#
for root, dirs, files in os.walk(params.input):
if root != dest: # it may happen that dest is inside root folder
log.info('current processed folders are %s and %s',root,dest)
if (params.createtree):
finalDest = generateIntermediateFolder(params.input, root, dest)
for name in files:
finalName = name
finalPath = root + os.sep + name
# deal with transcoding
if (params.transcode):
try :
if (hasattr(params,'transcodingString')):
finalName, finalPath = \
gst_transcoder.transcodeFile(name,root,finalDest, \
preserveDate=(params.nopreserve) is None, \
transcodingString=params.transcodingString)
else:
finalName, finalPath = \
gst_transcoder.transcodeFile(name,root,finalDest, \
preserveDate=(params.nopreserve) is None)
except RuntimeError:
log.info("File %s NOT transcoded", root+os.sep+name)
# deal with ftp
if ftp:
ftp_connector.upload(ftp,finalName,finalPath)
# deal with s3
elif s3:
s3_connector.upload(s3,bucketName,finalPath)
#
# Eventually remove local folder
#
if params.remove:
shutil.rmtree(dest, True)
log.info("%s folder removed", dest)
log.info('Done!')
def getOutputFolder ( input, output ) :
# check if input contains os.sep at the end
input = input if input[len(input)-1] == os.sep else input + os.sep
if output:
if ( os.path.isabs(output) ) is True:
dest = output
else:
dest = input + output
else:
dest = input + 'transcoded'
return dest
def generateIntermediateFolder (inputRoot, src, destRoot):
if (inputRoot == src):
return destRoot
intermPath = src.replace(inputRoot,"")
dest = destRoot + intermPath
try:
os.makedirs(dest)
except:
log.debug('%s folder already exists', dest)
return dest
if __name__ == '__main__': main()