Example #1
0
def get_language_from_stream(lang_code):
    if lang_code:
        lang = Locale.Language.Match(lang_code)
        if lang and lang != "xx":
            # Log.Debug("Found language: %r", lang)
            return Language.fromietf(lang)
        elif lang:
            try:
                return language_from_stream(lang)
            except LanguageError:
                pass
Example #2
0
def set_existing_languages(video, video_info, external_subtitles=False, embedded_subtitles=False, known_embedded=None,
                           forced_only=False, stored_subs=None, languages=None, only_one=False):
    logger.debug(u"Determining existing subtitles for %s", video.name)

    # scan for external subtitles
    external_langs_found = set(search_external_subtitles(video.name, forced_tag=forced_only, languages=languages,
                                                         only_one=only_one).values())

    # found external subtitles should be considered?
    if external_subtitles:
        # |= is update, thanks plex
        video.subtitle_languages.update(external_langs_found)

    else:
        # did we already download subtitles for this?
        if stored_subs and external_langs_found:
            for lang in external_langs_found:
                if has_external_subtitle(video_info["plex_part"].id, stored_subs, lang):
                    logger.info("Not re-downloading subtitle for language %s, it already exists on the filesystem",
                                lang)
                    video.subtitle_languages.add(lang)

    # add known embedded subtitles
    if embedded_subtitles and known_embedded:
        embedded_subtitle_languages = set()
        # mp4 and stuff, check burned in
        for language in known_embedded:
            try:
                embedded_subtitle_languages.add(language_from_stream(language))

            except LanguageError:
                logger.error('Embedded subtitle track language %r is not a valid language', language)
                embedded_subtitle_languages.add(Language('und'))

            logger.debug('Found embedded subtitle %r', embedded_subtitle_languages)
            video.subtitle_languages.update(embedded_subtitle_languages)
Example #3
0
def prepare_video(pms_video_info, ignore_all=False, hints=None, rating_key=None, providers=None, skip_hashing=False):
    """
    returnes a subliminal/guessit-refined parsed video
    :param pms_video_info:
    :param ignore_all:
    :param hints:
    :param rating_key:
    :return:
    """
    embedded_subtitles = not ignore_all and Prefs['subtitles.scan.embedded']
    external_subtitles = not ignore_all and Prefs['subtitles.scan.external']

    plex_part = pms_video_info["plex_part"]

    if ignore_all:
        Log.Debug("Force refresh intended.")

    Log.Debug("Detecting streams: %s, external_subtitles=%s, embedded_subtitles=%s" % (
        plex_part.file, external_subtitles, embedded_subtitles))

    known_embedded = []
    parts = []
    for media in list(Plex["library"].metadata(rating_key))[0].media:
        parts += media.parts

    plexpy_part = None
    for part in parts:
        if int(part.id) == int(plex_part.id):
            plexpy_part = part

    # embedded subtitles
    # fixme: skip the whole scanning process if known_embedded == wanted languages?
    audio_languages = []
    if plexpy_part:
        for stream in plexpy_part.streams:
            if stream.stream_type == 2:
                lang = None
                try:
                    lang = language_from_stream(stream.language_code)
                except LanguageError:
                    Log.Debug("Couldn't detect embedded audio stream language: %s", stream.language_code)

                # treat unknown language as lang1?
                if not lang and config.treat_und_as_first:
                    lang = Language.rebuild(list(config.lang_list)[0])

                audio_languages.append(lang)

            # subtitle stream
            elif stream.stream_type == 3 and embedded_subtitles:
                is_forced = helpers.is_stream_forced(stream)

                if ((config.forced_only or config.forced_also) and is_forced) or not is_forced:
                    # embedded subtitle
                    # fixme: tap into external subtitles here instead of scanning for ourselves later?
                    if stream.codec and getattr(stream, "index", None):
                        if config.exotic_ext or stream.codec.lower() in config.text_based_formats:
                            lang = None
                            try:
                                lang = language_from_stream(stream.language_code)
                            except LanguageError:
                                Log.Debug("Couldn't detect embedded subtitle stream language: %s", stream.language_code)

                            # treat unknown language as lang1?
                            if not lang and config.treat_und_as_first:
                                lang = Language.rebuild(list(config.lang_list)[0])

                            if lang:
                                if is_forced:
                                    lang.forced = True
                                known_embedded.append(lang)
    else:
        Log.Warn("Part %s missing of %s, not able to scan internal streams", plex_part.id, rating_key)

    # metadata subtitles
    known_metadata_subs = set()
    meta_subs = get_subtitles_from_metadata(plex_part)
    for language, subList in meta_subs.iteritems():
        try:
            lang = Language.fromietf(Locale.Language.Match(language))
        except LanguageError:
            if config.treat_und_as_first:
                lang = Language.rebuild(list(config.lang_list)[0])
            else:
                continue

        if subList:
            for key in subList:
                if key.startswith("subzero_md_forced"):
                    lang = Language.rebuild(lang, forced=True)

                known_metadata_subs.add(lang)
                Log.Debug("Found metadata subtitle %r:%s for %s", lang, key, plex_part.file)

    Log.Debug("Known metadata subtitles: %r", known_metadata_subs)
    Log.Debug("Known embedded subtitles: %r", known_embedded)

    subtitle_storage = get_subtitle_storage()
    stored_subs = subtitle_storage.load(rating_key)
    subtitle_storage.destroy()

    try:
        # get basic video info scan (filename)
        video = parse_video(plex_part.file, hints, skip_hashing=config.low_impact_mode or skip_hashing,
                            providers=providers)

        # set stream languages
        if audio_languages:
            video.audio_languages = audio_languages
            Log.Info("Found audio streams: %s" % ", ".join([str(l) for l in audio_languages]))

        if not ignore_all:
            set_existing_languages(video, pms_video_info, external_subtitles=external_subtitles,
                                   embedded_subtitles=embedded_subtitles, known_embedded=known_embedded,
                                   stored_subs=stored_subs, languages=config.lang_list,
                                   only_one=config.only_one, known_metadata_subs=known_metadata_subs)

        # add video fps info
        video.fps = plex_part.fps
        return video

    except ValueError:
        Log.Warn("File could not be guessed: %s: %s", plex_part.file, traceback.format_exc())
Example #4
0
def prepare_video(pms_video_info,
                  ignore_all=False,
                  hints=None,
                  rating_key=None,
                  providers=None,
                  skip_hashing=False):
    """
    returnes a subliminal/guessit-refined parsed video
    :param pms_video_info:
    :param ignore_all:
    :param hints:
    :param rating_key:
    :return:
    """
    embedded_subtitles = not ignore_all and Prefs['subtitles.scan.embedded']
    external_subtitles = not ignore_all and Prefs['subtitles.scan.external']

    plex_part = pms_video_info["plex_part"]

    if ignore_all:
        Log.Debug("Force refresh intended.")

    Log.Debug(
        "Detecting streams: %s, account_for_external_subtitles=%s, account_for_embedded_subtitles=%s"
        % (plex_part.file, external_subtitles, embedded_subtitles))

    known_embedded = []
    parts = []
    for media in list(Plex["library"].metadata(rating_key))[0].media:
        parts += media.parts

    plexpy_part = None
    for part in parts:
        if int(part.id) == int(plex_part.id):
            plexpy_part = part

    # embedded subtitles
    # fixme: skip the whole scanning process if known_embedded == wanted languages?
    audio_languages = []
    if plexpy_part:
        update_stream_info(plexpy_part)
        for stream in plexpy_part.streams:
            if stream.stream_type == 2:
                lang = None
                try:
                    lang = language_from_stream(stream.language_code)
                except LanguageError:
                    Log.Info(
                        "Couldn't detect embedded audio stream language: %s",
                        stream.language_code)

                # treat unknown language as lang1?
                if not lang and config.treat_und_as_first:
                    lang = Language.rebuild(list(config.lang_list)[0])
                    Log.Info("Assuming language %s for audio stream: %s", lang,
                             getattr(stream, "index", None))

                audio_languages.append(lang)

            # subtitle stream
            elif stream.stream_type == 3 and embedded_subtitles:
                is_forced = is_stream_forced(stream)

                if ((config.forced_only or config.forced_also)
                        and is_forced) or not is_forced:
                    # embedded subtitle
                    # fixme: tap into external subtitles here instead of scanning for ourselves later?
                    if stream.codec and getattr(stream, "index", None):
                        if config.exotic_ext or stream.codec.lower(
                        ) in config.text_based_formats:
                            lang = None
                            try:
                                lang = language_from_stream(
                                    stream.language_code)
                            except LanguageError:
                                Log.Info(
                                    "Couldn't detect embedded subtitle stream language: %s",
                                    stream.language_code)

                            # treat unknown language as lang1?
                            if not lang and config.treat_und_as_first:
                                lang = Language.rebuild(
                                    list(config.lang_list)[0])
                                Log.Info(
                                    "Assuming language %s for subtitle stream: %s",
                                    lang, getattr(stream, "index", None))

                            if lang:
                                if is_forced:
                                    lang.forced = True
                                known_embedded.append(lang)
    else:
        Log.Warn("Part %s missing of %s, not able to scan internal streams",
                 plex_part.id, rating_key)

    # metadata subtitles
    known_metadata_subs = set()
    meta_subs = get_subtitles_from_metadata(plex_part)
    for language, subList in meta_subs.iteritems():
        try:
            lang = Language.fromietf(Locale.Language.Match(language))
        except LanguageError:
            if config.treat_und_as_first:
                lang = Language.rebuild(list(config.lang_list)[0])
            else:
                continue

        if subList:
            for key in subList:
                if key.startswith("subzero_md_forced"):
                    lang = Language.rebuild(lang, forced=True)

                known_metadata_subs.add(lang)
                Log.Debug("Found metadata subtitle %r:%s for %s", lang, key,
                          plex_part.file)

    Log.Debug("Known metadata subtitles: %r", known_metadata_subs)
    Log.Debug("Known embedded subtitles: %r", known_embedded)

    subtitle_storage = get_subtitle_storage()
    stored_subs = subtitle_storage.load(rating_key)
    subtitle_storage.destroy()

    try:
        # get basic video info scan (filename)
        video = parse_video(plex_part.file,
                            hints,
                            skip_hashing=config.low_impact_mode
                            or skip_hashing,
                            providers=providers)

        # set stream languages
        if audio_languages:
            video.audio_languages = audio_languages
            Log.Info("Found audio streams: %s" %
                     ", ".join([str(l) for l in audio_languages]))

        if not ignore_all:
            set_existing_languages(
                video,
                pms_video_info,
                external_subtitles=external_subtitles,
                embedded_subtitles=embedded_subtitles,
                known_embedded=known_embedded,
                stored_subs=stored_subs,
                languages=config.lang_list,
                only_one=config.only_one,
                known_metadata_subs=known_metadata_subs,
                match_strictness=config.ext_match_strictness)

        # add video fps info
        video.fps = plex_part.fps
        return video

    except ValueError:
        Log.Warn("File could not be guessed: %s: %s", plex_part.file,
                 traceback.format_exc())
Example #5
0
def scan_video(pms_video_info,
               ignore_all=False,
               hints=None,
               rating_key=None,
               providers=None,
               skip_hashing=False):
    """
    returnes a subliminal/guessit-refined parsed video
    :param pms_video_info:
    :param ignore_all:
    :param hints:
    :param rating_key:
    :return:
    """
    embedded_subtitles = not ignore_all and Prefs['subtitles.scan.embedded']
    external_subtitles = not ignore_all and Prefs['subtitles.scan.external']

    plex_part = pms_video_info["plex_part"]

    if ignore_all:
        Log.Debug("Force refresh intended.")

    Log.Debug(
        "Scanning video: %s, external_subtitles=%s, embedded_subtitles=%s" %
        (plex_part.file, external_subtitles, embedded_subtitles))

    known_embedded = []
    parts = []
    for media in list(Plex["library"].metadata(rating_key))[0].media:
        parts += media.parts

    plexpy_part = None
    for part in parts:
        if int(part.id) == int(plex_part.id):
            plexpy_part = part

    # embedded subtitles
    # fixme: skip the whole scanning process if known_embedded == wanted languages?
    if plexpy_part:
        if embedded_subtitles:
            for stream in plexpy_part.streams:
                # subtitle stream
                if stream.stream_type == 3:
                    is_forced = helpers.is_stream_forced(stream)

                    if (config.forced_only and is_forced) or \
                            (not config.forced_only and not is_forced):

                        # embedded subtitle
                        # fixme: tap into external subtitles here instead of scanning for ourselves later?
                        if stream.codec and getattr(stream, "index", None):
                            if config.exotic_ext or stream.codec.lower(
                            ) in config.text_based_formats:
                                lang = None
                                try:
                                    lang = language_from_stream(
                                        stream.language_code)
                                except LanguageError:
                                    Log.Debug(
                                        "Couldn't detect embedded subtitle stream language: %s",
                                        stream.language_code)

                                # treat unknown language as lang1?
                                if not lang and config.treat_und_as_first:
                                    lang = list(config.lang_list)[0]

                                if lang:
                                    known_embedded.append(lang.alpha3)
    else:
        Log.Warn("Part %s missing of %s, not able to scan internal streams",
                 plex_part.id, rating_key)

    Log.Debug("Known embedded: %r", known_embedded)

    subtitle_storage = get_subtitle_storage()
    stored_subs = subtitle_storage.load(rating_key)
    subtitle_storage.destroy()

    try:
        # get basic video info scan (filename)
        video = parse_video(plex_part.file,
                            hints,
                            skip_hashing=config.low_impact_mode
                            or skip_hashing,
                            providers=providers)

        if not ignore_all:
            set_existing_languages(video,
                                   pms_video_info,
                                   external_subtitles=external_subtitles,
                                   embedded_subtitles=embedded_subtitles,
                                   known_embedded=known_embedded,
                                   forced_only=config.forced_only,
                                   stored_subs=stored_subs,
                                   languages=config.lang_list,
                                   only_one=config.only_one)

        # add video fps info
        video.fps = plex_part.fps
        return video

    except ValueError:
        Log.Warn("File could not be guessed: %s: %s", plex_part.file,
                 traceback.format_exc())