import os, subprocess, json from shlex import quote class FileCmds: def __init__(self, filePath): super().__init__() self.filePath = filePath self.trackCount = self.track_count() self.basicInfo = self.generate_basic() self.advancedInfo = self.generate_advanced() self.tabsList = self.generate_tabs_list() self.tabsContent = self.generate_tabs_content() def execute_bash(self, cmd): ''' Execute a bash command :type cmd: str :return: str ''' process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) output, error = process.communicate() return output def file_exist(self): ''' Check if the file exists :rtype: bool ''' if os.path.exists(self.filePath): return True else: print("The file doesn't exist") return False 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 track_count(self): ''' Return the number of track of each type :rtype: dict[str,int] ''' 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] = int(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] if not self.trackCount["Video"] == 0: informs.append(informVideo) if not self.trackCount["Audio"] == 0: informs.append(informAudio) if not self.trackCount["Text"] == 0: informs.append(informText) output = "" for inform in informs: output += self.media_inform(inform) if not self.trackCount["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 def generate_tabs_list(self): ''' Return the list of item for the QListWidget :rtype: list ''' tabsStr = "" tabs = ["Basic", "Advanced"] informGeneral = r"General;General (%Format%)\n" informs = [informGeneral] if self.trackCount["Video"] == 1: informVideo = r'Video;Video (%Format%)\n' informs.append(informVideo) elif self.trackCount["Video"] > 1: informVideo = r'Video;Video #%StreamKindPos% (%Format%)\n' informs.append(informVideo) if self.trackCount["Audio"] == 1: informAudio = r'Audio;Audio (%Format%)\n' informs.append(informAudio) elif self.trackCount["Audio"] > 1: informAudio = r'Audio;Audio #%StreamKindPos% (%Format%)\n' informs.append(informAudio) if self.trackCount["Text"] == 1: informText = r'Text;Text (%Format%)\n' informs.append(informText) elif self.trackCount["Text"] > 1: informText = r'Text;Text #%StreamKindPos% (%Format%)\n' informs.append(informText) for inform in informs: tabsStr += self.media_inform(inform) if not self.trackCount["Menu"] == 0: tabsStr += "Menu" tabs.extend(list(filter(None, tabsStr.split("\n")))) return tabs def generate_tabs_content(self): ''' Return the content of the tabs :rtype: dict[str:str] ''' tracksList = self.advancedInfo.split("\n\n") del tracksList[-1] tabs = {} tabs["Basic"] = self.basicInfo tabs["Advanced"] = self.advancedInfo for i in range(len(tracksList)): tabs.update({self.tabsList[i+2]: tracksList[i]}) return tabs