예제 #1
0
def get_embedded_subtitle_streams(part, requested_language=None, skip_duplicate_unknown=True, get_forced=None):
    streams = []
    has_unknown = False
    for stream in part.streams:
        # subtitle stream
        if stream.stream_type == 3 and not stream.stream_key and stream.codec in TEXT_SUBTITLE_EXTS:
            language = helpers.get_language_from_stream(stream.language_code)
            is_unknown = False
            found_requested_language = requested_language and requested_language == language
            is_forced = helpers.is_stream_forced(stream)

            if get_forced is not None:
                if (get_forced and not is_forced) or (not get_forced and is_forced):
                    continue

            if not language and config.treat_und_as_first:
                # only consider first unknown subtitle stream
                if has_unknown and skip_duplicate_unknown:
                    continue

                language = list(config.lang_list)[0]
                is_unknown = True
                has_unknown = True

            if not requested_language or found_requested_language or has_unknown:
                streams.append({"stream": stream, "is_unknown": is_unknown, "language": language,
                                "is_forced": is_forced})

                if found_requested_language:
                    break

    return streams
예제 #2
0
def get_embedded_subtitle_streams(part,
                                  requested_language=None,
                                  skip_duplicate_unknown=True,
                                  skip_unknown=False):
    streams = []
    streams_unknown = []
    has_unknown = False
    found_requested_language = False
    for stream in part.streams:
        # subtitle stream
        if stream.stream_type == 3 and not stream.stream_key and stream.codec in TEXT_SUBTITLE_EXTS:
            is_forced = helpers.is_stream_forced(stream)
            language = helpers.get_language_from_stream(stream.language_code)
            if language:
                language = Language.rebuild(language, forced=is_forced)

            is_unknown = False
            found_requested_language = requested_language and requested_language == language

            if not language and config.treat_und_as_first:
                # only consider first unknown subtitle stream
                if has_unknown and skip_duplicate_unknown:
                    continue

                language = Language.rebuild(list(config.lang_list)[0],
                                            forced=is_forced)
                is_unknown = True
                has_unknown = True
                streams_unknown.append({
                    "stream": stream,
                    "is_unknown": is_unknown,
                    "language": language,
                    "is_forced": is_forced
                })

            if not requested_language or found_requested_language:
                streams.append({
                    "stream": stream,
                    "is_unknown": is_unknown,
                    "language": language,
                    "is_forced": is_forced
                })

                if found_requested_language:
                    break

    if streams_unknown and not found_requested_language and not skip_unknown:
        streams = streams_unknown

    return streams
예제 #3
0
def get_embedded_subtitle_streams(part, requested_language=None, skip_duplicate_unknown=True, skip_unknown=False):
    streams = []
    streams_unknown = []
    has_unknown = False
    found_requested_language = False
    for stream in part.streams:
        # subtitle stream
        if stream.stream_type == 3 and not stream.stream_key and stream.codec in TEXT_SUBTITLE_EXTS:
            is_forced = helpers.is_stream_forced(stream)
            language = helpers.get_language_from_stream(stream.language_code)
            if language:
                language = Language.rebuild(language, forced=is_forced)

            is_unknown = False
            found_requested_language = requested_language and requested_language == language

            if not language and config.treat_und_as_first:
                # only consider first unknown subtitle stream
                if has_unknown and skip_duplicate_unknown:
                    continue

                language = Language.rebuild(list(config.lang_list)[0], forced=is_forced)
                is_unknown = True
                has_unknown = True
                streams_unknown.append({"stream": stream, "is_unknown": is_unknown, "language": language,
                                        "is_forced": is_forced})

            if not requested_language or found_requested_language:
                streams.append({"stream": stream, "is_unknown": is_unknown, "language": language,
                                "is_forced": is_forced})

                if found_requested_language:
                    break

    if streams_unknown and not found_requested_language and not skip_unknown:
        streams = streams_unknown

    return streams
예제 #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, 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())
예제 #5
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,
                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())
예제 #6
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())