-
Notifications
You must be signed in to change notification settings - Fork 1
/
allvideoconverter.py
1277 lines (1192 loc) · 56.8 KB
/
allvideoconverter.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
#!/usr/bin/python3
import os
import platform
import re
import shutil
import subprocess
import fnmatch
class converter:
def __init__(self, extension_convert: str = "mkv", resolution: int = 480, codec: str = "h264_omx", fps: int = 24,
crf: int = 20, preset: str = "slow", hwaccel="", threads=0, log_level=32, input_folder="./",
output_folder="./convert/", resize_log="./resize.log", resized_log="./resized.log",sort_size=False,ignore_resized_log=False,ignore_resize_log=False,custom_exec="",custom_ffprobe=""):
"""
class to convert huge amount of video files to same size,codec and other parameters specified below
:param extension_convert: extension used in output file
:param resolution: output resolution
:param codec:output codec,default will be h264,works in raspberry pi as default
:param fps:output fps,only tested with values below input file fps,not recomended use fps bigger than input file
:param crf: Constant Rate Factor,defined in some codecs of ffmpeg, as said in:https://trac.ffmpeg.org/wiki/Encode/H.264
:param preset: preset for ffmpeg to use with some codecs,as specified in: https://trac.ffmpeg.org/wiki/Encode/H.264
:param hwaccel: hardware acceleration,as defined in: https://trac.ffmpeg.org/wiki/HWAccelIntro
:param threads: number of threads used in ffmpeg when in cpu mode
:param log_level: log level of ffmpeg as specified in: https://ffmpeg.org/ffmpeg.html#Generic-options
:param input_folder: folder where will gather the video files
:param output_folder: folder where the output files will be saved
:param resize_log: the log file that informs all the converted files that ends correctly,if deleted or moved from the informed dir,it will restart all the conversion process
:param resized_log: the redundant log file that informs all the converted files that ends correctly,if deleted or moved from the informed dir,it will restart all the conversion process
"""
self.resized_log = resized_log
self.ignore_resized_log=ignore_resized_log
self.resize_log = resize_log
self.ignore_resize_log = ignore_resize_log
self.input_folder = input_folder
self.output_folder = output_folder
if not os.path.isdir(self.output_folder):
os.mkdir(self.output_folder)
self.codec = codec
self.extension_convert = str(extension_convert).lower()
self.resolution = int(resolution)
self.fps = int(fps)
self.crf = int(crf)
self.preset = preset
self.hwaccel = hwaccel
if threads !=0:
self.threads = int(threads)
else:
self.threads =int(os.cpu_count())
self.log_level = str(log_level)
self.dir_data = self.list_content_folder(self.input_folder)
self.files = []
self.fill_files_list()
if sort_size:
self.files.sort(key=lambda x: x.stat().st_size, reverse=False)
self.results = {}
if platform.system() == 'Windows':
self.so_folder_separator = "\\"
self.ffmpeg_executable = "ffmpeg.exe"
if codec == "h264_omx":
self.codec = "h264"
elif platform.system() == 'Linux':
self.so_folder_separator = "/"
self.ffmpeg_executable = "ffmpeg"
if "arm" not in str(platform.machine()) and codec == "h264_omx":
self.codec = "h264"
if custom_exec != "":
self.ffmpeg_executable=custom_exec
if custom_ffprobe != "":
self.ffprobe_executable=custom_exec
else:
self.ffprobe_executable="ffprobe"
# obligate de folder address ends with the so separator
if str(self.input_folder[-1]) != self.so_folder_separator:
self.input_folder = self.input_folder + self.so_folder_separator
if str(self.output_folder[-1]) != self.so_folder_separator:
self.output_folder = self.output_folder + self.so_folder_separator
@staticmethod
def treat_console_out(console_popen: subprocess.Popen):
"""
this treat the console output to show better output
:param console_popen: subprocess.popen process where the console log will be get
:return: return in case of error,the error code from ffmpeg
"""
number_of_frames = 0
error = 0
line_nun = 0
for line in iter(console_popen.stdout.readline, b''):
if "frame" in line and "fps" in line and "q" in line and "size" in line and "time" in line and "bitrate" in line and "speed" in line:
'''frame= 2246 fps=748 q=15.0 size= 23365kB time=00:01:33.97 bitrate=2036.9kbits/s speed=31.3x'''
# print(number_of_frames)
search = None
try:
search = re.search(
r'frame=\D*(\d*)\D*fps=\D*(\d*)\D*q=\D*([\d.]*)\D*size=\D*(\d*)\D*time=\D*(.*?)bitrate=\D*([\d.kmg]*)*bits/s\D*speed=\D*([\d.]*)\D*',
line)
try:
frame = int(search.group(1))
except:
frame = int(re.search(r'frame=\D*(\d+)\D*=', line).group(1))
try:
fps = int(search.group(2))
except:
fps = int(re.search(r'fps=\D*(\d+)\D*=', line).group(1))
try:
quality = float(search.group(3))
except:
quality = float(re.search(r'q=\D*([\d.]+)\D*=', line).group(1))
try:
size = int(search.group(4))
except:
size = int(re.search(r'size=\D*(\d+)\D*=', line).group(1))
try:
time = search.group(5)
except:
time = re.search(r'time=\D*(.*?)bitrate', line).group(1)
try:
bitrate = search.group(6)
except:
bitrate = re.search(r'bitrate=\D*([\d.kmg]+)*bits/s\D*=', line).group(1)
try:
speed = float(search.group(7))
except:
speed = float(re.search(r'speed=\D*([\d.]*)\D*', line).group(1))
conclusion = int((number_of_frames * int(frame)) / 100)
try:
eta = int((number_of_frames - int(frame)) / int(fps))
except:
eta = "nan"
out = str(str(line_nun) + str(conclusion) + " eta=" + str(eta) + "s quality=" + str(
quality) + " speed=" + str(speed))
print(out, end="\r")
except:
print(str(search), end="\r")
elif "NUMBER_OF_FRAMES" in line:
# print(str(line_nun)+str(line.rstrip()))
frames = re.search(r'NUMBER_OF_FRAMES\D*(\d+).*', line)
try:
number_of_frames = int(frames.group(1))
except:
pass
elif "Error" in line:
error += 1
print(str(error) + line)
elif "Conversion failed" in line:
console_popen.kill()
return 1
elif len(line) > 0 and line[-1] != "\r":
if line[-1] == "\n":
print(str(line[:-1]))
else:
print(str(line))
pass
# line_nun+=1
@staticmethod
def line_in_file(File: os.DirEntry,line:str):
_file=open(File.path,"r")
list_file=_file.readlines()
_file.close()
if line in list_file:
return True
else:
return False
def path_to_direntry(self,path:str):
log_file_name_dir=""
if len(path.split(self.so_folder_separator)) > 1:
for x in path.split(self.so_folder_separator)[:-1]:
log_file_name_dir = str(log_file_name_dir + str(x) + self.so_folder_separator)
else:
log_file_name_dir = str(log_file_name_dir + str(".") + self.so_folder_separator)
log_file=path.split(self.so_folder_separator)[-1]
for file in os.scandir(log_file_name_dir):
if file.name == log_file:
return file
open(path,"a").close()
for file in os.scandir(log_file_name_dir):
if file.name == log_file:
return file
return path
def fill_files_list(self, folders=""):
"""
search the media files in folder ,used also to update if any change happend
:param folders: folder where will be searched,works with more than just the input folder
"""
fileExtensions = ["webm", "flv", "vob", "ogg", "ogv", "drc", "gifv", "mng", "avi", "mov", "qt", "wmv", "yuv",
"rm", "rmvb", "asf", "amv", "mp4", "m4v", "mp\*", "m\?v", "svi", "3gp", "flv", "f4v", "mkv",
"divx"]
if folders == "":
folders = self.dir_data["dirs"]
for file in self.dir_data["files"]:
if str(os.path.splitext(file.name)[1][1:]).lower() in fileExtensions:
self.files.append(file)
for folder in folders:
dir_data = self.list_content_folder(folder.path)
for file in dir_data["files"]:
if os.path.splitext(file.name)[1][1:] in fileExtensions:
self.files.append(file)
self.fill_files_list(folders=dir_data["dirs"])
@staticmethod
def list_content_folder(path):
"""
get the folders and files in some path as an dict
:param path: folder where the data will be gether
:return: dict with the dirs and files
"""
retorno = {}
retorno["starting_path"] = path
retorno["dirs"] = []
retorno["files"] = []
for entry in os.scandir(path):
if entry.is_file():
retorno["files"].append(entry)
if entry.is_dir():
retorno["dirs"].append(entry)
return retorno
def get_video_time(self,File):
"""
get the time in seconds from the select file
:param File: media file that will be analyzed
:return: time in seconds,return a float value,so it is very precize
"""
check_dim = str(str(self.ffprobe_executable)+" -v quiet -show_entries format=duration -of default=noprint_wrappers=1:nokey=1").split(
" ")
if isinstance(File, str):
check_dim.append('"'+File+'"')
if isinstance(File, os.DirEntry):
check_dim.append('"'+str(File.path)+'"')
else:
return ""
dim = subprocess.run(' '.join(check_dim), stdout=subprocess.PIPE, shell=True)
if dim.returncode != 0:
return "error", int(dim.returncode)
dim = re.compile("\'(.*)\\\\").findall(str(dim.stdout))[0]
# print(dim)
return float(dim)
def get_video_codec(self,File: os.DirEntry, stream: int = 0):
"""
get the video codec from the media file
:param File: media file that will be analyzed
:param stream: stream wich will be extracted
:return: the codec from the video selected
"""
check_dim = str(
str(self.ffprobe_executable)+" -v quiet -select_streams v:" + str(
stream) + " -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1").split(
" ")
check_dim.append('"'+str(File.path)+'"')
dim = subprocess.run(' '.join(check_dim), stdout=subprocess.PIPE, shell=True)
if dim.returncode != 0:
return "error", int(dim.returncode)
dim = re.compile("\'(.*)\\\\").findall(str(dim.stdout))[0]
# print(dim)
return str(dim)
def get_audio_codec(self,File: os.DirEntry, stream: int = 0):
"""
get the audio codec from selected stream in file
:param File:media file that will be analyzed
:param stream: stream wich will be extracted
:return: the codec from the video selected
"""
check_dim = str(str(self.ffprobe_executable)+" -v quiet -select_streams a:" + str(
stream) + " -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1").split(
" ")
check_dim.append('"'+str(File.path)+'"')
dim = subprocess.run(' '.join(check_dim), stdout=subprocess.PIPE, shell=True)
if dim.returncode != 0:
return "error", int(dim.returncode)
dim = re.compile("\'(.*)\\\\").findall(str(dim.stdout))[0]
# print(dim)
return str(dim)
def get_subtitle_codec(self,File: os.DirEntry, stream: int = 0):
"""
get the subtitle codec from selected stream in file
:param File:media file that will be analyzed
:param stream: stream wich will be extracted
:return: the codec from the subtitle selected
"""
check_dim = str(str(self.ffprobe_executable)+" -v quiet -select_streams s:" + str(
stream) + " -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1").split(" ")
check_dim.append('"'+str(File.path)+'"')
dim = subprocess.run(' '.join(check_dim), stdout=subprocess.PIPE, shell=True)
if dim.returncode != 0:
return "error", int(dim.returncode)
dim = re.compile("\'(.*)\\\\").findall(str(dim.stdout))[0]
# print(dim)
return str(dim)
def get_resolution(self,File: os.DirEntry, stream: int = 0):
"""
get the resolution from selected stream in file
:param File:media file that will be analyzed
:param stream: stream wich will be extracted
:return: the resolution from the subtitle selected in an dict {x:,y:}
"""
check_dim = str(
str(self.ffprobe_executable)+" -v quiet -select_streams v:" + str(
stream) + " -show_entries stream=width,height -of csv=p=0").split(
" ")
check_dim.append('"'+str(File.path)+'"')
dim = subprocess.run(' '.join(check_dim), stdout=subprocess.PIPE, shell=True)
if dim.returncode != 0:
return "error", int(dim.returncode)
dim = re.compile("(\d+,\d+)").findall(str(dim.stdout))[0].split(",")
# print(dim)
dim = {"x": int(dim[0]), "y": int(dim[1])}
return dim
def get_fps(self,File: os.DirEntry, stream: int = 0):
"""
get the resolution from selected stream in file
:param File:media file that will be analyzed
:param stream: stream wich will be extracted
:return: the resolution from the subtitle selected in an dict {x:,y:}
"""
check_fps = str(
str(self.ffprobe_executable)+" -v quiet -select_streams v:" + str(
stream) + " -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate").split(
" ")
check_fps.append('"'+str(File.path)+'"')
dim = subprocess.run(' '.join(check_fps), stdout=subprocess.PIPE, shell=True)
if dim.returncode != 0:
return "error", int(dim.returncode)
dim = re.compile("(\d+/\d+)").findall(str(dim.stdout))[0].split("/")
dim = int(dim[0]) / int(dim[1])
return dim
def get_aspet_ratio(self,File:os.DirEntry,stream:int=0):
str(self.ffprobe_executable)+" -v error -select_streams v:0 -show_entries stream=display_aspect_ratio -of json=c=1"
check_dim = str(
str(self.ffprobe_executable)+" -v quiet -select_streams v:" + str(
stream) + " -show_entries stream=display_aspect_ratio -of csv=p=0").split(
" ")
check_dim.append('"'+str(File.path)+'"')
dim = subprocess.run(' '.join(check_dim), stdout=subprocess.PIPE, shell=True)
if dim.returncode != 0:
return "error", int(dim.returncode)
dim = re.compile("(\d+:\d+)").findall(str(dim.stdout))[0].split(":")
# print(dim)
dim = {"x": int(dim[0]), "y": int(dim[1])}
return dim
def new_resolution(self,File:os.DirEntry,height,stream:int=0):
res=self.get_resolution(File,stream)
if isinstance(res,tuple):
return res
final_width=(res["x"]/res["y"])*height
return int(final_width)
@staticmethod
def command_to_array(command, aditional_data: [] = [], previous_array: [] = []):
"""
convert the string to an array of words to use in a subprocess call
:param command: string wich will be turned into array
:param aditional_data: array with posterior parameters in array form to sum them to the result array,will be inserted after the string
:param previous_array: array with previous parameters in array form to sum them to the result array,will be inserted before the string
:return:
"""
result = previous_array
for command in command.split(" "):
result.append(command)
for aditional in aditional_data:
result.append(aditional)
return result
def compare_resolution(self,File: os.DirEntry, width: int, height: int, stream: int = 0):
"""
verify if the file resolution is smaller than the parsed values
:param File:
:param width:
:param height:
:return:
"""
check_dim = str(
str(self.ffprobe_executable)+" -v quiet -select_streams v:" + str(
stream) + " -show_entries stream=width,height -of csv=p=0").split(
" ")
check_dim.append('"'+str(File.path)+'"')
dim = subprocess.run(' '.join(check_dim), stdout=subprocess.PIPE, shell=True)
if dim.returncode != 0:
return "error", int(dim.returncode)
dim = re.compile("(\d+,\d+)").findall(str(dim.stdout))[0].split(",")
# print(dim)
if int(dim[0]) > width or int(dim[1]) > height:
return True
else:
return False
def compare_if_file_already_converted_successfully(self,File1: os.DirEntry,File2:str):
if not os.path.exists(File2):
return False
elif self.get_video_time(File2) == "" :
return False
elif self.get_video_time(File1) != self.get_video_time(File2):
return False
else:
return True
def compare_fps(self,File: os.DirEntry, fps, stream: int = 0):
"""
:param File:
:param fps:
:return:
"""
check_fps = str(
str(self.ffprobe_executable)+" -v quiet -select_streams v:" + str(
stream) + " -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate").split(
" ")
check_fps.append('"'+str(File.path)+'"')
dim = subprocess.run(' '.join(check_fps), stdout=subprocess.PIPE, shell=True)
if dim.returncode != 0:
return "error", int(dim.returncode)
dim = re.compile("(\d+/\d+)").findall(str(dim.stdout))[0].split("/")
dim = int(dim[0]) / int(dim[1])
if dim > fps:
return True
return False
def find_files(self, pattern,folder=""):
"""
used to find specific files with text pattern in defined folder,if none folder is defined uses input folder
:param pattern: regex pattern used to search files
:param folder: folder where the files will be searched
:return: an array of files that match the pattern
"""
'''Return list of files matching pattern in base folder.'''
if folder=="":
folder=self.input_folder
return [n for n in fnmatch.filter(os.listdir(folder), pattern) if
os.path.isfile(os.path.join(folder, n))]
def treat_file_name(self, File: os.DirEntry, current_dir=False, no_hierarchy=False, remove=False, debug=False):
"""
:param File:
:param current_dir:
:param no_hierarchy:
:param remove:
:param debug:
:return:
"""
fileExtensions = ["webm", "flv", "vob", "ogg", "ogv", "drc", "gifv", "mng", "avi", "mov", "qt", "wmv", "yuv",
"rm", "rmvb", "asf", "amv", "mp4", "m4v", "mp\*", "m\?v", "svi", "3gp", "flv", "f4v", "mkv",
"divx"]
file_name_no_extension = os.path.splitext(File.name)[0]
extension = str(os.path.splitext(File.name)[1][1:])
new_file_name = None
folder_to_create = None
log_file_name = None
if (extension.lower() or self.extension_convert.lower()) in fileExtensions:
convertable = True
##create relative dir
relative_dir = ""
if len(File.path.split(self.so_folder_separator)) > 1:
for x in File.path.split(self.so_folder_separator)[:-1]:
relative_dir = str(relative_dir + str(x) + self.so_folder_separator)
else:
relative_dir = str(relative_dir + str(".") + self.so_folder_separator)
if relative_dir[-1] != self.so_folder_separator:
relative_dir = relative_dir + self.so_folder_separator
relative_dir = relative_dir.replace(self.input_folder, "")
if relative_dir != "":
if relative_dir[0] == self.so_folder_separator:
relative_dir = relative_dir[1:]
# print("relative_dir")
##make the specificities from combination of parameters of current_dir and no_hierarchy
if not current_dir and not no_hierarchy:
## this works when will not save into current dir and will respect the folder hierarchy from imput folder
# print("no current dir and hierarchy")
new_file_name = str(str(self.output_folder) + str(relative_dir) + str(file_name_no_extension))
folder_to_create = str(self.output_folder) + str(relative_dir)
if self.resized_log[:2] == "./":
log_file_name = str(self.output_folder) + self.resized_log[2:]
elif not current_dir and no_hierarchy:
## this works when will not save into current dir and will not respect the folder hierarchy from imput folder
# print("no current dir and no hierarchy")
new_file_name = str(str(self.output_folder) + str(file_name_no_extension))
folder_to_create = str(self.output_folder)
if self.resized_log[:2] == "./":
log_file_name = str(self.output_folder) + self.resized_log[2:]
elif current_dir and not no_hierarchy:
## this works when will save into current dir and will respect the folder hierarchy from imput folder
# print("current dir and hierarchy")
# relative_dir = relative_dir.replace(self.input_folder, "")
if str(os.getcwd()[-1]) != self.so_folder_separator:
new_file_name = str(
str(os.getcwd()) + self.so_folder_separator + str(relative_dir) + str(file_name_no_extension))
else:
new_file_name = str(
str(os.getcwd()) + str(relative_dir) + str(file_name_no_extension))
folder_to_create = str(relative_dir)
log_file_name = self.resized_log
elif current_dir and no_hierarchy:
## this works when will save into current dir and will not respect the folder hierarchy from imput folder
print("current dir and no hierarchy")
# relative_dir = relative_dir.replace(self.input_folder, "")
new_file_name = str(os.getcwd()) + str(file_name_no_extension)
folder_to_create = ""
log_file_name = self.resized_log
else:
return ""
# print(folder_to_create)
if extension == self.extension_convert :
same_extension = True
else:
same_extension = False
if same_extension and not remove and (self.input_folder == self.output_folder or (os.getcwd() == self.input_folder and current_dir )):
new_file_name = new_file_name + ".convert"
#if debug:
# new_file_name = str(
# new_file_name + "." + str(self.codec) + "." + str(self.crf) + "." + str(self.preset) + "." + str(
# self.codec) + "." + str(self.resolution) + "." + str(self.fps))
new_file_name = new_file_name + "." + self.extension_convert
try:
if not self.ignore_resized_log:
log_file = open(log_file_name)
converted = False
# log_file=open(working_log,"r")
# for line in log_file.readlines():
# if line == new_file_name+"\n":
# log_file.close()
# print("working")
# return 0
# log_file.close()
for line in log_file.readlines():
if new_file_name in line:
converted = True
break
log_file.close()
if not self.ignore_resize_log:
log_file = open(self.resize_log)
for line in log_file.readlines():
if File.path in line:
converted = True
break
log_file.close()
except:
converted = False
else:
convertable = False
new_file_name = ""
folder_to_create = ""
same_extension = False
converted = False
relative_dir = ""
if converted and remove:
os.remove(File)
log_file_name=self.path_to_direntry(log_file_name)
return {"file_name_no_extension": file_name_no_extension, "extension": extension, "convertable": convertable,
"relative_dir": relative_dir, "new_file_name": new_file_name, "same_extension": same_extension,
"folder_to_create": folder_to_create, "converted": converted, "log_file_name": log_file_name}
def log_error_files(self, error_log_file="./error.log"):
"""
:param error_log_file:
:return:
"""
file = open(error_log_file, "a")
for key in self.results.keys():
if key != 0:
for error in self.results[key]:
file.write(str(key) + ";" + str(error.path) + "\n")
file.close()
def create_command(self, File: os.DirEntry, array=False, resize=False, current_dir=False, no_hierarchy=False,
force_change_fps=False, debug=False,output_name="",force_resize=False,not_overwrite=False):
"""
:param File:
:param array:
:param resize:
:param current_dir:
:param no_hierarchy:
:param force_change_fps:
:param debug:
:return:
"""
subtitle_srt = ["srt", "ass", "ssa"]
subtitle_bitmap = ["dvdsub", "dvd_subtitle", "pgssub", "hdmv_pgs_subtitle"]
name_data = self.treat_file_name(File, current_dir=current_dir, no_hierarchy=no_hierarchy, debug=debug)
# print(name_data)
if name_data=="":
return 0
converted=False
if not name_data["convertable"] or (name_data["converted"] and not debug) or (
name_data["extension"] == self.extension_convert and not resize):
return ""
# if os.path.isfile(name_data["new_file_name"]) and current_dir==True or os.path.isfile(str(name_data["relative_dir"]) + str(
# name_data["file_name_no_extension"]) + "." + self.extension_convert) and current_dir==False :
# return ""
if output_name!="":
if not self.ignore_resized_log:
try:
log_file = open(self.resized_log,"r+")
for line in log_file.readlines():
if output_name in line:
converted = True
break
log_file.close()
except:
pass
if converted:
return ""
command = [self.ffmpeg_executable]
if not not_overwrite:
command.append("-y")
else:
command.append("-n")
if self.hwaccel != "":
command.append("-hwaccel")
command.append(self.hwaccel)
command = command + ["-hide_banner", "-loglevel", self.log_level, "-i"]
if array:
comand.append( str(File.path))
else:
command.append('"'+str(File.path)+'"')
# subtitles_avaliable=self.find_files(name_data["file_name_no_extension"]+"*.srt")
# if len(subtitles_avaliable)>0:
# for i in subtitles_avaliable:
# command.append("-i")
# command.append(str(self.input_folder)+self.so_folder_separator+str(i))
command = command + ["-acodec", "aac", "-c:v",
self.codec, "-map_metadata", "0","-map", "0:v:?" , "-map", "0:a:?" , "-map","0:s:?", "-pix_fmt", "yuv420p",
"-threads", str(self.threads), "-copy_unknown"]
try:
subtitle_codec = self.get_subtitle_codec(File, 0)
if isinstance(subtitle_codec, int):
return ""
elif subtitle_codec in subtitle_srt:
command.append("-scodec")
command.append("srt")
except:
pass
if self.hwaccel == "qsv":
command.append("-load_plugin")
command.append("hevc_hw")
command.append("-max_muxing_queue_size")
command.append("1024")
resize_command = []
# if used crf the resize parameters of bitrate are not necessary
# but accord to official youtube data the bitrate used in resize parameters are
# the best for any situation
change_fps = False
if not resize:
if debug:
print("not resize")
resize_command.append("-crf")
resize_command.append(str(self.crf))
resize_command.append("-preset")
resize_command.append(self.preset)
else:
if debug:
print("resize")
if not name_data["converted"]:
small = False
out_scale = False
if self.resolution == 240:
if debug:
print("240p")
resize_command.append("-b:v")
resize_command.append("500k")
resize_command.append("-minrate")
resize_command.append("300k")
resize_command.append("-maxrate")
resize_command.append("700k")
compared_res = self.compare_resolution(File, 426, 240)
if (not isinstance(compared_res, tuple) and compared_res) or force_resize:
resize_command.append("-s")
dim=str(self.new_resolution(File,240))+"x240"
resize_command.append(dim) # determina a escala do arquivo para 480p
else:
small = True
elif self.resolution == 360:
if debug:
print("360p")
resize_command.append("-b:v")
resize_command.append("600k")
resize_command.append("-minrate")
resize_command.append("400k")
resize_command.append("-maxrate")
resize_command.append("1000k")
compared_res = self.compare_resolution(File, 640, 320)
if (not isinstance(compared_res, tuple) and compared_res) or force_resize:
resize_command.append("-s")
dim=str(self.new_resolution(File,320))+"x320"
resize_command.append(dim) # determina a escala do arquivo para 480p
else:
small = True
elif self.resolution == 480:
if debug:
print("480p")
resize_command.append("-b:v")
resize_command.append("1000k")
resize_command.append("-minrate")
resize_command.append("500k")
resize_command.append("-maxrate")
resize_command.append("1500k")
compared_res = self.compare_resolution(File, 854, 480)
if (not isinstance(compared_res, tuple) and compared_res) or force_resize:
resize_command.append("-s")
dim=str(self.new_resolution(File,480))+"x480"
resize_command.append(dim) # determina a escala do arquivo para 480p
else:
small = True
elif self.resolution == 720:
if debug:
print("720p")
resize_command.append("-b:v")
resize_command.append("2500k")
resize_command.append("-minrate")
resize_command.append("1200k")
resize_command.append("-maxrate")
resize_command.append("4000k")
compared_res = self.compare_resolution(File, 1280, 720)
if (not isinstance(compared_res, tuple) and compared_res) or force_resize:
resize_command.append("-s")
dim=str(self.new_resolution(File,720))+"x720"
resize_command.append(dim) # determina a escala do arquivo para 720p
else:
small = True
elif self.resolution == 1080:
if debug:
print("1080p")
resize_command.append("-b:v")
resize_command.append("4500k")
resize_command.append("-minrate")
resize_command.append("3000k")
resize_command.append("-maxrate")
resize_command.append("6000k")
compared_res = self.compare_resolution(File, 1920, 1080)
if (not isinstance(compared_res, tuple) and compared_res) or force_resize:
resize_command.append("-s")
dim=str(self.new_resolution(File,1080))+"x1080"
resize_command.append(dim) # determina a escala do arquivo para 1080p
else:
small = True
else:
out_scale = True
for c in resize_command:
command.append(c)
if not small and not out_scale:
if self.compare_fps(File, self.fps):
command.append("-filter:v")
command.append("fps=fps=" + str(self.fps))
change_fps = True
for i in resize_command:
command.append(i)
elif out_scale:
return ""
else:
return ""
if force_change_fps and not change_fps:
command.append("-filter:v")
command.append("fps=fps=" + str(self.fps))
compared_codec = self.get_video_codec(File)
if isinstance(compared_codec, int):
return ""
elif compared_codec == "mpeg4" and "h265" in self.codec:
command.append("-bsf:v")
command.append("mpeg4_unpack_bframes")
# command.append(str(name_data["new_file_name"])+".tmp")
# if len(subtitles_avaliable)>0:
# command.append("-c:s")
# command.append("mov_text")
# for i in range(0,len(subtitles_avaliable)+1):
# command.append("-map")
# command.append(i)
# print(command)
if array:
if output_name=="":
command.append(str(name_data["new_file_name"]))
else:
command.append(str(output_name))
return command
else:
if output_name=="":
command.append('"'+str(name_data["new_file_name"])+'"')
else:
command.append('"'+str(output_name)+'"')
retorno = ""
for comand in command:
retorno += comand + " "
return retorno
def convert_video(self, File: os.DirEntry, resize=False, remove=False, process=False, current_dir=False,
no_hierarchy=False,
force_change_fps=False, debug=False, working_log="./working.log",output_name="", force_resize=False,not_overwrite=False):
"""
:param File:
:param resize:
:param remove:
:param process:
:param current_dir:
:param no_hierarchy:
:param force_change_fps:
:param debug:
:param working_log:
:return:
"""
name_data = self.treat_file_name(File, current_dir=current_dir, no_hierarchy=no_hierarchy,
remove=remove,debug=debug)
if name_data == "":
return 0
if not name_data["convertable"] or (name_data["converted"] and not debug) or (
name_data["extension"] == self.extension_convert and not resize):
return 0
try:
os.makedirs(name_data["folder_to_create"])
except:
pass
command = self.create_command(File, array=False, resize=resize, current_dir=current_dir, debug=debug,
force_change_fps=force_change_fps, no_hierarchy=no_hierarchy,output_name=output_name,force_resize=force_resize,not_overwrite=not_overwrite)
if command == "" or (name_data["converted"] and not debug) :
return 0
elif self.compare_if_file_already_converted_successfully(File,command[-1]):
return 0
if not process:
if debug:
print(command)
os.chdir(self.output_folder)
result = subprocess.run(command, stdout=subprocess.PIPE, shell=True)
if result.returncode == 0 and output_name != "":
if debug:
print("selected output file ,done with success")
if not self.ignore_resized_log:
while self.line_in_file(name_data["log_file_name"].path,str(output_name + "\n")):
log_file = open(name_data["log_file_name"].path, "a")
log_file.write(output_name + "\n")
log_file.close()
elif resize and result.returncode == 0 and output_name != "":
if debug:
print("selected output file resize ,done with success")
if not self.ignore_resized_log:
while self.line_in_file(name_data["log_file_name"].path,str(output_name + "\n")):
log_file = open(name_data["log_file_name"].path, "a+")
log_file.write(output_name + "\n")
log_file.close()
elif resize and result.returncode == 0:
if debug:
print("automatic output file ,done with success")
'''
# log_file=open(working_log,"a")
# tmp_log_file=open(str(working_log)+".tmp","a")
# for line in log_file.readlines():
# if line == new_file_name+"\n":
# pass
# else:
# tmp_log_file.write(line)
# log_file.close()
# tmp_log_file.close()
# shutil.move(str(working_log)+".tmp",working_log)
# os.rename(name_data["new_file_name"]+".tmp",name_data["new_file_name"])
'''
if debug:
print(name_data["log_file_name"].path)
if not self.ignore_resized_log:
while self.line_in_file(name_data["log_file_name"],str(name_data["new_file_name"] + "\n")):
log_file = open(name_data["log_file_name"].path, "a+")
log_file.write(name_data["new_file_name"] + "\n")
log_file.close()
if remove and result.returncode == 0:
if debug:
print("removing original file")
if name_data["same_extension"] and (self.input_folder == self.output_folder or File.path == output_name):
if debug:
print("replacing with converted file")
# shutil.move(name_data["new_file_name"]+".tmp",
# str(name_data["relative_dir"]) + str(
# name_data["file_name_no_extension"]) + "." + self.extension_convert)
try:
while (os.path.exists(name_data["new_file_name"]) and os.path.exists(str(name_data["relative_dir"]) + str(
name_data["file_name_no_extension"]) + "." + self.extension_convert)):
shutil.move(name_data["new_file_name"],
str(name_data["relative_dir"]) + str(
name_data["file_name_no_extension"]) + "." + self.extension_convert)
except:
if "cannot overwrite original" not in self.results.keys():
self.results["cannot overwrite original"] = []
self.results["cannot overwrite original"].append(str(File.path))
else:
try:
if debug:
print("deleting original file")
while os.path.exists(str(File.path)):
os.remove(str(File.path))
except:
if "cannot delete original" not in self.results.keys():
self.results["cannot delete original"] = []
self.results["cannot delete original"].append(str(File.path))
elif result.returncode == 1 and name_data["new_file_name"] != "":
if debug:
print("selected output file ,error")
try:
while os.path.exists(str(name_data["new_file_name"] + ".tmp")):
os.remove(str(name_data["new_file_name"] + ".tmp"))
except:
if "cannot delete tmp" not in self.results.keys():
self.results["cannot delete tmp"] = []
self.results["cannot delete tmp"].append(str(name_data["new_file_name"] + ".tmp"))
return result.returncode
return result.returncode
else:
processo = subprocess.Popen(command)
retorno = {"process": processo, "aquivo": File}
return retorno
def convert_all_files_sequential(self, resize=False, remove=False, current_dir=False, no_hierarchy=False,
force_change_fps=False, debug=False,not_overwrite=False):
"""
:param resize:
:param remove:
:param current_dir:
:param no_hierarchy:
:param force_change_fps:
:param debug:
:return:
"""
if self.files != []:
for file in self.files:
tmp = self.convert_video(file, resize=resize, remove=remove, process=False, current_dir=current_dir,
no_hierarchy=no_hierarchy, force_change_fps=force_change_fps, debug=debug,not_overwrite=not_overwrite)
if tmp not in self.results.keys():
self.results[tmp] = []
self.results[tmp].append(file)
if tmp != 0:
pass
# print(self.results[tmp])
# input("waiting")
else:
self.files.remove(file)
else:
print(self.results)
class converter_identifier(converter):
"""
until second order this just works with movies
"""
#import tmdbsimple as tmdb
import shlex
#from imdbpie import Imdb
from json import JSONDecoder
import urllib
def __init__(self, imdb_api_key, extension_convert: str = "mkv", resolution: int = 480, codec: str = "h264_omx",
fps: int = 24, crf: int = 20, preset: str = "slow", hwaccel="", threads=2, log_level=32,
input_folder="./", output_folder="./convert/", resize_log="./resize.log", resized_log="./resized.log"):
"""
:param imdb_api_key:
:param extension_convert:
:param resolution:
:param codec:
:param fps:
:param crf:
:param preset:
:param hwaccel:
:param threads:
:param log_level:
:param input_folder:
:param output_folder:
:param resize_log:
:param resized_log: