Beispiel #1
0
def list_args(args):
    """
    Check if there's any list args.
    """
    if args.list_formats:
        print(_("List of output formats:\n"))
        print("{column_1}{column_2}".format(column_1=lang_code_utils.wjust(
            _("Format"), 18),
                                            column_2=_("Description")))
        for subtitles_format, format_description in sorted(
                constants.OUTPUT_FORMAT.items()):
            print("{column_1}{column_2}".format(column_1=lang_code_utils.wjust(
                subtitles_format, 18),
                                                column_2=format_description))
        print(_("\nList of input formats:\n"))
        print("{column_1}{column_2}".format(column_1=lang_code_utils.wjust(
            _("Format"), 18),
                                            column_2=_("Description")))
        for subtitles_format, format_description in sorted(
                constants.INPUT_FORMAT.items()):
            print("{column_1}{column_2}".format(column_1=lang_code_utils.wjust(
                subtitles_format, 18),
                                                column_2=format_description))
        return True

    if args.detect_sub_language:
        print(
            _("Use py-googletrans to detect a sub file's first line language.")
        )
        pysubs2_obj = pysubs2.SSAFile.load(args.detect_sub_language)
        translator = googletrans.Translator(user_agent=args.user_agent,
                                            service_urls=args.service_urls)
        result_obj = translator.detect(pysubs2_obj.events[0].text)
        print("{column_1}{column_2}".format(column_1=lang_code_utils.wjust(
            _("Lang code"), 18),
                                            column_2=_("Confidence")))
        print("{column_1}{column_2}\n".format(column_1=lang_code_utils.wjust(
            result_obj.lang, 18),
                                              column_2=result_obj.confidence))
        args.list_speech_codes = result_obj.lang

    if args.list_speech_codes:
        if args.list_speech_codes == ' ':
            print(_("List of all lang codes for speech-to-text:\n"))
            print("{column_1}{column_2}".format(column_1=lang_code_utils.wjust(
                _("Lang code"), 18),
                                                column_2=_("Description")))
            for code, language in sorted(
                    constants.SPEECH_TO_TEXT_LANGUAGE_CODES.items()):
                print("{column_1}{column_2}".format(
                    column_1=lang_code_utils.wjust(code, 18),
                    column_2=language))
        else:
            print(_("Match Google Speech V2 lang codes."))
            lang_code_utils.match_print(
                dsr_lang=args.list_speech_codes,
                match_list=list(
                    constants.SPEECH_TO_TEXT_LANGUAGE_CODES.keys()),
                min_score=args.min_score)
        return True

    if args.list_translation_codes:
        if args.list_translation_codes == ' ':
            print(_("List of all lang codes for translation:\n"))
            print("{column_1}{column_2}".format(column_1=lang_code_utils.wjust(
                _("Lang code"), 18),
                                                column_2=_("Description")))
            for code, language in sorted(
                    constants.TRANSLATION_LANGUAGE_CODES.items()):
                print("{column_1}{column_2}".format(
                    column_1=lang_code_utils.wjust(code, 18),
                    column_2=language))
        else:
            print(_("Match py-googletrans lang codes."))
            lang_code_utils.match_print(
                dsr_lang=args.list_translation_codes,
                match_list=list(constants.TRANSLATION_LANGUAGE_CODES.keys()),
                min_score=args.min_score)
        return True

    return False
