def __init__(self, file_path=None, title=None): self.mkvmerge_path = 'mkvmerge' self.title = title self._chapters_file = None self._chapter_language = None self._global_tags_file = None self._link_to_previous_file = None self._link_to_next_file = None self.tracks = [] self.attachments = [] if file_path is not None and not verify_mkvmerge(mkvmerge_path=self.mkvmerge_path): raise FileNotFoundError('mkvmerge is not at the specified path, add it there or change the mkvmerge_path ' 'property') if file_path is not None and verify_matroska(file_path): # add file title file_path = expanduser(file_path) info_json = json.loads(sp.check_output([self.mkvmerge_path, '-J', file_path]).decode()) if self.title is None and 'title' in info_json['container']['properties']: self.title = info_json['container']['properties']['title'] # add tracks with info for track in info_json['tracks']: new_track = MKVTrack(file_path, track_id=track['id']) if 'track_name' in track['properties']: new_track.track_name = track['properties']['track_name'] if 'language' in track['properties']: new_track.language = track['properties']['language'] if 'default_track' in track['properties']: new_track.default_track = track['properties']['default_track'] if 'forced_track' in track['properties']: new_track.forced_track = track['properties']['forced_track'] self.add_track(new_track) # split options self._split_options = []
def mux(self, output_path, silent=False): """Muxes the specified :class:`~pymkv.MKVFile`. Parameters ---------- output_path : str The path to be used as the output file in the mkvmerge command. silent : bool, optional By default the mkvmerge output will be shown unless silent is True. Raises ------ FileNotFoundError Raised if the path to mkvmerge could not be verified. """ if not verify_mkvmerge(mkvmerge_path=self.mkvmerge_path): raise FileNotFoundError('mkvmerge is not at the specified path, add it there or change the mkvmerge_path ' 'property') output_path = expanduser(output_path) if silent: sp.run(self.command(output_path, subprocess=True), stdout=open(devnull, 'wb')) else: command = self.command(output_path) print('Running with command:\n"' + command + '"') sp.run(self.command(output_path, subprocess=True))
def __init__(self, file_path=None, title=None): """A class that represents an MKV file. The MKVFile class can either import a pre-existing MKV file or create a new one. After an MKVFile object has been instantiated, MKVTracks or other MKVFile objects can be added using add_track() and add_file() respectively. Tracks are always added in the same order that they exist in a file or are added in. They can be reordered using move_track_front(), move_track_end(), move_track_forward(), move_track_backward(), or swap_tracks(). After an MKVFile has been created, an mkvmerge command can be generated using command() or the file can be muxed using mux(). file_path (str, optional): Path to a pre-existing MKV file. The file will be imported into the new MKVFile object. title (str, optional): The internal title given to the MKVFile. If no title is given, the title of the pre-existing file will be used if it exists. """ self.mkvmerge_path = 'mkvmerge' self.title = title self._chapters_file = None self._chapter_language = None self._global_tags_file = None self._link_to_previous_file = None self._link_to_next_file = None self.tracks = [] self.attachments = [] if file_path is not None and not verify_mkvmerge( mkvmerge_path=self.mkvmerge_path): raise FileNotFoundError( 'mkvmerge is not at the specified path, add it there or change the mkvmerge_path ' 'property') if file_path is not None and verify_matroska(file_path): # add file title file_path = expanduser(file_path) info_json = json.loads( sp.check_output([self.mkvmerge_path, '-J', file_path]).decode()) if self.title is None and 'title' in info_json['container'][ 'properties']: self.title = info_json['container']['properties']['title'] # add tracks with info for track in info_json['tracks']: new_track = MKVTrack(file_path, track_id=track['id']) if 'track_name' in track['properties']: new_track.track_name = track['properties']['track_name'] if 'language' in track['properties']: new_track.language = track['properties']['language'] if 'default_track' in track['properties']: new_track.default_track = track['properties'][ 'default_track'] if 'forced_track' in track['properties']: new_track.forced_track = track['properties'][ 'forced_track'] self.add_track(new_track) # split options self._split_options = []
def mux(self, output_path, silent=False): """Muxes the specified MKVFile. output_file (str): The path to be used as the output file in the mkvmerge command. silent (bool): By default the mkvmerge output will be shown unless silent is True. """ if not verify_mkvmerge(mkvmerge_path=self.mkvmerge_path): raise FileNotFoundError( 'mkvmerge is not at the specified path, add it there or change the mkvmerge_path ' 'property') output_path = expanduser(output_path) if silent: sp.run(self.command(output_path, subprocess=True), stdout=open(devnull, 'wb')) else: command = self.command(output_path) print('Running with command:\n"' + command + '"') sp.run(self.command(output_path, subprocess=True))