Working on the initialisation of Video and MediaLibrary classes

This commit is contained in:
Fabrice Quenneville 2020-11-17 22:54:48 -05:00
parent 8840988636
commit 29f50451f0
4 changed files with 127 additions and 86 deletions

View File

@ -65,7 +65,6 @@ def main():
else: else:
print(f"{BColors.FAIL}ERROR: No files or directories selected.{BColors.FAIL}") print(f"{BColors.FAIL}ERROR: No files or directories selected.{BColors.FAIL}")
print(medialibrary)
# Actions # Actions
if sys.argv[1] == "list": if sys.argv[1] == "list":
@ -87,7 +86,8 @@ def main():
if any("-files" in argv for argv in sys.argv): if any("-files" in argv for argv in sys.argv):
pass pass
elif any("-dir" in argv for argv in sys.argv): elif any("-dir" in argv for argv in sys.argv):
print(f"directories = {directories}, inputs = {inputs}, filters = {filters}, outputs = {outputs}") print(medialibrary)
#print(f"directories = {directories}, inputs = {inputs}, filters = {filters}, outputs = {outputs}")
exit() exit()
else: else:
print("{BColors.FAIL}Missing directory: {BColors.ENDC}") print("{BColors.FAIL}Missing directory: {BColors.ENDC}")

View File

