Git Product home page Git Product logo

botbahlul / autosrt Goto Github PK

View Code? Open in Web Editor NEW
44.0 3.0 11.0 4.45 MB

A python script COMMAND LINE utility to AUTO GENERATE SUBTITLE FILE (using free Google Speech Recognition API) and TRANSLATED SUBTITLE FILE (using unofficial online Google Translate API) for any video or audio file

License: MIT License

Python 99.90% Shell 0.03% Batchfile 0.06%
google-translate-api python speech-recognition srt-subtitle voice-recognition captions ffmpeg subtitle speechrecognition voicerecognition

autosrt's Introduction

autosrt

Auto generate subtitle files for any video / audio files

autosrt is a simple command line tool made with python to auto generate subtitle/closed caption (using free Google Speech Recognition API) for any video or audio files and translate it automatically for free using a simple unofficial online Google Translate API.

This script is a modified version of original autosub made by Anastasis Germanidis at https://github.com/agermanidis/autosub

UPDATE NOTES

Since version 1.2.4 I tried to make some class modules, so in case you want to use them in your own project you can just import them. They are Language, WavConverter, SpeechRegionFinder, FLACConverter, SpeechRecognizer, SentenceTranslator, SubtitleFormatter, and SubtitleWriter. You can import them like this :

from autosrt import Language, WavConverter, SpeechRegionFinder, FLACConverter, SpeechRecognizer, SentenceTranslator, \
    SubtitleFormatter,  SubtitleWriter

You can learn how to use them by playing around with those scripts inside test folder. Check every def function on each class to know how to use them. They are not to difficult to understand.

DON'T SAVE YOUR SCRIPT FILENAME WITH THE SAME NAME AS A MODULES NAME THAT YOU USE IN YOUR SCRIPT! IT WILL MAKE PYTHON CONFUSED!

Installation

If you don't have python on your Windows system you can try compiled version from this git release assets https://github.com/botbahlul/autosrt/releases

If it doesn't run well then you need to install python on your Windows system.

Just extract those ffmpeg.exe and autosrt.exe into a folder that has been added to PATH ENVIRONTMET for example in C:\Windows\system32

You can get latest version of ffmpeg from https://www.ffmpeg.org/

In Linux you have to install this script with python (version minimal 3.8 ) and install ffmpeg with your linux package manager for example in debian based linux distribution you can type :

sudo apt update
sudo apt install -y ffmpeg

To install this autosrt, just type :

pip install autosrt

You can try to compile that autosrt.py script in win/linux folder into a single executable file with pyinstaller by typing these :

pip install pyinstaller
pyinstaller --onefile autosrt.py

The executable compiled file will be placed by pyinstaller into dist subfolder of your current working folder, so you can just rename and put that compiled file into a folder that has been added to your PATH ENVIRONTMENT so you can execute it from anywhere

I was succesfuly compiled it in Windows 10 with pyinstaller-5.1 and Pyhton-3.10.4, and python-3.8.12 in Debian 9

Another alternative way to install this script with python is by cloning this git (or downloading this git as zip then extract it into a folder), and then just type :

pip install wheel
python setup.py build
python setup.py bdist_wheel

Then check the name of the whl file created in dist folder. In case the filename is autosrt-1.2.4-py2.py3-none-any.whl then you can install that whl file with pip :

cd dist
pip install autosrt-1.2.4-py2.py3-none-any.whl

You can also install this script (or any pip package) in ANDROID DEVICES via PYTHON package in TERMUX APP

https://github.com/termux/termux-app/releases/tag/v0.118.0

Choose the right apk for your device, install it, then open it

Type these commands to get python, pip, this autosrt, (and any other pip packages) :

termux-setup-storage
pkg update -y
pkg install -y python
pkg install -y ffmpeg
pip install autosrt

Simple usage example

autosrt --list-languages
autosrt -S zh-CN -D en "Episode 1.mp4"

For multiple video/audio files (starts from version 1.1.0), you can use wildcard

autosrt -S zh-CN -D en "C:\Movies\*.mp4"

If you don't need translations just type :

autosrt -S zh-CN "Episode 1.mp4"

Usage

usage: autosrt [-h] [-S SRC_LANGUAGE] [-D DST_LANGUAGE] [-ll] [-o OUTPUT] [-F FORMAT] [-lf] [-C CONCURRENCY] [-v] [source_path ...]

positional arguments:
  source_path           File path of the video or audio files to generate subtitles files (use wildcard for multiple files or
                        separate them with a space character)

