autosub 添加代理服务器参数 -P --proxy
--- autosub 2020-01-19 01:26:21 +0800 +++ autosub_app.py 2020-01-20 11:08:40 +0800 @@ -18,6 +18,11 @@ from autosub.constants import LANGUAGE_C GOOGLE_SPEECH_API_KEY, GOOGLE_SPEECH_API_URL from autosub.formatters import FORMATTERS +proxy_dict = { + 'http': 'http://127.0.0.1:1080', + 'https': 'http://127.0.0.1:1080', + 'use': False +} def percentile(arr, percent): arr = sorted(arr) @@ -45,7 +50,7 @@ class FLACConverter(object): start, end = region start = max(0, start - self.include_before) end += self.include_after - temp = tempfile.NamedTemporaryFile(suffix='.flac') + temp = tempfile.NamedTemporaryFile(suffix='.flac', delete=False) command = ["ffmpeg","-ss", str(start), "-t", str(end - start), "-y", "-i", self.source_path, "-loglevel", "error", temp.name] @@ -55,22 +60,29 @@ class FLACConverter(object): except KeyboardInterrupt: return - class SpeechRecognizer(object): - def __init__(self, language="en", rate=44100, retries=3, api_key=GOOGLE_SPEECH_API_KEY): + def __init__(self, language="en", rate=44100, retries=3, api_key=GOOGLE_SPEECH_API_KEY, proxy=proxy_dict): self.language = language self.rate = rate self.api_key = api_key self.retries = retries + self.proxy = proxy def __call__(self, data): try: + #print(self.proxy['use']) + #print(self.proxy['http']) for i in range(self.retries): url = GOOGLE_SPEECH_API_URL.format(lang=self.language, key=self.api_key) headers = {"Content-Type": "audio/x-flac; rate=%d" % self.rate} try: - resp = requests.post(url, data=data, headers=headers) + if self.proxy['use']: + #print("Use a proxy server") + resp = requests.post(url, data=data, headers=headers, proxies=self.proxy) + else: + #print("Do not use a proxy server") + resp = requests.post(url, data=data, headers=headers) except requests.exceptions.ConnectionError: continue @@ -135,7 +147,7 @@ def extract_audio(filename, channels=1, if not os.path.isfile(filename): print "The given file does not exist: {0}".format(filename) raise Exception("Invalid filepath: {0}".format(filename)) - if not which("ffmpeg"): + if not which("ffmpeg.exe"): print "ffmpeg: Executable not found on machine." raise Exception("Dependency not found: ffmpeg") command = ["ffmpeg", "-y", "-i", filename, "-ac", str(channels), "-ar", str(rate), "-loglevel", "error", temp.name] @@ -195,9 +207,17 @@ def main(): 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('-P', '--proxy', help="Set proxy server") args = parser.parse_args() + if args.proxy: + proxy_dict.update({ + 'http': args.proxy, + 'https': args.proxy, + 'use': True + }) + print("Use proxy " + args.proxy) + if args.list_formats: print("List of formats:") for subtitle_format in FORMATTERS.keys(): @@ -233,7 +253,7 @@ def main(): 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) + recognizer = SpeechRecognizer(language=args.src_language, rate=audio_rate, api_key=GOOGLE_SPEECH_API_KEY, proxy=proxy_dict) transcripts = [] if regions:
完整的代码下载地址 https://github.com/qq2225936589/autosub