Filters are now working. Improved verbosity and comments

This commit is contained in:
Fabrice Quenneville 2020-11-18 04:45:23 -05:00
parent 2d90e0d6e8
commit b6c36624d3
2 changed files with 68 additions and 25 deletions

View File

@ -4,6 +4,7 @@
'''
from pathlib import Path
import sys
from .bcolors import BColors
from .video import Video
@ -39,9 +40,9 @@ class MediaLibrary():
self.filter_videos()
for filepath in self.videos:
# if self.videos[filepath].useful:
# print(self.videos[filepath])
print(self.videos[filepath])
if self.videos[filepath].useful:
print(self.videos[filepath])
#print(self.videos[filepath])
@ -90,10 +91,9 @@ class MediaLibrary():
print(f"{BColors.OKGREEN}Analazing {len(videolist)} videos in {', '.join(map(str, self.directories))}{BColors.ENDC}")
iteration = 0
for video in videolist:
iteration += 1
#print(int(iteration / len(videolist)))
#print(f'{iteration} / {len(videolist)}% complete {int(iteration / len(videolist))}% complete', end='\r')
print(f'{int((iteration / len(videolist )* 100))}% complete', end='\r')
if "-verbose" in sys.argv:
iteration += 1
print(f'{int((iteration / len(videolist )* 100))}% complete', end='\r')
self.videos[video] = Video(video)
def filter_videos(self):
@ -103,32 +103,69 @@ class MediaLibrary():
print(f"{BColors.OKGREEN}Filtering {len(self.videos)} videos for the requested parameters{BColors.ENDC}")
iteration = 0
for filepath in self.videos:
iteration += 1
print(f'{int((iteration / len(self.videos)* 100))}% complete', end='\r')
if "-verbose" in sys.argv:
iteration += 1
print(f'{int((iteration / len(self.videos)* 100))}% complete', end='\r')
# Filter for codecs if codec filter passed by user
if len([filt for filt in self.filters if filt not in ["lowres", "hd", "720p", "1080p", "uhd", "fferror"]]) > 0:
if len([filtr for filtr in self.filters if filtr in ["old", "mpeg4", "mpeg", "wmv3", "wmv", "h264", "hevc", "x265", "av1"]]) > 0:
useful = False
if "old" in self.filters and self.videos[filepath].codec not in ["hevc", "av1"]:
useful = True
if ("mpeg4" in self.filters or "mpeg" in self.filters) and self.videos[filepath].codec in ["mpeg4", "msmpeg4v3"]:
useful = True
if "mpeg" in self.filters and self.videos[filepath].codec in ["mpeg1video"]:
useful = True
if ("wmv3" in self.filters or "wmv" in self.filters) and self.videos[filepath].codec in ["wmv3"]:
useful = True
if "x264" in self.filters and self.videos[filepath].codec in ["x264"]:
if "h264" in self.filters and self.videos[filepath].codec in ["h264"]:
useful = True
if ("hevc" in self.filters or "x265" in self.filters) and self.videos[filepath].codec in ["hevc"]:
useful = True
if "av1" in self.filters and self.videos[filepath].codec in ["av1"]:
useful = True
self.videos[filepath].useful = useful
# keep video if useful and user wants to also filter by selected resolutions
if self.videos[filepath].useful and len([filtr for filtr in self.filters if filtr in ["lowres", "hd", "720p", "1080p", "uhd"]]) > 0:
useful = False
if "lowres" in self.filters and self.videos[filepath].definition in ["sd"]:
useful = True
if "hd" in self.filters and self.videos[filepath].definition in ["720p", "1080p", "uhd"]:
useful = True
if "720p" in self.filters and self.videos[filepath].definition in ["720p"]:
useful = True
if "1080p" in self.filters and self.videos[filepath].definition in ["1080p"]:
useful = True
if "uhd" in self.filters and self.videos[filepath].definition in ["uhd"]:
useful = True
self.videos[filepath].useful = useful
# keep video if useful and user wants to also filter when there is an ffmpeg errors
if self.videos[filepath].useful and len([filtr for filtr in self.filters if filtr in ["fferror"]]) > 0:
useful = False
if self.videos[filepath].error:
useful = True
self.videos[filepath].useful = useful
print(f"{BColors.OKGREEN}Found {len([filepath for filepath in self.videos if self.videos[filepath].useful])} videos for the requested parameters{BColors.ENDC}")

View File

@ -4,6 +4,7 @@
from .bcolors import BColors
import subprocess
import os
import sys
class Video():
'''
@ -37,10 +38,18 @@ class Video():
self.filesize_origin = self.detect_filesize(filepath)
self.error = self.detect_fferror(filepath)
self.codec = self.detect_codec(filepath)
self.width, self.height = self.detect_resolution(filepath)
self.definition = self.detect_definition(
width = self.width,
height = self.height )
try:
self.width, self.height = self.detect_resolution(filepath)
self.definition = self.detect_definition(
width = self.width,
height = self.height )
except:
self.width, self.height = False, False
self.definition = False
if self.error and "-verbose" in sys.argv:
print(f"{BColors.FAIL}There seams to be an error with \"{filepath}\"{BColors.ENDC}")
print(f"{BColors.FAIL} {self.error}{BColors.ENDC}")
def __str__(self):
'''
@ -48,6 +57,7 @@ class Video():
'''
text = f"{self.path + self.filename_origin}\n"
text += f" Useful: {self.useful}\n"
# If the first character of the definition is not a number (ie UHD and not 720p) upper it
if self.definition[0] and not self.definition[0].isnumeric():
@ -64,9 +74,8 @@ class Video():
text += f" size: {self.filesize_origin} mb"
if self.error:
text += f"\n Errors: {self.error}"
text += f"{BColors.FAIL}\n Errors: {', '.join(map(str, self.error))}{BColors.ENDC}"
text += f"\n Useful: {self.useful}"
return text
@ -82,8 +91,7 @@ class Video():
# decoding from binary, stripping whitespace, keep only last line
# in case ffmprobe added error messages over the requested information
output = output.decode().strip().splitlines()[-1]
except subprocess.CalledProcessError:
print(f"{BColors.FAIL}There seams to be an error with {filepath}{BColors.ENDC}")
except (subprocess.CalledProcessError, IndexError):
return False
return output
@ -96,8 +104,8 @@ class Video():
output = output.decode().strip().splitlines()
if len(output) > 1:
return output[0:-1]
except subprocess.CalledProcessError:
return f'{BColors.FAIL}There seams to be a "subprocess.CalledProcessError" error with {filepath}{BColors.ENDC}'
except (subprocess.CalledProcessError, IndexError):
return f'{BColors.FAIL}There seams to be a "subprocess.CalledProcessError" error with \"{filepath}\"{BColors.ENDC}'
return False
@ -113,8 +121,7 @@ class Video():
# See if we got convertable data
output = [int(output.split("x")[0]), int(output.split("x")[1])]
except subprocess.CalledProcessError:
print(f"{BColors.FAIL}There seams to be an error with {filepath}{BColors.ENDC}")
except (subprocess.CalledProcessError, IndexError):
return False
return output[0], output[1]
@ -138,7 +145,6 @@ class Video():
try:
size = int(os.path.getsize(filepath) / 1024 / 1024)
except subprocess.CalledProcessError:
print(f"{BColors.FAIL}There seams to be an error with {filepath}{BColors.ENDC}")
return False
return size