options:
  -h, --help            show this help message and exit
  -S SRC_LANGUAGE, --src-language SRC_LANGUAGE
                        Language code of the audio language spoken in video/audio source_path
  -D DST_LANGUAGE, --dst-language DST_LANGUAGE
                        Desired translation language code for the subtitles
  -ll, --list-languages
                        List all supported languages
  -o OUTPUT, --output OUTPUT
                        Output file path for subtitles (by default, subtitles are saved in the same directory and named with the
                        source_path base name)
  -F FORMAT, --format FORMAT
                        Desired subtitle format
  -lf, --list-formats   List all supported subtitle formats
  -C CONCURRENCY, --concurrency CONCURRENCY
                        Number of concurrent API requests to make
  -v, --version         show program's version number and exit

License

MIT

Check my other SPEECH RECOGNITIION + TRANSLATE PROJECTS https://github.com/botbahlul?tab=repositories

Buy me coffee : https://sociabuzz.com/botbahlul/tribe

autosrt's People

Contributors

botbahlul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

autosrt's Issues

read operation time out

hello , i want to generate English to Chinese. it can complete Engilsh . but Convert Engilsh to Chines it show this error. can you help me?

ultiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Python311\Lib\site-packages\httpcore_exceptions.py", line 10, in map_exceptions
yield
File "C:\Python311\Lib\site-packages\httpcore\backends\sync.py", line 28, in read
return self._sock.recv(max_bytes)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\ssl.py", line 1263, in recv
return self.read(buflen)
^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\ssl.py", line 1136, in read
return self._sslobj.read(len)
^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Python311\Lib\site-packages\httpx_transports\default.py", line 60, in map_httpcore_exceptions
yield
File "C:\Python311\Lib\site-packages\httpx_transports\default.py", line 218, in handle_request
resp = self._pool.handle_request(req)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpcore_sync\connection_pool.py", line 253, in handle_request
raise exc
File "C:\Python311\Lib\site-packages\httpcore_sync\connection_pool.py", line 237, in handle_request
response = connection.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpcore_sync\http_proxy.py", line 316, in handle_request
return self._connection.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpcore_sync\http2.py", line 144, in handle_request
raise exc
File "C:\Python311\Lib\site-packages\httpcore_sync\http2.py", line 112, in handle_request
status, headers = self._receive_response(
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpcore_sync\http2.py", line 229, in _receive_response
event = self._receive_stream_event(request, stream_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpcore_sync\http2.py", line 260, in _receive_stream_event
self._receive_events(request, stream_id)
File "C:\Python311\Lib\site-packages\httpcore_sync\http2.py", line 281, in _receive_events
events = self._read_incoming_data(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpcore_sync\http2.py", line 341, in _read_incoming_data
raise exc
File "C:\Python311\Lib\site-packages\httpcore_sync\http2.py", line 327, in _read_incoming_data
data = self._network_stream.read(self.READ_NUM_BYTES, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpcore\backends\sync.py", line 26, in read
with map_exceptions(exc_map):
File "C:\Python311\Lib\contextlib.py", line 155, in exit
self.gen.throw(typ, value, traceback)
File "C:\Python311\Lib\site-packages\httpcore_exceptions.py", line 14, in map_exceptions
raise to_exc(exc)
httpcore.ReadTimeout: The read operation timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Python311\Lib\multiprocessing\pool.py", line 125, in worker
result = (True, func(*args, **kwds))
^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\autosrt_init_.py", line 458, in call
translated_sentence = GoogleTranslate(sentence, src=self.src, dst=self.dest)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\autosrt_init_.py", line 431, in GoogleTranslate
response = client.get(url+params)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpx_client.py", line 1045, in get
return self.request(
^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpx_client.py", line 821, in request
return self.send(request, auth=auth, follow_redirects=follow_redirects)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpx_client.py", line 908, in send
response = self._send_handling_auth(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpx_client.py", line 936, in _send_handling_auth
response = self._send_handling_redirects(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpx_client.py", line 973, in _send_handling_redirects
response = self._send_single_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpx_client.py", line 1009, in _send_single_request
response = transport.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\httpx_transports\default.py", line 217, in handle_request
with map_httpcore_exceptions():
File "C:\Python311\Lib\contextlib.py", line 155, in exit
self.gen.throw(typ, value, traceback)
File "C:\Python311\Lib\site-packages\httpx_transports\default.py", line 77, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ReadTimeout: The read operation timed out
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Python311\Scripts\autosrt-script.py", line 33, in
sys.exit(load_entry_point('autosrt==1.1.1', 'console_scripts', 'autosrt')())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\autosrt_init_.py", line 711, in main
for i, translated_subtitle in enumerate(pool.imap(transcript_translator, created_subtitles)):
File "C:\Python311\Lib\multiprocessing\pool.py", line 873, in next
raise value

cannot unpack non-iterable NoneType object

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/autosrt/init.py", line 3329, in main
wav_filepath, sample_rate = wav_converter(media_filepath)
^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot unpack non-iterable NoneType object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.11/bin/autosrt", line 8, in
sys.exit(main())
^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/autosrt/init.py", line 3548, in main
if not KeyboardInterrupt in str(e):
^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'in ' requires string as left operand, not type

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.