@ -3,7 +3,10 @@
This is the container for all the videos found in the folders passed by the user This is the container for all the videos found in the folders passed by the user
''' '''
from pathlib import Path
from .bcolors import BColors from .bcolors import BColors
from .video import Video
class MediaLibrary(): class MediaLibrary():
''' '''
@ -13,98 +16,114 @@ class MediaLibrary():
size = int() size = int()
videos = dict() videos = dict()
directories = list() directories = list()
inputs = []
filters = []
def __init__(self, files = False, directories = False): def __init__(self, files = False, directories = False, inputs = ["any"], filters = []):
''' '''
This is the library object who holds the information about the workspace and all the videos in it.
''' '''
if files: if files:
pass pass
# self.files = files # self.files = files
elif directories: elif directories:
self.directories = directories self.directories = directories
else:
return
self.inputs = inputs
self.filters = filters
self.load_videolist()
def __str__(self): def __str__(self):
''' print ''' ''' print '''
text = f"MediaCurator watching: "
if self.directories: if self.directories:
return f"MediaCurator watching: {', '.join(map(str, self.directories))}" text += f"{', '.join(map(str, self.directories))}"
return text
# def delete(filename):
# try:
# os.remove(filename)
# except OSError:
# print(f"{BColors.FAIL}Error deleting {filename}{BColors.ENDC}")
# return False
# print(f"{BColors.OKGREEN}Deleted {filename}{BColors.ENDC}")
# return True
# def get_videolist(parentdir, inputs = ["any"], filters = []): def load_videolist(self):
# print(f"{BColors.OKGREEN}Scanning files in {parentdir} for videos{BColors.ENDC}") print(f"{BColors.OKGREEN}Scanning files in {', '.join(map(str, self.directories))} for videos{BColors.ENDC}")
# videolist = [] videolist = []
for directory in self.directories:
path = Path(directory)
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:
videolist += list(path.rglob("*.[aA][vV][iI]"))
if "mkv" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
videolist += list(path.rglob("*.[mM][kK][vV]"))
if "mp4" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
videolist += list(path.rglob("*.[mM][pP]4"))
if "m4v" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
videolist += list(path.rglob("*.[mM]4[vV]"))
if "flv" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
videolist += list(path.rglob("*.[fF][lL][vV]"))
if "mpg" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
videolist += list(path.rglob("*.[mM][pP][gG]"))
if "vid" in self.inputs or "any" in self.inputs or len(self.inputs) < 1:
videolist += list(path.rglob("*.[vV][iI][dD]"))
# Remove folders
videolist_tmp = videolist
videolist = [video for video in videolist_tmp if video.is_file()]
video = Video(videolist[0])
#videolist = list(dict.fromkeys(videolist))
print(video)
exit()
# path = Path(parentdir)
# if "wmv" in inputs or "any" in inputs or len(inputs) < 1:
# videolist += list(path.rglob("*.[wW][mM][vV]"))
# if "avi" in inputs or "any" in inputs or len(inputs) < 1:
# videolist += list(path.rglob("*.[aA][vV][iI]"))
# if "mkv" in inputs or "any" in inputs or len(inputs) < 1:
# videolist += list(path.rglob("*.[mM][kK][vV]"))
# if "mp4" in inputs or "any" in inputs or len(inputs) < 1:
# videolist += list(path.rglob("*.[mM][pP]4"))
# if "m4v" in inputs or "any" in inputs or len(inputs) < 1:
# videolist += list(path.rglob("*.[mM]4[vV]"))
# if "flv" in inputs or "any" in inputs or len(inputs) < 1:
# videolist += list(path.rglob("*.[fF][lL][vV]"))
# if "mpg" in inputs or "any" in inputs or len(inputs) < 1:
# videolist += list(path.rglob("*.[mM][pP][gG]"))
# if "vid" in inputs or "any" in inputs or len(inputs) < 1:
# videolist += list(path.rglob("*.[vV][iI][dD]"))
# # Remove folders
# videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if video.is_file()]
# # Filter the list for specific codecs # # Filter the list for specific codecs
# videolist_tmp = videolist # videolist_tmp = videolist
# print(f"{BColors.OKGREEN}Filtering {len(videolist)} videos for the requested parameters{BColors.ENDC}") # print(f"{BColors.OKGREEN}Filtering {len(videolist)} videos for the requested parameters{BColors.ENDC}")
# if len([filt for filt in filters if filt not in ["lowres", "hd", "720p", "1080p", "uhd", "fferror"]]) > 0: # if len([filt for filt in self.filters if filt not in ["lowres", "hd", "720p", "1080p", "uhd", "fferror"]]) > 0:
# videolist = [] # videolist = []
# if "old" in filters: # if "old" in self.filters:
# videolist += [video for video in videolist_tmp if get_codec(video) not in ["hevc", "av1"]] # videolist += [video for video in videolist_tmp if get_codec(video) not in ["hevc", "av1"]]
# if "mpeg4" in filters or "mpeg" in filters: # if "mpeg4" in self.filters or "mpeg" in self.filters:
# videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg4", "msmpeg4v3"]] # videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg4", "msmpeg4v3"]]
# if "mpeg" in filters: # if "mpeg" in self.filters:
# videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg1video"]] # videolist += [video for video in videolist_tmp if get_codec(video) in ["mpeg1video"]]
# if "wmv3" in filters or "wmv" in filters: # if "wmv3" in self.filters or "wmv" in self.filters:
# videolist += [video for video in videolist_tmp if get_codec(video) in ["wmv3"]] # videolist += [video for video in videolist_tmp if get_codec(video) in ["wmv3"]]
# if "x264" in filters: # if "x264" in self.filters:
# videolist += [video for video in videolist_tmp if get_codec(video) in ["x264"]] # videolist += [video for video in videolist_tmp if get_codec(video) in ["x264"]]
# if len(filters) > 0 and "lowres" in filters: # if len(self.filters) > 0 and "lowres" in self.filters:
# videolist_tmp = videolist # videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if get_resolution(video)[1] < 1280 or get_resolution(video)[0] <= 480] # videolist = [video for video in videolist_tmp if get_resolution(video)[1] < 1280 or get_resolution(video)[0] <= 480]
# elif len(filters) > 0 and "hd" in filters: # elif len(self.filters) > 0 and "hd" in self.filters:
# videolist_tmp = videolist # videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if get_resolution(video)[1] >= 1280 or get_resolution(video)[0] >= 720] # videolist = [video for video in videolist_tmp if get_resolution(video)[1] >= 1280 or get_resolution(video)[0] >= 720]
# elif len(filters) > 0 and "720p" in filters: # elif len(self.filters) > 0 and "720p" in self.filters:
# videolist_tmp = videolist # videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if get_resolution(video)[1] >= 1280 or get_resolution(video)[0] == 720] # videolist = [video for video in videolist_tmp if get_resolution(video)[1] >= 1280 or get_resolution(video)[0] == 720]
# elif len(filters) > 0 and "1080p" in filters: # elif len(self.filters) > 0 and "1080p" in self.filters:
# videolist_tmp = videolist # videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if (get_resolution(video)[1] >= 1440 and get_resolution(video)[1] < 3840) or get_resolution(video)[0] == 1080] # videolist = [video for video in videolist_tmp if (get_resolution(video)[1] >= 1440 and get_resolution(video)[1] < 3840) or get_resolution(video)[0] == 1080]
# elif len(filters) > 0 and "uhd" in filters: # elif len(self.filters) > 0 and "uhd" in self.filters:
# videolist_tmp = videolist # videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if get_resolution(video)[1] >= 3840 or get_resolution(video)[0] >= 2160] # videolist = [video for video in videolist_tmp if get_resolution(video)[1] >= 3840 or get_resolution(video)[0] >= 2160]
# if len(filters) > 0 and "fferror" in filters: # if len(self.filters) > 0 and "fferror" in self.filters:
# videolist_tmp = videolist # videolist_tmp = videolist
# videolist = [video for video in videolist_tmp if get_fferror(video)] # videolist = [video for video in videolist_tmp if get_fferror(video)]

