Commenting the code

This commit is contained in:
Fabrice Quenneville 2020-11-20 00:17:30 -05:00
parent 0b30bf82d4
commit 05a60c278c
2 changed files with 31 additions and 60 deletions

View File

@ -15,11 +15,12 @@ class MediaLibrary():
Contains the information and methods of a video file.
'''
size = int()
videos = dict()
# User options
directories = list()
inputs = []
filters = []
inputs = list()
filters = list()
# The videos variable holds a dictionary of all videos in Video objects
videos = dict()
def __init__(self, files = False, directories = False, inputs = ["any"], filters = [], verbose = False):
'''
@ -36,14 +37,8 @@ class MediaLibrary():
self.inputs = inputs
self.filters = filters
self.load_videos(verbose = verbose)
self.filter_videos()
self.filter_videos(verbose = verbose)
def __str__(self):
''' print '''
@ -51,7 +46,6 @@ class MediaLibrary():
text = f"MediaCurator is watching the following directories: "
text += '\n '.join(map(str, self.directories)) + '\n'
text += f"MediaCurator is tracking {len(self.videos)} video files"
return text
def load_videos(self, verbose = False):
@ -65,6 +59,7 @@ class MediaLibrary():
for directory in self.directories:
path = Path(directory)
# get all video filetypes
if "wmv" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
videolist += list(path.rglob("*.[wW][mM][vV]"))
if "avi" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
@ -90,55 +85,36 @@ class MediaLibrary():
print(f"{BColors.OKGREEN}Analazing {len(videolist)} videos in {', '.join(map(str, self.directories))}{BColors.ENDC}")
iteration = 0
for video in videolist:
if "-verbose" in sys.argv:
if verbose:
iteration += 1
print(f'{int((iteration / len(videolist )* 100))}% complete', end='\r')
self.videos[video] = Video(video, verbose = verbose)
def filter_videos(self):
'''
Mark useless videos in the videos dictionary
'''
def filter_videos(self, verbose = False):
''' Mark useless videos in the videos dictionary (default is useful) '''
print(f"{BColors.OKGREEN}Filtering {len(self.videos)} videos for the requested parameters{BColors.ENDC}")
iteration = 0
for filepath in self.videos:
if "-verbose" in sys.argv:
iteration += 1
print(f'{int((iteration / len(self.videos)* 100))}% complete', end='\r')
# filter for filetypes
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 "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:
@ -154,17 +130,13 @@ class MediaLibrary():
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

View File

@ -8,27 +8,24 @@ import os
import sys
class Video():
'''
Contains the information and methods of a video file.
'''
'''Contains the information and methods of a video file.'''
path = ""
filename_origin = ""
filesize = ""
filename_new = ""
filename_tmp = ""
useful = True
codec= ""
error = ""
definition = ""
path = str()
filename_origin = str()
filesize = int()
filename_new = str()
filename_tmp = str()
useful = bool()
codec= str()
error = str()
definition = str()
width = int()
height = int()
def __init__(self, filepath, useful = True, verbose = False):
'''
'''
''' creates and analyse a video file '''
#Breaking down the full path in its components
self.path = str(filepath)[:str(filepath).rindex("/") + 1]
@ -55,9 +52,8 @@ class Video():
print(f"{BColors.FAIL} {self.error}{BColors.ENDC}")
def __str__(self):
'''
Building and returning formated information about the video file
'''
'''Returns a short formated string about the video'''
text = f"{self.codec} - "
# If the first character of the definition is not a number (ie UHD and not 720p) upper it
@ -87,9 +83,7 @@ class Video():
__repr__ = __str__
def fprint(self):
'''
Building and returning formated information about the video file
'''
'''Returns a long formated string about the video '''
text = f"{self.path + self.filename_origin}\n"
#text += f" Useful: {self.useful}\n"
@ -120,9 +114,9 @@ class Video():
def convert(self, vcodec = "x265", acodec = False, extension = "mkv", verbose = False):
'''
Convert to original file to the requested format / codec
verbose will print ffmpeg's output
'''
# Setting new filename
if "mp4" in extension:
newfilename = self.filename_origin[:-4] + ".mp4"
@ -179,6 +173,7 @@ class Video():
@staticmethod
def detect_fferror(filepath):
'''Returns a string with the detected errors'''
try:
args = ["ffprobe","-v","error",str(filepath)]
output = subprocess.check_output(args, stderr=subprocess.STDOUT)
@ -192,6 +187,7 @@ class Video():
@staticmethod
def detect_codec(filepath):
'''Returns a string with the detected codec'''
try:
args = ["ffprobe", "-v", "quiet", "-select_streams", "v:0", "-show_entries", "stream=codec_name", "-of", "default=noprint_wrappers=1:nokey=1", str(filepath)]
output = subprocess.check_output(args, stderr=subprocess.STDOUT)
@ -206,6 +202,7 @@ class Video():
@staticmethod
def detect_resolution(filepath):
'''Returns a list with the detected width(0) and height(1)'''
try:
args = ["ffprobe","-v","quiet","-select_streams","v:0", "-show_entries","stream=width,height","-of","csv=s=x:p=0",str(filepath)]
output = subprocess.check_output(args, stderr=subprocess.STDOUT)
@ -222,6 +219,7 @@ class Video():
@staticmethod
def detect_definition(filepath = False, width = False, height = False):
'''Returns a string with the detected definition corrected for dead space'''
if filepath:
width, height = Video.detect_resolution(filepath)
if not width and not height:
@ -237,6 +235,7 @@ class Video():
@staticmethod
def detect_filesize(filepath):
'''Returns an integer with size in mb'''
try:
size = int(os.path.getsize(filepath) / 1024 / 1024)
except subprocess.CalledProcessError: