-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslideshow_CONTROLLER.py
1605 lines (1456 loc) · 103 KB
/
slideshow_CONTROLLER.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) <2023> <ongoing> <hydra3333>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import sys
import os
print(f"",flush=True,file=sys.stderr)
print(f"<This Script: '{os.path.abspath(__file__)}'>",flush=True,file=sys.stderr)
print(f"<QN_Auto_Slideshow_Creator_for_Windows> Copyright (C) <2023> <congoing> <hydra3333>",flush=True,file=sys.stderr)
print(f"This program comes with ABSOLUTELY NO WARRANTY; for details refer to the license on",flush=True,file=sys.stderr)
print(f"github at https://github.com/hydra3333/QN_Auto_Slideshow_Creator_for_Windows/blob/main/LICENSE.",flush=True,file=sys.stderr)
print(f"This is free software, and you are welcome to redistribute it",flush=True,file=sys.stderr)
print(f"under certain conditions; for details refer to the license on",flush=True,file=sys.stderr)
print(f"github at https://github.com/hydra3333/QN_Auto_Slideshow_Creator_for_Windows/blob/main/LICENSE.",flush=True,file=sys.stderr)
print(f"",flush=True,file=sys.stderr)
# 1. Modifying the sys.path list in a module DOES NOT not affect the sys.path of other modules or the main program.
# 2. Modifying the sys.path list in the MAIN PROGRAM WILL affect the search path for all modules imported by that program.
# Ensure we can import modules from ".\" by adding the current default folder to the python path.
# (tried using just PYTHONPATH environment variable but it was unreliable)
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
import slideshow_GLOBAL_UTILITIES_AND_VARIABLES as UTIL # define utilities and make raw (defaulted) global variables available to everyone
import vapoursynth as vs
from vapoursynth import core
core = vs.core
#core.num_threads = 1
import multiprocessing
import importlib
import re
import argparse
from functools import partial
import pathlib
from pathlib import Path, PureWindowsPath
import shutil
import subprocess
import datetime
#from datetime import datetime, date, time, timezone
from fractions import Fraction
from ctypes import * # for mediainfo ... load via ctypes.CDLL(r'.\MediaInfo.dll')
from typing import Union # for mediainfo
from typing import NamedTuple
from collections import defaultdict, OrderedDict
from enum import Enum
from enum import auto
#from strenum import StrEnum
#from strenum import LowercaseStrEnum
#from strenum import UppercaseStrEnum
import itertools
import math
import random
import glob
import configparser # or in v3: configparser
import yaml
import json
import pprint
import ast
import uuid
import logging
# for subprocess control eg using Popen
import time
from queue import Queue, Empty
from threading import Thread
import gc # for inbuilt garbage collection
# THE NEXT STATEMENT IS ONLY FOR DEBUGGING AND WILL CAUSE EXTRANEOUS OUTPUT TO STDERR
#gc.set_debug(gc.DEBUG_LEAK | gc.DEBUG_STATS) # for debugging, additional garbage collection settings, writes to stderr https://docs.python.org/3/library/gc.html to help detect leaky memory issues
num_unreachable_objects = gc.collect() # collect straight away
from PIL import Image, ExifTags, UnidentifiedImageError
from PIL.ExifTags import TAGS
import pydub
from pydub import AudioSegment
CDLL(r'MediaInfo.dll') # note the hard-coded folder # per https://forum.videohelp.com/threads/408230-ffmpeg-avc-from-jpgs-of-arbitrary-dimensions-maintaining-aspect-ratio#post2678372
from MediaInfoDLL3 import MediaInfo, Stream, Info, InfoOption # per https://forum.videohelp.com/threads/408230-ffmpeg-avc-from-jpgs-of-arbitrary-dimensions-maintaining-aspect-ratio#post2678372
#from MediaInfoDLL3 import * # per https://github.com/MediaArea/MediaInfoLib/blob/master/Source/Example/HowToUse_Dll3.py
### ********** end of common header **********
global SETTINGS_DICT
global OLD_INI_DICT
global OLD_CALC_INI_DICT
global USER_SPECIFIED_SETTINGS_DICT
global ALL_CHUNKS
global ALL_CHUNKS_COUNT
global ALL_CHUNKS_COUNT_OF_FILES
#core.std.LoadPlugin(r'DGDecodeNV.dll')
#core.avs.LoadPlugin(r'DGDecodeNV.dll')
#********************************************************************************************************
#********************************************************************************************************
#--------------------------------------------------------------------------------------------------------
#********************************************************************************************************
#********************************************************************************************************
###
def sort_files_list(files_list, sort_type='alphabetic_files_folders'):
# Call like:
# # preset by load_SETTINGS:
# # SETTINGS_DICT["valid_SORT_TYPES"] = [r'alphabetic_files_folders'.lower(), r'alphabetic'.lower(), r'win_files_folders.lower()', r'win_files'.lower(), r'random'.lower()]
# # SETTINGS_DICT["SORT_TYPE"]
# current_directory = '/path/to/directory'
# glob_var = "**/*.*"
# files_LIST = files_LIST = [entry for entry in Path(current_directory).glob(glob_var) if (entry.is_file() and entry.suffix.lower() in SETTINGS_DICT['EXTENSIONS'])]
# sorted_files = sort_files_list(files_LIST, sort_type=SETTINGS_DICT["SORT_TYPE"])
#
import ctypes
import random
from pathlib import Path
if sort_type.lower() == 'alphabetic'.lower():
return sorted(files_list, key=lambda p: p.name.lower())
elif sort_type.lower() == 'alphabetic_files_folders'.lower():
#return sorted(files_list, key=lambda p: (p.parent.name.lower(), p.name.lower()))
return sorted(files_list, key=lambda p: (os.path.dirname(os.path.abspath(p)).lower(), p.name.lower()))
elif sort_type.lower() == 'win_files'.lower():
str_cmp_logical = ctypes.windll.Shlwapi.StrCmpLogicalW
windows_sort_key = lambda path: str_cmp_logical(str(path).encode('utf-16le'), str(path).encode('utf-16le'))
return sorted(files_list, key=lambda p: (p.parent.lower(), windows_sort_key(p.name.lower())))
elif sort_type.lower() == 'win_files_folders'.lower():
str_cmp_logical = ctypes.windll.Shlwapi.StrCmpLogicalW
windows_sort_key_full_path = lambda path: [str_cmp_logical(str(component).encode('utf-16le'), str(component).encode('utf-16le')) for component in Path(path).parts]
return sorted(files_list, key=lambda p: windows_sort_key_full_path(p))
elif sort_type.lower() == 'random'.lower():
random.shuffle(files_list)
return files_list
else:
raise ValueError(f'CONTROLLER: ERROR: Invalid Sort Type specified: "{sort_type}" Must be one of {SETTINGS_DICT["valid_SORT_TYPES"]}')
###
def find_all_chunks():
# only use globals: SETTINGS_DICT, DEBUG
def fac_get_filename(files_generator):
# get next filename of desired extensions from generator, ignoring extensions we have not specified
# loop around only returning a filename with a known extension
while 1: # loop until we do a "return", hitting past the end of the iterator returns None
try:
filename = next(files_generator)
#if UTIL.DEBUG: print(f'fac_get_filename: get success, filename.name=' + filename.name,flush=True)
except StopIteration:
return None
if filename.suffix.lower() in SETTINGS_DICT['EXTENSIONS']: # only return files which are in known extensions
#if UTIL.DEBUG: print(f'DEBUG: find_all_chunks: fac_get_filename: in EXTENSIONS success, filename.name=' + filename.name,flush=True)
return filename
def fac_check_clip_from_filename(filename, ext): # opens VID_EEK_EXTENSIONS only ... Source filter depends on extension
if not ext in SETTINGS_DICT['VID_EEK_EXTENSIONS']:
raise ValueError(f'get_clip_from_path: expected {filename} to have extension in {SETTINGS_DICT["VID_EEK_EXTENSIONS"]} ... aborting')
if ext in SETTINGS_DICT['VID_EXTENSIONS']:
try:
ffcachefile = UTIL.get_random_ffindex_filename(filename)
clip = core.ffms2.Source(str(filename), cachefile=ffcachefile)
del clip
if os.path.exists(ffcachefile):
os.remove(ffcachefile)
return True
except Exception as e:
print(f'CONTROLLER: WARNING: fac_check_clip_from_filename: error opening file via "ffms2": "{str(filename)}" ; ignoring this video clip. The error was:\n{e}\n{type(e)}\n{str(e)}',flush=True)
return False
elif ext in SETTINGS_DICT['EEK_EXTENSIONS']:
try:
clip = core.lsmas.LWLibavSource(str(filename))
del clip
return True
except Exception as e:
print(f'CONTROLLER: WARNING: fac_check_clip_from_filename: error opening file via "lsmas": "{filename.name}" ; ignoring this video clip. The error was:\n{e}\n{type(e)}\n{str(e)}',flush=True)
return False
else:
raise ValueError(f'ERROR: fac_check_clip_from_filename: get_clip_from_path: expected {filename} to have extension in {SETTINGS_DICT["VID_EEK_EXTENSIONS"]} ... aborting')
return False
def fac_check_clip_from_pic(filename, ext):
if ext in SETTINGS_DICT['PIC_EXTENSIONS']:
ffcachefile = UTIL.get_random_ffindex_filename(filename)
try:
clip = core.ffms2.Source(str(filename), cachefile=ffcachefile)
del clip
if os.path.exists(ffcachefile):
os.remove(ffcachefile)
return True
except Exception as e:
print(f'CONTROLLER: WARNING: fac_check_clip_from_pic: error opening file via "ffms2": "{filename.name}" ; ignoring this picture. The error was:\n{e}\n{type(e)}\n{str(e)}',flush=True)
return False
else:
raise ValueError(f'ERROR: fac_check_clip_from_pic: : expected {filename} to have extension in {SETTINGS_DICT["PIC_EXTENSIONS"]} ... aborting')
return False
def fac_check_file_validity_by_opening(filename):
if filename is None:
raise ValueError(f'ERROR: fac_check_file_validity_by_opening: "filename" not passed as an argument to fac_check_file_validity_by_opening')
sys.exit(1)
ext = filename.suffix.lower()
if ext in SETTINGS_DICT['VID_EXTENSIONS']:
is_valid = fac_check_clip_from_filename(filename, ext) # open depends on ext, the rest is the same
elif ext in SETTINGS_DICT['EEK_EXTENSIONS']:
is_valid = fac_check_clip_from_filename(filename, ext) # open depends on ext, the rest is the same
elif ext in SETTINGS_DICT['PIC_EXTENSIONS']:
is_valid = fac_check_clip_from_pic(filename, ext)
else:
raise ValueError(f'ERROR: fac_check_file_validity_by_opening: "{filename}" - UNRECOGNISED file extension "{ext}", aborting ...')
sys.exit()
return is_valid
#
TOLERANCE_FINAL_CHUNK = max(1, int(SETTINGS_DICT['MAX_FILES_PER_CHUNK'] * (float(SETTINGS_DICT['TOLERANCE_PERCENT_FINAL_CHUNK'])/100.0)))
print(f"CONTROLLER: Commencing assigning files into chunks for processing usng:",flush=True)
print(f"{UTIL.objPrettyPrint.pformat(SETTINGS_DICT['ROOT_FOLDER_SOURCES_LIST_FOR_IMAGES_PICS'])}",flush=True)
print(f"{UTIL.objPrettyPrint.pformat(SETTINGS_DICT['EXTENSIONS'])}",flush=True)
print(f"RECURSIVE={SETTINGS_DICT['RECURSIVE']}",flush=True)
if UTIL.DEBUG:
print( f"DEBUG: find_all_chunks: " +
f"MAX_FILES_PER_CHUNK={SETTINGS_DICT['MAX_FILES_PER_CHUNK']}, " +
f"TOLERANCE_PERCENT_FINAL_CHUNK={SETTINGS_DICT['TOLERANCE_PERCENT_FINAL_CHUNK']}, " +
f"TOLERANCE_FINAL_CHUNK={TOLERANCE_FINAL_CHUNK}",flush=True)
if SETTINGS_DICT['RECURSIVE']:
glob_var="**/*.*" # recursive
ff_glob_var="**/*.ffindex" # for .ffindex file deletion recursive
else:
glob_var="*.*" # non-recursive
ff_glob_var="*.ffindex" # for .ffindex file deletion non-recursive
count_of_files = 0
chunk_id = -1 # base 0 chunk id, remember
chunks = {}
file_list_in_chunk = []
for Directory in SETTINGS_DICT['ROOT_FOLDER_SOURCES_LIST_FOR_IMAGES_PICS']: # Use the order of folders as specified by the user in the LIST, unsorted
current_Directory = Directory
files_LIST = [entry for entry in Path(current_Directory).glob(glob_var) if (entry.is_file() and entry.suffix.lower() in SETTINGS_DICT['EXTENSIONS'])]
files = sort_files_list(files_LIST, sort_type=SETTINGS_DICT["SORT_TYPE"])
for filename in files: # filename type='<class 'pathlib.WindowsPath'>'
if UTIL.DEBUG: print(f"DEBUG: find_all_chunks: found file '{filename}', re-checking if file is in '{SETTINGS_DICT['EXTENSIONS']}'",flush=True)
if filename.suffix.lower() in SETTINGS_DICT['EXTENSIONS']:
print(f"CONTROLLER: Checking file {count_of_files}. '{filename}' for validity with fac_check_file_validity_by_opening ...",flush=True)
is_valid = fac_check_file_validity_by_opening(filename)
if not is_valid: # ignore clips which had an issue with being opened and return None
print(f'CONTROLLER: Unable to process {count_of_files} {str(filename)} ... ignoring it',flush=True)
else:
# if required, start a new chunk
if (count_of_files % SETTINGS_DICT['MAX_FILES_PER_CHUNK']) == 0:
chunk_id = chunk_id + 1
chunks[str(chunk_id)] = {
'chunk_id': chunk_id,
'chunk_fixed_json_filename' : UTIL.fully_qualified_filename(SETTINGS_DICT['CURRENT_CHUNK_FILENAME']), # always the same fixed filename
'proposed_ffv1_mkv_filename' : UTIL.fully_qualified_filename(SETTINGS_DICT['CHUNK_ENCODED_FFV1_FILENAME_BASE'] + str(chunk_id).zfill(5) + r'.mkv'), # filename related to chunk_id, with 5 digit zero padded sequential number
'proposed_h264_mkv_filename' : UTIL.fully_qualified_filename(SETTINGS_DICT['CHUNK_ENCODED_H264_FILENAME_BASE'] + str(chunk_id).zfill(5) + r'.mkv'), # filename related to chunk_id, with 5 digit zero padded sequential number
'num_frames_in_chunk' : 0, # initialize to 0, filled in by encoder
'start_frame_num_in_chunk': 0, # initialize to 0, filled in by encoder
'end_frame_num_in_chunk': 0, # initialize to 0, filled in by encoder
'start_frame_num_of_chunk_in_final_video': 0, # initialize to 0, # calculated AFTER encoder finished completely
'end_frame_num_of_chunk_in_final_video': 0, # initialize to 0, # calculated AFTER encoder finished completely
'num_files': 0, # initialized but filled in by this loop, number of files in file_list
'file_list': [], # each item is a fully qualified filename of a source file for this chunk
'num_snippets': 0, # # initialize to 0, number of files in file_list, filled in by encoder
'snippet_list': [], # an empty dict to be be filled in by encoder, it looks like this:
# snippet_list: [ # each snippet list item is a dict which looks like the below:
# {
# 'start_frame_of_snippet_in_chunk': 0, # filled in by encoder
# 'end_frame_of_snippet_in_chunk': XXX, # filled in by encoder
# 'start_frame_of_snippet_in_final_video': AAA, # AFTER all encoding completed, calculated and filled in by controller
# 'end_frame_of_snippet_in_final_video': XXX, # AFTER all encoding completed, calculated and filled in by controller
# 'snippet_num_frames': YYY, # filled in by encoder
# 'snippet_source_video_filename': '\a\b\c\ZZZ1.3GP' # filled in by encoder
# },
# ]
}
# add currently examined file to chunk
fully_qualified_path_string = UTIL.fully_qualified_filename(filename)
chunks[str(chunk_id)]['file_list'].append(fully_qualified_path_string)
chunks[str(chunk_id)]['num_files'] = chunks[str(chunk_id)]['num_files'] + 1
count_of_files = count_of_files + 1
#end for
#end for
if count_of_files <=0:
raise ValueError(f"ERROR: find_all_chunks: File Extensions:\n{SETTINGS_DICT['EXTENSIONS']}\nnot found in '{SETTINGS_DICT['ROOT_FOLDER_SOURCES_LIST_FOR_IMAGES_PICS']}'")
# If the final chunk is < 20% of SETTINGS_DICT['MAX_FILES_PER_CHUNK'] then merge it into the previous chunk
chunk_count = len(chunks)
if chunk_count > 1:
# if within tolerance, merge the final chunk into the previous chunk
if chunks[str(chunk_id)]['num_files'] <= TOLERANCE_FINAL_CHUNK:
print(f'CONTROLLER: Merging final chunk (chunk_id={chunk_id}, num_files={chunks[str(chunk_id)]["num_files"]}) into previous chunk (chunk_id={chunk_id - 1}, num_files={chunks[str(chunk_id - 1)]["num_files"]+chunks[str(chunk_id)]["num_files"]})',flush=True)
chunks[str(chunk_id - 1)]["file_list"] = chunks[str(chunk_id - 1)]["file_list"] + chunks[str(chunk_id)]["file_list"]
chunks[str(chunk_id - 1)]["num_files"] = chunks[str(chunk_id - 1)]["num_files"] + chunks[str(chunk_id)]["num_files"]
# remove the last chunk since we just merged it into the chunk prior
del chunks[str(chunk_id)]
chunk_count = len(chunks)
# OK lets print the chunks tree
if UTIL.DEBUG: print(f"DEBUG: find_all_chunks: Chunks tree contains {count_of_files} files:\n{UTIL.objPrettyPrint.pformat(chunks)}",flush=True)
# CHECK the chunks tree
if UTIL.DEBUG: print(f"DEBUG: find_all_chunks: Chunks tree contains {count_of_files} files:\n{UTIL.objPrettyPrint.pformat(chunks)}",flush=True)
for i in range(0,chunk_count): # i.e. 0 to (chunk_count-1)
if UTIL.DEBUG:
print(f'DEBUG: find_all_chunks: About to check-print data for chunks[{i}] : chunks[{i}]["num_files"] and chunks[{i}]["file_list"]:',flush=True)
print(f'DEBUG:find_all_chunks: chunks[{i}]["num_files"] = {chunks[str(i)]["num_files"]}',flush=True)
print(f'DEBUG:find_all_chunks: chunks[{i}]["file_list"] = \n{UTIL.objPrettyPrint.pformat(chunks[str(i)]["file_list"])}',flush=True)
num_files = chunks[str(i)]["num_files"]
file_list = chunks[str(i)]["file_list"]
for j in range(0,num_files):
# retrieve a file 2 different ways
file1 = file_list[j]
file2 = chunks[str(i)]["file_list"][j]
print(f"CONTROLLER: Finished assigning files into chunks for processing: {count_of_files} files into {chunk_count} chunks.",flush=True)
return chunk_count, count_of_files, chunks
###
def audio_standardize_and_import_file(audio_filename, headroom_db, ignore_error_converting=False):
# use global SETTINGS_DICT for the convert and import audio
# https://pydub.com/
# https://github.com/jiaaro/pydub/blob/master/API.markdown
# NOTE we MUST ensure the clips all have the SAME characteristics !!!!! or overlay etc will not work.
# using .from_file a file may be an arbitrary number of channels, which pydub cannot handle
# so we must first convert number of channels etc into a fixed file so we can use .from_file, eg
# ffmpeg -i "filename.mp4" -vn -ac 2 -ar 48000 -acodec pcm_s16le "some_audio_filename_in_temp_folder.wav"
# rely on multi-used settings variables defined in main
if os.path.exists(temporary_audio_filename):
os.remove(temporary_audio_filename)
if UTIL.DEBUG:
loglevel = r'verbose'
stats = r'-stats'
benchmark = r'-benchmark'
else:
loglevel = 'warning'
stats = r'-nostats'
benchmark = stats # a hack to workaround ffmpeg rejecting zero length string''
ffmpeg_commandline = [ UTIL.FFMPEG_EXE,
'-hide_banner',
'-loglevel', loglevel,
stats,
benchmark,
'-threads', str(UTIL.NUM_THREADS_FOR_FFMPEG_DECODER),
'-i', audio_filename,
'-vn',
'-af', f'ebur128=peak=true:target={headroom_db}:dualmono=true:framelog=quiet', # this normalizes audio using industry standard ebur128; ffmpeg takes a while and it may not even work
'-acodec', temporary_background_audio_codec,
'-threads', str(UTIL.NUM_THREADS_FOR_FFMPEG_ENCODER),
'-ac', str(target_background_audio_channels),
'-ar', str(target_background_audio_frequency),
'-y', temporary_audio_filename,
]
print(f"CONTROLLER: audio_standardize_and_import_file attempting to standardize audio using {ffmpeg_commandline}",flush=True)
if ignore_error_converting:
result = subprocess.run(ffmpeg_commandline, check=False)
if result.returncode != 0:
print(f"CONTROLLER: WARNING: audio_standardize_and_import_file: ignoring an audio file due to Unexpected error from subprocess.run\n{UTIL.objPrettyPrint.pformat(ffmpeg_commandline)}",flush=True,file=sys.stderr)
return None
else:
# this will crash if there's an error
subprocess.run(ffmpeg_commandline, check=True) # this will crash if there's an error
try:
audio = AudioSegment.from_file(temporary_audio_filename)
audio = audio.set_channels(target_background_audio_channels).set_sample_width(target_background_audio_bytedepth).set_frame_rate(target_background_audio_frequency)
audio = audio.apply_gain(headroom_db - audio.max_dBFS) # RE-normalize imported audio, not sure ffmpeg ebur128 does anything
#except FileNotFoundError:
# print(f"CONTROLLER: audio_standardize_and_import_file: audio File not found from AudioSegment.from_file('{temporary_audio_filename}')",flush=True,file=sys.stderr)
# sys.exit(1)
#except TypeError:
# print(f"CONTROLLER: audio_standardize_and_import_file: audio Type mismatch or unsupported operation from AudioSegment.from_file('{temporary_audio_filename}')",flush=True,file=sys.stderr)
# sys.exit(1)
#except ValueError:
# print(f"CONTROLLER: audio_standardize_and_import_file: audio Invalid or unsupported value from AudioSegment.from_file('{temporary_audio_filename}')",flush=True,file=sys.stderr)
# sys.exit(1)
#except IOError:
# print(f"CONTROLLER: audio_standardize_and_import_file: audio I/O error occurred from AudioSegment.from_file('{temporary_audio_filename}')",flush=True,file=sys.stderr)
# sys.exit(1)
#except OSError as e:
# print(f"CONTROLLER: audio_standardize_and_import_file: audio Unexpected OSError from AudioSegment.from_file('{temporary_audio_filename}')\n{str(e)}",flush=True,file=sys.stderr)
# sys.exit(1)
except Exception as e:
print(f"CONTROLLER: audio_standardize_and_import_file: audio Unexpected error from AudioSegment.from_file('{temporary_audio_filename}')\n{str(e)}",flush=True,file=sys.stderr)
sys.exit(1)
if os.path.exists(temporary_audio_filename):
os.remove(temporary_audio_filename)
return audio
def audio_standardize_and_import_background_audios_from_folder(background_audio_folder, extensions=['.mp2', '.mp3', '.mp4', '.m4a', '.wav', '.flac', '.aac', '.ogg', '.wma']):
# loop through files in a specified background_audio_folder in alphabetical order,
# standardize them (ffmpeg reads anything useful and converts it)
# and import and append them to form a large background audio clip
# rely on multi-used settings variables defined in main
background_audio = AudioSegment.empty()
background_audio = background_audio.set_channels(target_background_audio_channels)
background_audio = background_audio.set_sample_width(target_background_audio_bytedepth)
background_audio = background_audio.set_frame_rate(target_background_audio_frequency)
background_audio_folder = os.path.abspath(background_audio_folder).rstrip(os.linesep).strip('\r').strip('\n').strip()
#glob_var="**/*.*" # recursive
glob_var="*.*" # non-recursive
c = 0
v = 0
#files = sorted( (entry for entry in Path(background_audio_folder).glob(glob_var) if (entry.is_file() and entry.suffix.lower() in extensions)), key=lambda p: (p.parent.lower(), p.name.lower()) ) # consider files but exclude directories in the generator, sorting them
files_LIST = [entry for entry in Path(background_audio_folder).glob(glob_var) if (entry.is_file() and entry.suffix.lower() in extensions)]
files = sort_files_list(files_LIST, sort_type='alphabetic_files_folders') # always a fixed sort order for background audio
for filename in files:
c = c + 1
if UTIL.DEBUG: print(f"DEBUG: audio_standardize_and_import_background_audios_from_folder: found file '{filename}', checking if file is in '{extensions}'",flush=True)
filename = UTIL.fully_qualified_filename(filename)
# having found a suitable audio file in the background_audio_folder, standardize and import and append it
audio_imported_from_file = audio_standardize_and_import_file(filename, target_audio_background_normalize_headroom_db, ignore_error_converting=True)
if audio_imported_from_file is not None:
v = v + 1
background_audio = background_audio + audio_imported_from_file
#end for
print(f"CONTROLLER: audio_standardize_and_import_background_audios_from_folder: found {v} valid background audio files out of {c}, duration={UTIL.format_duration_ms_to_hh_mm_ss_hhh(len(background_audio))}, in '{extensions}' in '{background_audio_folder}', ",flush=True)
# return an empty audio of zero length if no background files were found
return files_LIST, background_audio
def audio_create_standardized_silence(duration_ms):
# use global SETTINGS_DICT for the convert and import audio
# https://pydub.com/
# https://github.com/jiaaro/pydub/blob/master/API.markdown
# NOTE we MUST ensure the clips all have the SAME characteristics !!!!! or overlay etc will not work.
# rely on multi-used settings variables defined in main
audio = AudioSegment.silent(duration=duration_ms)
audio = audio.set_channels(target_background_audio_channels).set_sample_width(target_background_audio_bytedepth).set_frame_rate(target_background_audio_frequency)
return audio
###
def encode_chunk_using_vsipe_ffmpeg_to_ffv1(individual_chunk_id):
# encode an individual chunk using vspipe and ffmpeg
#
# using ChatGPT suggested method for non-blocking reads of subprocess stderr, stdout
global SETTINGS_DICT
global ALL_CHUNKS
global ALL_CHUNKS_COUNT
global ALL_CHUNKS_COUNT_OF_FILES
if SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE'] != SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE_FFV1']:
print(f"CONTROLLER: ERROR: encode_chunk_using_vsipe_ffmpeg_to_ffv1: INVALID CHUNK_INTERIM_ENCODE_TYPE '{SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE']}' specified; must be '{SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE_FFV1']}'",flush=True)
sys.exit(1)
slideshow_ENCODER_legacy_path = SETTINGS_DICT['slideshow_ENCODER_legacy_path']
def enqueue_output(out, queue):
# for subprocess thread output queueing
for line in iter(out.readline, b''):
queue.put(line)
out.close()
individual_chunk_dict = ALL_CHUNKS[str(individual_chunk_id)]
chunk_json_filename = UTIL.fully_qualified_filename(individual_chunk_dict['chunk_fixed_json_filename']) # always the same fixed filename
proposed_ffv1_mkv_filename = UTIL.fully_qualified_filename(individual_chunk_dict['proposed_ffv1_mkv_filename']) # preset by find_all_chunks to: fixed filename plus a seqential 5-digit-zero-padded ending based on chunk_id + r'.mkv'
# remove any pre-existing files to be consumed and produced by the ENCODER
if os.path.exists(chunk_json_filename):
os.remove(chunk_json_filename)
if os.path.exists(proposed_ffv1_mkv_filename):
os.remove(proposed_ffv1_mkv_filename)
# create the fixed-filename chunk file consumed by the encoder; it contains the fixed-filename of the snippet file to produce
if UTIL.DEBUG: print(f"DEBUG: CONTROLLER: in encoder loop: attempting to create chunk_json_filename='{chunk_json_filename}' for encoder to consume.",flush=True)
try:
with open(chunk_json_filename, 'w') as fp:
json.dump(individual_chunk_dict, fp, indent=4)
except Exception as e:
print(f"CONTROLLER: ERROR: dumping current chunk to JSON file: '{chunk_json_filename}' for encoder, chunk_id={individual_chunk_id}, individual_chunk_dict=\n{UTIL.objPrettyPrint.pformat(individual_chunk_dict)}\n{str(e)}",flush=True,file=sys.stderr)
sys.exit(1)
print(f"CONTROLLER: Created fixed-filename chunk file for encoder to consume: for individual_chunk_id={individual_chunk_id} '{chunk_json_filename}' listing {ALL_CHUNKS[str(individual_chunk_id)]['num_files']} files, individual_chunk_dict=\n{UTIL.objPrettyPrint.pformat(individual_chunk_dict)}",flush=True)
# Define the commandlines for the subprocesses forming the ENCODER
if UTIL.DEBUG:
loglevel = r'verbose'
stats = r'-stats'
benchmark = r'-benchmark'
else:
loglevel = 'info'
stats = r'-stats'
benchmark = stats # a hack to workaround ffmpeg rejecting zero length string''
vspipe_commandline = [ UTIL.VSPIPE_EXE, '--progress', '--container', 'y4m', slideshow_ENCODER_legacy_path, '-' ]
ffmpeg_commandline = [ UTIL.FFMPEG_EXE,
'-hide_banner',
'-loglevel', loglevel,
stats,
benchmark,
'-colorspace', 'bt709',
'-color_primaries', 'bt709',
'-color_trc', 'bt709',
'-color_range', 'pc',
'-threads', str(UTIL.NUM_THREADS_FOR_FFMPEG_DECODER),
'-f', 'yuv4mpegpipe',
'-i', 'pipe:',
'-probesize', '200M',
'-analyzeduration', '200M',
'-sws_flags', 'lanczos+accurate_rnd+full_chroma_int+full_chroma_inp',
'-filter_complex', 'format=yuv420p,setdar=16/9',
'-an',
'-threads', str(UTIL.NUM_THREADS_FOR_FFMPEG_ENCODER),
'-c:v', 'ffv1', '-level', '3', '-coder', '1', '-context', '1', '-slicecrc', '1',
'-colorspace', 'bt709',
'-color_primaries', 'bt709',
'-color_trc', 'bt709',
'-color_range', 'pc',
'-y', proposed_ffv1_mkv_filename
]
# this vspipe commandline is for DEBUGGING only
# it produces the vspipe output but directs it to NUL and does not invoke ffmpeg
# but it is still handy becuase it prodices updated snippet into into ALL_CHUNKS
#vspipe_commandline_NUL = [ UTIL.VSPIPE_EXE, '--progress', '--container', 'y4m', slideshow_ENCODER_legacy_path, 'NUL' ]
# run the vspipe -> ffmpeg with non-blocking reads of stderr and stdout
piping_method = 3 # 3 works
if piping_method == 1: # this loses stdout from ffmpeg
# stderr from process_ffmpeg works OK. stdout from ffmpeg gets lost.
print(f"CONTROLLER: Running the ENCODER via piping_method={piping_method}, simple Popens, losing ffmpeg stdout?, using commandlines:\n\n{vspipe_commandline}\n{UTIL.objPrettyPrint.pformat(ffmpeg_commandline)}\n",flush=True)
process_vspipe = subprocess.Popen( vspipe_commandline, stdout=subprocess.PIPE)
process_ffmpeg = subprocess.Popen( ffmpeg_commandline, stdin=process_vspipe.stdout)
process_ffmpeg.communicate()
elif piping_method == 2: # this method DOES NOT WORK because subprocess.run hates the pipe symbol
# Execute the command using subprocess.run
vspipe_pipe_ffmpeg_commandline = vspipe_commandline + [r' | '] + ffmpeg_commandline
print(f"CONTROLLER: Running the ENCODER via piping_method={piping_method}, subprocess.run, with one commandline:\n\n{UTIL.objPrettyPrint.pformat(vspipe_pipe_ffmpeg_commandline)}\n",flush=True)
result = subprocess.run(vspipe_pipe_ffmpeg_commandline, shell=True)
if result.returncode != 0:
print(f"CONTROLLER: ERROR RUNNING ENCODER VSPIPE/FFMPEG via piping_method={piping_method} subprocess.run, Command execution failed with exit status: {result.returncode}",flush=True)
sys.exit(1)
else:
print(f"CONTROLLER: Returned successfully from the ENCODER via piping_method={piping_method}, subprocess.run, with one commandline:\n\n{UTIL.objPrettyPrint.pformat(vspipe_pipe_ffmpeg_commandline)}\n",flush=True)
elif piping_method == 3: # less control but you see everything
# Execute the command using subprocess.run but using a string not a list
def command_list_to_command_string(command_list):
command_parts = []
for part in command_list:
#if part.startswith('-') or part.lower() == r'pipe:'.lower(): # Check if the part starts with a dash (indicating a switch)
# command_parts.append(part) # Add the part as is (switch)
#else:
# command_parts.append(f'"{part}"') # Enclose the part in double quotes
if part.startswith('format='.lower()) or (len(part) >= 2 and part[1] == r':'):
command_parts.append(f'"{part}"') # Enclose the part in double quotes
else:
command_parts.append(part) # Add the part as is
commandline = ' '.join(command_parts)
return commandline
vspipe_cmd = command_list_to_command_string(vspipe_commandline)
ffmpeg_cmd = command_list_to_command_string(ffmpeg_commandline)
vspipe_pipe_ffmpeg_commandline = vspipe_cmd + r' | ' + ffmpeg_cmd
print(f"CONTROLLER: Running the ENCODER via piping_method={piping_method}, subprocess.run, with one commandline:\n\n{UTIL.objPrettyPrint.pformat(vspipe_pipe_ffmpeg_commandline)}\n",flush=True)
result = subprocess.run(vspipe_pipe_ffmpeg_commandline, shell=True)
if result.returncode != 0:
print(f"CONTROLLER: ERROR RUNNING ENCODER VSPIPE/FFMPEG via piping_method={piping_method} subprocess.run, Command execution failed with exit status: {result.returncode}",flush=True)
sys.exit(1)
else:
print(f"CONTROLLER: Returned successfully from the ENCODER via piping_method={piping_method}, subprocess.run, with one commandline:\n\n{UTIL.objPrettyPrint.pformat(vspipe_pipe_ffmpeg_commandline)}\n",flush=True)
elif piping_method == 4:
# Execute the command using os.system
def command_list_to_command_string(command_list):
command_parts = []
for part in command_list:
#if part.startswith('-') or part.lower() == r'pipe:'.lower(): # Check if the part starts with a dash (indicating a switch)
# command_parts.append(part) # Add the part as is (switch)
#else:
# command_parts.append(f'"{part}"') # Enclose the part in double quotes
if part.startswith('format='.lower()) or (len(part) >= 2 and part[1] == r':'):
command_parts.append(f'"{part}"') # Enclose the part in double quotes
else:
command_parts.append(part) # Add the part as is
commandline = ' '.join(command_parts)
return commandline
vspipe_cmd = command_list_to_command_string(vspipe_commandline)
ffmpeg_cmd = command_list_to_command_string(ffmpeg_commandline)
vspipe_pipe_ffmpeg_commandline = vspipe_cmd + r' | ' + ffmpeg_cmd
print(f"CONTROLLER: Running the ENCODER via piping_method={piping_method}, subprocess.run, with one commandline:\n\n{UTIL.objPrettyPrint.pformat(vspipe_pipe_ffmpeg_commandline)}\n",flush=True)
exit_status = os.system(vspipe_pipe_ffmpeg_commandline) # os.system fails to run this even though the string works in a dos box
if exit_status != 0:
print(f"CONTROLLER: ERROR RUNNING ENCODER VSPIPE/FFMPEG via piping_method={piping_method} os.system, Command execution failed with exit status: {exit_status}",flush=True)
sys.exit(1)
else:
print(f"CONTROLLER: Returned successfully from the ENCODER via piping_method={piping_method}, os.system, with one commandline:\n{UTIL.objPrettyPrint.pformat(vspipe_pipe_ffmpeg_commandline)}\n",flush=True)
elif piping_method == 5: # non-blocking reads, works fine as long as nothing goes wrong.
print(f"CONTROLLER: Running the ENCODER via piping_method={piping_method}, non=blocking reads, using commandlines:\n\n{UTIL.objPrettyPrint.pformat(vspipe_commandline)}\n\n{UTIL.objPrettyPrint.pformat(ffmpeg_commandline)}\n",flush=True)
try:
# Run the commands in subprocesses for the ENCODER
process1 = subprocess.Popen(vspipe_commandline, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process2 = subprocess.Popen(ffmpeg_commandline, stdin=process1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# FOR TESTING:
#pid1 = process1.pid
#pid2 = process2.pid
#time.sleep(3) # Add a delay of a couple of seconds
## Terminate subprocesses forcefully using taskkill if they aren't already terminated
#os.system(f'taskkill /F /PID {process1.pid}')
#os.system(f'taskkill /F /PID {process2.pid}')
#sys.exit()
# Create queues to store the output and error streams
stderr_queue1 = Queue()
stdout_queue2 = Queue()
stderr_queue2 = Queue()
# Launch separate threads to read the output and error streams
stderr_thread1 = Thread(target=enqueue_output, args=(process1.stderr, stderr_queue1))
stdout_thread2 = Thread(target=enqueue_output, args=(process2.stdout, stdout_queue2))
stderr_thread2 = Thread(target=enqueue_output, args=(process2.stderr, stderr_queue2))
stderr_thread1.daemon = True
stdout_thread2.daemon = True
stderr_thread2.daemon = True
stderr_thread1.start()
stdout_thread2.start()
stderr_thread2.start()
# Read output and error streams
while True:
try:
stderr_line1 = stderr_queue1.get_nowait().decode('utf-8').strip()
if stderr_line1:
print(f"vspipe: {stderr_line1}",flush=True,file=sys.stderr)
pass
except Empty:
pass
try:
stdout_line2 = stdout_queue2.get_nowait().decode('utf-8').strip()
if stdout_line2:
print(f"ffmpeg: {stdout_line2}",flush=True)
pass
except Empty:
pass
try:
stderr_line2 = stderr_queue2.get_nowait().decode('utf-8').strip()
if stderr_line2:
print(f"ffmpeg: {stderr_line2}",flush=True,file=sys.stderr)
pass
except Empty:
pass
if (not stderr_thread1.is_alive()) and (not stdout_thread2.is_alive()) and (not stderr_thread2.is_alive()) and (stderr_queue1.empty()) and (stdout_queue2.empty()) and (stderr_queue2.empty()):
break
# Introduce a 50ms delay to reduce CPU load
time.sleep(0.05) # Sleep for 50 milliseconds so as to not thrash the cpu
#end while
# Retrieve the remaining output and error streams
output, error2 = process2.communicate()
error1 = process1.stderr.read()
# Decode any ffmpeg final output from bytes to string and print it
print(f"ffmpeg: {output.decode('utf-8').strip()}",flush=True)
# Print any final error messages
if error1:
print(f"vspipe: {error1.decode('utf-8').strip()}",flush=True,file=sys.stderr)
if error2:
print(f"ffmpeg: {error2.decode('utf-8').strip()}",flush=True,file=sys.stderr)
# Close the queues
stderr_queue1.close()
stdout_queue2.close()
stderr_queue2.close()
# Close the subprocesses
process1.stdout.close()
process1.stderr.close()
process2.stdout.close()
process2.stderr.close()
except KeyboardInterrupt:
# Retrieve the process IDs
pid1 = process1.pid
pid2 = process2.pid
# Perform cleanup or other actions
# before terminating the program
process1.terminate()
process2.terminate()
process1.wait()
process2.wait()
# Delay before terminating forcefully with taskkill
time.sleep(2) # Add a delay of a couple of seconds
# Terminate subprocesses forcefully using taskkill if they aren't already terminated
os.system(f'taskkill /F /PID {process1.pid}')
os.system(f'taskkill /F /PID {process2.pid}')
# Raise the exception again
raise
except Exception as e:
print(f'CONTROLLER: ERROR RUNNING SUBPROCESSES, :\n{e}\n{type(e)}\n{str(e)}',flush=True)
raise e
else:
print(f"print(f'CONTROLLER: ERROR RUNNING VSPIPE/FFMPEG, invalid piping_method={piping_method}",flush=True)
sys.exit(1)
time.sleep(2.0) # give it a chance (2 seconds, or 2000ms) to settle down
print(f"CONTROLLER: Finished running the ENCODER.",flush=True)
if not os.path.exists(chunk_json_filename):
print(f"CONTROLLER: ERROR: CONTROLLER: encoder-updated current chunk to JSON file file not found '{chunk_json_filename}' not found !",flush=True)
sys.exit(1)
if not os.path.exists(proposed_ffv1_mkv_filename):
print(f"CONTROLLER: ERROR: CONTROLLER: encoder-produced .mkv video file not found '{proposed_ffv1_mkv_filename}' not found !",flush=True)
sys.exit(1)
# Now the encoder has encoded a chunk and produced an updated chunk file and an ffv1 encoded video .mkv
# ... we must import updated chunk file (which will include a new snippet_list) check the chunk, and update the ALL_CHUNKS dict with updated chunk data
# The format of the snippet_list produced by the encoder into the updated chunk JSON file is defined above.
if UTIL.DEBUG: print(f"DEBUG: CONTROLLER: in encoder loop: attempting to load chunk_json_filename={chunk_json_filename} produced by the encoder.",flush=True)
try:
with open(chunk_json_filename, 'r') as fp:
updated_individual_chunk_dict = json.load(fp)
except Exception as e:
print(f"CONTROLLER: ERROR: CONTROLLER: loading updated current chunk from JSON file: '{chunk_json_filename}' from encoder, chunk_id={individual_chunk_id}, related to individual_chunk_dict=\nUTIL.objPrettyPrint.pformat(individual_chunk_dict)\n{str(e)}",flush=True,file=sys.stderr)
sys.exit(1)
print(f"CONTROLLER: Loaded updated current chunk from ENCODER-updated JSON file: '{chunk_json_filename}'",flush=True)
if DEBUG: print(f"CONTROLLER: DEBUG: the chunk_id returned from the encoder={updated_individual_chunk_dict['chunk_id']} whereas local encode was specified as individual_chunk_id={individual_chunk_id}",flush=True)
if (updated_individual_chunk_dict['chunk_id'] != individual_chunk_dict['chunk_id']) or (updated_individual_chunk_dict['chunk_id'] != individual_chunk_id):
print(f"CONTROLLER: ERROR: the chunk_id returned from the encoder={updated_individual_chunk_dict['chunk_id']} in updated_individual_chunk_dict does not match both expected individual_chunk_dict chunk_id={individual_chunk_dict['chunk_id']} or loop's individual_chunk_id={individual_chunk_id}",flush=True)
sys.exit(1)
# poke the chunk updated by the encoder back into global ALL_CHUNKS ... it should contain snippet data now.
ALL_CHUNKS[str(individual_chunk_id)] = updated_individual_chunk_dict
return
###
def encode_chunk_using_vsipe_ffmpeg_to_h264(individual_chunk_id):
# encode an individual chunk using vspipe and ffmpeg
#
# using ChatGPT suggested method for non-blocking reads of subprocess stderr, stdout
global SETTINGS_DICT
global ALL_CHUNKS
global ALL_CHUNKS_COUNT
global ALL_CHUNKS_COUNT_OF_FILES
if SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE'] != SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE_H264']:
print(f"CONTROLLER: ERROR: encode_chunk_using_vsipe_ffmpeg_to_h264: INVALID CHUNK_INTERIM_ENCODE_TYPE '{SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE']}' specified; must be '{SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE_H264']}'",flush=True)
sys.exit(1)
slideshow_ENCODER_legacy_path = SETTINGS_DICT['slideshow_ENCODER_legacy_path']
individual_chunk_dict = ALL_CHUNKS[str(individual_chunk_id)]
chunk_json_filename = UTIL.fully_qualified_filename(individual_chunk_dict['chunk_fixed_json_filename']) # always the same fixed filename
proposed_h264_mkv_filename = UTIL.fully_qualified_filename(individual_chunk_dict['proposed_h264_mkv_filename']) # preset by find_all_chunks to: fixed filename plus a seqential 5-digit-zero-padded ending based on chunk_id + r'.mkv'
# remove any pre-existing files to be consumed and produced by the ENCODER
if os.path.exists(chunk_json_filename):
os.remove(chunk_json_filename)
if os.path.exists(proposed_h264_mkv_filename):
os.remove(proposed_h264_mkv_filename)
# create the fixed-filename chunk file consumed by the encoder; it contains the fixed-filename of the snippet file to produce
if UTIL.DEBUG: print(f"DEBUG: CONTROLLER: in encoder loop: attempting to create chunk_json_filename='{chunk_json_filename}' for encoder to consume.",flush=True)
try:
with open(chunk_json_filename, 'w') as fp:
json.dump(individual_chunk_dict, fp, indent=4)
except Exception as e:
print(f"CONTROLLER: ERROR: dumping current chunk to JSON file: '{chunk_json_filename}' for encoder, chunk_id={individual_chunk_id}, individual_chunk_dict=\n{UTIL.objPrettyPrint.pformat(individual_chunk_dict)}\n{str(e)}",flush=True,file=sys.stderr)
sys.exit(1)
print(f"CONTROLLER: Created fixed-filename chunk file for encoder to consume: for individual_chunk_id={individual_chunk_id} '{chunk_json_filename}' listing {ALL_CHUNKS[str(individual_chunk_id)]['num_files']} files, individual_chunk_dict=\n{UTIL.objPrettyPrint.pformat(individual_chunk_dict)}",flush=True)
# Define the commandlines for the subprocesses forming the ENCODER
if UTIL.DEBUG:
loglevel = r'verbose'
stats = r'-stats'
benchmark = r'-benchmark'
else:
loglevel = 'info'
stats = r'-stats'
benchmark = stats # a hack to workaround ffmpeg rejecting zero length string''
short_gop_size = int( 1 * SETTINGS_DICT['TARGET_FPS'] ) # 1 second worth of GOP @ the designated frametate
vspipe_commandline = [ UTIL.VSPIPE_EXE, '--progress', '--container', 'y4m', slideshow_ENCODER_legacy_path, '-' ]
ffmpeg_commandline_libx264 = [
UTIL.FFMPEG_EXE,
'-hide_banner',
'-loglevel', loglevel,
stats,
benchmark,
'-colorspace', 'bt709',
'-color_primaries', 'bt709',
'-color_trc', 'bt709',
'-color_range', 'pc',
'-threads', str(UTIL.NUM_THREADS_FOR_FFMPEG_DECODER),
'-f', 'yuv4mpegpipe',
'-i', 'pipe:',
#'-probesize', '200M',
#'-analyzeduration', '200M',
'-sws_flags', 'lanczos+accurate_rnd+full_chroma_int+full_chroma_inp',
'-filter_complex', 'format=yuv420p,setdar=16/9',
'-strict', 'experimental',
'-an',
'-threads', str(UTIL.NUM_THREADS_FOR_FFMPEG_ENCODER),
'-c:v', 'libx264',
'-preset', 'veryslow',
'-colorspace', 'bt709',
'-color_primaries', 'bt709',
'-color_trc', 'bt709',
'-color_range', 'pc',
'-forced-idr', '1', # If forcing keyframes, force them as IDR frames. (default false) 1=true
'-strict_gop', '1', # Set 1 to minimize GOP-to-GOP rate fluctuations (default false) 1=True
'-flags', '+cgop', # closed GOP
'-g', str(short_gop_size),
'-preset', 'veryslow',
'-refs', '3', # Set the number of reference frames to 3 SO THAT THE RESULTING MP4 IS TV COMPATIBLE !!! (it is 16 by default, which will not play on TVs)
#'-crf', '22', # use CRF so that we do not have to guess bitrates
'-b:v', SETTINGS_DICT['TARGET_VIDEO_BITRATE'], # 4.5M is ok (HQ) for h.264 1080p25 slideshow material; instead of crf 22
'-minrate:v', '500k', # a fixed minimum for TV compatibility
'-maxrate:v', '20M', # a fixed ceiling for TV compatibility
'-bufsize', '20M', # a fixed ceiling for TV compatibility
'-profile:v', 'high',
'-level', '5.1', # we are only 1080p so 5.1 is enough # H.264 Maximum supported bitrate: Level 5.1: 50 Mbps, Level 5.2: 62.5 Mbps
'-movflags', '+faststart+write_colr',
'-y', proposed_h264_mkv_filename,
]
ffmpeg_commandline_h264_nvenc = [ # h264_nvenc ... has parameters ONLY for use with an nvidia 2060plus or higher video encoding card
UTIL.FFMPEG_EXE,
'-hide_banner',
'-loglevel', loglevel,
stats,
benchmark,
'-colorspace', 'bt709',
'-color_primaries', 'bt709',
'-color_trc', 'bt709',
'-color_range', 'pc',
'-threads', str(UTIL.NUM_THREADS_FOR_FFMPEG_DECODER),
'-f', 'yuv4mpegpipe',
'-i', 'pipe:',
#'-probesize', '200M',
#'-analyzeduration', '200M',
'-sws_flags', 'lanczos+accurate_rnd+full_chroma_int+full_chroma_inp',
'-filter_complex', 'format=yuv420p,setdar=16/9',
'-strict', 'experimental',
'-an',
'-threads', str(UTIL.NUM_THREADS_FOR_FFMPEG_ENCODER),
'-c:v', 'h264_nvenc',
'-colorspace', 'bt709',
'-color_primaries', 'bt709',
'-color_trc', 'bt709',
'-color_range', 'pc',
'-pix_fmt', 'nv12',
'-preset', 'p7',
'-multipass', 'fullres',
'-forced-idr', '1', # If forcing keyframes, force them as IDR frames. (default false) 1=true
'-strict_gop', '1', # Set 1 to minimize GOP-to-GOP rate fluctuations (default false) 1=True
'-flags', '+cgop', # closed GOP
'-g', str(short_gop_size),
'-coder:v', 'cabac',
'-spatial-aq', '1',
'-temporal-aq', '1',
'-dpb_size', '0',
'-bf:v', '3',
'-b_ref_mode:v', '0',
# https://developer.nvidia.com/video-encode-and-decode-gpu-support-matrix-new
# https://www.reddit.com/r/ffmpeg/comments/xtl43y/hq_ffmpeg_encoding_with_gpu_nvenc_part_iii/
# NVIDIA Presets v2.0: https://developer.download.nvidia.com/video/gputechconf/gtc/2020/presentations/s21337-nvidia-video-technologies-video-codec-and-optical-flow-sdk.pdf
# P1 (highest performance) to P7 (highest quality) https://developer.nvidia.com/blog/introducing-video-codec-sdk-10-presets/
# Rate Control Mode: Constant QP, CBR, VBR
'-rc:v', 'vbr', # this is what the nvidia documentation says and ffmpeg exposes for nvidia PRESETS v2.0 https://developer.download.nvidia.com/video/gputechconf/gtc/2020/presentations/s21337-nvidia-video-technologies-video-codec-and-optical-flow-sdk.pdf
#
# ONE OR THE OTHER NOT BOTH OF THESE
#'-cq:v', '24', # for use with CQ -b:v 0 ... uses CRF so that we do not have to guess bitrates # circa double filesize of '-b:v', '5M' !! # Set target quality level (0 to 51, 0 means automatic) for constant quality mode in VBR rate control (from 0 to 51) (default 0)
#'-b:v', '0', # nominated CQ target bitrate see -cq:v 20 ... apparently this is REQUIRED for -cq to work
'-cq:v', '0', # for use with non-CQ -b:v 4M ... # Set target quality level (0 to 51, 0 means automatic) for constant quality mode in VBR rate control (from 0 to 51) (default 0)
'-b:v', SETTINGS_DICT['TARGET_VIDEO_BITRATE'], # 4.5M is ok (HQ) for h.264 1080p25 slideshow material ... nominated non-CQ target bitrate see -cq:v 0
#
'-tune', 'hq',
'-minrate:v', '500k', # a fixed minimum for TV compatibility
'-maxrate:v', '20M', # a fixed ceiling for TV compatibility
'-bufsize', '20M', # a fixed ceiling for TV compatibility
'-profile:v', 'high',
'-level', '5.1', # we are only 1080p so 5.1 is enough# H.264 Maximum supported bitrate: Level 5.1: 50 Mbps, Level 5.2: 62.5 Mbps
'-movflags', '+faststart+write_colr',
'-y', proposed_h264_mkv_filename,
]
ffmpeg_commandline = ffmpeg_commandline_libx264 # the default if nothing is in SETTINGS_DICT or it is not recognised
try:
if SETTINGS_DICT["FFMPEG_ENCODER"] == "libx264": ffmpeg_commandline = ffmpeg_commandline_libx264
if SETTINGS_DICT["FFMPEG_ENCODER"] == "h264_nvenc": ffmpeg_commandline = ffmpeg_commandline_h264_nvenc
except:
pass # ignore no key found exception for SETTINGS_DICT["FFMPEG_ENCODER"]
# Execute the command using subprocess.run but using a full string not a LIST ... also fix "-filter_complex" value which MUST have quotes around it
def command_list_to_command_string(command_list):
command_parts = []
for part in command_list:
#if part.startswith('-') or part.lower() == r'pipe:'.lower(): # Check if the part starts with a dash (indicating a switch)
# command_parts.append(part) # Add the part as is (switch)
#else:
# command_parts.append(f'"{part}"') # Enclose the part in double quotes
if part.startswith('format='.lower()) or (len(part) >= 2 and part[1] == r':'):
command_parts.append(f'"{part}"') # Enclose the part in double quotes
else:
command_parts.append(part) # Add the part as is
commandline = ' '.join(command_parts)
return commandline
vspipe_commandline = command_list_to_command_string(vspipe_commandline)
ffmpeg_commandline = command_list_to_command_string(ffmpeg_commandline)
vspipe_pipe_ffmpeg_commandline = vspipe_commandline + r' | ' + ffmpeg_commandline
print(f"CONTROLLER: START Running the ENCODER to produce interim encoded file ... using one commandline:\n{UTIL.objPrettyPrint.pformat(vspipe_pipe_ffmpeg_commandline)}\n",flush=True)
result = subprocess.run(vspipe_pipe_ffmpeg_commandline, shell=True)
if result.returncode != 0:
print(f"CONTROLLER: ERROR Running the ENCODER to produce interim encoded file ... Command execution failed with exit status: {result.returncode}",flush=True)
sys.exit(1)
time.sleep(2.0) # give it a chance (2 seconds, or 2000ms) to settle down
print(f"CONTROLLER: FINISHED Successfully Running the ENCODER with subprocess.run to produce interim encoded file ... using one commandline:\n{UTIL.objPrettyPrint.pformat(vspipe_pipe_ffmpeg_commandline)}",flush=True)
if not os.path.exists(chunk_json_filename):
print(f"CONTROLLER: ERROR: CONTROLLER: encoder-updated current chunk to JSON file file not found '{chunk_json_filename}' not found !",flush=True)
sys.exit(1)
if not os.path.exists(proposed_h264_mkv_filename):
print(f"CONTROLLER: ERROR: CONTROLLER: encoder-produced .mkv video file not found '{proposed_h264_mkv_filename}' not found !",flush=True)
sys.exit(1)
# Now the encoder has encoded a chunk and produced an updated chunk file and an h264 encoded video .mkv
# ... we must import updated chunk file (which will include a new snippet_list) check the chunk, and update the ALL_CHUNKS dict with updated chunk data
# The format of the snippet_list produced by the encoder into the updated chunk JSON file is defined above.
if UTIL.DEBUG: print(f"DEBUG: CONTROLLER: in encoder loop: attempting to load chunk_json_filename={chunk_json_filename} produced by the encoder.",flush=True)
try:
with open(chunk_json_filename, 'r') as fp:
updated_individual_chunk_dict = json.load(fp)
except Exception as e:
print(f"CONTROLLER: ERROR: CONTROLLER: loading updated current chunk from JSON file: '{chunk_json_filename}' from encoder, chunk_id={individual_chunk_id}, related to individual_chunk_dict=\nUTIL.objPrettyPrint.pformat(individual_chunk_dict)\n{str(e)}",flush=True,file=sys.stderr)
sys.exit(1)
print(f"CONTROLLER: Loaded updated current chunk from ENCODER-updated JSON file: '{chunk_json_filename}'",flush=True)
if DEBUG: print(f"CONTROLLER: DEBUG: the chunk_id returned from the encoder={updated_individual_chunk_dict['chunk_id']} whereas local encode was specified as individual_chunk_id={individual_chunk_id}",flush=True)
if (updated_individual_chunk_dict['chunk_id'] != individual_chunk_dict['chunk_id']) or (updated_individual_chunk_dict['chunk_id'] != individual_chunk_id):
print(f"CONTROLLER: ERROR: the chunk_id returned from the encoder={updated_individual_chunk_dict['chunk_id']} in updated_individual_chunk_dict does not match both expected individual_chunk_dict chunk_id={individual_chunk_dict['chunk_id']} or loop's individual_chunk_id={individual_chunk_id}",flush=True)
sys.exit(1)
# poke the chunk updated by the encoder back into global ALL_CHUNKS ... it should contain snippet data now.
ALL_CHUNKS[str(individual_chunk_id)] = updated_individual_chunk_dict
return
###
def final_concat_transcode_interim_ffv1():
if SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE'] != SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE_FFV1']:
print(f"CONTROLLER: ERROR: final_concat_transcode_interim_ffv1: INVALID CHUNK_INTERIM_ENCODE_TYPE '{SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE']}' specified; must be '{SETTINGS_DICT['CHUNK_INTERIM_ENCODE_TYPE_FFV1']}'",flush=True)
sys.exit(1)
print(f"{100*'-'}",flush=True)
print(f'CONTROLLER: STARTING CONCATENATE/TRANSCODE INTERIM {SETTINGS_DICT["CHUNK_INTERIM_ENCODE_TYPE"]} VIDEO FILES INTO ONE VIDEO MP4 AND AT SAME TIME MUX WITH BACKGROUND AUDIO',flush=True)
# create the video-concat input file for ffmpeg, listing all of the FFV1 files to be concatenated/transcoded
temporary_ffmpeg_concat_list_filename = SETTINGS_DICT['TEMPORARY_FFMPEG_CONCAT_LIST_FILENAME']
with open(temporary_ffmpeg_concat_list_filename, 'w') as fp:
for individual_chunk_id in range(0,ALL_CHUNKS_COUNT): # 0 to (ALL_CHUNKS_COUNT - 1)
ffv1_filename = ALL_CHUNKS[str(individual_chunk_id)]['proposed_ffv1_mkv_filename']
fp.write(f"file '{ffv1_filename}'\n")
fp.flush()
#end for
#end with
# We now have
# temporary_ffmpeg_concat_list_filename the concat list of videos to be concatenated and transcoded
# background_audio_with_overlayed_snippets_filename the background audio with video snippets audio overlayed onto it the final format we need
# Lets transcode/mux them together.
if UTIL.DEBUG:
loglevel = r'verbose'
stats = r'-stats'
benchmark = r'-benchmark'
else:
loglevel = 'info'
stats = r'-stats'
benchmark = stats # a hack to workaround ffmpeg rejecting zero length string ''
short_gop_size = int( 1 * SETTINGS_DICT['TARGET_FPS'] ) # 1 second worth of GOP @ the designated frametate
final_mp4_with_audio_filename = SETTINGS_DICT['FINAL_MP4_WITH_AUDIO_FILENAME']
ffmpeg_commandline_libx264 = [
UTIL.FFMPEG_EXE,
'-hide_banner',
'-loglevel', loglevel,
stats,
benchmark,
#'-colorspace', 'bt709',
#'-color_primaries', 'bt709',
#'-color_trc', 'bt709',
#'-color_range', 'pc',
'-threads', str(UTIL.NUM_THREADS_FOR_FFMPEG_DECODER),
'-i', background_audio_with_overlayed_snippets_filename,
'-f', 'concat', '-safe', '0', '-i', temporary_ffmpeg_concat_list_filename,
'-sws_flags', 'lanczos+accurate_rnd+full_chroma_int+full_chroma_inp',
'-filter_complex', 'format=yuv420p,setdar=16/9',
'-strict', 'experimental',
'-c:a', 'copy',