forked from kieranjol/IFIscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathififuncs.py
executable file
·258 lines (222 loc) · 9.62 KB
/
ififuncs.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
import subprocess
import sys
import base64
import time
import smtplib
import mimetypes
import getpass
import os
import filecmp
import hashlib
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import csv
def diff_textfiles(source_textfile, other_textfile):
if filecmp.cmp(source_textfile, other_textfile, shallow=False):
print "YOUR FILES ARE LOSSLESS YOU SHOULD BE SO HAPPY!!!"
return 'lossless'
else:
print "CHECKSUM MISMATCH - Further information on the next line!!!"
return 'lossy'
#sys.exit() # Script will exit the loop if transcode is not lossless.
def make_mediainfo(xmlfilename, xmlvariable, inputfilename):
with open(xmlfilename, "w+") as fo:
xmlvariable = subprocess.check_output(['mediainfo',
'-f',
'--language=raw','--File_TestContinuousFileNames=0', # Use verbose output.
'--output=XML',
inputfilename]) #input filename
fo.write(xmlvariable)
def make_qctools(input):
qctools_args = ['ffprobe', '-f', 'lavfi', '-i',]
qctools_args += ["movie=%s:s=v+a[in0][in1],[in0]signalstats=stat=tout+vrep+brng,cropdetect=reset=1:round=1,split[a][b];[a]field=top[a1];[b]field=bottom[b1],[a1][b1]psnr[out0];[in1]ebur128=metadata=1,astats=metadata=1:reset=1:length=0.4[out1]" % input]
qctools_args += ['-show_frames', '-show_versions', '-of', 'xml=x=1:q=1', '-noprivate']
print qctools_args
qctoolsreport = subprocess.check_output(qctools_args)
return qctoolsreport
def write_qctools_gz(qctoolsxml, sourcefile):
with open(qctoolsxml, "w+") as fo:
fo.write(make_qctools(sourcefile))
subprocess.call(['gzip', qctoolsxml])
def get_audio_stream_count():
audio_stream_count = subprocess.check_output(['ffprobe', '-v', 'error', '-select_streams', 'a', '-show_entries', 'stream=index', '-of', 'flat', sys.argv[1]]).splitlines()
return len(audio_stream_count)
def get_mediainfo(var_type, type, filename):
var_type = subprocess.check_output(['MediaInfo', '--Language=raw', '--Full', type , filename ]).replace('\n', '')
return var_type
# example - duration = get_mediainfo('duration', '--inform=General;%Duration_String4%', sys.argv[1] )
def send_gmail(email_to, attachment, subject, email_body, email_address, password):
emailfrom = ""
emailto = email_to
#emailto = ", ".join(emailto)
fileToSend = attachment
username = email_address
password = password
msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = ", ".join(emailto)
msg["Subject"] = subject
msg.preamble = "testtesttest"
body = MIMEText(email_body)
msg.attach(body)
ctype, encoding = mimetypes.guess_type(fileToSend)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
if maintype == "text":
fp = open(fileToSend)
# Note: we should handle calculating the charset
attachment = MIMEText(fp.read(), _subtype=subtype)
fp.close()
elif maintype == "image":
fp = open(fileToSend, "rb")
attachment = MIMEImage(fp.read(), _subtype=subtype)
fp.close()
elif maintype == "audio":
fp = open(fileToSend, "rb")
attachment = MIMEAudio(fp.read(), _subtype=subtype)
fp.close()
else:
fp = open(fileToSend, "rb")
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
msg.attach(attachment)
server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server_ssl.ehlo() # optional, called by login()
server_ssl.login(username, password)
# ssl server doesn't support or need tls, so don't call server_ssl.starttls()
server_ssl.sendmail(emailfrom, emailto, msg.as_string())
print msg.as_string()
#server_ssl.quit()
server_ssl.close()
print 'successfully sent the mail'
def frames_to_seconds(audio_entry_point):
audio_frame_count = float(audio_entry_point)
audio_frame_count = float(audio_frame_count) / 24.000 # Change to EditRate variable.
audio_frame_count = round(audio_frame_count, 3)
return audio_frame_count
def set_environment(logfile):
env_dict = os.environ.copy()
# https://github.com/imdn/scripts/blob/0dd89a002d38d1ff6c938d6f70764e6dd8815fdd/ffmpy.py#L272
env_dict['FFREPORT'] = 'file={}:level=48'.format(logfile)
return env_dict
def generate_log(log, what2log):
if not os.path.isfile(log):
with open(log,"wb") as fo:
fo.write(time.strftime("%Y-%m-%dT%H:%M:%S ") + getpass.getuser() + ' ' + what2log + ' \n')
else:
with open(log,"ab") as fo:
fo.write(time.strftime("%Y-%m-%dT%H:%M:%S ") + getpass.getuser() + ' ' + what2log + ' \n')
def hashlib_md5(filename):
m = hashlib.md5()
with open(str(filename), 'rb') as f:
while True:
buf = f.read(2**20)
if not buf:
break
m.update(buf)
md5_output = m.hexdigest()
return md5_output
def hashlib_manifest(manifest_dir, manifest_textfile, path_to_remove):
file_count = 0
for root, directories, filenames in os.walk(manifest_dir):
filenames = [f for f in filenames if not f[0] == '.']
directories[:] = [d for d in directories if not d[0] == '.']
for files in filenames:
print "Calculating number of files to process in current directory - %s files \r"% file_count,
file_count +=1
manifest_generator = ''
md5_counter = 1
for root, directories, filenames in os.walk(manifest_dir):
filenames = [f for f in filenames if not f[0] == '.']
directories[:] = [d for d in directories if not d[0] == '.']
for files in filenames:
print 'Generating MD5 for %s - file %d of %d' % (os.path.join(root,files), md5_counter, file_count)
md5 = hashlib_md5(os.path.join(root, files))
md5_counter +=1
root2 = os.path.abspath(root).replace(path_to_remove, '')
try:
if root2[0] == '/':
root2 = root2[1:]
if root2[0] == '\\':
root2 = root2[1:]
except: IndexError
manifest_generator += md5[:32] + ' ' + os.path.join(root2,files).replace("\\", "/") + '\n'
manifest_list = manifest_generator.splitlines()
files_in_manifest = len(manifest_list)
# http://stackoverflow.com/a/31306961/2188572
manifest_list = sorted(manifest_list, key=lambda x:(x[34:]))
with open(manifest_textfile,"wb") as fo:
for i in manifest_list:
fo.write(i + '\n')
def make_manifest(manifest_dir, relative_manifest_path, manifest_textfile):
os.chdir(manifest_dir)
if not os.path.isfile(manifest_textfile):
manifest_generator = subprocess.check_output(['md5deep', '-ler', relative_manifest_path])
manifest_list = manifest_generator.splitlines()
files_in_manifest = len(manifest_list)
# http://stackoverflow.com/a/31306961/2188572
manifest_list = sorted(manifest_list, key=lambda x:(x[34:]))
with open(manifest_textfile,"wb") as fo:
for i in manifest_list:
fo.write(i + '\n')
return files_in_manifest
else:
print 'Manifest already exists'
sys.exit()
def make_mediatrace(tracefilename, xmlvariable, inputfilename):
with open(tracefilename, "w+") as fo:
xmlvariable = subprocess.check_output(['mediainfo',
'-f',
'--Details=1','--File_TestContinuousFileNames=0', # Use verbose output.
'--output=XML',
inputfilename]) #input filename
fo.write(xmlvariable)
def check_overwrite(file2check):
if os.path.isfile(file2check):
print 'A manifest already exists at your destination. Overwrite? Y/N?'
overwrite_destination_manifest = ''
while overwrite_destination_manifest not in ('Y','y','N','n'):
overwrite_destination_manifest = raw_input()
if overwrite_destination_manifest not in ('Y','y','N','n'):
print 'Incorrect input. Please enter Y or N'
return overwrite_destination_manifest
def manifest_file_count(manifest2check):
if os.path.isfile(manifest2check):
print 'A manifest already exists'
with open(manifest2check, "r") as fo:
manifest_lines = [line.split(',') for line in fo.readlines()]
count_in_manifest = len(manifest_lines)
return count_in_manifest
def create_csv(csv_file, *args):
f = open(csv_file, 'wb')
try:
writer = csv.writer(f)
writer.writerow(*args)
finally:
f.close()
def append_csv(csv_file, *args):
f = open(csv_file, 'ab')
try:
writer = csv.writer(f)
writer.writerow(*args)
finally:
f.close()
def make_desktop_manifest_dir():
desktop_manifest_dir = os.path.expanduser("~/Desktop/moveit_manifests")
if not os.path.isdir(desktop_manifest_dir):
#I should probably ask permission here, or ask for alternative location
os.makedirs(desktop_manifest_dir)
return desktop_manifest_dir
def make_desktop_logs_dir():
desktop_logs_dir = os.path.expanduser("~/Desktop/ifiscripts_logs")
if not os.path.isdir(desktop_logs_dir):
#I should probably ask permission here, or ask for alternative location
os.makedirs(desktop_logs_dir)
return desktop_logs_dir