def get_subtitle_tracks(self): tracks = list() if self.movie is not None: for track in self.movie.tracks(): if self.is_subtitle_track(track): media = track.media().quickTimeMedia() lang = GetMediaLanguage(media) display_name = track.attributeForKey_( QTTrackDisplayNameAttribute) if lang == 32767: # 32764 = langUndefined name = display_name else: lang_code = script_codes.map_to_two_letters_code(lang) lang_info = iso639.find(lang_code) if lang_info is None: name = display_name else: name = lang_info["name"] if name != display_name: name = "%s (%s)" % (name, display_name) is_enabled = track.attributeForKey_( QTTrackEnabledAttribute) == 1 track_id = track.attributeForKey_(QTTrackIDAttribute) tracks.append((track_id, name, is_enabled)) return tracks
def copy_subtitle_file(sub_path, video_path): """Copies the subtitle file located at sub_path alongside the video file located at video_path. It also changes the name so that the subtitle file follows the rules of sidecar files. Returns the path the subtitle file was copied to. """ from miro import iso639 sub_basename = os.path.basename(sub_path) match = re.match("(.*)(\....?)(\..*)", sub_basename) if match is not None: sub_basename_root = match.group(1) sub_language = match.group(2) sub_ext = match.group(3) if iso639.find(sub_language[1:]) is not None: sub_ext = sub_language + sub_ext else: sub_basename_root = sub_basename_root + sub_language else: sub_basename_root, sub_ext = os.path.splitext(sub_basename) video_basename = os.path.basename(video_path) video_basename_root, video_ext = os.path.splitext(video_basename) if sub_basename_root != video_basename_root: sub_basename = video_basename_root + sub_ext dest_path = os.path.join(os.path.dirname(video_path), sub_basename) if sub_path != dest_path: if os.path.exists(dest_path): os.remove(dest_path) shutil.copyfile(sub_path, dest_path) return dest_path
def _get_subtitle_track_name(self, index): """Returns the language for the track at the specified index. """ if not self.supports_subtitles: return None tag_list = self.playbin.emit("get-text-tags", index) lang = None if tag_list is not None and gst.TAG_LANGUAGE_CODE in tag_list: code = tag_list[gst.TAG_LANGUAGE_CODE] lang = iso639.find(code) if lang is None: return None else: return lang['name']
def get_subtitle_tracks(self): tracks = list() if self.movie is not None: for track in self.movie.tracks(): if self.is_subtitle_track(track): media = track.media().quickTimeMedia() lang = GetMediaLanguage(media) display_name = track.attributeForKey_(QTTrackDisplayNameAttribute) if lang == 32767: # 32764 = langUndefined name = display_name else: lang_code = script_codes.map_to_two_letters_code(lang) lang_info = iso639.find(lang_code) if lang_info is None: name = display_name else: name = lang_info["name"] if name != display_name: name = "%s (%s)" % (name, display_name) track_id = track.attributeForKey_(QTTrackIDAttribute) tracks.append((track_id, name)) return tracks
def copy_subtitle_file(sub_path, video_path): """Copies the subtitle file located at sub_path alongside the video file located at video_path. It also changes the name so that the subtitle file follows the rules of sidecar files. Returns the path the subtitle file was copied to. """ from miro import iso639 sub_basename = os.path.basename(sub_path) match = re.match("(.*)(\....?)(\..*)", sub_basename) if match is not None: sub_basename_root = match.group(1) sub_language = match.group(2) sub_ext = match.group(3) if iso639.find(sub_language[1:]) is not None: sub_ext = sub_language + sub_ext else: sub_basename_root = sub_basename_root + sub_language else: sub_basename_root, sub_ext = os.path.splitext(sub_basename) video_basename = os.path.basename(video_path) video_basename_root, video_ext = os.path.splitext(video_basename) if sub_basename_root != video_basename_root: sub_basename = video_basename_root + sub_ext dest_path = os.path.join(os.path.dirname(video_path), sub_basename) if sub_path != dest_path: try: if os.path.exists(dest_path): os.remove(dest_path) shutil.copyfile(sub_path, dest_path) except EnvironmentError: logging.exception('unable to remove existing subtitle file ' 'or copy subtitle file') dest_path = '' return dest_path
def _get_subtitle_file_name(self, filename): """Returns the language for the file at the specified filename. """ if not self.supports_subtitles: return None basename, ext = os.path.splitext(filename) movie_file, code = os.path.splitext(basename) # if the filename is like "foo.srt" and "srt", then there # is no language code, so we return None if not code: return None # remove . in the code so we end up with what's probably # a two or three letter language code if "." in code: code = code.replace(".", "") lang = iso639.find(code) if lang is None: return None else: return lang['name']
def get_languages(): from miro import iso639 lang_paths = [] for path, dirs, files in os.walk(app.config.get(prefs.GETTEXT_PATHNAME)): if "miro.mo" in files: lang_paths.append(path) codes = [path.split(os.sep)[-2] for path in lang_paths] langs = [] for code in codes: if "_" in code: langcode, country = code.split("_") country = " (%s)" % country else: langcode = code country = "" lang = iso639.find(langcode) if lang is None: lang = code else: lang = lang['name'] + country # FIXME # note that this isn't completely correct, technically # it is <lang>_<region>.<encoding> (e.g. zh_TW.Big5). But in 2010 # the system is usually smart enough to figure this out. The # language stuff needs a closer look-at. (technically, the "country" # variable used here is incorrect too, it's actually 'region'). langs.append((code, lang)) langs.sort(key=lambda x: x[1]) langs.insert(0, ("en", "English")) return langs