示例#1
0
def validate(args):
    """
    Check that the CLI arguments passed to autosub are valid.
    """
    if args.format not in FORMATTERS:
        print(
            "Subtitle format not supported. "
            "Run with --list-formats to see all supported formats."
        )
        return False

    if args.src_language not in list(LANGUAGE_CODES.keys()):
        print(
            "Source language not supported. "
            "Run with --list-languages to see all supported languages."
        )
        return False

    if args.dst_language not in list(LANGUAGE_CODES.keys()):
        print(
            "Destination language not supported. "
            "Run with --list-languages to see all supported languages."
        )
        return False

    if not args.source_path:
        print("Error: You need to specify a source path.")
        return False

    return True
示例#2
0
def validate(args):
    """
    Check that the CLI arguments passed to autosub are valid.
    """
    if args.format not in FORMATTERS:
        print("Subtitle format not supported. "
              "Run with --list-formats to see all supported formats.")
        return False

    if args.src_language not in LANGUAGE_CODES.keys():
        print("Source language not supported. "
              "Run with --list-languages to see all supported languages.")
        return False

    if args.dst_language not in LANGUAGE_CODES.keys():
        print("Destination language not supported. "
              "Run with --list-languages to see all supported languages.")
        return False

    if not args.source_path:
        print("Error: You need to specify a source path.")
        return False

    source_file_name, source_file_extension = os.path.splitext(
        args.source_path)
    if os.path.isfile(source_file_name + '.srt'):
        print("Error: Subtitle is existing")
        return False

    return True
示例#3
0
def start(args):
    if args.format not in FORMATTERS.keys():
        print("Subtitle format not supported. "
              "Run with --list-formats to see all supported formats.")
        return 1

    if args.src_language not in LANGUAGE_CODES.keys():
        print("Source language not supported. "
              "Run with --list-languages to see all supported languages.")
        return 1

    if args.dst_language not in LANGUAGE_CODES.keys():
        print("Destination language not supported. "
              "Run with --list-languages to see all supported languages.")
        return 1

    if not args.source_path:
        print("Error: You need to specify a source path.")
        return 1

    try:
        subtitle_file_path = generate_subtitles(
            source_path=args.source_path,
            concurrency=args.concurrency,
            src_language=args.src_language,
            dst_language=args.dst_language,
            api_key=args.api_key,
            subtitle_file_format=args.format,
            output=args.output,
        )
        print("Subtitles file created at {}".format(subtitle_file_path))
    except KeyboardInterrupt:
        return 1

    return 0