Beispiel #2
0
def validate_sp_args(args):  # pylint: disable=too-many-branches,too-many-return-statements, too-many-statements
    """
    Check that the commandline arguments passed to autosub are valid
    for subtitles processing.
    """
    if args.src_language:
        if args.dst_language is None:
            raise exceptions.AutosubException(
                _("Error: Destination language not provided."))

        is_src_matched = False
        is_dst_matched = False

        for key in googletrans.constants.LANGUAGES:
            if args.src_language.lower() == key.lower():
                args.src_language = key
                is_src_matched = True
            if args.dst_language.lower() == key.lower():
                args.dst_language = key
                is_dst_matched = True

        if not is_src_matched:
            if not args.gtransv2:
                if args.best_match and 'src' in args.best_match:
                    print(
                        _("Warning: Source language \"{src}\" not supported. "
                          "Run with \"-lsc\"/\"--list-translation-codes\" "
                          "to see all supported languages.").format(
                              src=args.src_language))
                    best_result = lang_code_utils.match_print(
                        dsr_lang=args.src_language,
                        match_list=list(
                            googletrans.constants.LANGUAGES.keys()),
                        min_score=args.min_score)
                    if best_result:
                        print(
                            _("Use \"{lang_code}\" instead.").format(
                                lang_code=best_result[0]))
                        args.src_language = best_result[0]
                    else:
                        raise exceptions.AutosubException(
                            _("Match failed. Still using \"{lang_code}\". "
                              "Program stopped.").format(
                                  lang_code=args.src_language))

                else:
                    raise exceptions.AutosubException(
                        _("Error: Source language \"{src}\" not supported. "
                          "Run with \"-lsc\"/\"--list-translation-codes\" "
                          "to see all supported languages. "
                          "Or use \"-bm\"/\"--best-match\" to get a best match."
                          ).format(src=args.src_language))

        if not is_dst_matched:
            if not args.gtransv2:
                if args.best_match and 'd' in args.best_match:
                    print(
                        _("Warning: Destination language \"{dst}\" not supported. "
                          "Run with \"-lsc\"/\"--list-translation-codes\" "
                          "to see all supported languages.").format(
                              dst=args.dst_language))
                    best_result = lang_code_utils.match_print(
                        dsr_lang=args.dst_language,
                        match_list=list(
                            googletrans.constants.LANGUAGES.keys()),
                        min_score=args.min_score)
                    if best_result:
                        print(
                            _("Use \"{lang_code}\" instead.").format(
                                lang_code=best_result[0]))
                        args.dst_language = best_result[0]
                    else:
                        raise exceptions.AutosubException(
                            _("Match failed. Still using \"{lang_code}\". "
                              "Program stopped.").format(
                                  lang_code=args.dst_language))

                else:
                    raise exceptions.AutosubException(
                        _("Error: Destination language \"{dst}\" not supported. "
                          "Run with \"-lsc\"/\"--list-translation-codes\" "
                          "to see all supported languages. "
                          "Or use \"-bm\"/\"--best-match\" to get a best match."
                          ).format(dst=args.dst_language))

        if args.dst_language == args.src_language:
            raise exceptions.AutosubException(
                _("Error: Source language is the same as the Destination language."
                  ))

    else:
        raise exceptions.AutosubException(
            _("Error: Source language not provided."))

    if args.styles == ' ':
        # when args.styles is used but without option
        # its value is ' '
        if not args.ext_regions:
            raise exceptions.AutosubException(
                _("Error: External speech regions file not provided."))
        else:
            args.styles = args.ext_regions
