-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncode_as_720p_HEVC.py
More file actions
executable file
·304 lines (252 loc) · 11.5 KB
/
Encode_as_720p_HEVC.py
File metadata and controls
executable file
·304 lines (252 loc) · 11.5 KB
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
#!/usr/local/bin/python3.11
import os
import sys
import pathlib
import subprocess
import datetime as dt
import humanize as hm
import re
import shutil
####################################################################################
# Global variables
####################################################################################
FILE_STUB = os.path.basename(__file__).replace('.py', '')
START_TIME = dt.datetime.now()
MARKER_CHAR = '#'
SPACER = ' '
# Define Trash Directory
TRASH_DIR = '/Users/scott/.Trash/'
# Define Archive_Fail_Over_Dir
ARCHIVE_FAIL_OVER_DIR = '/Users/scott/_Encoder_Archive'
# Logging Parameters
TODAY_DATESTAMP = dt.date.today().strftime("%Y-%m-%d")
LOG_SPACER = ' '
LOG_DIR = f'/Users/scott/Logs/ffmpeg/{FILE_STUB}'
LOG_NAME = f'{TODAY_DATESTAMP}.log'
LOGFILE_FULL_PATH = os.path.join(LOG_DIR, LOG_NAME)
# Create "LOG_DIR" if it doesn't exist
os.makedirs(LOG_DIR, exist_ok=True)
# ffmpeg Parameters
FF_BIN = '/usr/local/bin/ffmpeg'
FF_EXECUTION_FLAGS = ['-hide_banner', '-i']
# VIDEO_PARAMS = ['-c:v', 'libx265', '-tag:v', 'hvc1', '-b:v', '1500k', '-pix_fmt', 'yuv420p']
VIDEO_PARAMS = ['-c:v', 'libx265', '-tag:v', 'hvc1', '-crf', '25', '-pix_fmt', 'yuv420p']
AUDIO_PARAMS = ['-c:a', 'aac', '-b:a', '192k']
FF_SWITCHES = [*VIDEO_PARAMS, *AUDIO_PARAMS, '-map_metadata', '-1', '-metadata']
####################################################################################
# End Globals
####################################################################################
# Function to calculate percentages
def percentage(part, whole):
return round((100 * float(part) / float(whole)), 2)
####################################################################################
####################################################################################
# Function to calculate percent decrease
def percentage_decrease(new, old):
return round((((float(old) - float(new)) / float(old)) * 100), 2)
####################################################################################
####################################################################################
# Function to enclose object year in parentheses
def enclose_year_in_parentheses(text):
return re.sub(r'(\b\d{4}\b)$', r'(\1)', text)
####################################################################################
####################################################################################
def logger(status, data):
status_list = ['none', 'info', 'success', 'failure', 'warning']
if status.lower() not in status_list:
status_key = 'UNKNOWN'
else:
status_key = status.upper()
# Write to logfile
with open(LOGFILE_FULL_PATH, "a") as log_pipe:
log_timestamp = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
if status_key == 'NONE':
log_data = data
else:
log_data = '{:23} || {:^7} || {:<80}\n'.format(log_timestamp, status_key, data)
log_pipe.write(f'{log_data}')
####################################################################################
####################################################################################
def human_but_smaller(data: str):
swap_dict = {
' and': '',
' hour': ' hr',
' minute': ' min',
' seconds': ' sec'
}
for key, value in swap_dict.items():
data = data.replace(key, value)
return data
####################################################################################
####################################################################################
def create_notification_content(total_count, failed_list, body_str):
MSG_TITLE = f'({dt.datetime.now().strftime("%H:%M")})\t{FILE_STUB.replace("_", " ")}'
message_content_list = [MSG_TITLE]
msg_body = ''
if len(failed_list) > 0:
msg_subtitle = f'FAILED to encode {len(failed_list)} of {total_count} files'
msg_body += f'Filenames in log: "{LOG_NAME}"\n'
else:
msg_subtitle = f'All {total_count} files were encoded successfully'
msg_body += f'{body_str}'
message_content_list.extend([msg_subtitle, body_str])
if len(message_content_list) == 3:
message_content = '|'.join(message_content_list)
else:
message_content = f' Automator Task Completed With Errors | Check Log File For More Detail | {LOG_NAME} '
return message_content
####################################################################################
####################################################################################
def encode(target_file):
p = pathlib.Path(target_file)
ppp = pathlib.PurePosixPath(target_file)
ts_now = dt.datetime.now()
metadata_title = ppp.stem.replace('.', ' ').replace('-', ':')
output_dir = p.resolve().parent
output_file_stem = str(ppp.stem)
output_file_ext = str(ppp.suffix)
temp_file_name = f'{output_file_stem}.TEMP{output_file_ext}'
before_size_raw = os.path.getsize(target_file)
# Use pathlib to extract the volume name parts, else use ARCHIVE_FAIL_OVER_DIR
# Assemble the volume name with safety checks
if len(p.parts) >= 3:
volume = str(p.parts[0] + p.parts[1] + '/' + p.parts[2])
encoder_archive = os.path.join(volume, '_Encoder_Archive')
else:
encoder_archive = ARCHIVE_FAIL_OVER_DIR
# Create "encoder_archive" if it doesn't exist
os.makedirs(encoder_archive, exist_ok=True)
logger('info', f'{SPACER * 3} Target file path:\t {output_dir}')
logger('info', f'{SPACER * 3} Target file name:\t {p.name}')
logger('info', f'{SPACER * 3} Target file title:\t {metadata_title}')
logger('info', f'{SPACER * 3} Target file size:\t {hm.naturalsize(before_size_raw)}')
# Create filename for our working copy, before downgrading.
temp_file = os.path.join(output_dir, temp_file_name)
# Assemble metadata title string
metadata_title_string = ''.join(['title="', metadata_title, '"'])
# ffmpeg command
convert_cmd = [
FF_BIN,
*FF_EXECUTION_FLAGS,
target_file,
*FF_SWITCHES, # Correct: Unpacks the list into separate arguments
metadata_title_string,
temp_file
]
# Convert File
logger('info', f'{SPACER * 3} Begin encoding of target file....')
try:
subprocess.run(convert_cmd, shell=False, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
logger('success', f'{SPACER * 3} File encoded successfully')
# Downgraded successfully
after_size_raw = os.path.getsize(temp_file)
after_size = hm.naturalsize(after_size_raw)
# Move original file to encoder_archive
logger('info', f'{SPACER * 3} Archiving source file')
try:
shutil.move(target_file, encoder_archive)
logger('success', f'{SPACER * 3} File Archived successfully')
except Exception as e:
logger('failure', f'{SPACER * 3} Failed to archive file. Please perform manually')
logger('failure', f'{SPACER * 3} Response:\t {str(e)}')
# Rename encoded file as original file name
logger('info', f'{SPACER * 3} Renaming encoded file')
try:
shutil.move(temp_file, target_file)
logger('success', f'{SPACER * 3} File Renamed successfully')
except Exception as e:
logger('failure', f'{SPACER * 3} Failed to rename file. Please perform manually')
logger('failure', f'{SPACER * 3} Response:\t {str(e)}')
logger('info', f'{SPACER * 3} Encoded file size:\t {after_size}')
logger('info', f'{SPACER * 3} Capacity recovered:\t {percentage_decrease(after_size_raw, before_size_raw)}%')
execution_time = hm.precisedelta(dt.datetime.now() - ts_now)
logger('info', '{:<50} {:>16}'.format('File processing time:', execution_time))
return 1, p.name, before_size_raw, after_size_raw
except Exception as e:
# Encoding process failed. Log event
logger('failure', f'{SPACER * 3}')
logger('failure', f'{SPACER * 6} *** Encoding failed *** Response: "{str(e)}"')
logger('failure', f'{SPACER * 6} *** Cleaning up.....')
logger('failure', f'{SPACER * 3}')
# Delete temp file
logger('info', f'{SPACER * 3} Deleting TEMP file')
try:
shutil.move(temp_file, TRASH_DIR)
logger('success', f'{SPACER * 3} Successfully deleted TEMP file')
except Exception as e:
logger('failure', f'{SPACER * 3} Failed to delete TEMP file. Please perform manually')
logger('failure', f'{SPACER * 3} Response:\t {str(e)}')
return 0, p.name, before_size_raw, before_size_raw
####################################################################################
####################################################################################
def main():
logger('none', f'\n\n{MARKER_CHAR * 140}\n')
logger('info', f'Executing script:\t {__file__}')
logger('info', 'Checking for targets.... ')
logger('none', f'{MARKER_CHAR * 140}\n')
if len(sys.argv) > 1:
if isinstance(sys.argv[1], int):
targets_list = sys.argv[2:]
logger('info', f"Found {(len(targets_list))} targets to encode. Let's begin!")
else:
targets_list = sys.argv[1:]
logger('info', f"Found {(len(targets_list))} targets to encode. Let's begin!")
else:
logger('failure', f' *** Nothing found to encode ***. Exiting.....')
logger('info', f'{MARKER_CHAR * 100}')
execution_time = hm.precisedelta(dt.datetime.now() - START_TIME)
MSG_TITLE = 'BORK BORK'
message_content = f' Automator Task Completed | {MSG_TITLE} | Nothing found to encode\nTotal runtime: {human_but_smaller(execution_time)} '
logger('info', f'Display Notification data:\t"{message_content[20:]}"...')
logger('info', '{:<62} {:>16}'.format(f'Execution completed. Total runtime:', human_but_smaller(execution_time)))
logger('none', f'{MARKER_CHAR * 140}\n')
print(message_content)
# Make sure to flush stdout to ensure immediate output
sys.stdout.flush()
exit(0)
loop_counter = 1
success_counter = 0
failed_list = []
before_size_raw = 0
after_size_raw = 0
for f in targets_list:
logger('info', f'{MARKER_CHAR * 100}')
logger('info', f'Target ({loop_counter} of {len(targets_list)})')
result, name, old_size, new_size = encode(f)
success_counter = success_counter + result
if result == 0:
failed_list.append(name)
before_size_raw += old_size
after_size_raw += new_size
loop_counter += 1
logger('info', f'{MARKER_CHAR * 100}')
execution_time = hm.precisedelta(dt.datetime.now() - START_TIME)
saved_size = hm.naturalsize(before_size_raw - after_size_raw)
body_str = 'Disk space recovered: {} ({}%)\nAvg. encode time: {}\nTime: {}'.format(
saved_size, percentage_decrease(after_size_raw, before_size_raw),
human_but_smaller(hm.precisedelta((dt.datetime.now() - START_TIME) / len(targets_list))),
human_but_smaller(execution_time)
)
# Print notification content to stdout
message_content = create_notification_content(len(targets_list), failed_list, body_str)
print(message_content)
# Make sure to flush stdout to ensure immediate output
sys.stdout.flush()
# Write cumulative results to logfile
logger('info', '{:>35} {:>16}'.format(' Total file size (targets passed): ', hm.naturalsize(before_size_raw)))
logger('info', '{:>35} {:>16}'.format(' Total file size (after encoding): ', hm.naturalsize(after_size_raw)))
logger('info', '{:>35} {:5} {:<16}'.format(
'Average file re-encoding time: ', '',
human_but_smaller(hm.precisedelta((dt.datetime.now() - START_TIME) / len(targets_list)))
# human_but_smaller(execution_time / len(targets_list)))
))
logger('info', '{:>35} {:>16}'.format(' Total disk space recovered: ', saved_size))
if failed_list:
logger('info', '{:>36} {:>16}'.format(' Encoding failed for: ', f'{len(failed_list)} objects'))
execution_time = hm.precisedelta(dt.datetime.now() - START_TIME)
logger('info', '{:<42} {:>16}'.format(f'Execution completed. Total runtime: ', human_but_smaller(execution_time)))
logger('none', f'{MARKER_CHAR * 140}\n')
####################################################################################
####################################################################################
if __name__ == "__main__":
main()