示例#4
0
def main():
    """
    Run autosub as a command-line program.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('source_path', help="Path to the video or audio file to subtitle",
                        nargs='?')
    parser.add_argument('-C', '--concurrency', help="Number of concurrent API requests to make",
                        type=int, default=DEFAULT_CONCURRENCY)
    parser.add_argument('-o', '--output',
                        help="Output path for subtitles (by default, subtitles are saved in \
                        the same directory and name as the source path)")
    parser.add_argument('-F', '--format', help="Destination subtitle format",
                        default=DEFAULT_SUBTITLE_FORMAT)
    parser.add_argument('-S', '--src-language', help="Language spoken in source file",
                        default=DEFAULT_SRC_LANGUAGE)
    parser.add_argument('-D', '--dst-language', help="Desired language for the subtitles",
                        default=DEFAULT_DST_LANGUAGE)
    parser.add_argument('-K', '--api-key',
                        help="The Google Translate API key to be used. \
                        (Required for subtitle translation)")
    parser.add_argument('--list-formats', help="List all available subtitle formats",
                        action='store_true')
    parser.add_argument('--list-languages', help="List all available source/destination languages",
                        action='store_true')

    args = parser.parse_args()

    if args.list_formats:
        print("List of formats:")
        for subtitle_format in FORMATTERS:
            print("{format}".format(format=subtitle_format))
        return 0

    if args.list_languages:
        print("List of all languages:")
        for code, language in sorted(LANGUAGE_CODES.items()):
            print("{code}\t{language}".format(code=code, language=language))
        return 0

    if not validate(args):
        return 1

    try:
        subtitle_file_path = generate_subtitles(
            source_path=args.source_path,
            concurrency=args.concurrency,
            src_language=args.src_language,
            dst_language=args.dst_language,
            api_key=args.api_key,
            subtitle_file_format=args.format,
            output=args.output,
        )
        print("Subtitles file created at {}".format(subtitle_file_path))
    except KeyboardInterrupt:
        return 1

    return 0
示例#5
0
        def main():
            parser = argparse.ArgumentParser()
            parser.add_argument(
                'source_path',
                help="Path to the video or audio file to subtitle",
                nargs='?')
            parser.add_argument(
                '-C',
                '--concurrency',
                help="Number of concurrent API requests to make",
                type=int,
                default=10)
            parser.add_argument(
                '-o',
                '--output',
                help=
                "Output path for subtitles (by default, subtitles are saved in \ the same directory and name as the source path)"
            )
            parser.add_argument('-F',
                                '--format',
                                help="Destination subtitle format",
                                default="srt")
            parser.add_argument('-S',
                                '--src-language',
                                help="Language spoken in source file",
                                default="en")
            parser.add_argument('-D',
                                '--dst-language',
                                help="Desired language for the subtitles",
                                default="en")
            parser.add_argument(
                '-K',
                '--api-key',
                help=
                "The Google Translate API key to be used. (Required for subtitle translation)"
            )
            parser.add_argument('--list-formats',
                                help="List all available subtitle formats",
                                action='store_true')
            parser.add_argument(
                '--list-languages',
                help="List all available source/destination languages",
                action='store_true')

            args = parser.parse_args()
            print args

            if (os.name == "posix"):
                args.source_path = str(self.filename)
            else:
                args.source_path = (str(self.filename)).replace("/", "\\")
                pas = (args.source_path).replace("/", "\\")
                args.source_path = pas
                print " Printing pas >>>", pas
            print args

            path = args.source_path[:-3]
            srt_path = path + "srt"

            if args.list_formats:
                print("List of formats:")
                for subtitle_format in FORMATTERS.keys():
                    print("{format}".format(format=subtitle_format))
                return 0

            if args.list_languages:
                print("List of all languages:")
                for code, language in sorted(LANGUAGE_CODES.items()):
                    print("{code}\t{language}".format(code=code,
                                                      language=languages))
                return 0

            if args.format not in FORMATTERS.keys():
                print(
                    "Subtitle format not supported. Run with --list-formats to see all supported formats."
                )
                return 1

            if args.src_language not in LANGUAGE_CODES.keys():
                print(
                    "Source language not supported. Run with --list-languages to see all supported languages."
                )
                return 1

            if args.dst_language not in LANGUAGE_CODES.keys():
                print(
                    "Destination language not supported. Run with --list-languages to see all supported languages."
                )
                return 1

            if not args.source_path:
                print("Error: You need to specify a source path.")
                return 1

            audio_filename, audio_rate = extract_audio(args.source_path)

            regions = find_speech_regions(audio_filename)
            pool = ProcessingPool(args.concurrency)
            converter = FLACConverter(source_path=audio_filename)
            recognizer = SpeechRecognizer(language=args.src_language,
                                          rate=audio_rate,
                                          api_key=GOOGLE_SPEECH_API_KEY)

            transcripts = []
            if regions:
                try:
                    widgets = [
                        "Converting speech regions to FLAC files: ",
                        Percentage(), ' ',
                        Bar(), ' ',
                        ETA()
                    ]
                    pbar = ProgressBar(widgets=widgets,
                                       maxval=len(regions)).start()
                    extracted_regions = []
                    for i, extracted_region in enumerate(
                            pool.imap(converter, regions)):
                        extracted_regions.append(extracted_region)
                        pbar.update(i)
                        self.progress1.setValue(i)
                    pbar.finish()

                    widgets = [
                        "Performing speech recognition: ",
                        Percentage(), ' ',
                        Bar(), ' ',
                        ETA()
                    ]
                    pbar = ProgressBar(widgets=widgets,
                                       maxval=len(regions)).start()

                    for i, transcript in enumerate(
                            pool.imap(recognizer, extracted_regions)):
                        transcripts.append(transcript)
                        pbar.update(i)
                        self.progress2.setValue(i)
                    pbar.finish()
                    QMessageBox.about(self, "Subtitles created",
                                      "Created at " + srt_path)
                    if not is_same_language(args.src_language,
                                            args.dst_language):
                        if args.api_key:
                            google_translate_api_key = args.api_key
                            translator = Translator(args.dst_language,
                                                    google_translate_api_key,
                                                    dst=args.dst_language,
                                                    src=args.src_language)
                            prompt = "Translating from {0} to {1}: ".format(
                                args.src_language, args.dst_language)
                            widgets = [
                                prompt,
                                Percentage(), ' ',
                                Bar(), ' ',
                                ETA()
                            ]
                            pbar = ProgressBar(widgets=widgets,
                                               maxval=len(regions)).start()
                            translated_transcripts = []
                            for i, transcript in enumerate(
                                    pool.imap(translator, transcripts)):
                                translated_transcripts.append(transcript)
                                pbar.update(i)
                                self.progress2.setValue(i)
                            pbar.finish()
                            transcripts = translated_transcripts
                        else:
                            print "Error: Subtitle translation requires specified Google Translate API key. \See --help for further information."
                            return 1

                except KeyboardInterrupt:
                    pbar.finish()
                    pool.terminate()
                    pool.join()
                    print "Cancelling transcription"
                    return 1

            timed_subtitles = [(r, t) for r, t in zip(regions, transcripts)
                               if t]
            formatter = FORMATTERS.get(args.format)
            formatted_subtitles = formatter(timed_subtitles)

            dest = args.output

            if not dest:
                base, ext = os.path.splitext(args.source_path)
                dest = "{base}.{format}".format(base=base, format=args.format)

            with open(dest, 'wb') as f:
                f.write(formatted_subtitles.encode("utf-8"))

            print "Subtitles file created at {}".format(dest)

            os.remove(audio_filename)

            return 0
示例#6
0
文件: __init__.py 项目: tbfly/autosub
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('source_path',
                        help="Path to the video or audio file to subtitle",
                        nargs='?')
    parser.add_argument('-C',
                        '--concurrency',
                        help="Number of concurrent API requests to make",
                        type=int,
                        default=DEFAULT_CONCURRENCY)
    parser.add_argument(
        '-o',
        '--output',
        help="Output path for subtitles (by default, subtitles are saved in \
                        the same directory and name as the source path)")
    parser.add_argument('-F',
                        '--format',
                        help="Destination subtitle format",
                        default=DEFAULT_SUBTITLE_FORMAT)
    parser.add_argument('-S',
                        '--src-language',
                        help="Language spoken in source file",
                        default=DEFAULT_SRC_LANGUAGE)
    parser.add_argument('-D',
                        '--dst-language',
                        help="Desired language for the subtitles",
                        default=DEFAULT_DST_LANGUAGE)
    parser.add_argument(
        '-K',
        '--api-key',
        help=
        "The Google Translate API key to be used. (Required for subtitle translation)"
    )
    parser.add_argument('--list-formats',
                        help="List all available subtitle formats",
                        action='store_true')
    parser.add_argument('--list-languages',
                        help="List all available source/destination languages",
                        action='store_true')

    args = parser.parse_args()

    if args.list_formats:
        print("List of formats:")
        for subtitle_format in FORMATTERS.keys():
            print("{format}".format(format=subtitle_format))
        return 0

    if args.list_languages:
        print("List of all languages:")
        for code, language in sorted(LANGUAGE_CODES.items()):
            print("{code}\t{language}".format(code=code, language=language))
        return 0

    if args.format not in FORMATTERS.keys():
        print("Subtitle format not supported. "
              "Run with --list-formats to see all supported formats.")
        return 1

    if args.src_language not in LANGUAGE_CODES.keys():
        print("Source language not supported. "
              "Run with --list-languages to see all supported languages.")
        return 1

    if "," in args.dst_language:
        dst_language_list = args.dst_language.split(",")
        unsupport_lang_list = [
            x for x in dst_language_list if x not in LANGUAGE_CODES.keys()
        ]
        if len(unsupport_lang_list) > 0:
            print("Destination language %s not supported. "
                  "Run with --list-languages to see all supported languages."
                  ) % unsupport_lang_list
            return 1
    else:
        dst_language_list = [args.dst_language]
        if args.dst_language not in LANGUAGE_CODES.keys():
            print("Destination language not supported. "
                  "Run with --list-languages to see all supported languages.")
            return 1

    if not args.source_path:
        print("Error: You need to specify a source path.")
        return 1

    try:
        subtitle_file_path = generate_subtitles(
            source_path=args.source_path,
            concurrency=args.concurrency,
            src_language=args.src_language,
            dst_language_list=dst_language_list,
            api_key=args.api_key,
            subtitle_file_format=args.format,
            output=args.output,
        )
        print("Subtitles file created at {}".format(subtitle_file_path))
    except KeyboardInterrupt:
        return 1

    return 0
示例#7
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('source_path',
                        help="Path to the video or audio file to subtitle",
                        nargs='?')
    parser.add_argument('-C',
                        '--concurrency',
                        help="Number of concurrent API requests to make",
                        type=int,
                        default=DEFAULT_CONCURRENCY)
    parser.add_argument(
        '-o',
        '--output',
        help="Output path for subtitles (by default, subtitles are saved in \
                        the same directory and name as the source path)")
    parser.add_argument('-F',
                        '--format',
                        help="Destination subtitle format",
                        default=DEFAULT_SUBTITLE_FORMAT)
    parser.add_argument('-S',
                        '--src-language',
                        help="Language spoken in source file",
                        default=DEFAULT_SRC_LANGUAGE)
    parser.add_argument('-D',
                        '--dst-language',
                        help="Desired language for the subtitles",
                        default=DEFAULT_DST_LANGUAGE)
    parser.add_argument(
        '-K',
        '--api-key',
        help=
        "The Google Translate API key to be used. (Required for subtitle translation)"
    )
    parser.add_argument('-L',
                        '--min-length',
                        help="Minimum length of a speech sample.",
                        type=float,
                        default=DEFAULT_MIN_LENGTH)
    parser.add_argument('-U',
                        '--max-length',
                        help="Maximum length of a speech sample.",
                        type=float,
                        default=DEFAULT_MAX_LENGTH)
    parser.add_argument(
        '-P',
        '--silent-percentile',
        help="Percentile of speech which is considered silent.",
        type=float,
        default=DEFAULT_PERCENTILE)
    parser.add_argument('-SF',
                        '--silent-frame-cut',
                        help="Amount of silent frames till cut.",
                        type=int,
                        default=DEFAULT_FRAME_CUT)
    parser.add_argument('-I',
                        '--interval',
                        help="Interval in which the percentile will be taken.",
                        type=int,
                        default=DEFAULT_INTERVAL)
    parser.add_argument('--list-formats',
                        help="List all available subtitle formats",
                        action='store_true')
    parser.add_argument('--list-languages',
                        help="List all available source/destination languages",
                        action='store_true')

    args = parser.parse_args()

    if args.list_formats:
        print("List of formats:")
        for subtitle_format in FORMATTERS.keys():
            print("{format}".format(format=subtitle_format))
        return 0

    if args.list_languages:
        print("List of all languages:")
        for code, language in sorted(LANGUAGE_CODES.items()):
            print("{code}\t{language}".format(code=code, language=language))
        return 0

    if args.format not in FORMATTERS.keys():
        print("Subtitle format not supported. "
              "Run with --list-formats to see all supported formats.")
        return 1

    if args.src_language not in LANGUAGE_CODES.keys():
        print("Source language not supported. "
              "Run with --list-languages to see all supported languages.")
        return 1

    if args.dst_language not in LANGUAGE_CODES.keys():
        print("Destination language not supported. "
              "Run with --list-languages to see all supported languages.")
        return 1

    if not args.source_path:
        print("Error: You need to specify a source path.")
        return 1

    try:
        subtitle_file_path = generate_subtitles(
            source_path=args.source_path,
            concurrency=args.concurrency,
            src_language=args.src_language,
            dst_language=args.dst_language,
            api_key=args.api_key,
            subtitle_file_format=args.format,
            output=args.output,
            min_sample_length=args.min_length,
            max_sample_length=args.max_length,
            silent_percentile=args.silent_percentile,
            silent_frame_cut=args.silent_frame_cut,
            interval=args.interval)
        print("Subtitles file created at {}".format(subtitle_file_path))
    except KeyboardInterrupt:
        return 1

    return 0
示例#8
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('source_path',
                        help="Path to the video or audio file to subtitle",
                        nargs='?')
    parser.add_argument('-C',
                        '--concurrency',
                        help="Number of concurrent API requests to make",
                        type=int,
                        default=10)
    parser.add_argument(
        '-o',
        '--output',
        help="Output path for subtitles (by default, subtitles are saved in \
                    the same directory and name as the source path)")
    parser.add_argument('-F',
                        '--format',
                        help="Destination subtitle format",
                        default="srt")
    parser.add_argument('-S',
                        '--src-language',
                        help="Language spoken in source file",
                        default="en")
    parser.add_argument('-D',
                        '--dst-language',
                        help="Desired language for the subtitles",
                        default="en")
    parser.add_argument(
        '-K',
        '--api-key',
        help=
        "The Google Translate API key to be used. (Required for subtitle translation)"
    )
    parser.add_argument('--list-formats',
                        help="List all available subtitle formats",
                        action='store_true')
    parser.add_argument('--list-languages',
                        help="List all available source/destination languages",
                        action='store_true')

    args = parser.parse_args()

    if args.list_formats:
        print("List of formats:")
        for subtitle_format in FORMATTERS.keys():
            print("{format}".format(format=subtitle_format))
        return 0

    if args.list_languages:
        print("List of all languages:")
        for code, language in sorted(LANGUAGE_CODES.items()):
            print("{code}\t{language}".format(code=code, language=language))
        return 0

    if args.format not in FORMATTERS.keys():
        print(
            "Subtitle format not supported. Run with --list-formats to see all supported formats."
        )
        return 1

    if args.src_language not in LANGUAGE_CODES.keys():
        print(
            "Source language not supported. Run with --list-languages to see all supported languages."
        )
        return 1

    if args.dst_language not in LANGUAGE_CODES.keys():
        print(
            "Destination language not supported. Run with --list-languages to see all supported languages."
        )
        return 1

    #if not args.source_path:
    #    print("Error: You need to specify a source path.")
    #    return 1

    config = get_config("apikey.conf")
    args.api_key = config['apikey']
    args.src_language = config['source_lang']
    args.dst_language = config['dest_lang']
    transpath = config['path']
    entries = os.listdir(transpath)

    for fp in entries:
        # Split the extension from the path and normalise it to lowercase.
        ext = os.path.splitext(fp)[-1].lower()
        rpath = os.path.normpath(os.path.join(transpath, fp))
        #rpath = os.path.abspath(fp)
        print(rpath)

        rightf = ext.endswith(('.mp4', '.mp3', '.m4a', '.wav', 'mov', '.3gp',
                               '.avi', '.ogg', '.webm', 'mkv', 'flv'))
        if rightf:
            #command = ["python.exe Scripts/autosub", "-S", "en", "-D", "zh-TW", "-K", "AIzaSyCEPufSi1M0SD-dcmfAnDZNw6tUwHcsLzM", rpath]
            #print(command)
            #subprocess.check_output(command, stdin=open(os.devnull))
            #else:
            #    print fp, "is an unknown file format.

            audio_filename, audio_rate = extract_audio(rpath)

            regions = find_speech_regions(audio_filename)

            pool = multiprocessing.Pool(args.concurrency)
            converter = FLACConverter(source_path=audio_filename)
            recognizer = SpeechRecognizer(language=args.src_language,
                                          rate=audio_rate,
                                          api_key=GOOGLE_SPEECH_API_KEY)

            transcripts = []
            if regions:
                try:
                    widgets = [
                        "Converting speech regions to FLAC files: ",
                        Percentage(), ' ',
                        Bar(), ' ',
                        ETA()
                    ]
                    pbar = ProgressBar(widgets=widgets,
                                       maxval=len(regions)).start()
                    extracted_regions = []
                    for i, extracted_region in enumerate(
                            pool.imap(converter, regions)):
                        extracted_regions.append(extracted_region)
                        pbar.update(i)
                    pbar.finish()

                    widgets = [
                        "Performing speech recognition: ",
                        Percentage(), ' ',
                        Bar(), ' ',
                        ETA()
                    ]
                    pbar = ProgressBar(widgets=widgets,
                                       maxval=len(regions)).start()

                    for i, transcript in enumerate(
                            pool.imap(recognizer, extracted_regions)):
                        transcripts.append(transcript)
                        pbar.update(i)
                    pbar.finish()

                    if not is_same_language(args.src_language,
                                            args.dst_language):
                        if args.api_key:
                            google_translate_api_key = args.api_key
                            translator = Translator(args.dst_language,
                                                    google_translate_api_key,
                                                    dst=args.dst_language,
                                                    src=args.src_language)
                            prompt = "Translating from {0} to {1}: ".format(
                                args.src_language, args.dst_language)
                            widgets = [
                                prompt,
                                Percentage(), ' ',
                                Bar(), ' ',
                                ETA()
                            ]
                            pbar = ProgressBar(widgets=widgets,
                                               maxval=len(regions)).start()
                            translated_transcripts = []
                            for i, transcript in enumerate(
                                    pool.imap(translator, transcripts)):
                                translated_transcripts.append(transcript)
                                pbar.update(i)
                            pbar.finish()
                            transcripts = translated_transcripts
                        else:
                            print(
                                "Error: Subtitle translation requires specified Google Translate API key. \
                                    See --help for further information.")
                            return 1

                except KeyboardInterrupt:
                    pbar.finish()
                    pool.terminate()
                    pool.join()
                    print("Cancelling transcription")
                    return 1

            timed_subtitles = [(r, t) for r, t in zip(regions, transcripts)
                               if t]
            formatter = FORMATTERS.get(args.format)
            formatted_subtitles = formatter(timed_subtitles)

            dest = args.output

            if not dest:
                base, ext = os.path.splitext(rpath)
                dest = "{base}.{format}".format(base=base, format=args.format)

            with open(dest, 'wb') as f:
                f.write(formatted_subtitles.encode("utf-8"))

            print("Subtitles file created at {}".format(dest))

            os.remove(audio_filename)

    return 0
示例#9
0
def main():
    """
    Run autosub as a command-line program.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('source_path',
                        help="Path to the video or audio file to subtitle",
                        nargs='?')
    parser.add_argument('-C',
                        '--concurrency',
                        help="Number of concurrent API requests to make",
                        type=int,
                        default=DEFAULT_CONCURRENCY)
    parser.add_argument(
        '-o',
        '--output',
        help="Output path for subtitles (by default, subtitles are saved in \
                        the same directory and name as the source path)")
    parser.add_argument('-F',
                        '--format',
                        help="Destination subtitle format",
                        default=DEFAULT_SUBTITLE_FORMAT)
    parser.add_argument('-S',
                        '--src-language',
                        help="Language spoken in source file",
                        default=DEFAULT_SRC_LANGUAGE)
    parser.add_argument('-D',
                        '--dst-language',
                        help="Desired language for the subtitles",
                        default=DEFAULT_DST_LANGUAGE)
    parser.add_argument('-P',
                        '--project-id',
                        help="Your Google Cloud Project ID")
    parser.add_argument('-L',
                        '--location',
                        help="Google Cloud region id(e.g. us-central1)")
    parser.add_argument(
        '-M',
        '--model',
        help=
        "Speech regocnition model(default, video, phone_call, command_and_search",
        default=DEFAULT_SPEECH_MODEL)
    parser.add_argument('--list-formats',
                        help="List all available subtitle formats",
                        action='store_true')
    parser.add_argument('--list-languages',
                        help="List all available source/destination languages",
                        action='store_true')

    args = parser.parse_args()

    if args.list_formats:
        print("List of formats:")
        for subtitle_format in FORMATTERS:
            print("{format}".format(format=subtitle_format))
        return 0

    if args.list_languages:
        print("List of all languages:")
        for code, language in sorted(LANGUAGE_CODES.items()):
            print("{code}\t{language}".format(code=code, language=language))
        return 0

    if not validate(args):
        return 1

    try:
        subtitle_file_path = generate_subtitles(
            source_path=args.source_path,
            concurrency=args.concurrency,
            src_language=args.src_language,
            dst_language=args.dst_language,
            project_id=args.project_id,
            location=args.location,
            subtitle_file_format=args.format,
            output=args.output,
            model=args.model)
        print("Subtitles file created at {}".format(subtitle_file_path))
    except KeyboardInterrupt:
        return 1

    return 0
