diff --git a/.travis.yml b/.travis.yml index 4d65ed3e..e42bbef5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: python python: - - "2.7" + - "3.7" install: - pip install . - pip install pylint diff --git a/autosub/__init__.py b/autosub/__init__.py index d85e4db9..cd15f5ca 100644 --- a/autosub/__init__.py +++ b/autosub/__init__.py @@ -4,7 +4,7 @@ #!/usr/bin/env python -from __future__ import absolute_import, print_function, unicode_literals + import argparse import audioop @@ -51,7 +51,7 @@ def percentile(arr, percent): return low_value + high_value -class FLACConverter(object): # pylint: disable=too-few-public-methods +class FLACConverter: # pylint: disable=too-few-public-methods """ Class for converting a region of an input audio or video file into a FLAC audio file """ @@ -65,14 +65,14 @@ def __call__(self, region): start, end = region start = max(0, start - self.include_before) end += self.include_after - 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] - use_shell = True if os.name == "nt" else False - subprocess.check_output(command, stdin=open(os.devnull), shell=use_shell) - read_data = temp.read() - temp.close() + with tempfile.NamedTemporaryFile(suffix='.flac', delete=False) as temp: + command = ["ffmpeg", "-ss", str(start), "-t", str(end - start), + "-y", "-i", self.source_path, + "-loglevel", "error", temp.name] + use_shell = os.name == "nt" + subprocess.check_output(command, stdin=open(os.devnull), shell=use_shell) + read_data = temp.read() + temp.close() os.unlink(temp.name) return read_data @@ -80,7 +80,7 @@ def __call__(self, region): return None -class SpeechRecognizer(object): # pylint: disable=too-few-public-methods +class SpeechRecognizer: # pylint: disable=too-few-public-methods """ Class for performing speech-to-text for an input FLAC file. """ @@ -90,7 +90,7 @@ def __init__(self, language="en", rate=44100, retries=3, api_key=GOOGLE_SPEECH_A self.api_key = api_key self.retries = retries - def __call__(self, data): + def __call__(self, data): # pylint: disable=inconsistent-return-statements try: for _ in range(self.retries): url = GOOGLE_SPEECH_API_URL.format(lang=self.language, key=self.api_key) @@ -116,7 +116,7 @@ def __call__(self, data): return None -class Translator(object): # pylint: disable=too-few-public-methods +class Translator: # pylint: disable=too-few-public-methods """ Class for translating a sentence from a one language to another. """ @@ -176,19 +176,19 @@ def extract_audio(filename, channels=1, rate=16000): """ Extract audio from an input file to a temporary WAV file. """ - temp = tempfile.NamedTemporaryFile(suffix='.wav', delete=False) - if not os.path.isfile(filename): - print("The given file does not exist: {}".format(filename)) - raise Exception("Invalid filepath: {}".format(filename)) - if not which("ffmpeg"): - 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] - use_shell = True if os.name == "nt" else False - subprocess.check_output(command, stdin=open(os.devnull), shell=use_shell) - return temp.name, rate + with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp: + if not os.path.isfile(filename): + print("The given file does not exist: {}".format(filename)) + raise Exception("Invalid filepath: {}".format(filename)) + if not which("ffmpeg"): + 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] + use_shell = os.name == "nt" + subprocess.check_output(command, stdin=open(os.devnull), shell=use_shell) + return temp.name, rate def find_speech_regions(filename, frame_width=4096, min_region_size=0.5, max_region_size=6): # pylint: disable=too-many-locals @@ -246,7 +246,7 @@ def generate_subtitles( # pylint: disable=too-many-locals,too-many-arguments regions = find_speech_regions(audio_filename) - pool = multiprocessing.Pool(concurrency) + pool = multiprocessing.Pool(concurrency) # pylint: disable=consider-using-with converter = FLACConverter(source_path=audio_filename) recognizer = SpeechRecognizer(language=src_language, rate=audio_rate, api_key=GOOGLE_SPEECH_API_KEY) @@ -329,14 +329,14 @@ def validate(args): ) return False - if args.src_language not in LANGUAGE_CODES.keys(): + 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 LANGUAGE_CODES.keys(): + if args.dst_language not in list(LANGUAGE_CODES.keys()): print( "Destination language not supported. " "Run with --list-languages to see all supported languages." diff --git a/autosub/constants.py b/autosub/constants.py index c776c112..a18cfc3f 100644 --- a/autosub/constants.py +++ b/autosub/constants.py @@ -2,7 +2,7 @@ Defines constants used by autosub. """ -from __future__ import unicode_literals + GOOGLE_SPEECH_API_KEY = "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw" GOOGLE_SPEECH_API_URL = "http://www.google.com/speech-api/v2/recognize?client=chromium&lang={lang}&key={key}" # pylint: disable=line-too-long diff --git a/autosub/formatters.py b/autosub/formatters.py index b0d581c0..f56dd0ab 100644 --- a/autosub/formatters.py +++ b/autosub/formatters.py @@ -3,7 +3,7 @@ """ # -*- coding: utf-8 -*- -from __future__ import unicode_literals + import json diff --git a/setup.py b/setup.py index c9ac20c0..814a94d4 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from __future__ import unicode_literals + try: from setuptools import setup @@ -20,7 +20,8 @@ setup( name='autosub', - version='0.4.0', + version='0.5.0', + python_requires='>=3', description='Auto-generates subtitles for any video or audio file', long_description=long_description, author='Anastasis Germanidis',