Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to syntax usable with python3 #189

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: python
python:
- "2.7"
- "3.7"
install:
- pip install .
- pip install pylint
Expand Down
58 changes: 29 additions & 29 deletions autosub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#!/usr/bin/env python

from __future__ import absolute_import, print_function, unicode_literals


import argparse
import audioop
Expand Down Expand Up @@ -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
"""
Expand All @@ -65,22 +65,22 @@ 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

except KeyboardInterrupt:
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.
"""
Expand All @@ -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)
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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."
Expand Down
2 changes: 1 addition & 1 deletion autosub/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion autosub/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

# -*- coding: utf-8 -*-
from __future__ import unicode_literals


import json

Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
from __future__ import unicode_literals


try:
from setuptools import setup
Expand All @@ -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',
Expand Down