示例#10
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('source_path',
                        help="Path to the video or audio file to subtitle",
                        nargs='?')
    parser.add_argument('-C',
                        '--concurrency',
                        help="Number of concurrent API requests to make",
                        type=int,
                        default=DEFAULT_CONCURRENCY)
    parser.add_argument(
        '-o',
        '--output',
        help="Output path for subtitles (by default, subtitles are saved in \
                        the same directory and name as the source path)")
    parser.add_argument('-F',
                        '--format',
                        help="Destination subtitle format",
                        default=DEFAULT_SUBTITLE_FORMAT)
    parser.add_argument('-S',
                        '--src-language',
                        help="Language spoken in source file",
                        default=DEFAULT_SRC_LANGUAGE)
    parser.add_argument('-D',
                        '--dst-language',
                        help="Desired language for the subtitles",
                        default=DEFAULT_DST_LANGUAGE)
    parser.add_argument(
        '-K',
        '--api-key',
        help=
        "The Google Translate API key to be used. (Required for subtitle translation)"
    )
    parser.add_argument('--list-formats',
                        help="List all available subtitle formats",
                        action='store_true')
    parser.add_argument('--list-languages',
                        help="List all available source/destination languages",
                        action='store_true')
    parser.add_argument('-d',
                        '--dir',
                        help="check for files in subdirectories recursively",
                        action='store_true')
    parser.add_argument('-i',
                        '--input-format',
                        help="Input video or audio file format to subtitle")

    args = parser.parse_args()

    if args.list_formats:
        print("List of formats:")
        for subtitle_format in FORMATTERS.keys():
            print("{format}".format(format=subtitle_format))
        return 0

    if args.list_languages:
        print("List of all languages:")
        for code, language in sorted(LANGUAGE_CODES.items()):
            print("{code}\t{language}".format(code=code, language=language))
        return 0

    if args.format not in FORMATTERS.keys():
        print("Subtitle format not supported. "
              "Run with --list-formats to see all supported formats.")
        return 1

    if args.src_language not in LANGUAGE_CODES.keys():
        print("Source language not supported. "
              "Run with --list-languages to see all supported languages.")
        return 1

    if args.dst_language not in LANGUAGE_CODES.keys():
        print("Destination language not supported. "
              "Run with --list-languages to see all supported languages.")
        return 1

    if not args.source_path:
        print("Error: You need to specify a source path.")
        return 1

    if args.dir and not args.input_format:
        print(
            "Error: You need to specify an input format when you specify a directory."
        )
        return 1

    if args.dir and os.path.isfile(args.source_path):
        print(
            "Error: You must not select a file when you specify a directory.")
        return 1

    if not args.dir and not os.path.isfile(args.source_path):
        print("Error: You must use the --dir for directories.")
        return 1

    try:
        if args.dir:
            for root, dirs, files in os.walk(args.source_path):
                for name in files:
                    mediaFile = os.path.join(root, name).decode("utf8")
                    is_right_format = mediaFile.split(
                        '.')[-1] == args.input_format
                    subtitle_extension = args.format or DEFAULT_SUBTITLE_FORMAT

                    if not check_exists_subtitle(
                            mediaFile, subtitle_extension) and is_right_format:
                        print(
                            "Generating subtitles for '{}'".format(mediaFile))
                        subtitle_file_path = generate_subtitles(
                            source_path=mediaFile,
                            concurrency=args.concurrency,
                            src_language=args.src_language,
                            dst_language=args.dst_language,
                            api_key=args.api_key,
                            subtitle_file_format=args.format,
                            output=args.output,
                        )

                    elif check_exists_subtitle(
                            mediaFile, subtitle_extension) and is_right_format:
                        print("There is already a subtitle for '{}'".format(
                            mediaFile))
        else:
            subtitle_file_path = generate_subtitles(
                source_path=args.source_path,
                concurrency=args.concurrency,
                src_language=args.src_language,
                dst_language=args.dst_language,
                api_key=args.api_key,
                subtitle_file_format=args.format,
                output=args.output,
            )
            print("Subtitles file created at {}".format(subtitle_file_path))
    except KeyboardInterrupt:
        return 1

    return 0