View File

@ -4,6 +4,7 @@
''' '''
import subprocess import subprocess
from .bcolors import BColors
def detect_ffmpeg(): def detect_ffmpeg():
@ -21,3 +22,13 @@ def user_confirm(question):
return False return False
print("Please answer with yes (Y) or no (N)...") print("Please answer with yes (Y) or no (N)...")
return user_confirm(question) return user_confirm(question)
def delete(filename):
try:
os.remove(filename)
except OSError:
print(f"{BColors.FAIL}Error deleting {filename}{BColors.ENDC}")
return False
print(f"{BColors.OKGREEN}Deleted {filename}{BColors.ENDC}")
return True

View File

@ -38,8 +38,19 @@ class Video():
width = self.width, width = self.width,
height = self.height ) height = self.height )
@classmethod def __str__(self):
def detect_codec(Video, filepath): text = f"{self.path + self.filename_origin}\n"
text += f" Definition: {self.definition}: ({self.width}x{self.height})\n"
text += f" Codec: {self.codec}\n"
text += f" size: {self.filesize_origin}mb"
if self.error:
text += f"\n Errors: {self.error}"
return text
@staticmethod
def detect_codec(filepath):
try: try:
args = ["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=codec_name", "-of", "default=noprint_wrappers=1:nokey=1", str(filepath)] args = ["ffprobe", "-v", "error", "-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)
@ -53,8 +64,8 @@ class Video():
return output return output
@classmethod @staticmethod
def detect_fferror(Video, filepath): def detect_fferror(filepath):
try: try:
args = ["ffprobe","-v","error","-select_streams","v:0", "-show_entries","stream=width,height","-of","csv=s=x:p=0",str(filepath)] args = ["ffprobe","-v","error","-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)
@ -66,8 +77,8 @@ class Video():
return False return False
@classmethod @staticmethod
def detect_resolution(Video, filepath): def detect_resolution(filepath):
try: try:
args = ["ffprobe","-v","error","-select_streams","v:0", "-show_entries","stream=width,height","-of","csv=s=x:p=0",str(filepath)] args = ["ffprobe","-v","error","-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)
@ -83,8 +94,8 @@ class Video():
return False return False
return output[0], output[1] return output[0], output[1]
@classmethod @staticmethod
def detect_definition(Video, filepath = False, width = False, height = False): def detect_definition(filepath = False, width = False, height = False):
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:
@ -98,8 +109,8 @@ class Video():
return "720p" return "720p"
return "SD" return "SD"
@classmethod @staticmethod
def detect_filesize(Video, filepath): def detect_filesize(filepath):
try: try:
size = int(os.path.getsize(filepath) / 1024 / 1024) size = int(os.path.getsize(filepath) / 1024 / 1024)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
@ -107,7 +118,7 @@ class Video():
return False return False
return size return size
# @classmethod # @staticmethod
# def convert(oldfilename, newfilename, codec = "x265"): # def convert(oldfilename, newfilename, codec = "x265"):
# oldsize = get_size(oldfilename) # oldsize = get_size(oldfilename)
# print(f"{BColors.OKGREEN}Starting conversion of {oldfilename}{BColors.OKCYAN}({oldsize}mb)({get_print_resolution(oldfilename)}){BColors.OKGREEN} from {BColors.OKCYAN}{get_codec(oldfilename)}{BColors.OKGREEN} to {BColors.OKCYAN}{codec}{BColors.OKGREEN}...{BColors.ENDC}") # print(f"{BColors.OKGREEN}Starting conversion of {oldfilename}{BColors.OKCYAN}({oldsize}mb)({get_print_resolution(oldfilename)}){BColors.OKGREEN} from {BColors.OKCYAN}{get_codec(oldfilename)}{BColors.OKGREEN} to {BColors.OKCYAN}{codec}{BColors.OKGREEN}...{BColors.ENDC}")