Commenting the code
This commit is contained in:
parent
0b30bf82d4
commit
05a60c278c
@ -15,11 +15,12 @@ class MediaLibrary():
|
|||||||
Contains the information and methods of a video file.
|
Contains the information and methods of a video file.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
size = int()
|
# User options
|
||||||
videos = dict()
|
|
||||||
directories = list()
|
directories = list()
|
||||||
inputs = []
|
inputs = list()
|
||||||
filters = []
|
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):
|
def __init__(self, files = False, directories = False, inputs = ["any"], filters = [], verbose = False):
|
||||||
'''
|
'''
|
||||||
@ -36,14 +37,8 @@ class MediaLibrary():
|
|||||||
|
|
||||||
self.inputs = inputs
|
self.inputs = inputs
|
||||||
self.filters = filters
|
self.filters = filters
|
||||||
|
|
||||||
self.load_videos(verbose = verbose)
|
self.load_videos(verbose = verbose)
|
||||||
|
self.filter_videos(verbose = verbose)
|
||||||
self.filter_videos()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
''' print '''
|
''' print '''
|
||||||
@ -51,7 +46,6 @@ class MediaLibrary():
|
|||||||
text = f"MediaCurator is watching the following directories: "
|
text = f"MediaCurator is watching the following directories: "
|
||||||
text += '\n '.join(map(str, self.directories)) + '\n'
|
text += '\n '.join(map(str, self.directories)) + '\n'
|
||||||
text += f"MediaCurator is tracking {len(self.videos)} video files"
|
text += f"MediaCurator is tracking {len(self.videos)} video files"
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def load_videos(self, verbose = False):
|
def load_videos(self, verbose = False):
|
||||||
@ -65,6 +59,7 @@ class MediaLibrary():
|
|||||||
|
|
||||||
for directory in self.directories:
|
for directory in self.directories:
|
||||||
path = Path(directory)
|
path = Path(directory)
|
||||||
|
# get all video filetypes
|
||||||
if "wmv" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
|
if "wmv" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
|
||||||
videolist += list(path.rglob("*.[wW][mM][vV]"))
|
videolist += list(path.rglob("*.[wW][mM][vV]"))
|
||||||
if "avi" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
|
if "avi" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
|
||||||
@ -90,56 +85,37 @@ class MediaLibrary():
|
|||||||
print(f"{BColors.OKGREEN}Analazing {len(videolist)} videos in {', '.join(map(str, self.directories))}{BColors.ENDC}")
|
print(f"{BColors.OKGREEN}Analazing {len(videolist)} videos in {', '.join(map(str, self.directories))}{BColors.ENDC}")
|
||||||
iteration = 0
|
iteration = 0
|
||||||
for video in videolist:
|
for video in videolist:
|
||||||
if "-verbose" in sys.argv:
|
if verbose:
|
||||||
iteration += 1
|
iteration += 1
|
||||||
print(f'{int((iteration / len(videolist )* 100))}% complete', end='\r')
|
print(f'{int((iteration / len(videolist )* 100))}% complete', end='\r')
|
||||||
|
|
||||||
self.videos[video] = Video(video, verbose = verbose)
|
self.videos[video] = Video(video, verbose = verbose)
|
||||||
|
|
||||||
def filter_videos(self):
|
def filter_videos(self, verbose = False):
|
||||||
'''
|
''' Mark useless videos in the videos dictionary (default is useful) '''
|
||||||
Mark useless videos in the videos dictionary
|
|
||||||
'''
|
|
||||||
|
|
||||||
print(f"{BColors.OKGREEN}Filtering {len(self.videos)} videos for the requested parameters{BColors.ENDC}")
|
print(f"{BColors.OKGREEN}Filtering {len(self.videos)} videos for the requested parameters{BColors.ENDC}")
|
||||||
|
|
||||||
|
|
||||||
iteration = 0
|
|
||||||
for filepath in self.videos:
|
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:
|
if len([filtr for filtr in self.filters if filtr in ["old", "mpeg4", "mpeg", "wmv3", "wmv", "h264", "hevc", "x265", "av1"]]) > 0:
|
||||||
useful = False
|
useful = False
|
||||||
|
|
||||||
if "old" in self.filters and self.videos[filepath].codec not in ["hevc", "av1"]:
|
if "old" in self.filters and self.videos[filepath].codec not in ["hevc", "av1"]:
|
||||||
useful = True
|
useful = True
|
||||||
|
|
||||||
if ("mpeg4" in self.filters or "mpeg" in self.filters) and self.videos[filepath].codec in ["mpeg4", "msmpeg4v3"]:
|
if ("mpeg4" in self.filters or "mpeg" in self.filters) and self.videos[filepath].codec in ["mpeg4", "msmpeg4v3"]:
|
||||||
useful = True
|
useful = True
|
||||||
|
|
||||||
|
|
||||||
if "mpeg" in self.filters and self.videos[filepath].codec in ["mpeg1video"]:
|
if "mpeg" in self.filters and self.videos[filepath].codec in ["mpeg1video"]:
|
||||||
useful = True
|
useful = True
|
||||||
|
|
||||||
if ("wmv3" in self.filters or "wmv" in self.filters) and self.videos[filepath].codec in ["wmv3"]:
|
if ("wmv3" in self.filters or "wmv" in self.filters) and self.videos[filepath].codec in ["wmv3"]:
|
||||||
useful = True
|
useful = True
|
||||||
|
|
||||||
if "h264" in self.filters and self.videos[filepath].codec in ["h264"]:
|
if "h264" in self.filters and self.videos[filepath].codec in ["h264"]:
|
||||||
useful = True
|
useful = True
|
||||||
|
|
||||||
if ("hevc" in self.filters or "x265" in self.filters) and self.videos[filepath].codec in ["hevc"]:
|
if ("hevc" in self.filters or "x265" in self.filters) and self.videos[filepath].codec in ["hevc"]:
|
||||||
useful = True
|
useful = True
|
||||||
|
|
||||||
if "av1" in self.filters and self.videos[filepath].codec in ["av1"]:
|
if "av1" in self.filters and self.videos[filepath].codec in ["av1"]:
|
||||||
useful = True
|
useful = True
|
||||||
|
|
||||||
self.videos[filepath].useful = useful
|
self.videos[filepath].useful = useful
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# keep video if useful and user wants to also filter by selected resolutions
|
# 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:
|
if self.videos[filepath].useful and len([filtr for filtr in self.filters if filtr in ["lowres", "hd", "720p", "1080p", "uhd"]]) > 0:
|
||||||
useful = False
|
useful = False
|
||||||
@ -154,17 +130,13 @@ class MediaLibrary():
|
|||||||
useful = True
|
useful = True
|
||||||
if "uhd" in self.filters and self.videos[filepath].definition in ["uhd"]:
|
if "uhd" in self.filters and self.videos[filepath].definition in ["uhd"]:
|
||||||
useful = True
|
useful = True
|
||||||
|
|
||||||
self.videos[filepath].useful = useful
|
self.videos[filepath].useful = useful
|
||||||
|
|
||||||
# keep video if useful and user wants to also filter when there is an ffmpeg errors
|
# 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:
|
if self.videos[filepath].useful and len([filtr for filtr in self.filters if filtr in ["fferror"]]) > 0:
|
||||||
|
|
||||||
useful = False
|
useful = False
|
||||||
|
|
||||||
if self.videos[filepath].error:
|
if self.videos[filepath].error:
|
||||||
useful = True
|
useful = True
|
||||||
|
|
||||||
self.videos[filepath].useful = useful
|
self.videos[filepath].useful = useful
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -8,27 +8,24 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
class Video():
|
class Video():
|
||||||
'''
|
'''Contains the information and methods of a video file.'''
|
||||||
Contains the information and methods of a video file.
|
|
||||||
'''
|
|
||||||
|
|
||||||
path = ""
|
path = str()
|
||||||
filename_origin = ""
|
filename_origin = str()
|
||||||
filesize = ""
|
filesize = int()
|
||||||
filename_new = ""
|
filename_new = str()
|
||||||
filename_tmp = ""
|
filename_tmp = str()
|
||||||
useful = True
|
useful = bool()
|
||||||
codec= ""
|
codec= str()
|
||||||
error = ""
|
error = str()
|
||||||
definition = ""
|
definition = str()
|
||||||
width = int()
|
width = int()
|
||||||
height = int()
|
height = int()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, filepath, useful = True, verbose = False):
|
def __init__(self, filepath, useful = True, verbose = False):
|
||||||
'''
|
''' creates and analyse a video file '''
|
||||||
'''
|
|
||||||
|
|
||||||
#Breaking down the full path in its components
|
#Breaking down the full path in its components
|
||||||
self.path = str(filepath)[:str(filepath).rindex("/") + 1]
|
self.path = str(filepath)[:str(filepath).rindex("/") + 1]
|
||||||
@ -55,9 +52,8 @@ class Video():
|
|||||||
print(f"{BColors.FAIL} {self.error}{BColors.ENDC}")
|
print(f"{BColors.FAIL} {self.error}{BColors.ENDC}")
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
'''
|
'''Returns a short formated string about the video'''
|
||||||
Building and returning formated information about the video file
|
|
||||||
'''
|
|
||||||
text = f"{self.codec} - "
|
text = f"{self.codec} - "
|
||||||
|
|
||||||
# If the first character of the definition is not a number (ie UHD and not 720p) upper it
|
# 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__
|
__repr__ = __str__
|
||||||
|
|
||||||
def fprint(self):
|
def fprint(self):
|
||||||
'''
|
'''Returns a long formated string about the video '''
|
||||||
Building and returning formated information about the video file
|
|
||||||
'''
|
|
||||||
|
|
||||||
text = f"{self.path + self.filename_origin}\n"
|
text = f"{self.path + self.filename_origin}\n"
|
||||||
#text += f" Useful: {self.useful}\n"
|
#text += f" Useful: {self.useful}\n"
|
||||||
@ -120,9 +114,9 @@ class Video():
|
|||||||
def convert(self, vcodec = "x265", acodec = False, extension = "mkv", verbose = False):
|
def convert(self, vcodec = "x265", acodec = False, extension = "mkv", verbose = False):
|
||||||
'''
|
'''
|
||||||
Convert to original file to the requested format / codec
|
Convert to original file to the requested format / codec
|
||||||
|
verbose will print ffmpeg's output
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
# Setting new filename
|
# Setting new filename
|
||||||
if "mp4" in extension:
|
if "mp4" in extension:
|
||||||
newfilename = self.filename_origin[:-4] + ".mp4"
|
newfilename = self.filename_origin[:-4] + ".mp4"
|
||||||
@ -179,6 +173,7 @@ class Video():
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def detect_fferror(filepath):
|
def detect_fferror(filepath):
|
||||||
|
'''Returns a string with the detected errors'''
|
||||||
try:
|
try:
|
||||||
args = ["ffprobe","-v","error",str(filepath)]
|
args = ["ffprobe","-v","error",str(filepath)]
|
||||||
output = subprocess.check_output(args, stderr=subprocess.STDOUT)
|
output = subprocess.check_output(args, stderr=subprocess.STDOUT)
|
||||||
@ -192,6 +187,7 @@ class Video():
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def detect_codec(filepath):
|
def detect_codec(filepath):
|
||||||
|
'''Returns a string with the detected codec'''
|
||||||
try:
|
try:
|
||||||
args = ["ffprobe", "-v", "quiet", "-select_streams", "v:0", "-show_entries", "stream=codec_name", "-of", "default=noprint_wrappers=1:nokey=1", str(filepath)]
|
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)
|
output = subprocess.check_output(args, stderr=subprocess.STDOUT)
|
||||||
@ -206,6 +202,7 @@ class Video():
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def detect_resolution(filepath):
|
def detect_resolution(filepath):
|
||||||
|
'''Returns a list with the detected width(0) and height(1)'''
|
||||||
try:
|
try:
|
||||||
args = ["ffprobe","-v","quiet","-select_streams","v:0", "-show_entries","stream=width,height","-of","csv=s=x:p=0",str(filepath)]
|
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)
|
output = subprocess.check_output(args, stderr=subprocess.STDOUT)
|
||||||
@ -222,6 +219,7 @@ class Video():
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def detect_definition(filepath = False, width = False, height = False):
|
def detect_definition(filepath = False, width = False, height = False):
|
||||||
|
'''Returns a string with the detected definition corrected for dead space'''
|
||||||
if filepath:
|
if filepath:
|
||||||
width, height = Video.detect_resolution(filepath)
|
width, height = Video.detect_resolution(filepath)
|
||||||
if not width and not height:
|
if not width and not height:
|
||||||
@ -237,6 +235,7 @@ class Video():
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def detect_filesize(filepath):
|
def detect_filesize(filepath):
|
||||||
|
'''Returns an integer with size in mb'''
|
||||||
try:
|
try:
|
||||||
size = int(os.path.getsize(filepath) / 1024 / 1024)
|
size = int(os.path.getsize(filepath) / 1024 / 1024)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user