示例#11
0
def argparser():
    parser = argparse.ArgumentParser(description='auto sub video or audios')
    parser.add_argument('-i',
                        '--input_path',
                        help="Path to the video or audio file to subtitle")
    parser.add_argument('-c',
                        '--concurrency',
                        help="Number of concurrent Google API requests",
                        type=int,
                        default=10)
    parser.add_argument('-o', '--output', help="output for path of subtitle")
    parser.add_argument('-f',
                        '--format',
                        help="subtitle format",
                        default="srt")
    parser.add_argument('-sl',
                        '--src-language',
                        help="source language",
                        default="en")
    parser.add_argument('-dl',
                        '--dst-language',
                        help="dest language",
                        default="en")
    parser.add_argument('--list-formats',
                        help="List all available subtitle formats",
                        action='store_true')
    parser.add_argument('--list-languages',
                        help="List all available source/destination languages",
                        action='store_true')
    parser.add_argument('-k', '--api-key', help="The google translate API key")

    parser.add_argument('-ts',
                        '--timed_subtitle',
                        help="timed subtitle to replace auto-generated")

    args = parser.parse_args()

    if args.list_formats:
        print("List of formats:")
        for subtitle_format in FORMATTERS.keys():
            print("{format}".format(format=subtitle_format))
        return 0

    if args.list_languages:
        print("List of all languages:")
        for code, language in sorted(LANGUAGE_CODES.items()):
            print("{code}\t{language}".format(code=code, language=language))
        return 0

    if args.format not in FORMATTERS.keys():
        print("Subtitle format not supported. \
              Run with --list-formats to see all supported formats.")
        return 1

    if args.src_language not in LANGUAGE_CODES.keys():
        print("Source language not supported. \
              Run with --list-languages to see all supported languages.")
        return 1

    if args.dst_language not in LANGUAGE_CODES.keys():
        print("Destination language not supported. \
             Run with --list-languages to see all supported languages.")
        return 1

    if not args.input_path:
        print("Error: You need to specify a source path.")
        return 1
    return args
