def applyFor(self, filePath, data): subtitleFile = File(filePath) # fps if self._subProperties.autoFps: videoInfo = subtitleFile.detectFps(default=self._subProperties.fps) data.subtitles.changeFps(videoInfo.fps) data.fps = videoInfo.fps data.videoPath = videoInfo.videoPath else: data.subtitles.changeFps(self._subProperties.fps) data.fps = self._subProperties.fps data.videoPath = None # input encoding inputEncoding = self._subProperties.inputEncoding if self._subProperties.autoInputEncoding: inputEncoding = subtitleFile.detectEncoding().lower() if data.inputEncoding != inputEncoding: data.encode(inputEncoding) # output encoding if self._subProperties.changeEncoding: data.outputEncoding = self._subProperties.outputEncoding else: data.outputEncoding = self._subProperties.inputEncoding # subtitle format data.outputFormat = self._subProperties.outputFormat return data
def createDataFromFile(self, filePath, inputEncoding = None, defaultFps = None): """Fetch a given filePath and parse its contents. May raise the following exceptions: * RuntimeError - generic exception telling that parsing was unsuccessfull * IOError - failed to open a file at given filePath @return SubtitleData filled with non-empty, default datafields. Client should modify them and then perform an add/update operation""" file_ = File(filePath) if inputEncoding is None: inputEncoding = file_.detectEncoding() inputEncoding = inputEncoding.lower() videoInfo = VideoInfo(defaultFps) if defaultFps is not None else file_.detectFps() subtitles = self._parseFile(file_, inputEncoding, videoInfo.fps) data = SubtitleData() data.subtitles = subtitles data.fps = videoInfo.fps data.inputEncoding = inputEncoding data.outputEncoding = inputEncoding data.outputFormat = self._parser.parsedFormat() data.videoPath = videoInfo.videoPath return data
def detectFps(self): data = self.data if data.videoPath is not None: fpsInfo = File.detectFpsFromMovie(data.videoPath) if data.videoPath != fpsInfo.videoPath or data.fps != fpsInfo.fps: data.videoPath = fpsInfo.videoPath data.subtitles.changeFps(fpsInfo.fps) data.fps = fpsInfo.fps command = ChangeData(self.filePath, data, _("Detected FPS: %s") % data.fps) self._subtitleData.execute(command)
def detectSelectedFilesFps(self): items = self.__fileList.selectedItems() for item in items: filePath = item.text(0) data = self._subtitleData.data(filePath) if data.videoPath is not None: fpsInfo = File.detectFpsFromMovie(data.videoPath) if data.videoPath != fpsInfo.videoPath or data.fps != fpsInfo.fps: data.videoPath = fpsInfo.videoPath data.subtitles.changeFps(fpsInfo.fps) data.fps = fpsInfo.fps command = ChangeData(filePath, data, _("Detected FPS: %s") % data.fps) self._subtitleData.execute(command)
def _writeFile(self, filePath, newFilePath=None): if newFilePath is None: newFilePath = filePath data = self._subtitleData.data(filePath) converter = SubConverter() content = converter.convert(data.outputFormat, data.subtitles) if File.exists(newFilePath): file_ = File(newFilePath) file_.overwrite(content, data.outputEncoding) else: File.write(newFilePath, content, data.outputEncoding) self._subtitleData.setCleanState(filePath) self.__updateMenuItemsState()
def writeSubtitles(self, convertedSubtitles, filePath, encoding): try: file_ = File(filePath) except: # File doesn't exist, we can safely write to it File.write(filePath, convertedSubtitles, encoding) log.info(_("File %s saved.") % filePath) else: # A little hack to ensure that translator won't make a mistake choices = { 'yes': _('y'), 'no': _('n'), 'quit': _('q'), 'backup': _('b') } choice = '' if self._args.force: choice = choices["yes"] while(choice not in choices.values()): vals = { "file": filePath, "yes": choices["yes"], "no": choices['no'], "bck": choices['backup'], "quit": choices["quit"] } choice = input( _("File '%(file)s' exists. Overwrite? [%(yes)s/%(no)s/%(bck)s/%(quit)s]") % vals) if choice == choices['backup']: backupFilePath = file_.backup() log.info(_("Backup: %s") % backupFilePath) log.info(_("Overwriting %s") % filePath) file_.overwrite(convertedSubtitles, encoding) elif choice == choices['no']: log.info(_("Skipping %s") % filePath) return elif choice == choices['yes']: log.info(_("Overwriting %s") % filePath) file_.overwrite(convertedSubtitles, encoding) elif choice == choices['quit']: log.info(_("Quitting converting work.")) sys.exit(0)