Beispiel #3
0
def validate_aovp_args(args):  # pylint: disable=too-many-branches, too-many-return-statements, too-many-statements
    """
    Check that the commandline arguments passed to autosub are valid
    for audio or video processing.
    """
    if args.sleep_seconds < 0 or args.lines_per_trans < 0:
        raise exceptions.AutosubException(
            _("Error: \"-slp\"/\"--sleep-seconds\" arg is illegal."))

    if args.speech_language:  # pylint: disable=too-many-nested-blocks
        if not args.gspeechv2:
            args.speech_language = args.speech_language.lower()
            if args.speech_language \
                    not in constants.SPEECH_TO_TEXT_LANGUAGE_CODES:
                print(
                    _("Warning: Speech language \"{src}\" not recommended. "
                      "Run with \"-lsc\"/\"--list-speech-codes\" "
                      "to see all supported languages.").format(
                          src=args.speech_language))
                if args.best_match and 's' in args.best_match:
                    best_result = lang_code_utils.match_print(
                        dsr_lang=args.speech_language,
                        match_list=list(
                            constants.SPEECH_TO_TEXT_LANGUAGE_CODES.keys()),
                        min_score=args.min_score)
                    if best_result:
                        print(
                            _("Use langcodes-py2 to standardize the result."))
                        args.speech_language = langcodes.standardize_tag(
                            best_result[0])
                        print(
                            _("Use \"{lang_code}\" instead.").format(
                                lang_code=args.speech_language))
                    else:
                        print(
                            _("Match failed. Still using \"{lang_code}\".").
                            format(lang_code=args.speech_language))

            if args.min_confidence < 0.0 or args.min_confidence > 1.0:
                raise exceptions.AutosubException(
                    _("Error: The arg of \"-mnc\"/\"--min-confidence\" isn't legal."
                      ))

        if args.dst_language is None:
            print(
                _("Destination language not provided. "
                  "Only performing speech recognition."))

        else:
            if not args.src_language:
                print(
                    _("Source language not provided. "
                      "Use Speech language instead."))
                args.src_language = args.speech_language
                if not args.best_match:
                    args.best_match = {'src'}
                elif 'src' not in args.best_match:
                    args.best_match.add('src')

            is_src_matched = False
            is_dst_matched = False

            for key in googletrans.constants.LANGUAGES:
                if args.src_language.lower() == key.lower():
                    args.src_language = key
                    is_src_matched = True
                if args.dst_language.lower() == key.lower():
                    args.dst_language = key
                    is_dst_matched = True

            if not is_src_matched:
                if not args.gtransv2:
                    if args.best_match and 'src' in args.best_match:
                        print(
                            _("Warning: Source language \"{src}\" not supported. "
                              "Run with \"-lsc\"/\"--list-translation-codes\" "
                              "to see all supported languages.").format(
                                  src=args.src_language))
                        best_result = lang_code_utils.match_print(
                            dsr_lang=args.src_language,
                            match_list=list(
                                googletrans.constants.LANGUAGES.keys()),
                            min_score=args.min_score)
                        if best_result:
                            print(
                                _("Use \"{lang_code}\" instead.").format(
                                    lang_code=best_result[0]))
                            args.src_language = best_result[0]
                        else:
                            raise exceptions.AutosubException(
                                _("Match failed. Still using \"{lang_code}\". "
                                  "Program stopped.").format(
                                      lang_code=args.src_language))

                    else:
                        raise exceptions.AutosubException(
                            _("Error: Source language \"{src}\" not supported. "
                              "Run with \"-lsc\"/\"--list-translation-codes\" "
                              "to see all supported languages. "
                              "Or use \"-bm\"/\"--best-match\" to get a best match."
                              ).format(src=args.src_language))

            if not is_dst_matched:
                if not args.gtransv2:
                    if args.best_match and 'd' in args.best_match:
                        print(
                            _("Warning: Destination language \"{dst}\" not supported. "
                              "Run with \"-lsc\"/\"--list-translation-codes\" "
                              "to see all supported languages.").format(
                                  dst=args.dst_language))
                        best_result = lang_code_utils.match_print(
                            dsr_lang=args.dst_language,
                            match_list=list(
                                googletrans.constants.LANGUAGES.keys()),
                            min_score=args.min_score)
                        if best_result:
                            print(
                                _("Use \"{lang_code}\" instead.").format(
                                    lang_code=best_result[0]))
                            args.dst_language = best_result[0]
                        else:
                            raise exceptions.AutosubException(
                                _("Match failed. Still using \"{lang_code}\". "
                                  "Program stopped.").format(
                                      lang_code=args.dst_language))

                    else:
                        raise exceptions.AutosubException(
                            _("Error: Destination language \"{dst}\" not supported. "
                              "Run with \"-lsc\"/\"--list-translation-codes\" "
                              "to see all supported languages. "
                              "Or use \"-bm\"/\"--best-match\" to get a best match."
                              ).format(dst=args.dst_language))

        if args.dst_language == args.speech_language \
                or args.src_language == args.dst_language:
            print(
                _("Speech language is the same as the Destination language. "
                  "Only performing speech recognition."))
            args.dst_language = None
            args.src_language = None

    else:
        if args.ext_regions:
            if not args.keep:
                raise exceptions.AutosubException(
                    _("You've already input times. "
                      "No works done."))

        else:
            print(
                _("Speech language not provided. "
                  "Only performing speech regions detection."))

    if args.styles == ' ':
        # when args.styles is used but without option
        # its value is ' '
        if not args.ext_regions:
            raise exceptions.AutosubException(
                _("Error: External speech regions file not provided."))
        else:
            args.styles = args.ext_regions