示例#12
0
def main():
    window = Tk(className="ExSubtitle")
    window.title("字幕抽取")
    window.geometry("480x480+300+100")
    # window.resizable(0, 0)

    # log
    label_log = LabelFrame(window, text=" 运行日志: ")
    log_area = Text(label_log, width=65, height=5)
    log_out = StdoutRedirector(log_area)
    # log_out = sys.stdout
    sys.stdout = log_out
    sys.stderr = log_out
    label_log.grid(column=0, row=3, padx=10)
    log_area.grid(column=0, row=0)

    # 组件
    labels = LabelFrame(window, text=" 温馨提示: ",width=300,height=50)
    label_zero = Label(labels, text=u'请确保能够使用ffmpeg!', font=('Arial', 12), width=50, height=2)
    label_one = Label(labels, text=u'请确保能够访问Google!', font=('Arial', 12), width=50, height=2)
    label_two = Label(labels, text=u'请确保文件及文件夹名称不包含中文!', font=('Arial', 12), width=50, height=2)

    # 添加组件
    labels.grid(column=0, row=0, padx=10, pady=10)
    label_zero.grid(column=0, row=0)
    label_one.grid(column=0, row=1)
    label_two.grid(column=0, row=2)

    # 配置相关
    options = LabelFrame(window, text=" 配置选项: ",width=300,height=50)

    # 语言
    label_lang = Label(options, text=u'语言:', font=('Arial', 10),width= 5, height=2)
    lang_name = StringVar()
    language_box = ttk.Combobox(options, width=50, height=10,textvariable=lang_name)
    language_box['values'] = [k+u" : "+v for k,v in sorted(LANGUAGE_CODES.items())]
    language_box.current(14)

    # 字幕格式
    label_sub = Label(options, text=u'格式:', font=('Arial', 10), width= 5, height=2)
    subtitle_name = StringVar()
    subtitle_format = ttk.Combobox(options, width=50, height=4,textvariable=subtitle_name)
    subtitle_format['values'] = ['srt', 'vtt', 'json', 'raw']
    subtitle_format.current(0)

    options.grid(column=0, row=1, padx=10, pady=10)

    label_lang.grid(column=0, row=1, padx=10, pady=2)
    language_box.grid(column=1, row=1, padx=10, pady=2)

    label_sub.grid(column=0, row=2, padx=10, pady=2)
    subtitle_format.grid(column=1, row=2, padx=10, pady=2)

    start = Start(log_out=log_area,lang=language_box,form=subtitle_format)

    # 文件
    file_btn = Button(options, text=u'选择文件', width=32, height=1, command=start.get_file)
    file_btn.grid(column=1, row=0, padx=10, pady=2)

    start_btn = Button(window, text=u'抽取', width=8, height=1, command=start)

    start_btn.grid(column=0, row=2, padx=10, pady=10)

    # 事件循环
    window.mainloop()