mediaInfoSama/file_cmds.py
cuissedemouche 7cc07c2657 Basic and advanced tab
First commit of the app
Basic and advanced tab can be generated via code to the QPlainTextEdit
2022-12-13 14:51:50 +00:00

134 lines
3.6 KiB
Python

import os, subprocess, json
from shlex import quote
class FileCmds:
def __init__(self, filePath):
super().__init__()
self.filePath = filePath
def execute_bash(self, cmd):
'''
Execute a bash command
:type cmd: str
:return: output
'''
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output, error = process.communicate()
return output
def file_exist(self):
if os.path.exists(self.filePath):
return True
else:
print("The file doesn't exist")
return False
def get_mkv_infos(self):
'''
Process MKV infos into a dictionary
:rtype: list or None
'''
process = self.execute_bash(f"mediainfo --Output=JSON {quote(self.filePath)}")
mkvInfo = json.loads(process)["media"]["track"]
return mkvInfo
# def get_video_track(self):
# '''
# Get the video track infos
# :rtype: dict or None
# '''
# for track in self.mkvInfo:
# if track["@type"] == "Video":
# return track
# print("The file does not contain a video track")
# return(None)
def media_inform(self, inform):
'''
Execute mediainfo --Inform
:type inform: rstr
:rtype: str
'''
bashCommand = f"mediainfo --Inform={quote(inform)} {quote(self.filePath)}"
output = self.execute_bash(bashCommand).decode('utf-8')
return output
def tracksCount(self):
'''
Return the number of track of each type
:rtype: dict
'''
trackTypes = [
"Video",
"Audio",
"Text",
"Image",
"Menu",
]
output = {}
for trackType in trackTypes:
count = self.media_inform(f"General;%{trackType}Count%").replace('\n', '')
if not count:
count = 0
output[trackType] = count
return output
def generate_basic(self):
'''
Return the basic tab
:rtype: string
'''
informGeneral = r"General;G: %Format%, %FileSize_String%, %Duration_String%, %BitRate_String%\n"
informVideo = r"Video;V: %Format%, %Format_Profile%, %StreamSize_String%, %Width%x%Height%, %FrameRate% FPS, %BitRate_String%\n"
informAudio = r"Audio;A: %Language_String%, %Format%, %StreamSize_String%, %BitRate_String%, %Channel(s)_String%, %SamplingRate_String%\n"
informText = r"Text;T: %Language_String%, %Format%\n"
informs = [informGeneral]
count = self.tracksCount()
if not count["Video"] == 0:
informs.append(informVideo)
count = self.tracksCount()
if not count["Audio"] == 0:
informs.append(informAudio)
count = self.tracksCount()
if not count["Text"] == 0:
informs.append(informText)
output = ""
for inform in informs:
output += self.media_inform(inform)
if not count["Menu"] == 0:
output += "M: Menu\n\n"
output += "\n"
output += self.execute_bash(f"mediainfo {quote(self.filePath)}").decode('utf-8')
return(output)
def generate_advanced(self):
'''
Return the advanced tab
:rtype: string
'''
output = self.execute_bash(f"mediainfo --Full {quote(self.filePath)}").decode('utf-8')
return output