Spaces:
Runtime error
Runtime error
https://github.com/McCloudS/subgen/tree/main
#1
by
hbs2
- opened
- .gitignore +0 -12
- Dockerfile +1 -1
- Dockerfile.cpu +0 -34
- LICENSE +0 -21
- README.md +9 -6
- docker-compose.yml +0 -40
- icon.png +0 -0
- language_code.py +0 -198
- launcher.py +0 -182
- requirements.txt +1 -1
- subgen.env +0 -6
- subgen.py +0 -2148
- subgen.xml +0 -56
.gitignore
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
.vscode/*
|
| 2 |
-
|
| 3 |
-
# Local History for Visual Studio Code
|
| 4 |
-
.history/
|
| 5 |
-
|
| 6 |
-
# Built Visual Studio Code Extensions
|
| 7 |
-
*.vsix
|
| 8 |
-
|
| 9 |
-
#ignore our settings
|
| 10 |
-
subgen.env
|
| 11 |
-
|
| 12 |
-
models/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dockerfile
CHANGED
|
@@ -42,4 +42,4 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
| 42 |
ENV PYTHONUNBUFFERED=1
|
| 43 |
|
| 44 |
# Set command to run the application
|
| 45 |
-
CMD ["python3", "launcher.py"]
|
|
|
|
| 42 |
ENV PYTHONUNBUFFERED=1
|
| 43 |
|
| 44 |
# Set command to run the application
|
| 45 |
+
CMD ["python3", "launcher.py"]
|
Dockerfile.cpu
DELETED
|
@@ -1,34 +0,0 @@
|
|
| 1 |
-
# === Stage 1: Build dependencies and install packages ===
|
| 2 |
-
FROM python:3.11-slim-bullseye AS builder
|
| 3 |
-
|
| 4 |
-
WORKDIR /subgen
|
| 5 |
-
|
| 6 |
-
# Install required build dependencies
|
| 7 |
-
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
-
ffmpeg \
|
| 9 |
-
git \
|
| 10 |
-
tzdata \
|
| 11 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
-
|
| 13 |
-
# Copy and install dependencies
|
| 14 |
-
COPY requirements.txt .
|
| 15 |
-
RUN pip install --no-cache-dir --prefix=/install torch torchaudio --extra-index-url https://download.pytorch.org/whl/cpu && pip install --no-cache-dir --prefix=/install -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu
|
| 16 |
-
|
| 17 |
-
# === Stage 2: Create a minimal runtime image ===
|
| 18 |
-
FROM python:3.11-slim-bullseye AS runtime
|
| 19 |
-
|
| 20 |
-
WORKDIR /subgen
|
| 21 |
-
|
| 22 |
-
# Install only required runtime dependencies
|
| 23 |
-
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 24 |
-
ffmpeg \
|
| 25 |
-
curl \
|
| 26 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 27 |
-
|
| 28 |
-
# Copy only necessary files from builder stage
|
| 29 |
-
COPY --from=builder /install /usr/local
|
| 30 |
-
|
| 31 |
-
# Copy source code
|
| 32 |
-
COPY launcher.py subgen.py language_code.py /subgen/
|
| 33 |
-
|
| 34 |
-
CMD ["python3", "launcher.py"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
| 1 |
-
MIT License
|
| 2 |
-
|
| 3 |
-
Copyright (c) 2023 McCloudS
|
| 4 |
-
|
| 5 |
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
-
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
-
in the Software without restriction, including without limitation the rights
|
| 8 |
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
-
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
-
furnished to do so, subject to the following conditions:
|
| 11 |
-
|
| 12 |
-
The above copyright notice and this permission notice shall be included in all
|
| 13 |
-
copies or substantial portions of the Software.
|
| 14 |
-
|
| 15 |
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
-
SOFTWARE.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
|
| 3 |
-
title: dssa
|
| 4 |
-
sdk: docker
|
| 5 |
emoji: 📊
|
| 6 |
-
colorFrom:
|
| 7 |
-
colorTo:
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
---
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: '0100'
|
|
|
|
|
|
|
| 3 |
emoji: 📊
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
license: mit
|
| 9 |
+
short_description: '0'
|
| 10 |
---
|
| 11 |
+
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
docker-compose.yml
DELETED
|
@@ -1,40 +0,0 @@
|
|
| 1 |
-
#docker-compose.yml
|
| 2 |
-
version: '2'
|
| 3 |
-
services:
|
| 4 |
-
subgen:
|
| 5 |
-
container_name: subgen
|
| 6 |
-
tty: true
|
| 7 |
-
image: mccloud/subgen
|
| 8 |
-
environment:
|
| 9 |
-
- "WHISPER_MODEL=distil-large-v3.5"
|
| 10 |
-
- "WHISPER_THREADS=4"
|
| 11 |
-
- "PROCADDEDMEDIA=True"
|
| 12 |
-
- "PROCMEDIAONPLAY=False"
|
| 13 |
-
- "NAMESUBLANG=aa"
|
| 14 |
-
- "SKIPIFINTERNALSUBLANG=eng"
|
| 15 |
-
- "PLEXTOKEN=plextoken"
|
| 16 |
-
- "PLEXSERVER=http://plexserver:32400"
|
| 17 |
-
- "JELLYFINTOKEN=token here"
|
| 18 |
-
- "JELLYFINSERVER=http://jellyfin:8096"
|
| 19 |
-
- "WEBHOOKPORT=9000"
|
| 20 |
-
- "CONCURRENT_TRANSCRIPTIONS=2"
|
| 21 |
-
- "WORD_LEVEL_HIGHLIGHT=False"
|
| 22 |
-
- "DEBUG=True"
|
| 23 |
-
- "USE_PATH_MAPPING=False"
|
| 24 |
-
- "PATH_MAPPING_FROM=/tv"
|
| 25 |
-
- "PATH_MAPPING_TO=/Volumes/TV"
|
| 26 |
-
- "TRANSCRIBE_DEVICE=cpu"
|
| 27 |
-
- "CLEAR_VRAM_ON_COMPLETE=True"
|
| 28 |
-
- "MODEL_PATH=./models"
|
| 29 |
-
- "UPDATE=False"
|
| 30 |
-
- "APPEND=False"
|
| 31 |
-
- "USE_MODEL_PROMPT=False"
|
| 32 |
-
- "CUSTOM_MODEL_PROMPT="
|
| 33 |
-
- "LRC_FOR_AUDIO_FILES=True"
|
| 34 |
-
- "CUSTOM_REGROUP=cm_sl=84_sl=42++++++1"
|
| 35 |
-
volumes:
|
| 36 |
-
- "${TV}:/tv"
|
| 37 |
-
- "${MOVIES}:/movies"
|
| 38 |
-
- "${APPDATA}/subgen/models:/subgen/models"
|
| 39 |
-
ports:
|
| 40 |
-
- "9000:9000"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
icon.png
DELETED
|
Binary file (75.4 kB)
|
|
|
language_code.py
DELETED
|
@@ -1,198 +0,0 @@
|
|
| 1 |
-
from enum import Enum
|
| 2 |
-
|
| 3 |
-
class LanguageCode(Enum):
|
| 4 |
-
# ISO 639-1, ISO 639-2/T, ISO 639-2/B, English Name, Native Name
|
| 5 |
-
AFAR = ("aa", "aar", "aar", "Afar", "Afar")
|
| 6 |
-
AFRIKAANS = ("af", "afr", "afr", "Afrikaans", "Afrikaans")
|
| 7 |
-
AMHARIC = ("am", "amh", "amh", "Amharic", "አማርኛ")
|
| 8 |
-
ARABIC = ("ar", "ara", "ara", "Arabic", "العربية")
|
| 9 |
-
ASSAMESE = ("as", "asm", "asm", "Assamese", "অসমীয়া")
|
| 10 |
-
AZERBAIJANI = ("az", "aze", "aze", "Azerbaijani", "Azərbaycanca")
|
| 11 |
-
BASHKIR = ("ba", "bak", "bak", "Bashkir", "Башҡортса")
|
| 12 |
-
BELARUSIAN = ("be", "bel", "bel", "Belarusian", "Беларуская")
|
| 13 |
-
BULGARIAN = ("bg", "bul", "bul", "Bulgarian", "Български")
|
| 14 |
-
BENGALI = ("bn", "ben", "ben", "Bengali", "বাংলা")
|
| 15 |
-
TIBETAN = ("bo", "bod", "tib", "Tibetan", "བོད་ཡིག")
|
| 16 |
-
BRETON = ("br", "bre", "bre", "Breton", "Brezhoneg")
|
| 17 |
-
BOSNIAN = ("bs", "bos", "bos", "Bosnian", "Bosanski")
|
| 18 |
-
CATALAN = ("ca", "cat", "cat", "Catalan", "Català")
|
| 19 |
-
CZECH = ("cs", "ces", "cze", "Czech", "Čeština")
|
| 20 |
-
WELSH = ("cy", "cym", "wel", "Welsh", "Cymraeg")
|
| 21 |
-
DANISH = ("da", "dan", "dan", "Danish", "Dansk")
|
| 22 |
-
GERMAN = ("de", "deu", "ger", "German", "Deutsch")
|
| 23 |
-
GREEK = ("el", "ell", "gre", "Greek", "Ελληνικά")
|
| 24 |
-
ENGLISH = ("en", "eng", "eng", "English", "English")
|
| 25 |
-
SPANISH = ("es", "spa", "spa", "Spanish", "Español")
|
| 26 |
-
ESTONIAN = ("et", "est", "est", "Estonian", "Eesti")
|
| 27 |
-
BASQUE = ("eu", "eus", "baq", "Basque", "Euskara")
|
| 28 |
-
PERSIAN = ("fa", "fas", "per", "Persian", "فارسی")
|
| 29 |
-
FINNISH = ("fi", "fin", "fin", "Finnish", "Suomi")
|
| 30 |
-
FAROESE = ("fo", "fao", "fao", "Faroese", "Føroyskt")
|
| 31 |
-
FRENCH = ("fr", "fra", "fre", "French", "Français")
|
| 32 |
-
GALICIAN = ("gl", "glg", "glg", "Galician", "Galego")
|
| 33 |
-
GUJARATI = ("gu", "guj", "guj", "Gujarati", "ગુજરાતી")
|
| 34 |
-
HAUSA = ("ha", "hau", "hau", "Hausa", "Hausa")
|
| 35 |
-
HAWAIIAN = ("haw", "haw", "haw", "Hawaiian", "ʻŌlelo Hawaiʻi")
|
| 36 |
-
HEBREW = ("he", "heb", "heb", "Hebrew", "עברית")
|
| 37 |
-
HINDI = ("hi", "hin", "hin", "Hindi", "हिन्दी")
|
| 38 |
-
CROATIAN = ("hr", "hrv", "hrv", "Croatian", "Hrvatski")
|
| 39 |
-
HAITIAN_CREOLE = ("ht", "hat", "hat", "Haitian Creole", "Kreyòl Ayisyen")
|
| 40 |
-
HUNGARIAN = ("hu", "hun", "hun", "Hungarian", "Magyar")
|
| 41 |
-
ARMENIAN = ("hy", "hye", "arm", "Armenian", "Հայերեն")
|
| 42 |
-
INDONESIAN = ("id", "ind", "ind", "Indonesian", "Bahasa Indonesia")
|
| 43 |
-
ICELANDIC = ("is", "isl", "ice", "Icelandic", "Íslenska")
|
| 44 |
-
ITALIAN = ("it", "ita", "ita", "Italian", "Italiano")
|
| 45 |
-
JAPANESE = ("ja", "jpn", "jpn", "Japanese", "日本語")
|
| 46 |
-
JAVANESE = ("jw", "jav", "jav", "Javanese", "ꦧꦱꦗꦮ")
|
| 47 |
-
GEORGIAN = ("ka", "kat", "geo", "Georgian", "ქართული")
|
| 48 |
-
KAZAKH = ("kk", "kaz", "kaz", "Kazakh", "Қазақша")
|
| 49 |
-
KHMER = ("km", "khm", "khm", "Khmer", "ភាសាខ្មែរ")
|
| 50 |
-
KANNADA = ("kn", "kan", "kan", "Kannada", "ಕನ್ನಡ")
|
| 51 |
-
KOREAN = ("ko", "kor", "kor", "Korean", "한국어")
|
| 52 |
-
LATIN = ("la", "lat", "lat", "Latin", "Latina")
|
| 53 |
-
LUXEMBOURGISH = ("lb", "ltz", "ltz", "Luxembourgish", "Lëtzebuergesch")
|
| 54 |
-
LINGALA = ("ln", "lin", "lin", "Lingala", "Lingála")
|
| 55 |
-
LAO = ("lo", "lao", "lao", "Lao", "ພາສາລາວ")
|
| 56 |
-
LITHUANIAN = ("lt", "lit", "lit", "Lithuanian", "Lietuvių")
|
| 57 |
-
LATVIAN = ("lv", "lav", "lav", "Latvian", "Latviešu")
|
| 58 |
-
MALAGASY = ("mg", "mlg", "mlg", "Malagasy", "Malagasy")
|
| 59 |
-
MAORI = ("mi", "mri", "mao", "Maori", "Te Reo Māori")
|
| 60 |
-
MACEDONIAN = ("mk", "mkd", "mac", "Macedonian", "Македонски")
|
| 61 |
-
MALAYALAM = ("ml", "mal", "mal", "Malayalam", "മലയാളം")
|
| 62 |
-
MONGOLIAN = ("mn", "mon", "mon", "Mongolian", "Монгол")
|
| 63 |
-
MARATHI = ("mr", "mar", "mar", "Marathi", "मराठी")
|
| 64 |
-
MALAY = ("ms", "msa", "may", "Malay", "Bahasa Melayu")
|
| 65 |
-
MALTESE = ("mt", "mlt", "mlt", "Maltese", "Malti")
|
| 66 |
-
BURMESE = ("my", "mya", "bur", "Burmese", "မြန်မာစာ")
|
| 67 |
-
NEPALI = ("ne", "nep", "nep", "Nepali", "नेपाली")
|
| 68 |
-
DUTCH = ("nl", "nld", "dut", "Dutch", "Nederlands")
|
| 69 |
-
NORWEGIAN_NYNORSK = ("nn", "nno", "nno", "Norwegian Nynorsk", "Nynorsk")
|
| 70 |
-
NORWEGIAN = ("no", "nor", "nor", "Norwegian", "Norsk")
|
| 71 |
-
OCCITAN = ("oc", "oci", "oci", "Occitan", "Occitan")
|
| 72 |
-
PUNJABI = ("pa", "pan", "pan", "Punjabi", "ਪੰਜਾਬੀ")
|
| 73 |
-
POLISH = ("pl", "pol", "pol", "Polish", "Polski")
|
| 74 |
-
PASHTO = ("ps", "pus", "pus", "Pashto", "پښتو")
|
| 75 |
-
PORTUGUESE = ("pt", "por", "por", "Portuguese", "Português")
|
| 76 |
-
ROMANIAN = ("ro", "ron", "rum", "Romanian", "Română")
|
| 77 |
-
RUSSIAN = ("ru", "rus", "rus", "Russian", "Русский")
|
| 78 |
-
SANSKRIT = ("sa", "san", "san", "Sanskrit", "संस्कृतम्")
|
| 79 |
-
SINDHI = ("sd", "snd", "snd", "Sindhi", "سنڌي")
|
| 80 |
-
SINHALA = ("si", "sin", "sin", "Sinhala", "සිංහල")
|
| 81 |
-
SLOVAK = ("sk", "slk", "slo", "Slovak", "Slovenčina")
|
| 82 |
-
SLOVENE = ("sl", "slv", "slv", "Slovene", "Slovenščina")
|
| 83 |
-
SHONA = ("sn", "sna", "sna", "Shona", "ChiShona")
|
| 84 |
-
SOMALI = ("so", "som", "som", "Somali", "Soomaaliga")
|
| 85 |
-
ALBANIAN = ("sq", "sqi", "alb", "Albanian", "Shqip")
|
| 86 |
-
SERBIAN = ("sr", "srp", "srp", "Serbian", "Српски")
|
| 87 |
-
SUNDANESE = ("su", "sun", "sun", "Sundanese", "Basa Sunda")
|
| 88 |
-
SWEDISH = ("sv", "swe", "swe", "Swedish", "Svenska")
|
| 89 |
-
SWAHILI = ("sw", "swa", "swa", "Swahili", "Kiswahili")
|
| 90 |
-
TAMIL = ("ta", "tam", "tam", "Tamil", "தமிழ்")
|
| 91 |
-
TELUGU = ("te", "tel", "tel", "Telugu", "తెలుగు")
|
| 92 |
-
TAJIK = ("tg", "tgk", "tgk", "Tajik", "Тоҷикӣ")
|
| 93 |
-
THAI = ("th", "tha", "tha", "Thai", "ไทย")
|
| 94 |
-
TURKMEN = ("tk", "tuk", "tuk", "Turkmen", "Türkmençe")
|
| 95 |
-
TAGALOG = ("tl", "tgl", "tgl", "Tagalog", "Tagalog")
|
| 96 |
-
TURKISH = ("tr", "tur", "tur", "Turkish", "Türkçe")
|
| 97 |
-
TATAR = ("tt", "tat", "tat", "Tatar", "Татарча")
|
| 98 |
-
UKRAINIAN = ("uk", "ukr", "ukr", "Ukrainian", "Українська")
|
| 99 |
-
URDU = ("ur", "urd", "urd", "Urdu", "اردو")
|
| 100 |
-
UZBEK = ("uz", "uzb", "uzb", "Uzbek", "Oʻzbek")
|
| 101 |
-
VIETNAMESE = ("vi", "vie", "vie", "Vietnamese", "Tiếng Việt")
|
| 102 |
-
YIDDISH = ("yi", "yid", "yid", "Yiddish", "ייִדיש")
|
| 103 |
-
YORUBA = ("yo", "yor", "yor", "Yoruba", "Yorùbá")
|
| 104 |
-
CHINESE = ("zh", "zho", "chi", "Chinese", "中文")
|
| 105 |
-
CANTONESE = ("yue", "yue", "yue", "Cantonese", "粵語")
|
| 106 |
-
NONE = (None, None, None, None, None) # For no language
|
| 107 |
-
# und for Undetermined aka unknown language https://www.loc.gov/standards/iso639-2/faq.html#25
|
| 108 |
-
|
| 109 |
-
def __init__(self, iso_639_1, iso_639_2_t, iso_639_2_b, name_en, name_native):
|
| 110 |
-
self.iso_639_1 = iso_639_1
|
| 111 |
-
self.iso_639_2_t = iso_639_2_t
|
| 112 |
-
self.iso_639_2_b = iso_639_2_b
|
| 113 |
-
self.name_en = name_en
|
| 114 |
-
self.name_native = name_native
|
| 115 |
-
|
| 116 |
-
@staticmethod
|
| 117 |
-
def from_iso_639_1(code):
|
| 118 |
-
for lang in LanguageCode:
|
| 119 |
-
if lang.iso_639_1 == code:
|
| 120 |
-
return lang
|
| 121 |
-
return LanguageCode.NONE
|
| 122 |
-
|
| 123 |
-
@staticmethod
|
| 124 |
-
def from_iso_639_2(code):
|
| 125 |
-
for lang in LanguageCode:
|
| 126 |
-
if lang.iso_639_2_t == code or lang.iso_639_2_b == code:
|
| 127 |
-
return lang
|
| 128 |
-
return LanguageCode.NONE
|
| 129 |
-
|
| 130 |
-
@staticmethod
|
| 131 |
-
def from_name(name : str):
|
| 132 |
-
"""Convert a language name (either English or native) to LanguageCode enum."""
|
| 133 |
-
for lang in LanguageCode:
|
| 134 |
-
if lang.name_en.lower() == name.lower() or lang.name_native.lower() == name.lower():
|
| 135 |
-
return lang
|
| 136 |
-
LanguageCode.NONE
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
@staticmethod
|
| 140 |
-
def from_string(value: str):
|
| 141 |
-
"""
|
| 142 |
-
Convert a string to a LanguageCode instance. Matches on ISO codes, English name, or native name.
|
| 143 |
-
"""
|
| 144 |
-
if value is None:
|
| 145 |
-
return LanguageCode.NONE
|
| 146 |
-
value = value.strip().lower()
|
| 147 |
-
for lang in LanguageCode:
|
| 148 |
-
if lang is LanguageCode.NONE:
|
| 149 |
-
continue
|
| 150 |
-
elif (
|
| 151 |
-
value == lang.iso_639_1
|
| 152 |
-
or value == lang.iso_639_2_t
|
| 153 |
-
or value == lang.iso_639_2_b
|
| 154 |
-
or value == lang.name_en.lower()
|
| 155 |
-
or value == lang.name_native.lower()
|
| 156 |
-
):
|
| 157 |
-
return lang
|
| 158 |
-
return LanguageCode.NONE
|
| 159 |
-
|
| 160 |
-
# is valid language
|
| 161 |
-
@staticmethod
|
| 162 |
-
def is_valid_language(language: str):
|
| 163 |
-
return LanguageCode.from_string(language) is not LanguageCode.NONE
|
| 164 |
-
|
| 165 |
-
def to_iso_639_1(self):
|
| 166 |
-
return self.iso_639_1
|
| 167 |
-
|
| 168 |
-
def to_iso_639_2_t(self):
|
| 169 |
-
return self.iso_639_2_t
|
| 170 |
-
|
| 171 |
-
def to_iso_639_2_b(self):
|
| 172 |
-
return self.iso_639_2_b
|
| 173 |
-
|
| 174 |
-
def to_name(self, in_english=True):
|
| 175 |
-
return self.name_en if in_english else self.name_native
|
| 176 |
-
def __str__(self):
|
| 177 |
-
if self.name_en is None:
|
| 178 |
-
return "Unknown"
|
| 179 |
-
return self.name_en
|
| 180 |
-
|
| 181 |
-
def __bool__(self):
|
| 182 |
-
return True if self.iso_639_1 is not None else False
|
| 183 |
-
|
| 184 |
-
def __eq__(self, other):
|
| 185 |
-
"""
|
| 186 |
-
Compare the LanguageCode instance to another object.
|
| 187 |
-
Explicitly handle comparison to None.
|
| 188 |
-
"""
|
| 189 |
-
if other is None:
|
| 190 |
-
# If compared to None, return False unless self is None
|
| 191 |
-
return self.iso_639_1 is None
|
| 192 |
-
if isinstance(other, str): # Allow comparison with a string
|
| 193 |
-
return self.value == LanguageCode.from_string(other)
|
| 194 |
-
if isinstance(other, LanguageCode):
|
| 195 |
-
# Normal comparison for LanguageCode instances
|
| 196 |
-
return self.iso_639_1 == other.iso_639_1
|
| 197 |
-
# Otherwise, defer to the default equality
|
| 198 |
-
return NotImplemented
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-
import sys
|
| 3 |
-
import urllib.request
|
| 4 |
-
import subprocess
|
| 5 |
-
import argparse
|
| 6 |
-
|
| 7 |
-
def convert_to_bool(in_bool):
|
| 8 |
-
# Convert the input to string and lower case, then check against true values
|
| 9 |
-
return str(in_bool).lower() in ('true', 'on', '1', 'y', 'yes')
|
| 10 |
-
|
| 11 |
-
def install_packages_from_requirements(requirements_file):
|
| 12 |
-
try:
|
| 13 |
-
subprocess.run(['pip3', 'install', '-r', requirements_file, '--upgrade'], check=True)
|
| 14 |
-
print("Packages installed successfully using pip3.")
|
| 15 |
-
except subprocess.CalledProcessError:
|
| 16 |
-
try:
|
| 17 |
-
subprocess.run(['pip', 'install', '-r', requirements_file, '--upgrade'], check=True)
|
| 18 |
-
print("Packages installed successfully using pip.")
|
| 19 |
-
except subprocess.CalledProcessError:
|
| 20 |
-
print("Failed to install packages using both pip3 and pip.")
|
| 21 |
-
|
| 22 |
-
def download_from_github(url, output_file):
|
| 23 |
-
try:
|
| 24 |
-
with urllib.request.urlopen(url) as response, open(output_file, 'wb') as out_file:
|
| 25 |
-
data = response.read()
|
| 26 |
-
out_file.write(data)
|
| 27 |
-
print(f"File downloaded successfully to {output_file}")
|
| 28 |
-
except urllib.error.HTTPError as e:
|
| 29 |
-
print(f"Failed to download file from {url}. HTTP Error Code: {e.code}")
|
| 30 |
-
except urllib.error.URLError as e:
|
| 31 |
-
print(f"URL Error: {e.reason}")
|
| 32 |
-
except Exception as e:
|
| 33 |
-
print(f"An error occurred: {e}")
|
| 34 |
-
|
| 35 |
-
def prompt_and_save_bazarr_env_variables():
|
| 36 |
-
instructions = (
|
| 37 |
-
"You will be prompted for several configuration values.\n"
|
| 38 |
-
"If you wish to use the default value for any of them, simply press Enter without typing anything.\n"
|
| 39 |
-
"The default values are shown in brackets [] next to the prompts.\n"
|
| 40 |
-
"Items can be the value of true, on, 1, y, yes, false, off, 0, n, no, or an appropriate text response.\n"
|
| 41 |
-
)
|
| 42 |
-
print(instructions)
|
| 43 |
-
env_vars = {
|
| 44 |
-
'WHISPER_MODEL': ('Whisper Model', 'Enter the Whisper model you want to run: tiny, tiny.en, base, base.en, small, small.en, medium, medium.en, large, distil-large-v2, distil-medium.en, distil-small.en', 'distil-large-v3.5'),
|
| 45 |
-
'WEBHOOKPORT': ('Webhook Port', 'Default listening port for subgen.py', '9000'),
|
| 46 |
-
'TRANSCRIBE_DEVICE': ('Transcribe Device', 'Set as cpu or gpu', 'cpu'),
|
| 47 |
-
# Defaulting to False here for the prompt, user can change
|
| 48 |
-
'DEBUG': ('Debug', 'Enable debug logging (true/false)', 'true'),
|
| 49 |
-
'CLEAR_VRAM_ON_COMPLETE': ('Clear VRAM', 'Attempt to clear VRAM when complete (Windows users may need to set this to False)', 'true'),
|
| 50 |
-
'APPEND': ('Append', 'Append \'Transcribed by whisper\' to generated subtitle (true/false)', 'False'),
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
user_input = {}
|
| 54 |
-
with open('subgen.env', 'w') as file:
|
| 55 |
-
for var, (description, prompt, default) in env_vars.items():
|
| 56 |
-
value = input(f"{prompt} [{default}]: ") or default
|
| 57 |
-
file.write(f"{var}={value}\n")
|
| 58 |
-
print("Environment variables have been saved to subgen.env")
|
| 59 |
-
|
| 60 |
-
def load_env_variables(env_filename='subgen.env'):
|
| 61 |
-
try:
|
| 62 |
-
with open(env_filename, 'r') as file:
|
| 63 |
-
for line in file:
|
| 64 |
-
line = line.strip()
|
| 65 |
-
if line and not line.startswith('#') and '=' in line:
|
| 66 |
-
var, value = line.split('=', 1)
|
| 67 |
-
# Only set if not already set by a higher priority mechanism (like external env var)
|
| 68 |
-
# For this simple loader, we'll let it overwrite,
|
| 69 |
-
# and CLI args will overwrite these later if specified.
|
| 70 |
-
os.environ[var] = value
|
| 71 |
-
print(f"Environment variables have been loaded from {env_filename}")
|
| 72 |
-
except FileNotFoundError:
|
| 73 |
-
print(f"{env_filename} file not found. Consider running with --setup-bazarr or creating it manually.")
|
| 74 |
-
|
| 75 |
-
def main():
|
| 76 |
-
if 'python3' in sys.executable:
|
| 77 |
-
python_cmd = 'python3'
|
| 78 |
-
elif 'python' in sys.executable:
|
| 79 |
-
python_cmd = 'python'
|
| 80 |
-
else:
|
| 81 |
-
print("Script started with an unknown command")
|
| 82 |
-
sys.exit(1)
|
| 83 |
-
if sys.version_info[0] < 3:
|
| 84 |
-
print(f"This script requires Python 3 or higher, you are running {sys.version}")
|
| 85 |
-
sys.exit(1)
|
| 86 |
-
|
| 87 |
-
os.chdir(os.path.dirname(os.path.abspath(__file__)))
|
| 88 |
-
|
| 89 |
-
parser = argparse.ArgumentParser(prog="python launcher.py", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
| 90 |
-
# Changed: action='store_true' means it's False by default, True if flag is present
|
| 91 |
-
parser.add_argument('-d', '--debug', action='store_true', help="Enable console debugging (overrides .env and external ENV)")
|
| 92 |
-
parser.add_argument('-i', '--install', action='store_true', help="Install/update all necessary packages")
|
| 93 |
-
# Changed: action='store_true'
|
| 94 |
-
parser.add_argument('-a', '--append', action='store_true', help="Append 'Transcribed by whisper' (overrides .env and external ENV)")
|
| 95 |
-
parser.add_argument('-u', '--update', action='store_true', help="Update Subgen")
|
| 96 |
-
parser.add_argument('-x', '--exit-early', action='store_true', help="Exit without running subgen.py")
|
| 97 |
-
parser.add_argument('-s', '--setup-bazarr', action='store_true', help="Prompt for common Bazarr setup parameters and save them for future runs")
|
| 98 |
-
parser.add_argument('-b', '--branch', type=str, default='main', help='Specify the branch to download from')
|
| 99 |
-
parser.add_argument('-l', '--launcher-update', action='store_true', help="Update launcher.py and re-launch")
|
| 100 |
-
|
| 101 |
-
args = parser.parse_args()
|
| 102 |
-
|
| 103 |
-
branch_name = args.branch if args.branch != 'main' else os.getenv('BRANCH', 'main')
|
| 104 |
-
script_name_suffix = f"-{branch_name}.py" if branch_name != "main" else ".py"
|
| 105 |
-
subgen_script_to_run = f"subgen{script_name_suffix}"
|
| 106 |
-
language_code_script_to_download = f"language_code{script_name_suffix}"
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
if args.launcher_update or convert_to_bool(os.getenv('LAUNCHER_UPDATE')):
|
| 110 |
-
print(f"Updating launcher.py from GitHub branch {branch_name}...")
|
| 111 |
-
download_from_github(f"https://raw.githubusercontent.com/McCloudS/subgen/{branch_name}/launcher.py", f'launcher{script_name_suffix}')
|
| 112 |
-
excluded_args = ['--launcher-update', '-l']
|
| 113 |
-
new_args = [arg for arg in sys.argv[1:] if arg not in excluded_args]
|
| 114 |
-
print(f"Relaunching updated launcher: launcher{script_name_suffix}")
|
| 115 |
-
os.execl(sys.executable, sys.executable, f"launcher{script_name_suffix}", *new_args)
|
| 116 |
-
# The script will not continue past os.execl
|
| 117 |
-
|
| 118 |
-
# --- Environment Variable Handling ---
|
| 119 |
-
# 1. Load from .env file first. This sets a baseline.
|
| 120 |
-
# External environment variables (set before launcher.py) will already be in os.environ
|
| 121 |
-
# and won't be overwritten by load_env_variables IF load_env_variables checked for existence.
|
| 122 |
-
# For simplicity, this version of load_env_variables *will* overwrite.
|
| 123 |
-
# If you need to preserve external env vars over .env, load_env_variables needs adjustment.
|
| 124 |
-
if args.setup_bazarr:
|
| 125 |
-
prompt_and_save_bazarr_env_variables()
|
| 126 |
-
# After saving, load them immediately for this run
|
| 127 |
-
load_env_variables()
|
| 128 |
-
else:
|
| 129 |
-
# Load if not setting up, assuming subgen.env might exist
|
| 130 |
-
load_env_variables()
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
# 2. Override with command-line arguments (highest priority for these specific flags)
|
| 134 |
-
if args.debug: # If -d or --debug was passed
|
| 135 |
-
os.environ['DEBUG'] = 'True'
|
| 136 |
-
print("Launcher CLI: DEBUG set to True")
|
| 137 |
-
elif 'DEBUG' not in os.environ: # If not set by CLI and not by .env or external
|
| 138 |
-
os.environ['DEBUG'] = 'False' # Default to False if nothing else specified it
|
| 139 |
-
print("Launcher: DEBUG defaulted to False (no prior setting)")
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
if args.append: # If -a or --append was passed
|
| 143 |
-
os.environ['APPEND'] = 'True'
|
| 144 |
-
print("Launcher CLI: APPEND set to True")
|
| 145 |
-
elif 'APPEND' not in os.environ: # If not set by CLI and not by .env or external
|
| 146 |
-
os.environ['APPEND'] = 'False' # Default to False if nothing else specified it
|
| 147 |
-
#print("Launcher: APPEND defaulted to False (no prior setting)")
|
| 148 |
-
# --- End Environment Variable Handling ---
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
requirements_url = "https://raw.githubusercontent.com/McCloudS/subgen/main/requirements.txt"
|
| 152 |
-
requirements_file = "requirements.txt"
|
| 153 |
-
|
| 154 |
-
if args.install:
|
| 155 |
-
download_from_github(requirements_url, requirements_file)
|
| 156 |
-
install_packages_from_requirements(requirements_file)
|
| 157 |
-
|
| 158 |
-
if not os.path.exists(subgen_script_to_run) or args.update or convert_to_bool(os.getenv('UPDATE')):
|
| 159 |
-
print(f"Downloading {subgen_script_to_run} from GitHub branch {branch_name}...")
|
| 160 |
-
download_from_github(f"https://raw.githubusercontent.com/McCloudS/subgen/{branch_name}/subgen.py", subgen_script_to_run)
|
| 161 |
-
print(f"Downloading {language_code_script_to_download} from GitHub branch {branch_name}...")
|
| 162 |
-
download_from_github(f"https://raw.githubusercontent.com/McCloudS/subgen/{branch_name}/language_code.py", language_code_script_to_download)
|
| 163 |
-
|
| 164 |
-
else:
|
| 165 |
-
print(f"{subgen_script_to_run} exists and UPDATE is set to False, skipping download.")
|
| 166 |
-
|
| 167 |
-
if not args.exit_early:
|
| 168 |
-
#print(f"DEBUG environment variable for subgen.py: {os.getenv('DEBUG')}")
|
| 169 |
-
#print(f"APPEND environment variable for subgen.py: {os.getenv('APPEND')}")
|
| 170 |
-
print(f'Launching {subgen_script_to_run}')
|
| 171 |
-
try:
|
| 172 |
-
subprocess.run([python_cmd, '-u', subgen_script_to_run], check=True)
|
| 173 |
-
except FileNotFoundError:
|
| 174 |
-
print(f"Error: Could not find {subgen_script_to_run}. Make sure it was downloaded correctly.")
|
| 175 |
-
except subprocess.CalledProcessError as e:
|
| 176 |
-
print(f"Error running {subgen_script_to_run}: {e}")
|
| 177 |
-
|
| 178 |
-
else:
|
| 179 |
-
print("Not running subgen.py: -x or --exit-early set")
|
| 180 |
-
|
| 181 |
-
if __name__ == "__main__":
|
| 182 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -7,4 +7,4 @@ uvicorn
|
|
| 7 |
python-multipart
|
| 8 |
ffmpeg-python
|
| 9 |
whisper
|
| 10 |
-
watchdog
|
|
|
|
| 7 |
python-multipart
|
| 8 |
ffmpeg-python
|
| 9 |
whisper
|
| 10 |
+
watchdog
|
subgen.env
DELETED
|
@@ -1,6 +0,0 @@
|
|
| 1 |
-
WHISPER_MODEL=large-v3-turbo
|
| 2 |
-
WEBHOOKPORT=9000
|
| 3 |
-
TRANSCRIBE_DEVICE=cpu
|
| 4 |
-
DEBUG=True
|
| 5 |
-
CLEAR_VRAM_ON_COMPLETE=True
|
| 6 |
-
APPEND=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subgen.py
DELETED
|
@@ -1,2148 +0,0 @@
|
|
| 1 |
-
subgen_version = '2026.01.22'
|
| 2 |
-
|
| 3 |
-
"""
|
| 4 |
-
ENVIRONMENT VARIABLES DOCUMENTATION
|
| 5 |
-
|
| 6 |
-
This application supports both new standardized environment variable names and legacy names for backwards compatibility. The new names follow a consistent naming convention:
|
| 7 |
-
|
| 8 |
-
STANDARDIZED NAMING CONVENTION:
|
| 9 |
-
- Use UPPERCASE with underscores for separation
|
| 10 |
-
- Group related variables with consistent prefixes:
|
| 11 |
-
* PLEX_* for Plex server integration
|
| 12 |
-
* JELLYFIN_* for Jellyfin server integration
|
| 13 |
-
* PROCESS_* for media processing triggers
|
| 14 |
-
* SKIP_* for all skip conditions
|
| 15 |
-
* SUBTITLE_* for subtitle-related settings
|
| 16 |
-
* WHISPER_* for Whisper model settings
|
| 17 |
-
* TRANSCRIBE_* for transcription settings
|
| 18 |
-
|
| 19 |
-
BACKWARDS COMPATIBILITY:
|
| 20 |
-
Legacy environment variable names are still supported. If both new and old names are set,
|
| 21 |
-
the new standardized name takes precedence.
|
| 22 |
-
|
| 23 |
-
NEW NAME → OLD NAME (for backwards compatibility):
|
| 24 |
-
- PLEX_TOKEN → PLEXTOKEN
|
| 25 |
-
- PLEX_SERVER → PLEXSERVER
|
| 26 |
-
- JELLYFIN_TOKEN → JELLYFINTOKEN
|
| 27 |
-
- JELLYFIN_SERVER → JELLYFINSERVER
|
| 28 |
-
- PROCESS_ADDED_MEDIA → PROCADDEDMEDIA
|
| 29 |
-
- PROCESS_MEDIA_ON_PLAY → PROCMEDIAONPLAY
|
| 30 |
-
- SUBTITLE_LANGUAGE_NAME → NAMESUBLANG
|
| 31 |
-
- WEBHOOK_PORT → WEBHOOKPORT
|
| 32 |
-
- SKIP_IF_EXTERNAL_SUBTITLES_EXIST → SKIPIFEXTERNALSUB
|
| 33 |
-
- SKIP_IF_TARGET_SUBTITLES_EXIST → SKIP_IF_TO_TRANSCRIBE_SUB_ALREADY_EXIST
|
| 34 |
-
- SKIP_IF_INTERNAL_SUBTITLES_LANGUAGE → SKIPIFINTERNALSUBLANG
|
| 35 |
-
- SKIP_SUBTITLE_LANGUAGES → SKIP_LANG_CODES
|
| 36 |
-
- SKIP_IF_AUDIO_LANGUAGES → SKIP_IF_AUDIO_TRACK_IS
|
| 37 |
-
- SKIP_ONLY_SUBGEN_SUBTITLES → ONLY_SKIP_IF_SUBGEN_SUBTITLE
|
| 38 |
-
- SKIP_IF_NO_LANGUAGE_BUT_SUBTITLES_EXIST → SKIP_IF_LANGUAGE_IS_NOT_SET_BUT_SUBTITLES_EXIST
|
| 39 |
-
|
| 40 |
-
MIGRATION GUIDE:
|
| 41 |
-
Users can gradually migrate to the new names. Both will work simultaneously during the
|
| 42 |
-
transition period. The old names may be deprecated in future versions.
|
| 43 |
-
"""
|
| 44 |
-
|
| 45 |
-
from language_code import LanguageCode
|
| 46 |
-
from datetime import datetime
|
| 47 |
-
from threading import Lock, Event, Timer
|
| 48 |
-
import os
|
| 49 |
-
import json
|
| 50 |
-
import xml.etree.ElementTree as ET
|
| 51 |
-
import threading
|
| 52 |
-
import sys
|
| 53 |
-
import time
|
| 54 |
-
import queue
|
| 55 |
-
import logging
|
| 56 |
-
import gc
|
| 57 |
-
import random
|
| 58 |
-
import hashlib
|
| 59 |
-
from typing import Union, Any, Optional
|
| 60 |
-
from fastapi import FastAPI, File, UploadFile, Query, Header, Body, Form, Request
|
| 61 |
-
from fastapi.responses import StreamingResponse
|
| 62 |
-
import numpy as np
|
| 63 |
-
import stable_whisper
|
| 64 |
-
from stable_whisper import Segment
|
| 65 |
-
import requests
|
| 66 |
-
import av
|
| 67 |
-
import ffmpeg
|
| 68 |
-
import whisper
|
| 69 |
-
import ast
|
| 70 |
-
from watchdog.observers.polling import PollingObserver as Observer
|
| 71 |
-
from watchdog.events import FileSystemEventHandler
|
| 72 |
-
import faster_whisper
|
| 73 |
-
from io import BytesIO
|
| 74 |
-
import io
|
| 75 |
-
import asyncio
|
| 76 |
-
import torch
|
| 77 |
-
import ctypes, ctypes.util
|
| 78 |
-
from typing import List
|
| 79 |
-
from enum import Enum
|
| 80 |
-
|
| 81 |
-
def convert_to_bool(in_bool):
|
| 82 |
-
# Convert the input to string and lower case, then check against true values
|
| 83 |
-
return str(in_bool).lower() in ('true', 'on', '1', 'y', 'yes')
|
| 84 |
-
|
| 85 |
-
def get_env_with_fallback(new_name: str, old_name: str, default_value=None, convert_func=None):
|
| 86 |
-
"""
|
| 87 |
-
Get environment variable with backwards compatibility fallback.
|
| 88 |
-
|
| 89 |
-
Args:
|
| 90 |
-
new_name: The new standardized environment variable name
|
| 91 |
-
old_name: The legacy environment variable name for backwards compatibility
|
| 92 |
-
default_value: Default value if neither variable is set
|
| 93 |
-
convert_func: Optional function to convert the value (e.g., convert_to_bool, int)
|
| 94 |
-
|
| 95 |
-
Returns:
|
| 96 |
-
The environment variable value, converted if convert_func is provided
|
| 97 |
-
"""
|
| 98 |
-
# Try new name first, then fall back to old name
|
| 99 |
-
value = os.getenv(new_name) or os.getenv(old_name)
|
| 100 |
-
|
| 101 |
-
if value is None:
|
| 102 |
-
value = default_value
|
| 103 |
-
|
| 104 |
-
# Apply conversion function if provided
|
| 105 |
-
if convert_func and value is not None:
|
| 106 |
-
return convert_func(value)
|
| 107 |
-
|
| 108 |
-
return value
|
| 109 |
-
|
| 110 |
-
# Server Integration - with backwards compatibility
|
| 111 |
-
plextoken = get_env_with_fallback('PLEX_TOKEN', 'PLEXTOKEN', 'token here')
|
| 112 |
-
plexserver = get_env_with_fallback('PLEX_SERVER', 'PLEXSERVER', 'http://192.168.1.111:32400')
|
| 113 |
-
jellyfintoken = get_env_with_fallback('JELLYFIN_TOKEN', 'JELLYFINTOKEN', 'token here')
|
| 114 |
-
jellyfinserver = get_env_with_fallback('JELLYFIN_SERVER', 'JELLYFINSERVER', 'http://192.168.1.111:8096')
|
| 115 |
-
|
| 116 |
-
# Whisper Configuration
|
| 117 |
-
whisper_model = os.getenv('WHISPER_MODEL', 'large-v3-turbo')
|
| 118 |
-
whisper_threads = int(os.getenv('WHISPER_THREADS', 4))
|
| 119 |
-
concurrent_transcriptions = int(os.getenv('CONCURRENT_TRANSCRIPTIONS', 2))
|
| 120 |
-
transcribe_device = os.getenv('TRANSCRIBE_DEVICE', 'cpu')
|
| 121 |
-
|
| 122 |
-
# Processing Control - with backwards compatibility
|
| 123 |
-
procaddedmedia = get_env_with_fallback('PROCESS_ADDED_MEDIA', 'PROCADDEDMEDIA', True, convert_to_bool)
|
| 124 |
-
procmediaonplay = get_env_with_fallback('PROCESS_MEDIA_ON_PLAY', 'PROCMEDIAONPLAY', True, convert_to_bool)
|
| 125 |
-
|
| 126 |
-
# Subtitle Configuration - with backwards compatibility
|
| 127 |
-
namesublang = get_env_with_fallback('SUBTITLE_LANGUAGE_NAME', 'NAMESUBLANG', '')
|
| 128 |
-
|
| 129 |
-
# System Configuration - with backwards compatibility
|
| 130 |
-
webhookport = get_env_with_fallback('WEBHOOK_PORT', 'WEBHOOKPORT', 80, int)
|
| 131 |
-
word_level_highlight = convert_to_bool(os.getenv('WORD_LEVEL_HIGHLIGHT', False))
|
| 132 |
-
debug = convert_to_bool(os.getenv('DEBUG', True))
|
| 133 |
-
use_path_mapping = convert_to_bool(os.getenv('USE_PATH_MAPPING', False))
|
| 134 |
-
path_mapping_from = os.getenv('PATH_MAPPING_FROM', r'/tv')
|
| 135 |
-
path_mapping_to = os.getenv('PATH_MAPPING_TO', r'/Volumes/TV')
|
| 136 |
-
model_location = os.getenv('MODEL_PATH', './models')
|
| 137 |
-
monitor = convert_to_bool(os.getenv('MONITOR', False))
|
| 138 |
-
transcribe_folders = os.getenv('TRANSCRIBE_FOLDERS', '')
|
| 139 |
-
transcribe_or_translate = os.getenv('TRANSCRIBE_OR_TRANSLATE', 'transcribe').lower()
|
| 140 |
-
clear_vram_on_complete = convert_to_bool(os.getenv('CLEAR_VRAM_ON_COMPLETE', True))
|
| 141 |
-
compute_type = os.getenv('COMPUTE_TYPE', 'auto')
|
| 142 |
-
append = convert_to_bool(os.getenv('APPEND', False))
|
| 143 |
-
reload_script_on_change = convert_to_bool(os.getenv('RELOAD_SCRIPT_ON_CHANGE', False))
|
| 144 |
-
lrc_for_audio_files = convert_to_bool(os.getenv('LRC_FOR_AUDIO_FILES', True))
|
| 145 |
-
custom_regroup = os.getenv('CUSTOM_REGROUP', 'cm_sl=84_sl=42++++++1')
|
| 146 |
-
detect_language_length = int(os.getenv('DETECT_LANGUAGE_LENGTH', 30))
|
| 147 |
-
detect_language_offset = int(os.getenv('DETECT_LANGUAGE_OFFSET', 0))
|
| 148 |
-
model_cleanup_delay = int(os.getenv('MODEL_CLEANUP_DELAY', 30))
|
| 149 |
-
|
| 150 |
-
# Skip Configuration - with backwards compatibility
|
| 151 |
-
skipifexternalsub = get_env_with_fallback('SKIP_IF_EXTERNAL_SUBTITLES_EXIST', 'SKIPIFEXTERNALSUB', False, convert_to_bool)
|
| 152 |
-
skip_if_to_transcribe_sub_already_exist = get_env_with_fallback('SKIP_IF_TARGET_SUBTITLES_EXIST', 'SKIP_IF_TO_TRANSCRIBE_SUB_ALREADY_EXIST', True, convert_to_bool)
|
| 153 |
-
skipifinternalsublang = LanguageCode.from_string(get_env_with_fallback('SKIP_IF_INTERNAL_SUBTITLES_LANGUAGE', 'SKIPIFINTERNALSUBLANG', ''))
|
| 154 |
-
plex_queue_next_episode = convert_to_bool(os.getenv('PLEX_QUEUE_NEXT_EPISODE', False))
|
| 155 |
-
plex_queue_season = convert_to_bool(os.getenv('PLEX_QUEUE_SEASON', False))
|
| 156 |
-
plex_queue_series = convert_to_bool(os.getenv('PLEX_QUEUE_SERIES', False))
|
| 157 |
-
# Language and Skip Configuration - with backwards compatibility
|
| 158 |
-
skip_lang_codes_list = (
|
| 159 |
-
[LanguageCode.from_string(code) for code in get_env_with_fallback('SKIP_SUBTITLE_LANGUAGES', 'SKIP_LANG_CODES', '').split("|")]
|
| 160 |
-
if get_env_with_fallback('SKIP_SUBTITLE_LANGUAGES', 'SKIP_LANG_CODES')
|
| 161 |
-
else []
|
| 162 |
-
)
|
| 163 |
-
force_detected_language_to = LanguageCode.from_string(os.getenv('FORCE_DETECTED_LANGUAGE_TO', ''))
|
| 164 |
-
preferred_audio_languages = [
|
| 165 |
-
LanguageCode.from_string(code)
|
| 166 |
-
for code in os.getenv('PREFERRED_AUDIO_LANGUAGES', 'eng').split("|")
|
| 167 |
-
] # in order of preference
|
| 168 |
-
limit_to_preferred_audio_languages = convert_to_bool(os.getenv('LIMIT_TO_PREFERRED_AUDIO_LANGUAGE', False)) #TODO: add support for this
|
| 169 |
-
skip_if_audio_track_is_in_list = (
|
| 170 |
-
[LanguageCode.from_string(code) for code in get_env_with_fallback('SKIP_IF_AUDIO_LANGUAGES', 'SKIP_IF_AUDIO_TRACK_IS', '').split("|")]
|
| 171 |
-
if get_env_with_fallback('SKIP_IF_AUDIO_LANGUAGES', 'SKIP_IF_AUDIO_TRACK_IS')
|
| 172 |
-
else []
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
# Additional Subtitle Configuration - with backwards compatibility
|
| 176 |
-
subtitle_language_naming_type = os.getenv('SUBTITLE_LANGUAGE_NAMING_TYPE', 'ISO_639_2_B')
|
| 177 |
-
only_skip_if_subgen_subtitle = get_env_with_fallback('SKIP_ONLY_SUBGEN_SUBTITLES', 'ONLY_SKIP_IF_SUBGEN_SUBTITLE', False, convert_to_bool)
|
| 178 |
-
skip_unknown_language = convert_to_bool(os.getenv('SKIP_UNKNOWN_LANGUAGE', False))
|
| 179 |
-
skip_if_language_is_not_set_but_subtitles_exist = get_env_with_fallback('SKIP_IF_NO_LANGUAGE_BUT_SUBTITLES_EXIST', 'SKIP_IF_LANGUAGE_IS_NOT_SET_BUT_SUBTITLES_EXIST', False, convert_to_bool)
|
| 180 |
-
should_whiser_detect_audio_language = convert_to_bool(os.getenv('SHOULD_WHISPER_DETECT_AUDIO_LANGUAGE', False))
|
| 181 |
-
show_in_subname_subgen = convert_to_bool(os.getenv('SHOW_IN_SUBNAME_SUBGEN', True))
|
| 182 |
-
show_in_subname_model = convert_to_bool(os.getenv('SHOW_IN_SUBNAME_MODEL', True))
|
| 183 |
-
|
| 184 |
-
# Advanced Configuration
|
| 185 |
-
try:
|
| 186 |
-
kwargs = ast.literal_eval(os.getenv('SUBGEN_KWARGS', '{}') or '{}')
|
| 187 |
-
except ValueError:
|
| 188 |
-
kwargs = {}
|
| 189 |
-
logging.info("kwargs (SUBGEN_KWARGS) is an invalid dictionary, defaulting to empty '{}'")
|
| 190 |
-
|
| 191 |
-
if transcribe_device == "gpu":
|
| 192 |
-
transcribe_device = "cuda"
|
| 193 |
-
|
| 194 |
-
VIDEO_EXTENSIONS = (
|
| 195 |
-
".mp4", ".mkv", ".avi", ".mov", ".wmv", ".flv", ".webm", ".mpg", ".mpeg",
|
| 196 |
-
".3gp", ".ogv", ".vob", ".rm", ".rmvb", ".ts", ".m4v", ".f4v", ".svq3",
|
| 197 |
-
".asf", ".m2ts", ".divx", ".xvid"
|
| 198 |
-
)
|
| 199 |
-
|
| 200 |
-
AUDIO_EXTENSIONS = (
|
| 201 |
-
".mp3", ".wav", ".aac", ".flac", ".ogg", ".wma", ".alac", ".m4a", ".opus",
|
| 202 |
-
".aiff", ".aif", ".pcm", ".ra", ".ram", ".mid", ".midi", ".ape", ".wv",
|
| 203 |
-
".amr", ".vox", ".tak", ".spx", ".m4b", ".mka"
|
| 204 |
-
)
|
| 205 |
-
|
| 206 |
-
app = FastAPI()
|
| 207 |
-
model = None
|
| 208 |
-
model_cleanup_timer = None
|
| 209 |
-
model_cleanup_lock = Lock()
|
| 210 |
-
|
| 211 |
-
in_docker = os.path.exists('/.dockerenv')
|
| 212 |
-
docker_status = "Docker" if in_docker else "Standalone"
|
| 213 |
-
|
| 214 |
-
# ============================================================================
|
| 215 |
-
# TASK RESULT STORAGE (for blocking endpoints)
|
| 216 |
-
# ============================================================================
|
| 217 |
-
|
| 218 |
-
class TaskResult:
|
| 219 |
-
"""Stores the result of a queued task for blocking retrieval"""
|
| 220 |
-
def __init__(self):
|
| 221 |
-
self.result = None
|
| 222 |
-
self.error = None
|
| 223 |
-
self.done = Event()
|
| 224 |
-
|
| 225 |
-
def set_result(self, result):
|
| 226 |
-
self.result = result
|
| 227 |
-
self.done.set()
|
| 228 |
-
|
| 229 |
-
def set_error(self, error):
|
| 230 |
-
self.error = error
|
| 231 |
-
self.done.set()
|
| 232 |
-
|
| 233 |
-
def wait(self, timeout=None):
|
| 234 |
-
"""Block until result is ready. Returns True if completed, False if timeout."""
|
| 235 |
-
return self.done.wait(timeout)
|
| 236 |
-
|
| 237 |
-
# Dictionary to store task results keyed by task_id
|
| 238 |
-
task_results = {}
|
| 239 |
-
task_results_lock = Lock()
|
| 240 |
-
|
| 241 |
-
# ============================================================================
|
| 242 |
-
# HASH GENERATION FOR DEDUPLICATION
|
| 243 |
-
# ============================================================================
|
| 244 |
-
|
| 245 |
-
def generate_audio_hash(audio_content: bytes, task: str = None, language: str = None) -> str:
|
| 246 |
-
"""
|
| 247 |
-
Generate a deterministic hash from audio content and optional parameters.
|
| 248 |
-
|
| 249 |
-
Same audio + same task + same language = always same hash.
|
| 250 |
-
This ensures duplicate requests are caught by the queue.
|
| 251 |
-
|
| 252 |
-
Args:
|
| 253 |
-
audio_content: Raw audio bytes from uploaded file
|
| 254 |
-
task: Optional task type ('transcribe' or 'translate')
|
| 255 |
-
language: Optional target language code
|
| 256 |
-
|
| 257 |
-
Returns:
|
| 258 |
-
SHA256 hash (first 16 chars for brevity in logs)
|
| 259 |
-
"""
|
| 260 |
-
hash_input = audio_content
|
| 261 |
-
|
| 262 |
-
# Include task and language for fine-grained deduplication
|
| 263 |
-
if task:
|
| 264 |
-
hash_input += task.encode('utf-8')
|
| 265 |
-
if language:
|
| 266 |
-
hash_input += language.encode('utf-8')
|
| 267 |
-
|
| 268 |
-
full_hash = hashlib.sha256(hash_input).hexdigest()
|
| 269 |
-
return full_hash[:16] # Use first 16 chars for shorter IDs in logs
|
| 270 |
-
|
| 271 |
-
# ============================================================================
|
| 272 |
-
# REFACTORED DEDUPLICATED QUEUE WITH BETTER TRACKING
|
| 273 |
-
# ============================================================================
|
| 274 |
-
|
| 275 |
-
class DeduplicatedQueue(queue.PriorityQueue):
|
| 276 |
-
"""Queue that prevents duplicates, handles priority, and tracks status."""
|
| 277 |
-
def __init__(self):
|
| 278 |
-
super().__init__()
|
| 279 |
-
self._queued = set() # Tracks task IDs waiting in queue
|
| 280 |
-
self._processing = set() # Tracks task IDs currently being handled
|
| 281 |
-
self._lock = Lock()
|
| 282 |
-
|
| 283 |
-
def put(self, item, block=True, timeout=None):
|
| 284 |
-
with self._lock:
|
| 285 |
-
task_id = item["path"]
|
| 286 |
-
if task_id not in self._queued and task_id not in self._processing:
|
| 287 |
-
# Priority: 0 (Detect), 1 (ASR), 2 (Transcribe)
|
| 288 |
-
task_type = item.get("type", "transcribe")
|
| 289 |
-
priority = 0 if task_type == "detect_language" else (1 if task_type == "asr" else 2)
|
| 290 |
-
|
| 291 |
-
# PriorityQueue requires a tuple: (priority, tie_breaker, item)
|
| 292 |
-
super().put((priority, time.time(), item), block, timeout)
|
| 293 |
-
self._queued.add(task_id)
|
| 294 |
-
return True
|
| 295 |
-
return False
|
| 296 |
-
|
| 297 |
-
def get(self, block=True, timeout=None):
|
| 298 |
-
# PriorityQueue returns the tuple, we want just the item
|
| 299 |
-
priority, timestamp, item = super().get(block, timeout)
|
| 300 |
-
with self._lock:
|
| 301 |
-
task_id = item["path"]
|
| 302 |
-
self._queued.discard(task_id)
|
| 303 |
-
self._processing.add(task_id)
|
| 304 |
-
return item
|
| 305 |
-
|
| 306 |
-
def mark_done(self, item):
|
| 307 |
-
with self._lock:
|
| 308 |
-
task_id = item["path"]
|
| 309 |
-
self._processing.discard(task_id)
|
| 310 |
-
|
| 311 |
-
def is_idle(self):
|
| 312 |
-
with self._lock:
|
| 313 |
-
return self.empty() and len(self._processing) == 0
|
| 314 |
-
|
| 315 |
-
def is_active(self, task_id):
|
| 316 |
-
"""Checks if a task_id is currently queued or processing."""
|
| 317 |
-
with self._lock:
|
| 318 |
-
return task_id in self._queued or task_id in self._processing
|
| 319 |
-
|
| 320 |
-
def get_queued_tasks(self):
|
| 321 |
-
with self._lock:
|
| 322 |
-
return list(self._queued)
|
| 323 |
-
|
| 324 |
-
def get_processing_tasks(self):
|
| 325 |
-
with self._lock:
|
| 326 |
-
return list(self._processing)
|
| 327 |
-
|
| 328 |
-
# Start queue
|
| 329 |
-
task_queue = DeduplicatedQueue()
|
| 330 |
-
|
| 331 |
-
# ============================================================================
|
| 332 |
-
# TRANSCRIPTION WORKER
|
| 333 |
-
# ============================================================================
|
| 334 |
-
|
| 335 |
-
def transcription_worker():
|
| 336 |
-
"""Main worker thread with centralized logging and status tracking."""
|
| 337 |
-
while True:
|
| 338 |
-
task = None
|
| 339 |
-
try:
|
| 340 |
-
task = task_queue.get(block=True, timeout=1)
|
| 341 |
-
task_type = task.get("type", "transcribe")
|
| 342 |
-
path = task.get("path", "unknown")
|
| 343 |
-
display_name = os.path.basename(path) if ("/" in str(path) or "\\" in str(path)) else path
|
| 344 |
-
|
| 345 |
-
# Status for START log
|
| 346 |
-
proc_count = len(task_queue.get_processing_tasks())
|
| 347 |
-
queue_count = len(task_queue.get_queued_tasks())
|
| 348 |
-
logging.info(f"WORKER START : [{task_type.upper():<10}] {display_name:^40} | Jobs: {proc_count} processing, {queue_count} queued")
|
| 349 |
-
|
| 350 |
-
start_time = time.time()
|
| 351 |
-
if task_type == "detect_language":
|
| 352 |
-
if "audio_content" in task:
|
| 353 |
-
detect_language_from_upload(task)
|
| 354 |
-
else:
|
| 355 |
-
# Pass the full task data so we don't lose the Plex ID
|
| 356 |
-
detect_language_task(task['path'], original_task_data=task)
|
| 357 |
-
elif task_type == "asr":
|
| 358 |
-
asr_task_worker(task)
|
| 359 |
-
else: # transcribe
|
| 360 |
-
gen_subtitles(task['path'], task['transcribe_or_translate'], task['force_language'])
|
| 361 |
-
|
| 362 |
-
# --- METADATA REFRESH LOGIC ---
|
| 363 |
-
# This runs ONLY after subtitles are successfully generated
|
| 364 |
-
if 'plex_item_id' in task:
|
| 365 |
-
try:
|
| 366 |
-
logging.info(f"Refreshing Plex Metadata for item {task['plex_item_id']}")
|
| 367 |
-
refresh_plex_metadata(task['plex_item_id'], task['plex_server'], task['plex_token'])
|
| 368 |
-
except Exception as e:
|
| 369 |
-
logging.error(f"Failed to refresh Plex metadata: {e}")
|
| 370 |
-
|
| 371 |
-
if 'jellyfin_item_id' in task:
|
| 372 |
-
try:
|
| 373 |
-
logging.info(f"Refreshing Jellyfin Metadata for item {task['jellyfin_item_id']}")
|
| 374 |
-
refresh_jellyfin_metadata(task['jellyfin_item_id'], task['jellyfin_server'], task['jellyfin_token'])
|
| 375 |
-
except Exception as e:
|
| 376 |
-
logging.error(f"Failed to refresh Jellyfin metadata: {e}")
|
| 377 |
-
# ------------------------------
|
| 378 |
-
|
| 379 |
-
# Status for FINISH log
|
| 380 |
-
elapsed = time.time() - start_time
|
| 381 |
-
m, s = divmod(int(elapsed), 60)
|
| 382 |
-
remaining_queued = len(task_queue.get_queued_tasks())
|
| 383 |
-
logging.info(f"WORKER FINISH: [{task_type.upper():<10}] {display_name:^40} in {m}m {s}s | Remaining: {remaining_queued} queued")
|
| 384 |
-
|
| 385 |
-
except queue.Empty:
|
| 386 |
-
continue
|
| 387 |
-
except Exception as e:
|
| 388 |
-
logging.error(f"Error processing task: {e}", exc_info=True)
|
| 389 |
-
finally:
|
| 390 |
-
if task:
|
| 391 |
-
task_queue.task_done()
|
| 392 |
-
task_queue.mark_done(task)
|
| 393 |
-
delete_model()
|
| 394 |
-
# Create worker threads
|
| 395 |
-
for _ in range(concurrent_transcriptions):
|
| 396 |
-
threading.Thread(target=transcription_worker, daemon=True).start()
|
| 397 |
-
|
| 398 |
-
# Define a filter class to hide common logging we don't want to see
|
| 399 |
-
class MultiplePatternsFilter(logging.Filter):
|
| 400 |
-
def filter(self, record):
|
| 401 |
-
# Define the patterns to search for
|
| 402 |
-
patterns = [
|
| 403 |
-
"Compression ratio threshold is not met",
|
| 404 |
-
"Processing segment at",
|
| 405 |
-
"Log probability threshold is",
|
| 406 |
-
"Reset prompt",
|
| 407 |
-
"Attempting to release",
|
| 408 |
-
"released on ",
|
| 409 |
-
"Attempting to acquire",
|
| 410 |
-
"acquired on",
|
| 411 |
-
"header parsing failed",
|
| 412 |
-
"timescale not set",
|
| 413 |
-
"misdetection possible",
|
| 414 |
-
"srt was added",
|
| 415 |
-
"doesn't have any audio to transcribe",
|
| 416 |
-
"Calling on_"
|
| 417 |
-
]
|
| 418 |
-
# Return False if any of the patterns are found, True otherwise
|
| 419 |
-
return not any(pattern in record.getMessage() for pattern in patterns)
|
| 420 |
-
|
| 421 |
-
# Configure logging
|
| 422 |
-
if debug:
|
| 423 |
-
level = logging.DEBUG
|
| 424 |
-
else:
|
| 425 |
-
level = logging.INFO
|
| 426 |
-
|
| 427 |
-
logging.basicConfig(
|
| 428 |
-
stream=sys.stderr,
|
| 429 |
-
level=level,
|
| 430 |
-
format="%(asctime)s %(levelname)s: %(message)s",
|
| 431 |
-
datefmt="%Y-%m-%d %H:%M:%S" # This removes the ,123 part
|
| 432 |
-
)
|
| 433 |
-
|
| 434 |
-
# Get the root logger
|
| 435 |
-
logger = logging.getLogger()
|
| 436 |
-
logger.setLevel(level) # Set the logger level
|
| 437 |
-
|
| 438 |
-
for handler in logger.handlers:
|
| 439 |
-
handler.addFilter(MultiplePatternsFilter())
|
| 440 |
-
|
| 441 |
-
logging.getLogger("multipart").setLevel(logging.WARNING)
|
| 442 |
-
logging.getLogger("urllib3").setLevel(logging.WARNING)
|
| 443 |
-
logging.getLogger("watchfiles").setLevel(logging.WARNING)
|
| 444 |
-
logging.getLogger("asyncio").setLevel(logging.WARNING)
|
| 445 |
-
logging.getLogger("httpcore").setLevel(logging.WARNING)
|
| 446 |
-
logging.getLogger("httpx").setLevel(logging.WARNING)
|
| 447 |
-
logging.getLogger("huggingface_hub").setLevel(logging.WARNING)
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
class ProgressHandler:
|
| 451 |
-
def __init__(self, filename):
|
| 452 |
-
self.filename = filename
|
| 453 |
-
self.start_time = time.time()
|
| 454 |
-
self.last_print_time = 0
|
| 455 |
-
self.interval = 5
|
| 456 |
-
|
| 457 |
-
def __call__(self, seek, total):
|
| 458 |
-
if docker_status == 'Docker' or debug:
|
| 459 |
-
current_time = time.time()
|
| 460 |
-
if self.last_print_time == 0 or (current_time - self.last_print_time) >= self.interval:
|
| 461 |
-
self.last_print_time = current_time
|
| 462 |
-
|
| 463 |
-
# 1. Math for Metrics
|
| 464 |
-
pct = int((seek / total) * 100) if total > 0 else 0
|
| 465 |
-
elapsed = current_time - self.start_time
|
| 466 |
-
speed = seek / elapsed if elapsed > 0 else 0
|
| 467 |
-
eta = (total - seek) / speed if speed > 0 else 0
|
| 468 |
-
|
| 469 |
-
# 2. Precise Time Formatting (removes milliseconds)
|
| 470 |
-
def fmt_t(seconds):
|
| 471 |
-
m, s = divmod(int(seconds), 60)
|
| 472 |
-
h, m = divmod(m, 60)
|
| 473 |
-
if h > 0:
|
| 474 |
-
return f"{h}:{m:02d}:{s:02d}"
|
| 475 |
-
return f"{m:02d}:{s:02d}"
|
| 476 |
-
|
| 477 |
-
# 3. Get Queue Stats
|
| 478 |
-
proc = len(task_queue.get_processing_tasks())
|
| 479 |
-
queued = len(task_queue.get_queued_tasks())
|
| 480 |
-
|
| 481 |
-
# 4. Alignment Logic
|
| 482 |
-
# :<40 = Left-align, 40 chars wide (Filename)
|
| 483 |
-
# :>3 = Right-align, 3 chars wide (Percentage)
|
| 484 |
-
# :>5 = Right-align, 5 chars wide (Seconds)
|
| 485 |
-
# :>5 = Right-align, 5 chars wide (Time strings)
|
| 486 |
-
|
| 487 |
-
clean_name = (self.filename[:37] + '..') if len(self.filename) > 40 else self.filename
|
| 488 |
-
|
| 489 |
-
logging.info(
|
| 490 |
-
f"[ {clean_name:<40}] {pct:>3}% | "
|
| 491 |
-
f"{int(seek):>5}/{int(total):<5}s "
|
| 492 |
-
f"[{fmt_t(elapsed):>5}<{fmt_t(eta):>5}, {speed:>5.2f}s/s] | "
|
| 493 |
-
f"Jobs: {proc} processing, {queued} queued"
|
| 494 |
-
)
|
| 495 |
-
|
| 496 |
-
TIME_OFFSET = 5
|
| 497 |
-
|
| 498 |
-
def appendLine(result):
|
| 499 |
-
if append:
|
| 500 |
-
lastSegment = result.segments[-1]
|
| 501 |
-
date_time_str = datetime.now().strftime("%d %b %Y - %H:%M:%S")
|
| 502 |
-
appended_text = f"Transcribed by whisperAI with faster-whisper ({whisper_model}) on {date_time_str}"
|
| 503 |
-
|
| 504 |
-
# Create a new segment with the updated information
|
| 505 |
-
newSegment = Segment(
|
| 506 |
-
start=lastSegment.start + TIME_OFFSET,
|
| 507 |
-
end=lastSegment.end + TIME_OFFSET,
|
| 508 |
-
text=appended_text,
|
| 509 |
-
words=[], # Empty list for words
|
| 510 |
-
id=lastSegment.id + 1
|
| 511 |
-
)
|
| 512 |
-
|
| 513 |
-
# Append the new segment to the result's segments
|
| 514 |
-
result.segments.append(newSegment)
|
| 515 |
-
|
| 516 |
-
@app.get("/plex")
|
| 517 |
-
@app.get("/webhook")
|
| 518 |
-
@app.get("/jellyfin")
|
| 519 |
-
@app.get("/asr")
|
| 520 |
-
@app.get("/emby")
|
| 521 |
-
@app.get("/detect-language")
|
| 522 |
-
@app.get("/tautulli")
|
| 523 |
-
def handle_get_request(request: Request):
|
| 524 |
-
return {"You accessed this request incorrectly via a GET request. See https://github.com/McCloudS/subgen for proper configuration"}
|
| 525 |
-
|
| 526 |
-
@app.get("/")
|
| 527 |
-
def webui():
|
| 528 |
-
return {"The webui for configuration was removed on 1 October 2024, please configure via environment variables or in your Docker settings. "}
|
| 529 |
-
|
| 530 |
-
@app.get("/status")
|
| 531 |
-
def status():
|
| 532 |
-
return {"version": f"Subgen {subgen_version}, stable-ts {stable_whisper.__version__}, faster-whisper {faster_whisper.__version__} ({docker_status})"}
|
| 533 |
-
|
| 534 |
-
@app.post("/tautulli")
|
| 535 |
-
def receive_tautulli_webhook(
|
| 536 |
-
source: Union[str, None] = Header(None),
|
| 537 |
-
event: str = Body(None),
|
| 538 |
-
file: str = Body(None),
|
| 539 |
-
):
|
| 540 |
-
if source == "Tautulli":
|
| 541 |
-
logging.debug(f"Tautulli event detected is: {event}")
|
| 542 |
-
if((event == "added" and procaddedmedia) or (event == "played" and procmediaonplay)):
|
| 543 |
-
fullpath = file
|
| 544 |
-
logging.debug(f"Full file path: {fullpath}")
|
| 545 |
-
|
| 546 |
-
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate)
|
| 547 |
-
else:
|
| 548 |
-
return {
|
| 549 |
-
"message": "This doesn't appear to be a properly configured Tautulli webhook, please review the instructions again!"}
|
| 550 |
-
|
| 551 |
-
return ""
|
| 552 |
-
|
| 553 |
-
@app.post("/plex")
|
| 554 |
-
@app.post("/plex")
|
| 555 |
-
def receive_plex_webhook(
|
| 556 |
-
user_agent: Union[str] = Header(None),
|
| 557 |
-
payload: Union[str] = Form(),
|
| 558 |
-
):
|
| 559 |
-
try:
|
| 560 |
-
plex_json = json.loads(payload)
|
| 561 |
-
#logging.debug(f"Raw response: {payload}")
|
| 562 |
-
|
| 563 |
-
if "PlexMediaServer" not in user_agent:
|
| 564 |
-
return {"message": "This doesn't appear to be a properly configured Plex webhook, please review the instructions again"}
|
| 565 |
-
|
| 566 |
-
event = plex_json["event"]
|
| 567 |
-
logging.debug(f"Plex event detected is: {event}")
|
| 568 |
-
|
| 569 |
-
if (event == "library.new" and procaddedmedia) or (event == "media.play" and procmediaonplay):
|
| 570 |
-
rating_key = plex_json['Metadata']['ratingKey']
|
| 571 |
-
fullpath = get_plex_file_name(rating_key, plexserver, plextoken)
|
| 572 |
-
logging.debug(f"Full file path: {fullpath}")
|
| 573 |
-
|
| 574 |
-
# Queue the current item with its specific ID for refreshing
|
| 575 |
-
gen_subtitles_queue(
|
| 576 |
-
path_mapping(fullpath),
|
| 577 |
-
transcribe_or_translate,
|
| 578 |
-
plex_item_id=rating_key,
|
| 579 |
-
plex_server=plexserver,
|
| 580 |
-
plex_token=plextoken
|
| 581 |
-
)
|
| 582 |
-
|
| 583 |
-
# Note: refresh_plex_metadata is removed here; it is now handled by the worker thread.
|
| 584 |
-
|
| 585 |
-
if plex_queue_next_episode:
|
| 586 |
-
next_key = get_next_plex_episode(plex_json['Metadata']['ratingKey'], stay_in_season=False)
|
| 587 |
-
if next_key:
|
| 588 |
-
next_file = get_plex_file_name(next_key, plexserver, plextoken)
|
| 589 |
-
gen_subtitles_queue(
|
| 590 |
-
path_mapping(next_file),
|
| 591 |
-
transcribe_or_translate,
|
| 592 |
-
plex_item_id=next_key, # Pass the NEXT ID so it refreshes when done
|
| 593 |
-
plex_server=plexserver,
|
| 594 |
-
plex_token=plextoken
|
| 595 |
-
)
|
| 596 |
-
|
| 597 |
-
if plex_queue_series or plex_queue_season:
|
| 598 |
-
current_rating_key = plex_json['Metadata']['ratingKey']
|
| 599 |
-
stay_in_season = plex_queue_season # Determine if we're staying in the season or not
|
| 600 |
-
|
| 601 |
-
while current_rating_key is not None:
|
| 602 |
-
try:
|
| 603 |
-
# Queue the current episode
|
| 604 |
-
file_path = path_mapping(get_plex_file_name(current_rating_key, plexserver, plextoken))
|
| 605 |
-
|
| 606 |
-
gen_subtitles_queue(
|
| 607 |
-
file_path,
|
| 608 |
-
transcribe_or_translate,
|
| 609 |
-
plex_item_id=current_rating_key, # Pass the specific loop ID for refreshing
|
| 610 |
-
plex_server=plexserver,
|
| 611 |
-
plex_token=plextoken
|
| 612 |
-
)
|
| 613 |
-
|
| 614 |
-
logging.debug(f"Queued episode with ratingKey {current_rating_key}")
|
| 615 |
-
|
| 616 |
-
# Get the next episode
|
| 617 |
-
next_episode_rating_key = get_next_plex_episode(current_rating_key, stay_in_season=stay_in_season)
|
| 618 |
-
if next_episode_rating_key is None:
|
| 619 |
-
break # Exit the loop if no next episode
|
| 620 |
-
current_rating_key = next_episode_rating_key
|
| 621 |
-
|
| 622 |
-
except Exception as e:
|
| 623 |
-
logging.error(f"Error processing episode with ratingKey {current_rating_key} or reached end of series: {e}")
|
| 624 |
-
break # Stop processing on error
|
| 625 |
-
|
| 626 |
-
logging.info("All episodes in the series (or season) have been queued.")
|
| 627 |
-
|
| 628 |
-
except Exception as e:
|
| 629 |
-
logging.error(f"Failed to process Plex webhook: {e}")
|
| 630 |
-
|
| 631 |
-
return ""
|
| 632 |
-
|
| 633 |
-
@app.post("/jellyfin")
|
| 634 |
-
def receive_jellyfin_webhook(
|
| 635 |
-
user_agent: str = Header(None),
|
| 636 |
-
NotificationType: str = Body(None),
|
| 637 |
-
file: str = Body(None),
|
| 638 |
-
ItemId: str = Body(None),
|
| 639 |
-
):
|
| 640 |
-
if "Jellyfin-Server" in user_agent:
|
| 641 |
-
logging.debug(f"Jellyfin event detected is: {NotificationType}")
|
| 642 |
-
logging.debug(f"itemid is: {ItemId}")
|
| 643 |
-
|
| 644 |
-
if (NotificationType == "ItemAdded" and procaddedmedia) or (NotificationType == "PlaybackStart" and procmediaonplay):
|
| 645 |
-
fullpath = get_jellyfin_file_name(ItemId, jellyfinserver, jellyfintoken)
|
| 646 |
-
logging.debug(f"Full file path: {fullpath}")
|
| 647 |
-
|
| 648 |
-
# Queue item with Jellyfin metadata ID for delayed refresh
|
| 649 |
-
gen_subtitles_queue(
|
| 650 |
-
path_mapping(fullpath),
|
| 651 |
-
transcribe_or_translate,
|
| 652 |
-
jellyfin_item_id=ItemId,
|
| 653 |
-
jellyfin_server=jellyfinserver,
|
| 654 |
-
jellyfin_token=jellyfintoken
|
| 655 |
-
)
|
| 656 |
-
|
| 657 |
-
# Note: refresh_jellyfin_metadata removed here; handled by worker.
|
| 658 |
-
else:
|
| 659 |
-
return {
|
| 660 |
-
"message": "This doesn't appear to be a properly configured Jellyfin webhook, please review the instructions again!"}
|
| 661 |
-
|
| 662 |
-
return ""
|
| 663 |
-
|
| 664 |
-
@app.post("/emby")
|
| 665 |
-
def receive_emby_webhook(
|
| 666 |
-
user_agent: Union[str, None] = Header(None),
|
| 667 |
-
data: Union[str, None] = Form(None),
|
| 668 |
-
):
|
| 669 |
-
#logging.debug("Raw response: %s", data)
|
| 670 |
-
|
| 671 |
-
if not data:
|
| 672 |
-
return ""
|
| 673 |
-
|
| 674 |
-
data_dict = json.loads(data)
|
| 675 |
-
event = data_dict['Event']
|
| 676 |
-
logging.debug("Emby event detected is: " + event)
|
| 677 |
-
|
| 678 |
-
# Check if it's a notification test event
|
| 679 |
-
if event == "system.notificationtest":
|
| 680 |
-
logging.info("Emby test message received!")
|
| 681 |
-
return {"message": "Notification test received successfully!"}
|
| 682 |
-
|
| 683 |
-
if (event == "library.new" and procaddedmedia) or (event == "playback.start" and procmediaonplay):
|
| 684 |
-
fullpath = data_dict['Item']['Path']
|
| 685 |
-
logging.debug(f"Full file path: {fullpath}")
|
| 686 |
-
gen_subtitles_queue(path_mapping(fullpath), transcribe_or_translate)
|
| 687 |
-
|
| 688 |
-
return ""
|
| 689 |
-
|
| 690 |
-
@app.post("/batch")
|
| 691 |
-
def batch(
|
| 692 |
-
directory: str = Query(...),
|
| 693 |
-
forceLanguage: Union[str, None] = Query(default=None)
|
| 694 |
-
):
|
| 695 |
-
transcribe_existing(directory, LanguageCode.from_string(forceLanguage))
|
| 696 |
-
|
| 697 |
-
# ============================================================================
|
| 698 |
-
# REFACTORED /ASR ENDPOINT WITH HASH-BASED DEDUPLICATION AND BLOCKING
|
| 699 |
-
# ============================================================================
|
| 700 |
-
|
| 701 |
-
@app.post("/asr")
|
| 702 |
-
async def asr(
|
| 703 |
-
task: Union[str, None] = Query(default="transcribe", enum=["transcribe", "translate"]),
|
| 704 |
-
language: Union[str, None] = Query(default=None),
|
| 705 |
-
video_file: Union[str, None] = Query(default=None),
|
| 706 |
-
initial_prompt: Union[str, None] = Query(default=None),
|
| 707 |
-
audio_file: UploadFile = File(...),
|
| 708 |
-
encode: bool = Query(default=True, description="Encode audio first through ffmpeg"),
|
| 709 |
-
output: Union[str, None] = Query(default="srt", enum=["txt", "vtt", "srt", "tsv", "json"]),
|
| 710 |
-
word_timestamps: bool = Query(default=False, description="Word-level timestamps"),
|
| 711 |
-
):
|
| 712 |
-
"""
|
| 713 |
-
ASR endpoint that uses audio content hash for deduplication.
|
| 714 |
-
BLOCKS until processing is complete, then returns the result.
|
| 715 |
-
|
| 716 |
-
If identical audio + task + language is already being processed,
|
| 717 |
-
waits for that task to complete and returns the same result.
|
| 718 |
-
"""
|
| 719 |
-
task_id = None
|
| 720 |
-
|
| 721 |
-
try:
|
| 722 |
-
logging.info(
|
| 723 |
-
f"ASR {task.capitalize()} received for file '{video_file}'"
|
| 724 |
-
if video_file
|
| 725 |
-
else f"ASR {task.capitalize()} received"
|
| 726 |
-
)
|
| 727 |
-
|
| 728 |
-
# Read audio file content into memory
|
| 729 |
-
file_content = await audio_file.read()
|
| 730 |
-
|
| 731 |
-
if not file_content:
|
| 732 |
-
await audio_file.close()
|
| 733 |
-
return {
|
| 734 |
-
"status": "error",
|
| 735 |
-
"message": "Audio file is empty"
|
| 736 |
-
}
|
| 737 |
-
|
| 738 |
-
# Generate deterministic hash from audio (and optionally task/language)
|
| 739 |
-
audio_hash = generate_audio_hash(file_content, task, language)
|
| 740 |
-
task_id = f"asr-{audio_hash}"
|
| 741 |
-
|
| 742 |
-
logging.debug(f"Generated audio hash: {audio_hash} for ASR request")
|
| 743 |
-
|
| 744 |
-
# Handle forced language
|
| 745 |
-
final_language = language
|
| 746 |
-
if force_detected_language_to:
|
| 747 |
-
final_language = force_detected_language_to.to_iso_639_1()
|
| 748 |
-
logging.info(f"Forcing detected language to {force_detected_language_to}")
|
| 749 |
-
|
| 750 |
-
# Create result container for this task
|
| 751 |
-
with task_results_lock:
|
| 752 |
-
if task_id not in task_results:
|
| 753 |
-
task_results[task_id] = TaskResult()
|
| 754 |
-
task_result = task_results[task_id]
|
| 755 |
-
|
| 756 |
-
# Queue the ASR task
|
| 757 |
-
asr_task_data = {
|
| 758 |
-
'path': task_id, # DeduplicatedQueue uses this for dedup
|
| 759 |
-
'type': 'asr',
|
| 760 |
-
'task': task,
|
| 761 |
-
'language': final_language,
|
| 762 |
-
'video_file': video_file,
|
| 763 |
-
'initial_prompt': initial_prompt,
|
| 764 |
-
'audio_content': file_content,
|
| 765 |
-
'encode': encode,
|
| 766 |
-
'output': output,
|
| 767 |
-
'word_timestamps': word_timestamps,
|
| 768 |
-
'result_container': task_result,
|
| 769 |
-
}
|
| 770 |
-
|
| 771 |
-
# Try to queue (returns False if already queued/processing)
|
| 772 |
-
if task_queue.put(asr_task_data):
|
| 773 |
-
logging.info(f"ASR task {task_id} queued")
|
| 774 |
-
else:
|
| 775 |
-
logging.info(f"ASR task {task_id} already queued/processing - waiting for result")
|
| 776 |
-
|
| 777 |
-
# BLOCK HERE until worker completes (respects concurrent_transcriptions)
|
| 778 |
-
timeout = 3600 # 1 hour
|
| 779 |
-
if task_result.wait(timeout=timeout):
|
| 780 |
-
if task_result.error:
|
| 781 |
-
logging.error(f"ASR task {task_id} failed: {task_result.error}")
|
| 782 |
-
return {
|
| 783 |
-
"status": "error",
|
| 784 |
-
"task_id": task_id,
|
| 785 |
-
"message": f"ASR processing failed: {task_result.error}"
|
| 786 |
-
}
|
| 787 |
-
else:
|
| 788 |
-
logging.info(f"ASR task {task_id} completed")
|
| 789 |
-
return StreamingResponse(
|
| 790 |
-
iter(task_result.result),
|
| 791 |
-
media_type="text/plain",
|
| 792 |
-
headers={'Source': f'{task.capitalize()}d using stable-ts from Subgen!'}
|
| 793 |
-
)
|
| 794 |
-
else:
|
| 795 |
-
logging.error(f"ASR task {task_id} timed out")
|
| 796 |
-
return {
|
| 797 |
-
"status": "timeout",
|
| 798 |
-
"task_id": task_id,
|
| 799 |
-
"message": f"ASR processing timed out after {timeout} seconds"
|
| 800 |
-
}
|
| 801 |
-
|
| 802 |
-
except Exception as e:
|
| 803 |
-
logging.error(f"Error in ASR endpoint: {e}", exc_info=True)
|
| 804 |
-
return {"status": "error", "message": f"Error: {str(e)}"}
|
| 805 |
-
finally:
|
| 806 |
-
await audio_file.close()
|
| 807 |
-
|
| 808 |
-
# ============================================================================
|
| 809 |
-
# ASR WORKER FUNCTION
|
| 810 |
-
# ============================================================================
|
| 811 |
-
|
| 812 |
-
def asr_task_worker(task_data: dict) -> None:
|
| 813 |
-
"""
|
| 814 |
-
Worker function that processes ASR tasks from the queue.
|
| 815 |
-
Called by transcription_worker when task type is 'asr'.
|
| 816 |
-
"""
|
| 817 |
-
result = None
|
| 818 |
-
task_id = task_data.get('path', 'unknown')
|
| 819 |
-
result_container = task_data.get('result_container')
|
| 820 |
-
|
| 821 |
-
try:
|
| 822 |
-
task = task_data['task']
|
| 823 |
-
language = task_data['language']
|
| 824 |
-
video_file = task_data.get('video_file')
|
| 825 |
-
initial_prompt = task_data.get('initial_prompt')
|
| 826 |
-
file_content = task_data['audio_content']
|
| 827 |
-
encode = task_data['encode']
|
| 828 |
-
|
| 829 |
-
start_model()
|
| 830 |
-
|
| 831 |
-
args = {}
|
| 832 |
-
display_name = os.path.basename(video_file) if video_file else task_id
|
| 833 |
-
args['progress_callback'] = ProgressHandler(display_name)
|
| 834 |
-
|
| 835 |
-
# Handle audio encoding
|
| 836 |
-
if encode:
|
| 837 |
-
args['audio'] = file_content
|
| 838 |
-
else:
|
| 839 |
-
args['audio'] = np.frombuffer(file_content, np.int16).flatten().astype(np.float32) / 32768.0
|
| 840 |
-
args['input_sr'] = 16000
|
| 841 |
-
|
| 842 |
-
if custom_regroup and custom_regroup.lower() != 'default':
|
| 843 |
-
args['regroup'] = custom_regroup
|
| 844 |
-
|
| 845 |
-
args.update(kwargs)
|
| 846 |
-
|
| 847 |
-
# Perform transcription
|
| 848 |
-
result = model.transcribe(task=task, language=language, **args, verbose=None)
|
| 849 |
-
appendLine(result)
|
| 850 |
-
|
| 851 |
-
# Set result for blocking endpoint
|
| 852 |
-
if result_container:
|
| 853 |
-
result_container.set_result(result.to_srt_vtt(filepath=None, word_level=word_level_highlight))
|
| 854 |
-
|
| 855 |
-
except Exception as e:
|
| 856 |
-
logging.error(f"Error processing ASR (ID: {task_id}): {e}", exc_info=True)
|
| 857 |
-
if result_container:
|
| 858 |
-
result_container.set_error(str(e))
|
| 859 |
-
|
| 860 |
-
finally:
|
| 861 |
-
delete_model()
|
| 862 |
-
|
| 863 |
-
async def get_audio_chunk(audio_file, offset=detect_language_offset, length=detect_language_length, sample_rate=16000, audio_format=np.int16):
|
| 864 |
-
"""
|
| 865 |
-
Extract a chunk of audio from a file, starting at the given offset and of the given length.
|
| 866 |
-
|
| 867 |
-
:param audio_file: The audio file (UploadFile or file-like object).
|
| 868 |
-
:param offset: The offset in seconds to start the extraction.
|
| 869 |
-
:param length: The length in seconds for the chunk to be extracted.
|
| 870 |
-
:param sample_rate: The sample rate of the audio (default 16000).
|
| 871 |
-
:param audio_format: The audio format to interpret (default int16, 2 bytes per sample).
|
| 872 |
-
|
| 873 |
-
:return: A numpy array containing the extracted audio chunk.
|
| 874 |
-
"""
|
| 875 |
-
|
| 876 |
-
# Number of bytes per sample (for int16, 2 bytes per sample)
|
| 877 |
-
bytes_per_sample = np.dtype(audio_format).itemsize
|
| 878 |
-
|
| 879 |
-
# Calculate the start byte based on offset and sample rate
|
| 880 |
-
start_byte = offset * sample_rate * bytes_per_sample
|
| 881 |
-
|
| 882 |
-
# Calculate the length in bytes based on the length in seconds
|
| 883 |
-
length_in_bytes = length * sample_rate * bytes_per_sample
|
| 884 |
-
|
| 885 |
-
# Seek to the start position (this assumes the audio_file is a file-like object)
|
| 886 |
-
await audio_file.seek(start_byte)
|
| 887 |
-
|
| 888 |
-
# Read the required chunk of audio (length_in_bytes)
|
| 889 |
-
chunk = await audio_file.read(length_in_bytes)
|
| 890 |
-
|
| 891 |
-
# Convert the chunk into a numpy array (normalized to float32)
|
| 892 |
-
audio_data = np.frombuffer(chunk, dtype=audio_format).flatten().astype(np.float32) / 32768.0
|
| 893 |
-
|
| 894 |
-
return audio_data
|
| 895 |
-
|
| 896 |
-
# ============================================================================
|
| 897 |
-
# REFACTORED /DETECT-LANGUAGE ENDPOINT WITH HASH-BASED DEDUPLICATION AND BLOCKING
|
| 898 |
-
# ============================================================================
|
| 899 |
-
|
| 900 |
-
@app.post("/detect-language")
|
| 901 |
-
async def detect_language(
|
| 902 |
-
audio_file: UploadFile = File(...),
|
| 903 |
-
encode: bool = Query(default=True),
|
| 904 |
-
video_file: Union[str, None] = Query(default=None),
|
| 905 |
-
detect_lang_length: int = Query(default=detect_language_length),
|
| 906 |
-
detect_lang_offset: int = Query(default=detect_language_offset)
|
| 907 |
-
):
|
| 908 |
-
if force_detected_language_to:
|
| 909 |
-
await audio_file.close()
|
| 910 |
-
return {"detected_language": force_detected_language_to.to_name(), "language_code": force_detected_language_to.to_iso_639_1()}
|
| 911 |
-
|
| 912 |
-
try:
|
| 913 |
-
file_content = await audio_file.read()
|
| 914 |
-
if not file_content:
|
| 915 |
-
return {"detected_language": "Unknown", "language_code": "und", "status": "error"}
|
| 916 |
-
|
| 917 |
-
logging.info(f"Immediate language detection (Queue Bypass)" + (f" for {video_file}" if video_file else ""))
|
| 918 |
-
|
| 919 |
-
# --- RUN IMMEDIATELY ---
|
| 920 |
-
start_model()
|
| 921 |
-
|
| 922 |
-
if encode:
|
| 923 |
-
audio_bytes = extract_audio_segment_from_content(await audio_file.read(), detect_lang_offset, detect_lang_length)
|
| 924 |
-
audio_data = np.frombuffer(audio_bytes, np.int16).flatten().astype(np.float32) / 32768.0
|
| 925 |
-
else:
|
| 926 |
-
audio_data = await get_audio_chunk(audio_file, detect_lang_offset, detect_lang_length)
|
| 927 |
-
|
| 928 |
-
result = model.transcribe(audio_data, input_sr=16000, verbose=None)
|
| 929 |
-
detected = LanguageCode.from_name(result.language)
|
| 930 |
-
|
| 931 |
-
logging.info(f"Detect Language Result: {detected.to_name()} ({detected.to_iso_639_1()})")
|
| 932 |
-
|
| 933 |
-
return {
|
| 934 |
-
"detected_language": detected.to_name(),
|
| 935 |
-
"language_code": detected.to_iso_639_1()
|
| 936 |
-
}
|
| 937 |
-
|
| 938 |
-
except Exception as e:
|
| 939 |
-
logging.error(f"Error in API detect-language: {e}", exc_info=True)
|
| 940 |
-
return {"detected_language": "Unknown", "language_code": "und", "status": "error"}
|
| 941 |
-
finally:
|
| 942 |
-
await audio_file.close()
|
| 943 |
-
delete_model() # Schedules VRAM cleanup if system is idle
|
| 944 |
-
|
| 945 |
-
# ============================================================================
|
| 946 |
-
# DETECT LANGUAGE WORKER FOR UPLOADED AUDIO
|
| 947 |
-
# ============================================================================
|
| 948 |
-
|
| 949 |
-
def detect_language_from_upload(task_data: dict) -> None:
|
| 950 |
-
"""
|
| 951 |
-
Worker function that processes detect-language tasks from uploaded audio.
|
| 952 |
-
Sets the result in the result_container when complete.
|
| 953 |
-
"""
|
| 954 |
-
detected_language = LanguageCode.NONE
|
| 955 |
-
task_id = task_data.get('path', 'unknown')
|
| 956 |
-
result_container = task_data.get('result_container')
|
| 957 |
-
|
| 958 |
-
try:
|
| 959 |
-
video_file = task_data.get('video_file')
|
| 960 |
-
file_content = task_data['audio_content']
|
| 961 |
-
encode = task_data['encode']
|
| 962 |
-
detect_lang_length = task_data['detect_lang_length']
|
| 963 |
-
detect_lang_offset = task_data['detect_lang_offset']
|
| 964 |
-
|
| 965 |
-
logging.info(
|
| 966 |
-
f"Detecting language for '{video_file}' ({detect_lang_length}s starting at {detect_lang_offset}s) - ID: {task_id}"
|
| 967 |
-
if video_file
|
| 968 |
-
else f"Detecting language ({detect_lang_length}s starting at {detect_lang_offset}s) - ID: {task_id}"
|
| 969 |
-
)
|
| 970 |
-
|
| 971 |
-
start_model()
|
| 972 |
-
|
| 973 |
-
args = {}
|
| 974 |
-
args['progress_callback'] = progress
|
| 975 |
-
|
| 976 |
-
# Handle audio extraction
|
| 977 |
-
if encode:
|
| 978 |
-
audio_bytes = extract_audio_segment_from_content(
|
| 979 |
-
file_content,
|
| 980 |
-
detect_lang_offset,
|
| 981 |
-
detect_lang_length
|
| 982 |
-
)
|
| 983 |
-
args['audio'] = audio_bytes
|
| 984 |
-
args['input_sr'] = 16000
|
| 985 |
-
else:
|
| 986 |
-
args['audio'] = np.frombuffer(file_content, np.int16).flatten().astype(np.float32) / 32768.0
|
| 987 |
-
args['input_sr'] = 16000
|
| 988 |
-
|
| 989 |
-
args.update(kwargs)
|
| 990 |
-
|
| 991 |
-
detected_language = LanguageCode.from_name(model.transcribe(**args).language)
|
| 992 |
-
language_code = detected_language.to_iso_639_1()
|
| 993 |
-
|
| 994 |
-
logging.info(f"Detected language: {detected_language.to_name()} ({language_code}) - ID: {task_id}")
|
| 995 |
-
|
| 996 |
-
# Set the result for the blocking endpoint
|
| 997 |
-
if result_container:
|
| 998 |
-
result_container.set_result({
|
| 999 |
-
"detected_language": detected_language.to_name(),
|
| 1000 |
-
"language_code": language_code
|
| 1001 |
-
})
|
| 1002 |
-
|
| 1003 |
-
except Exception as e:
|
| 1004 |
-
logging.error(
|
| 1005 |
-
f"Error detecting language (ID: {task_id}) for '{task_data.get('video_file')}': {e}"
|
| 1006 |
-
if task_data.get('video_file')
|
| 1007 |
-
else f"Error detecting language (ID: {task_id}): {e}",
|
| 1008 |
-
exc_info=True
|
| 1009 |
-
)
|
| 1010 |
-
if result_container:
|
| 1011 |
-
result_container.set_error(str(e))
|
| 1012 |
-
|
| 1013 |
-
finally:
|
| 1014 |
-
delete_model()
|
| 1015 |
-
|
| 1016 |
-
# ============================================================================
|
| 1017 |
-
# HELPER: Extract audio segment from in-memory content
|
| 1018 |
-
# ============================================================================
|
| 1019 |
-
|
| 1020 |
-
def extract_audio_segment_from_content(audio_content: bytes, start_time: int, duration: int) -> bytes:
|
| 1021 |
-
"""
|
| 1022 |
-
Extract a segment of audio from in-memory content using FFmpeg.
|
| 1023 |
-
|
| 1024 |
-
Args:
|
| 1025 |
-
audio_content: Raw audio bytes
|
| 1026 |
-
start_time: Start time in seconds
|
| 1027 |
-
duration: Duration in seconds
|
| 1028 |
-
|
| 1029 |
-
Returns:
|
| 1030 |
-
Audio bytes of the extracted segment
|
| 1031 |
-
"""
|
| 1032 |
-
try:
|
| 1033 |
-
logging.info(f"Extracting audio segment: start_time={start_time}s, duration={duration}s")
|
| 1034 |
-
|
| 1035 |
-
out, _ = (
|
| 1036 |
-
ffmpeg
|
| 1037 |
-
.input('pipe:0', ss=start_time, t=duration)
|
| 1038 |
-
.output('pipe:1', format='wav', acodec='pcm_s16le', ar=16000)
|
| 1039 |
-
.run(input=audio_content, capture_stdout=True, capture_stderr=True)
|
| 1040 |
-
)
|
| 1041 |
-
|
| 1042 |
-
if not out:
|
| 1043 |
-
raise ValueError("FFmpeg output is empty")
|
| 1044 |
-
|
| 1045 |
-
return out
|
| 1046 |
-
|
| 1047 |
-
except ffmpeg.Error as e:
|
| 1048 |
-
logging.error(f"FFmpeg error: {e.stderr.decode()}")
|
| 1049 |
-
return audio_content # Fallback to original if extraction fails
|
| 1050 |
-
except Exception as e:
|
| 1051 |
-
logging.error(f"Error extracting audio segment: {str(e)}")
|
| 1052 |
-
return audio_content # Fallback to original
|
| 1053 |
-
|
| 1054 |
-
def detect_language_task(path, original_task_data=None):
|
| 1055 |
-
"""
|
| 1056 |
-
Worker function that detects language for a local file.
|
| 1057 |
-
Then queues the actual transcription with the detected language.
|
| 1058 |
-
"""
|
| 1059 |
-
detected_language = LanguageCode.NONE
|
| 1060 |
-
|
| 1061 |
-
try:
|
| 1062 |
-
logging.info(
|
| 1063 |
-
f"Detecting language of file: {path} "
|
| 1064 |
-
f"({detect_language_length}s starting at {detect_language_offset}s)"
|
| 1065 |
-
)
|
| 1066 |
-
|
| 1067 |
-
start_model()
|
| 1068 |
-
|
| 1069 |
-
audio_segment = extract_audio_segment_to_memory(
|
| 1070 |
-
path,
|
| 1071 |
-
detect_language_offset,
|
| 1072 |
-
int(detect_language_length)
|
| 1073 |
-
).read()
|
| 1074 |
-
|
| 1075 |
-
detected_language = LanguageCode.from_name(model.transcribe(audio_segment).language)
|
| 1076 |
-
|
| 1077 |
-
logging.info(f"Detected language: {detected_language.to_name()}")
|
| 1078 |
-
|
| 1079 |
-
except Exception as e:
|
| 1080 |
-
logging.error(f"Error detecting language for file: {e}", exc_info=True)
|
| 1081 |
-
|
| 1082 |
-
finally:
|
| 1083 |
-
delete_model()
|
| 1084 |
-
|
| 1085 |
-
# Queue transcription with detected language
|
| 1086 |
-
task_data = {
|
| 1087 |
-
'path': path,
|
| 1088 |
-
'type': 'transcribe',
|
| 1089 |
-
'transcribe_or_translate': transcribe_or_translate,
|
| 1090 |
-
'force_language': detected_language
|
| 1091 |
-
}
|
| 1092 |
-
|
| 1093 |
-
# Carry over metadata (Plex IDs, etc.) from the original task
|
| 1094 |
-
if original_task_data:
|
| 1095 |
-
for key, value in original_task_data.items():
|
| 1096 |
-
if key not in task_data:
|
| 1097 |
-
task_data[key] = value
|
| 1098 |
-
|
| 1099 |
-
if task_queue.put(task_data):
|
| 1100 |
-
logging.debug(f"Queued transcription for detected language: {path}")
|
| 1101 |
-
else:
|
| 1102 |
-
logging.debug(f"Transcription already queued/processing for: {path}")
|
| 1103 |
-
|
| 1104 |
-
def extract_audio_segment_to_memory(input_file, start_time, duration):
|
| 1105 |
-
"""
|
| 1106 |
-
Extract a segment of audio from input_file, starting at start_time for duration seconds.
|
| 1107 |
-
|
| 1108 |
-
:param input_file: UploadFile object or path to the input audio file
|
| 1109 |
-
:param start_time: Start time in seconds (e.g., 60 for 1 minute)
|
| 1110 |
-
:param duration: Duration in seconds (e.g., 30 for 30 seconds)
|
| 1111 |
-
:return: BytesIO object containing the audio segment
|
| 1112 |
-
"""
|
| 1113 |
-
try:
|
| 1114 |
-
if hasattr(input_file, 'file') and hasattr(input_file.file, 'read'): # Handling UploadFile
|
| 1115 |
-
input_file.file.seek(0) # Ensure the file pointer is at the beginning
|
| 1116 |
-
input_stream = 'pipe:0'
|
| 1117 |
-
input_kwargs = {'input': input_file.file.read()}
|
| 1118 |
-
elif isinstance(input_file, str): # Handling local file path
|
| 1119 |
-
input_stream = input_file
|
| 1120 |
-
input_kwargs = {}
|
| 1121 |
-
else:
|
| 1122 |
-
raise ValueError("Invalid input: input_file must be a file path or an UploadFile object.")
|
| 1123 |
-
|
| 1124 |
-
logging.info(f"Extracting audio from: {input_stream}, start_time: {start_time}, duration: {duration}")
|
| 1125 |
-
|
| 1126 |
-
# Run FFmpeg to extract the desired segment
|
| 1127 |
-
out, _ = (
|
| 1128 |
-
ffmpeg
|
| 1129 |
-
.input(input_stream, ss=start_time, t=duration) # Set start time and duration
|
| 1130 |
-
.output('pipe:1', format='wav', acodec='pcm_s16le', ar=16000) # Output to pipe as WAV
|
| 1131 |
-
.run(capture_stdout=True, capture_stderr=True, **input_kwargs)
|
| 1132 |
-
)
|
| 1133 |
-
|
| 1134 |
-
# Check if the output is empty or null
|
| 1135 |
-
if not out:
|
| 1136 |
-
raise ValueError("FFmpeg output is empty, possibly due to invalid input.")
|
| 1137 |
-
|
| 1138 |
-
return io.BytesIO(out) # Convert output to BytesIO for in-memory processing
|
| 1139 |
-
|
| 1140 |
-
except ffmpeg.Error as e:
|
| 1141 |
-
logging.error(f"FFmpeg error: {e.stderr.decode()}")
|
| 1142 |
-
return None
|
| 1143 |
-
except Exception as e:
|
| 1144 |
-
logging.error(f"Error: {str(e)}")
|
| 1145 |
-
return None
|
| 1146 |
-
|
| 1147 |
-
def start_model():
|
| 1148 |
-
global model
|
| 1149 |
-
if model is None:
|
| 1150 |
-
logging.debug("Model was purged, need to re-create")
|
| 1151 |
-
model = stable_whisper.load_faster_whisper(whisper_model, download_root=model_location, device=transcribe_device, cpu_threads=whisper_threads, num_workers=concurrent_transcriptions, compute_type=compute_type)
|
| 1152 |
-
|
| 1153 |
-
def schedule_model_cleanup():
|
| 1154 |
-
"""Schedule model cleanup with a delay to allow concurrent requests."""
|
| 1155 |
-
global model_cleanup_timer, model_cleanup_lock
|
| 1156 |
-
|
| 1157 |
-
with model_cleanup_lock:
|
| 1158 |
-
# Cancel any existing timer
|
| 1159 |
-
if model_cleanup_timer is not None:
|
| 1160 |
-
model_cleanup_timer.cancel()
|
| 1161 |
-
logging.debug("Cancelled previous model cleanup timer")
|
| 1162 |
-
|
| 1163 |
-
# Schedule a new cleanup timer
|
| 1164 |
-
model_cleanup_timer = Timer(model_cleanup_delay, perform_model_cleanup)
|
| 1165 |
-
model_cleanup_timer.daemon = True
|
| 1166 |
-
model_cleanup_timer.start()
|
| 1167 |
-
logging.debug(f"Model cleanup scheduled in {model_cleanup_delay} seconds")
|
| 1168 |
-
|
| 1169 |
-
def perform_model_cleanup():
|
| 1170 |
-
"""Actually perform the model cleanup."""
|
| 1171 |
-
global model, model_cleanup_timer, model_cleanup_lock
|
| 1172 |
-
|
| 1173 |
-
with model_cleanup_lock:
|
| 1174 |
-
logging.debug("Executing scheduled model cleanup")
|
| 1175 |
-
|
| 1176 |
-
if clear_vram_on_complete and task_queue.is_idle():
|
| 1177 |
-
logging.debug("Queue idle; clearing model from memory.")
|
| 1178 |
-
if model:
|
| 1179 |
-
try:
|
| 1180 |
-
model.model.unload_model()
|
| 1181 |
-
del model
|
| 1182 |
-
model = None
|
| 1183 |
-
logging.info("Model unloaded from memory")
|
| 1184 |
-
except Exception as e:
|
| 1185 |
-
logging.error(f"Error unloading model: {e}")
|
| 1186 |
-
|
| 1187 |
-
if transcribe_device.lower() == 'cuda' and torch.cuda.is_available():
|
| 1188 |
-
try:
|
| 1189 |
-
torch.cuda.empty_cache()
|
| 1190 |
-
logging.debug("CUDA cache cleared.")
|
| 1191 |
-
except Exception as e:
|
| 1192 |
-
logging.error(f"Error clearing CUDA cache: {e}")
|
| 1193 |
-
else:
|
| 1194 |
-
logging.debug("Queue not idle or clear_vram disabled; skipping model cleanup")
|
| 1195 |
-
|
| 1196 |
-
if os.name != 'nt': # don't garbage collect on Windows
|
| 1197 |
-
gc.collect()
|
| 1198 |
-
ctypes.CDLL(ctypes.util.find_library('c')).malloc_trim(0)
|
| 1199 |
-
|
| 1200 |
-
model_cleanup_timer = None
|
| 1201 |
-
|
| 1202 |
-
def delete_model():
|
| 1203 |
-
"""
|
| 1204 |
-
Only schedules a cleanup timer if the system is actually idle.
|
| 1205 |
-
This prevents unnecessary timer resets when a large batch is being processed.
|
| 1206 |
-
"""
|
| 1207 |
-
# 1. If we aren't supposed to clear VRAM, don't bother with timers at all.
|
| 1208 |
-
if not clear_vram_on_complete:
|
| 1209 |
-
return
|
| 1210 |
-
|
| 1211 |
-
# 2. Only schedule cleanup if the queue is empty AND no other workers are processing.
|
| 1212 |
-
if task_queue.is_idle():
|
| 1213 |
-
schedule_model_cleanup()
|
| 1214 |
-
else:
|
| 1215 |
-
# If there are 10 items left in the queue, we simply do nothing.
|
| 1216 |
-
# The very last worker to finish the last item will trigger the timer.
|
| 1217 |
-
logging.debug("Tasks still in queue or processing; skipping model cleanup scheduling.")
|
| 1218 |
-
|
| 1219 |
-
def isAudioFileExtension(file_extension):
|
| 1220 |
-
return file_extension.casefold() in AUDIO_EXTENSIONS
|
| 1221 |
-
|
| 1222 |
-
def write_lrc(result, file_path):
|
| 1223 |
-
with open(file_path, "w") as file:
|
| 1224 |
-
for segment in result.segments:
|
| 1225 |
-
minutes, seconds = divmod(int(segment.start), 60)
|
| 1226 |
-
fraction = int((segment.start - int(segment.start)) * 100)
|
| 1227 |
-
# remove embedded newlines in text, since some players ignore text after newlines
|
| 1228 |
-
text = segment.text[:].replace('\n', '')
|
| 1229 |
-
file.write(f"[{minutes:02d}:{seconds:02d}. {fraction:02d}]{text}\n")
|
| 1230 |
-
|
| 1231 |
-
def gen_subtitles(file_path: str, transcription_type: str, force_language: LanguageCode = LanguageCode.NONE) -> None:
|
| 1232 |
-
"""Generates subtitles for a video file.
|
| 1233 |
-
|
| 1234 |
-
Args:
|
| 1235 |
-
file_path: str - The path to the video file.
|
| 1236 |
-
transcription_type: str - The type of transcription or translation to perform.
|
| 1237 |
-
force_language: str - The language to force for transcription or translation. Default is None.
|
| 1238 |
-
"""
|
| 1239 |
-
|
| 1240 |
-
try:
|
| 1241 |
-
start_model()
|
| 1242 |
-
|
| 1243 |
-
# Check if the file is an audio file before trying to extract audio
|
| 1244 |
-
file_name, file_extension = os.path.splitext(file_path)
|
| 1245 |
-
is_audio_file = isAudioFileExtension(file_extension)
|
| 1246 |
-
|
| 1247 |
-
data = file_path
|
| 1248 |
-
# Extract audio from the file if it has multiple audio tracks
|
| 1249 |
-
extracted_audio_file = handle_multiple_audio_tracks(file_path, force_language)
|
| 1250 |
-
if extracted_audio_file:
|
| 1251 |
-
data = extracted_audio_file.read()
|
| 1252 |
-
|
| 1253 |
-
args = {}
|
| 1254 |
-
display_name = os.path.basename(file_path)
|
| 1255 |
-
args['progress_callback'] = ProgressHandler(display_name)
|
| 1256 |
-
|
| 1257 |
-
if custom_regroup and custom_regroup.lower() != 'default':
|
| 1258 |
-
args['regroup'] = custom_regroup
|
| 1259 |
-
|
| 1260 |
-
args.update(kwargs)
|
| 1261 |
-
|
| 1262 |
-
result = model.transcribe(data, language=force_language.to_iso_639_1(), task=transcription_type, verbose=None, **args)
|
| 1263 |
-
|
| 1264 |
-
appendLine(result)
|
| 1265 |
-
|
| 1266 |
-
# If it is an audio file, write the LRC file
|
| 1267 |
-
if is_audio_file and lrc_for_audio_files:
|
| 1268 |
-
write_lrc(result, file_name + '.lrc')
|
| 1269 |
-
else:
|
| 1270 |
-
if not force_language:
|
| 1271 |
-
force_language = LanguageCode.from_string(result.language)
|
| 1272 |
-
result.to_srt_vtt(name_subtitle(file_path, force_language), word_level=word_level_highlight)
|
| 1273 |
-
|
| 1274 |
-
except Exception as e:
|
| 1275 |
-
logging.info(f"Error processing or transcribing {file_path} in {force_language}: {e}")
|
| 1276 |
-
|
| 1277 |
-
finally:
|
| 1278 |
-
delete_model()
|
| 1279 |
-
|
| 1280 |
-
def define_subtitle_language_naming(language: LanguageCode, type):
|
| 1281 |
-
"""
|
| 1282 |
-
Determines the naming format for a subtitle language based on the given type.
|
| 1283 |
-
|
| 1284 |
-
Args:
|
| 1285 |
-
language (LanguageCode): The language code object containing methods to get different formats of the language name.
|
| 1286 |
-
type (str): The type of naming format desired, such as 'ISO_639_1', 'ISO_639_2_T', 'ISO_639_2_B', 'NAME', or 'NATIVE'.
|
| 1287 |
-
|
| 1288 |
-
Returns:
|
| 1289 |
-
str: The language name in the specified format. If an invalid type is provided, it defaults to the language's name.
|
| 1290 |
-
"""
|
| 1291 |
-
if namesublang:
|
| 1292 |
-
return namesublang
|
| 1293 |
-
# If we are translating, then we ALWAYS output an english file.
|
| 1294 |
-
switch_dict = {
|
| 1295 |
-
"ISO_639_1": language.to_iso_639_1,
|
| 1296 |
-
"ISO_639_2_T": language.to_iso_639_2_t,
|
| 1297 |
-
"ISO_639_2_B": language.to_iso_639_2_b,
|
| 1298 |
-
"NAME": language.to_name,
|
| 1299 |
-
"NATIVE": lambda: language.to_name(in_english=False)
|
| 1300 |
-
}
|
| 1301 |
-
if transcribe_or_translate == 'translate':
|
| 1302 |
-
language = LanguageCode.ENGLISH
|
| 1303 |
-
return switch_dict.get(type, language.to_name)()
|
| 1304 |
-
|
| 1305 |
-
def name_subtitle(file_path: str, language: LanguageCode) -> str:
|
| 1306 |
-
"""
|
| 1307 |
-
Name the subtitle file to be written, based on the source file and the language of the subtitle.
|
| 1308 |
-
|
| 1309 |
-
Args:
|
| 1310 |
-
file_path: The path to the source file.
|
| 1311 |
-
language: The language of the subtitle.
|
| 1312 |
-
|
| 1313 |
-
Returns:
|
| 1314 |
-
The name of the subtitle file to be written.
|
| 1315 |
-
"""
|
| 1316 |
-
subgen_part = ".subgen" if show_in_subname_subgen else ""
|
| 1317 |
-
model_part = f".{whisper_model}" if show_in_subname_model else ""
|
| 1318 |
-
lang_part = define_subtitle_language_naming(language, subtitle_language_naming_type)
|
| 1319 |
-
|
| 1320 |
-
return f"{os.path.splitext(file_path)[0]}{subgen_part}{model_part}.{lang_part}.srt"
|
| 1321 |
-
|
| 1322 |
-
def handle_multiple_audio_tracks(file_path: str, language: LanguageCode | None = None) -> BytesIO | None:
|
| 1323 |
-
"""
|
| 1324 |
-
Handles the possibility of a media file having multiple audio tracks.
|
| 1325 |
-
|
| 1326 |
-
If the media file has multiple audio tracks, it will extract the audio track of the selected language. Otherwise, it will extract the first audio track.
|
| 1327 |
-
|
| 1328 |
-
Parameters:
|
| 1329 |
-
file_path (str): The path to the media file.
|
| 1330 |
-
language (LanguageCode | None): The language of the audio track to search for. If None, it will extract the first audio track.
|
| 1331 |
-
|
| 1332 |
-
Returns:
|
| 1333 |
-
io.BytesIO | None: The audio or None if no audio track was extracted.
|
| 1334 |
-
"""
|
| 1335 |
-
audio_bytes = None
|
| 1336 |
-
audio_tracks = get_audio_tracks(file_path)
|
| 1337 |
-
|
| 1338 |
-
if len(audio_tracks) > 1:
|
| 1339 |
-
logging.debug(f"Handling multiple audio tracks from {file_path} and planning to extract audio track of language {language}")
|
| 1340 |
-
logging.debug(
|
| 1341 |
-
"Audio tracks:\n"
|
| 1342 |
-
+ "\n".join([f" - {track['index']}: {track['codec']} {track['language']} {('default' if track['default'] else '')}" for track in audio_tracks])
|
| 1343 |
-
)
|
| 1344 |
-
|
| 1345 |
-
if language is not None:
|
| 1346 |
-
audio_track = get_audio_track_by_language(audio_tracks, language)
|
| 1347 |
-
if audio_track is None:
|
| 1348 |
-
audio_track = audio_tracks[0]
|
| 1349 |
-
|
| 1350 |
-
audio_bytes = extract_audio_track_to_memory(file_path, audio_track["index"])
|
| 1351 |
-
if audio_bytes is None:
|
| 1352 |
-
logging.error(f"Failed to extract audio track {audio_track['index']} from {file_path}")
|
| 1353 |
-
return None
|
| 1354 |
-
return audio_bytes
|
| 1355 |
-
|
| 1356 |
-
def extract_audio_track_to_memory(input_video_path, track_index) -> BytesIO | None:
|
| 1357 |
-
"""
|
| 1358 |
-
Extract a specific audio track from a video file to memory using FFmpeg.
|
| 1359 |
-
|
| 1360 |
-
Args:
|
| 1361 |
-
input_video_path (str): The path to the video file.
|
| 1362 |
-
track_index (int): The index of the audio track to extract. If None, skip extraction.
|
| 1363 |
-
|
| 1364 |
-
Returns:
|
| 1365 |
-
io.BytesIO | None: The audio data as a BytesIO object, or None if extraction failed.
|
| 1366 |
-
"""
|
| 1367 |
-
if track_index is None:
|
| 1368 |
-
logging.warning(f"Skipping audio track extraction for {input_video_path} because track index is None")
|
| 1369 |
-
return None
|
| 1370 |
-
|
| 1371 |
-
try:
|
| 1372 |
-
# Use FFmpeg to extract the specific audio track and output to memory
|
| 1373 |
-
out, _ = (
|
| 1374 |
-
ffmpeg.input(input_video_path)
|
| 1375 |
-
.output(
|
| 1376 |
-
"pipe:", # Direct output to a pipe
|
| 1377 |
-
map=f"0:{track_index}", # Select the specific audio track
|
| 1378 |
-
format="wav", # Output format
|
| 1379 |
-
ac=1, # Mono audio (optional)
|
| 1380 |
-
ar=16000, # Sample rate 16 kHz (recommended for speech models)
|
| 1381 |
-
loglevel="quiet"
|
| 1382 |
-
)
|
| 1383 |
-
.run(capture_stdout=True, capture_stderr=True) # Capture output in memory
|
| 1384 |
-
)
|
| 1385 |
-
# Return the audio data as a BytesIO object
|
| 1386 |
-
return BytesIO(out)
|
| 1387 |
-
|
| 1388 |
-
except ffmpeg.Error as e:
|
| 1389 |
-
print("An error occurred:", e.stderr.decode())
|
| 1390 |
-
return None
|
| 1391 |
-
|
| 1392 |
-
def get_audio_track_by_language(audio_tracks, language):
|
| 1393 |
-
"""
|
| 1394 |
-
Returns the first audio track with the given language.
|
| 1395 |
-
|
| 1396 |
-
Args:
|
| 1397 |
-
audio_tracks (list): A list of dictionaries containing information about each audio track.
|
| 1398 |
-
language (str): The language of the audio track to search for.
|
| 1399 |
-
|
| 1400 |
-
Returns:
|
| 1401 |
-
dict: The first audio track with the given language, or None if no match is found.
|
| 1402 |
-
"""
|
| 1403 |
-
for track in audio_tracks:
|
| 1404 |
-
if track['language'] == language:
|
| 1405 |
-
return track
|
| 1406 |
-
return None
|
| 1407 |
-
|
| 1408 |
-
def choose_transcribe_language(file_path, forced_language):
|
| 1409 |
-
"""
|
| 1410 |
-
Determines the language to be used for transcription based on the provided
|
| 1411 |
-
file path and language preferences.
|
| 1412 |
-
|
| 1413 |
-
Args:
|
| 1414 |
-
file_path: The path to the file for which the audio tracks are analyzed.
|
| 1415 |
-
forced_language: The language to force for transcription if specified.
|
| 1416 |
-
|
| 1417 |
-
Returns:
|
| 1418 |
-
The language code to be used for transcription. It prioritizes the
|
| 1419 |
-
`forced_language`, then the environment variable `force_detected_language_to`,
|
| 1420 |
-
then the preferred audio language if available, and finally the default
|
| 1421 |
-
language of the audio tracks. Returns None if no language preference is
|
| 1422 |
-
determined.
|
| 1423 |
-
"""
|
| 1424 |
-
|
| 1425 |
-
#logger.debug(f"choose_transcribe_language({file_path}, {forced_language})")
|
| 1426 |
-
|
| 1427 |
-
if forced_language:
|
| 1428 |
-
logger.debug(f"ENV FORCE_LANGUAGE is set: Forcing language to {forced_language}")
|
| 1429 |
-
return forced_language
|
| 1430 |
-
|
| 1431 |
-
if force_detected_language_to:
|
| 1432 |
-
logger.debug(f"ENV FORCE_DETECTED_LANGUAGE_TO is set: Forcing detected language to {force_detected_language_to}")
|
| 1433 |
-
return force_detected_language_to
|
| 1434 |
-
|
| 1435 |
-
audio_tracks = get_audio_tracks(file_path)
|
| 1436 |
-
|
| 1437 |
-
preferred_track_language = find_language_audio_track(audio_tracks, preferred_audio_languages)
|
| 1438 |
-
|
| 1439 |
-
if preferred_track_language:
|
| 1440 |
-
#logging.debug(f"Preferred language found: {preferred_track_language}")
|
| 1441 |
-
return preferred_track_language
|
| 1442 |
-
|
| 1443 |
-
default_language = find_default_audio_track_language(audio_tracks)
|
| 1444 |
-
if default_language:
|
| 1445 |
-
logger.debug(f"Default language found: {default_language}")
|
| 1446 |
-
return default_language
|
| 1447 |
-
|
| 1448 |
-
return LanguageCode.NONE
|
| 1449 |
-
|
| 1450 |
-
def get_audio_tracks(video_file):
|
| 1451 |
-
"""
|
| 1452 |
-
Extracts information about the audio tracks in a file.
|
| 1453 |
-
|
| 1454 |
-
Returns:
|
| 1455 |
-
List of dictionaries with information about each audio track.
|
| 1456 |
-
Each dictionary has the following keys:
|
| 1457 |
-
index (int): The stream index of the audio track.
|
| 1458 |
-
codec (str): The name of the audio codec.
|
| 1459 |
-
channels (int): The number of audio channels.
|
| 1460 |
-
language (LanguageCode): The language of the audio track.
|
| 1461 |
-
title (str): The title of the audio track.
|
| 1462 |
-
default (bool): Whether the audio track is the default for the file.
|
| 1463 |
-
forced (bool): Whether the audio track is forced.
|
| 1464 |
-
original (bool): Whether the audio track is the original.
|
| 1465 |
-
commentary (bool): Whether the audio track is a commentary.
|
| 1466 |
-
"""
|
| 1467 |
-
try:
|
| 1468 |
-
# Probe the file to get audio stream metadata
|
| 1469 |
-
probe = ffmpeg.probe(video_file, select_streams='a')
|
| 1470 |
-
audio_streams = probe.get('streams', [])
|
| 1471 |
-
|
| 1472 |
-
# Extract information for each audio track
|
| 1473 |
-
audio_tracks = []
|
| 1474 |
-
for stream in audio_streams:
|
| 1475 |
-
audio_track = {
|
| 1476 |
-
"index": int(stream.get("index", None)),
|
| 1477 |
-
"codec": stream.get("codec_name", "Unknown"),
|
| 1478 |
-
"channels": int(stream.get("channels", None)),
|
| 1479 |
-
"language": LanguageCode.from_iso_639_2(stream.get("tags", {}).get("language", "Unknown")),
|
| 1480 |
-
"title": stream.get("tags", {}).get("title", "None"),
|
| 1481 |
-
"default": stream.get("disposition", {}).get("default", 0) == 1,
|
| 1482 |
-
"forced": stream.get("disposition", {}).get("forced", 0) == 1,
|
| 1483 |
-
"original": stream.get("disposition", {}).get("original", 0) == 1,
|
| 1484 |
-
"commentary": "commentary" in stream.get("tags", {}).get("title", "").lower()
|
| 1485 |
-
}
|
| 1486 |
-
audio_tracks.append(audio_track)
|
| 1487 |
-
return audio_tracks
|
| 1488 |
-
|
| 1489 |
-
except ffmpeg.Error as e:
|
| 1490 |
-
logging.error(f"FFmpeg error: {e.stderr}")
|
| 1491 |
-
return []
|
| 1492 |
-
except Exception as e:
|
| 1493 |
-
logging.error(f"An error occurred while reading audio track information: {str(e)}")
|
| 1494 |
-
return []
|
| 1495 |
-
|
| 1496 |
-
def find_language_audio_track(audio_tracks, find_languages):
|
| 1497 |
-
"""
|
| 1498 |
-
Checks if an audio track with any of the given languages is present in the list of audio tracks.
|
| 1499 |
-
Returns the first language from `find_languages` that matches.
|
| 1500 |
-
|
| 1501 |
-
Args:
|
| 1502 |
-
audio_tracks (list): A list of dictionaries containing information about each audio track.
|
| 1503 |
-
find_languages (list): A list language codes to search for.
|
| 1504 |
-
|
| 1505 |
-
Returns:
|
| 1506 |
-
str or None: The first language found from `find_languages`, or None if no match is found.
|
| 1507 |
-
"""
|
| 1508 |
-
for language in find_languages:
|
| 1509 |
-
for track in audio_tracks:
|
| 1510 |
-
if track['language'] == language:
|
| 1511 |
-
return language
|
| 1512 |
-
return None
|
| 1513 |
-
|
| 1514 |
-
def find_default_audio_track_language(audio_tracks):
|
| 1515 |
-
"""
|
| 1516 |
-
Finds the language of the default audio track in the given list of audio tracks.
|
| 1517 |
-
|
| 1518 |
-
Args:
|
| 1519 |
-
audio_tracks (list): A list of dictionaries containing information about each audio track.
|
| 1520 |
-
Must contain the key "default" which is a boolean indicating if the track is the default track.
|
| 1521 |
-
|
| 1522 |
-
Returns:
|
| 1523 |
-
str: The ISO 639-2 code of the language of the default audio track, or None if no default track was found.
|
| 1524 |
-
"""
|
| 1525 |
-
for track in audio_tracks:
|
| 1526 |
-
if track['default'] is True:
|
| 1527 |
-
return track['language']
|
| 1528 |
-
return None
|
| 1529 |
-
|
| 1530 |
-
def gen_subtitles_queue(file_path: str, transcription_type: str, force_language: LanguageCode = LanguageCode.NONE, **kwargs) -> None:
|
| 1531 |
-
global task_queue
|
| 1532 |
-
|
| 1533 |
-
# Check if this file is already in the queue or being processed
|
| 1534 |
-
if task_queue.is_active(file_path):
|
| 1535 |
-
logging.debug(f"Ignored: {os.path.basename(file_path)} is already queued or processing.")
|
| 1536 |
-
return
|
| 1537 |
-
|
| 1538 |
-
if not has_audio(file_path):
|
| 1539 |
-
logging.debug(f"{file_path} doesn't have any audio to transcribe!")
|
| 1540 |
-
return
|
| 1541 |
-
|
| 1542 |
-
force_language = choose_transcribe_language(file_path, force_language)
|
| 1543 |
-
|
| 1544 |
-
if should_skip_file(file_path, force_language): # skip a file before we waste time detecting it's language
|
| 1545 |
-
return
|
| 1546 |
-
|
| 1547 |
-
# check if we would like to detect audio language in case of no audio language specified. Will return here again with specified language from whisper
|
| 1548 |
-
if not force_language and should_whiser_detect_audio_language:
|
| 1549 |
-
# make a detect language task
|
| 1550 |
-
task_id = {'path': file_path, 'type': "detect_language"}
|
| 1551 |
-
# Pass metadata info (kwargs) to the detect task
|
| 1552 |
-
task_id.update(kwargs)
|
| 1553 |
-
task_queue.put(task_id)
|
| 1554 |
-
#logging.debug(f"Added to queue: {task_id['path']} [type: {task_id.get('type', 'transcribe')}]")
|
| 1555 |
-
return
|
| 1556 |
-
|
| 1557 |
-
task = {
|
| 1558 |
-
'path': file_path,
|
| 1559 |
-
'transcribe_or_translate': transcription_type,
|
| 1560 |
-
'force_language': force_language
|
| 1561 |
-
}
|
| 1562 |
-
# Pass metadata info (kwargs) to the transcribe task
|
| 1563 |
-
task.update(kwargs)
|
| 1564 |
-
|
| 1565 |
-
task_queue.put(task)
|
| 1566 |
-
#logging.debug(f"Added to queue: {task['path']}, {task['transcribe_or_translate']}, {task['force_language']}")
|
| 1567 |
-
|
| 1568 |
-
def should_skip_file(file_path: str, target_language: LanguageCode) -> bool:
|
| 1569 |
-
"""
|
| 1570 |
-
Determines if subtitle generation should be skipped for a file.
|
| 1571 |
-
|
| 1572 |
-
Args:
|
| 1573 |
-
file_path: Path to the media file.
|
| 1574 |
-
target_language: The desired language for transcription.
|
| 1575 |
-
|
| 1576 |
-
Returns:
|
| 1577 |
-
True if the file should be skipped, False otherwise.
|
| 1578 |
-
"""
|
| 1579 |
-
base_name = os.path.basename(file_path)
|
| 1580 |
-
file_name, file_ext = os.path.splitext(base_name)
|
| 1581 |
-
if transcribe_or_translate == 'translate':
|
| 1582 |
-
target_language = LanguageCode.ENGLISH # Force our target language as english if we are translating
|
| 1583 |
-
# 1. Skip if it's an audio file and an LRC file already exists.
|
| 1584 |
-
if isAudioFileExtension(file_ext) and lrc_for_audio_files:
|
| 1585 |
-
lrc_path = os.path.join(os.path.dirname(file_path), f"{file_name}.lrc")
|
| 1586 |
-
if os.path.exists(lrc_path):
|
| 1587 |
-
logging.info(f"Skipping {base_name}: LRC file already exists.")
|
| 1588 |
-
return True
|
| 1589 |
-
|
| 1590 |
-
# 2. Skip if language detection failed and we are configured to skip unknowns.
|
| 1591 |
-
if skip_unknown_language and target_language == LanguageCode.NONE:
|
| 1592 |
-
logging.info(f"Skipping {base_name}: Unknown language and skip_unknown_language is enabled.")
|
| 1593 |
-
return True
|
| 1594 |
-
|
| 1595 |
-
# 3. Skip if a subtitle already exists in the target language.
|
| 1596 |
-
if skip_if_to_transcribe_sub_already_exist and has_subtitle_language(file_path, target_language):
|
| 1597 |
-
lang_name = target_language.to_name()
|
| 1598 |
-
logging.info(f"Skipping {base_name}: Subtitles already exist in {lang_name}.")
|
| 1599 |
-
return True
|
| 1600 |
-
|
| 1601 |
-
# 4. Skip if an internal subtitle exists in skipifinternalsublang language.
|
| 1602 |
-
if skipifinternalsublang and has_subtitle_language_in_file(file_path, skipifinternalsublang):
|
| 1603 |
-
lang_name = skipifinternalsublang.to_name()
|
| 1604 |
-
logging.info(f"Skipping {base_name}: Internal subtitles in {lang_name} already exist.")
|
| 1605 |
-
return True
|
| 1606 |
-
|
| 1607 |
-
# 5. Skip if an external subtitle exists in the namesublang language
|
| 1608 |
-
if skipifexternalsub and namesublang and LanguageCode.is_valid_language(namesublang):
|
| 1609 |
-
external_lang = LanguageCode.from_string(namesublang)
|
| 1610 |
-
if has_subtitle_of_language_in_folder(file_path, external_lang):
|
| 1611 |
-
lang_name = external_lang.to_name()
|
| 1612 |
-
logging.info(f"Skipping {base_name}: External subtitles in {lang_name} already exist.")
|
| 1613 |
-
return True
|
| 1614 |
-
|
| 1615 |
-
# 6. Skip if any subtitle language is in the skip list.
|
| 1616 |
-
if any(lang in skip_lang_codes_list for lang in get_subtitle_languages(file_path)):
|
| 1617 |
-
logging.info(f"Skipping {base_name}: Contains a skipped subtitle language.")
|
| 1618 |
-
return True
|
| 1619 |
-
|
| 1620 |
-
# 7. Audio track checks
|
| 1621 |
-
audio_langs = get_audio_languages(file_path)
|
| 1622 |
-
|
| 1623 |
-
# 7a. Limit to preferred audio languages
|
| 1624 |
-
if limit_to_preferred_audio_languages:
|
| 1625 |
-
if not any(lang in preferred_audio_languages for lang in audio_langs):
|
| 1626 |
-
preferred_names = [lang.to_name() for lang in preferred_audio_languages]
|
| 1627 |
-
logging.info(f"Skipping {base_name}: No preferred audio tracks found (looking for {', '.join(preferred_names)})")
|
| 1628 |
-
return True
|
| 1629 |
-
|
| 1630 |
-
# 7b. Skip if the audio track language is in the skip list
|
| 1631 |
-
if any(lang in skip_if_audio_track_is_in_list for lang in audio_langs):
|
| 1632 |
-
logging.info(f"Skipping {base_name}: Contains a skipped audio language.")
|
| 1633 |
-
return True
|
| 1634 |
-
|
| 1635 |
-
#logging.debug(f"Processing {base_name}: No skip conditions met.")
|
| 1636 |
-
return False
|
| 1637 |
-
|
| 1638 |
-
def get_subtitle_languages(video_path):
|
| 1639 |
-
"""
|
| 1640 |
-
Extract language codes from each audio stream in the video file using pyav.
|
| 1641 |
-
:param video_path: Path to the video file
|
| 1642 |
-
:return: List of language codes for each subtitle stream
|
| 1643 |
-
"""
|
| 1644 |
-
languages = []
|
| 1645 |
-
|
| 1646 |
-
# Open the video file
|
| 1647 |
-
with av.open(video_path) as container:
|
| 1648 |
-
# Iterate through each audio stream
|
| 1649 |
-
for stream in container.streams.subtitles:
|
| 1650 |
-
# Access the metadata for each audio stream
|
| 1651 |
-
lang_code = stream.metadata.get('language')
|
| 1652 |
-
if lang_code:
|
| 1653 |
-
languages.append(LanguageCode.from_iso_639_2(lang_code))
|
| 1654 |
-
else:
|
| 1655 |
-
# Append 'und' (undefined) if no language metadata is present
|
| 1656 |
-
languages.append(LanguageCode.NONE)
|
| 1657 |
-
|
| 1658 |
-
return languages
|
| 1659 |
-
|
| 1660 |
-
def get_file_name_without_extension(file_path):
|
| 1661 |
-
file_name, file_extension = os.path.splitext(file_path)
|
| 1662 |
-
return file_name
|
| 1663 |
-
|
| 1664 |
-
def get_audio_languages(video_path):
|
| 1665 |
-
"""
|
| 1666 |
-
Extract language codes from each audio stream in the video file.
|
| 1667 |
-
|
| 1668 |
-
:param video_path: Path to the video file
|
| 1669 |
-
:return: List of language codes for each audio stream
|
| 1670 |
-
"""
|
| 1671 |
-
audio_tracks = get_audio_tracks(video_path)
|
| 1672 |
-
return [track['language'] for track in audio_tracks]
|
| 1673 |
-
|
| 1674 |
-
def has_subtitle_language(video_file, target_language: LanguageCode):
|
| 1675 |
-
"""
|
| 1676 |
-
Determines if a subtitle file with the target language is available for a specified video file.
|
| 1677 |
-
|
| 1678 |
-
This function checks both within the video file and in its associated folder for subtitles
|
| 1679 |
-
matching the specified language.
|
| 1680 |
-
|
| 1681 |
-
Args:
|
| 1682 |
-
video_file: The path to the video file.
|
| 1683 |
-
target_language: The language of the subtitle file to search for.
|
| 1684 |
-
|
| 1685 |
-
Returns:
|
| 1686 |
-
bool: True if a subtitle file with the target language is found, False otherwise.
|
| 1687 |
-
"""
|
| 1688 |
-
return has_subtitle_language_in_file(video_file, target_language) or has_subtitle_of_language_in_folder(video_file, target_language)
|
| 1689 |
-
|
| 1690 |
-
def has_subtitle_language_in_file(video_file: str, target_language: Union[LanguageCode, None]):
|
| 1691 |
-
"""
|
| 1692 |
-
Checks if a video file contains subtitles with a specific language.
|
| 1693 |
-
|
| 1694 |
-
Args:
|
| 1695 |
-
video_file (str): The path to the video file.
|
| 1696 |
-
target_language (LanguageCode | None): The language of the subtitle file to search for.
|
| 1697 |
-
|
| 1698 |
-
Returns:
|
| 1699 |
-
bool: True if a subtitle file with the target language is found, False otherwise.
|
| 1700 |
-
"""
|
| 1701 |
-
try:
|
| 1702 |
-
with av.open(video_file) as container:
|
| 1703 |
-
# Create a list of subtitle streams with 'language' metadata
|
| 1704 |
-
subtitle_streams = [
|
| 1705 |
-
stream for stream in container.streams
|
| 1706 |
-
if stream.type == 'subtitle' and 'language' in stream.metadata
|
| 1707 |
-
]
|
| 1708 |
-
|
| 1709 |
-
# Skip logic if target_language is None
|
| 1710 |
-
if target_language is LanguageCode.NONE:
|
| 1711 |
-
if skip_if_language_is_not_set_but_subtitles_exist and subtitle_streams:
|
| 1712 |
-
logging.debug("Language is not set, but internal subtitles exist.")
|
| 1713 |
-
return True
|
| 1714 |
-
if only_skip_if_subgen_subtitle:
|
| 1715 |
-
#logging.debug("Skipping since only external subgen subtitles are considered.")
|
| 1716 |
-
return False # Skip if only looking for external subgen subtitles
|
| 1717 |
-
|
| 1718 |
-
# Check if any subtitle stream matches the target language
|
| 1719 |
-
for stream in subtitle_streams:
|
| 1720 |
-
# Convert the subtitle stream's language to a LanguageCode instance and compare
|
| 1721 |
-
stream_language = LanguageCode.from_string(stream.metadata.get('language', '').lower())
|
| 1722 |
-
if stream_language == target_language:
|
| 1723 |
-
#logging.debug(f"Subtitles in '{target_language}' language found in the video.")
|
| 1724 |
-
return True
|
| 1725 |
-
|
| 1726 |
-
#logging.debug(f"No subtitles in '{target_language}' language found in the video.")
|
| 1727 |
-
return False
|
| 1728 |
-
|
| 1729 |
-
except Exception as e:
|
| 1730 |
-
logging.error(f"An error occurred while checking the file with pyav: {type(e).__name__}: {e}")
|
| 1731 |
-
return False
|
| 1732 |
-
|
| 1733 |
-
def has_subtitle_of_language_in_folder(video_file: str, target_language: LanguageCode, recursion: bool = True, only_skip_if_subgen_subtitle: bool = False) -> bool:
|
| 1734 |
-
"""Checks if the given folder has a subtitle file with the given language.
|
| 1735 |
-
Args:
|
| 1736 |
-
video_file (str): The path of the video file.
|
| 1737 |
-
target_language (LanguageCode): The language of the subtitle file to search for.
|
| 1738 |
-
recursion (bool): If True, search subfolders. If False, only the current folder.
|
| 1739 |
-
only_skip_if_subgen_subtitle (bool): If True, only skip if subtitles are auto-generated ("subgen").
|
| 1740 |
-
Returns:
|
| 1741 |
-
bool: True if a matching subtitle file is found, False otherwise.
|
| 1742 |
-
"""
|
| 1743 |
-
subtitle_extensions = {'.srt', '.vtt', '.sub', '.ass', '.ssa', '.idx', '.sbv', '.pgs', '.ttml', '.lrc'}
|
| 1744 |
-
|
| 1745 |
-
video_folder = os.path.dirname(video_file)
|
| 1746 |
-
video_name = os.path.splitext(os.path.basename(video_file))[0]
|
| 1747 |
-
|
| 1748 |
-
# logging.debug(f"Searching for subtitles in: {video_folder}")
|
| 1749 |
-
|
| 1750 |
-
for file_name in os.listdir(video_folder):
|
| 1751 |
-
file_path = os.path.join(video_folder, file_name)
|
| 1752 |
-
|
| 1753 |
-
# If it's a file and has a subtitle extension
|
| 1754 |
-
if os.path.isfile(file_path) and file_path.endswith(tuple(subtitle_extensions)):
|
| 1755 |
-
subtitle_name, ext = os.path.splitext(file_name)
|
| 1756 |
-
|
| 1757 |
-
# Ensure the subtitle name starts with the video name
|
| 1758 |
-
if not subtitle_name.startswith(video_name):
|
| 1759 |
-
continue
|
| 1760 |
-
|
| 1761 |
-
# Extract parts after video filename
|
| 1762 |
-
subtitle_parts = subtitle_name[len(video_name):].lstrip(".").split(".")
|
| 1763 |
-
|
| 1764 |
-
# Check for "subgen"
|
| 1765 |
-
has_subgen = "subgen" in subtitle_parts
|
| 1766 |
-
|
| 1767 |
-
# Special handling if only skipping for subgen subtitles
|
| 1768 |
-
if target_language == LanguageCode.NONE:
|
| 1769 |
-
if only_skip_if_subgen_subtitle:
|
| 1770 |
-
if has_subgen:
|
| 1771 |
-
logging.debug("Skipping subtitles because they are auto-generated ('subgen').")
|
| 1772 |
-
return False
|
| 1773 |
-
logging.debug("Skipping subtitles because language is NONE.")
|
| 1774 |
-
return True # Default behavior if subtitles exist
|
| 1775 |
-
|
| 1776 |
-
# Check if the subtitle file matches the target language
|
| 1777 |
-
if is_valid_subtitle_language(subtitle_parts, target_language):
|
| 1778 |
-
if only_skip_if_subgen_subtitle and not has_subgen:
|
| 1779 |
-
continue # Ignore non-subgen subtitles if flag is set
|
| 1780 |
-
logging.debug(f"Found matching subtitle: {file_name} for language {target_language.name} (subgen={has_subgen})")
|
| 1781 |
-
return True
|
| 1782 |
-
|
| 1783 |
-
# Recursively search subfolders
|
| 1784 |
-
elif os.path.isdir(file_path) and recursion:
|
| 1785 |
-
if has_subtitle_of_language_in_folder(os.path.join(file_path, os.path.basename(video_file)), target_language, False, only_skip_if_subgen_subtitle):
|
| 1786 |
-
return True
|
| 1787 |
-
|
| 1788 |
-
return False
|
| 1789 |
-
|
| 1790 |
-
def is_valid_subtitle_language(subtitle_parts: List[str], target_language: LanguageCode) -> bool:
|
| 1791 |
-
"""Checks if any part of the subtitle name matches the target language."""
|
| 1792 |
-
return any(LanguageCode.from_string(part) == target_language for part in subtitle_parts)
|
| 1793 |
-
|
| 1794 |
-
def get_next_plex_episode(current_episode_rating_key, stay_in_season: bool = False):
|
| 1795 |
-
"""
|
| 1796 |
-
Get the next episode's ratingKey based on the current episode in Plex.
|
| 1797 |
-
Args:
|
| 1798 |
-
current_episode_rating_key (str): The ratingKey of the current episode.
|
| 1799 |
-
stay_in_season (bool): If True, only find the next episode within the current season.
|
| 1800 |
-
If False, find the next episode in the series.
|
| 1801 |
-
Returns:
|
| 1802 |
-
str: The ratingKey of the next episode, or None if it's the last episode.
|
| 1803 |
-
"""
|
| 1804 |
-
try:
|
| 1805 |
-
# Get current episode's metadata to fetch parent (season) ratingKey
|
| 1806 |
-
url = f"{plexserver}/library/metadata/{current_episode_rating_key}"
|
| 1807 |
-
headers = {"X-Plex-Token": plextoken}
|
| 1808 |
-
response = requests.get(url, headers=headers)
|
| 1809 |
-
response.raise_for_status()
|
| 1810 |
-
|
| 1811 |
-
# Parse XML response
|
| 1812 |
-
root = ET.fromstring(response.content)
|
| 1813 |
-
|
| 1814 |
-
# Find the show ID
|
| 1815 |
-
grandparent_rating_key = root.find(".//Video").get("grandparentRatingKey")
|
| 1816 |
-
if grandparent_rating_key is None:
|
| 1817 |
-
logging.debug(f"Show not found for episode {current_episode_rating_key}")
|
| 1818 |
-
return None
|
| 1819 |
-
|
| 1820 |
-
# Find the parent season ratingKey
|
| 1821 |
-
parent_rating_key = root.find(".//Video").get("parentRatingKey")
|
| 1822 |
-
if parent_rating_key is None:
|
| 1823 |
-
logging.debug(f"Parent season not found for episode {current_episode_rating_key}")
|
| 1824 |
-
return None
|
| 1825 |
-
|
| 1826 |
-
# Get the list of seasons
|
| 1827 |
-
url = f"{plexserver}/library/metadata/{grandparent_rating_key}/children"
|
| 1828 |
-
response = requests.get(url, headers=headers)
|
| 1829 |
-
response.raise_for_status()
|
| 1830 |
-
seasons = ET.fromstring(response.content).findall(".//Directory[@type='season']")
|
| 1831 |
-
|
| 1832 |
-
# Get the list of episodes in the parent season
|
| 1833 |
-
url = f"{plexserver}/library/metadata/{parent_rating_key}/children"
|
| 1834 |
-
response = requests.get(url, headers=headers)
|
| 1835 |
-
response.raise_for_status()
|
| 1836 |
-
#print(response.content)
|
| 1837 |
-
|
| 1838 |
-
# Parse XML response for the list of episodes
|
| 1839 |
-
episodes = ET.fromstring(response.content).findall(".//Video")
|
| 1840 |
-
episodes_in_season = len(episodes) #episodes.get('size') # changed from episodes.get("size") because size is not available
|
| 1841 |
-
|
| 1842 |
-
# Find the current episode index and get the next one
|
| 1843 |
-
current_episode_number = None
|
| 1844 |
-
current_season_number = None
|
| 1845 |
-
next_season_number = None
|
| 1846 |
-
for episode in episodes:
|
| 1847 |
-
if episode.get("ratingKey") == current_episode_rating_key:
|
| 1848 |
-
current_episode_number = int(episode.get("index"))
|
| 1849 |
-
current_season_number = episode.get("parentIndex")
|
| 1850 |
-
break
|
| 1851 |
-
#if rating_key_element is None:
|
| 1852 |
-
# logging.warning(f"ratingKey not found for episode at index")
|
| 1853 |
-
# continue
|
| 1854 |
-
|
| 1855 |
-
# Logic to find the next episode
|
| 1856 |
-
if stay_in_season:
|
| 1857 |
-
if current_episode_number == episodes_in_season:
|
| 1858 |
-
return None # End of season
|
| 1859 |
-
for episode in episodes:
|
| 1860 |
-
if int(episode.get("index")) == int(current_episode_number)+1:
|
| 1861 |
-
return episode.get("ratingKey")
|
| 1862 |
-
else: # Not staying in season, find the next overall episode
|
| 1863 |
-
# Find next season if it exists
|
| 1864 |
-
for season in seasons:
|
| 1865 |
-
if int(season.get("index")) == int(current_season_number)+1:
|
| 1866 |
-
#print(f"next season is: {episode.get('ratingKey')}")
|
| 1867 |
-
#print(season.get("title"))
|
| 1868 |
-
next_season_number = season.get("ratingKey")
|
| 1869 |
-
break
|
| 1870 |
-
|
| 1871 |
-
if current_episode_number == episodes_in_season: # changed to episodes_in_season from int(episodes_in_season)
|
| 1872 |
-
if next_season_number is not None:
|
| 1873 |
-
logging.debug("At end of season, try to find next season and first episode.")
|
| 1874 |
-
url = f"{plexserver}/library/metadata/{next_season_number}/children"
|
| 1875 |
-
response = requests.get(url, headers=headers)
|
| 1876 |
-
response.raise_for_status()
|
| 1877 |
-
episodes = ET.fromstring(response.content).findall(".//Video")
|
| 1878 |
-
current_episode_number = 0
|
| 1879 |
-
else:
|
| 1880 |
-
return None
|
| 1881 |
-
for episode in episodes:
|
| 1882 |
-
if int(episode.get("index")) == int(current_episode_number)+1:
|
| 1883 |
-
return episode.get("ratingKey")
|
| 1884 |
-
|
| 1885 |
-
logging.debug(f"No next episode found for {get_plex_file_name(current_episode_rating_key, plexserver, plextoken)}, possibly end of season or series")
|
| 1886 |
-
return None
|
| 1887 |
-
|
| 1888 |
-
except requests.exceptions.RequestException as e:
|
| 1889 |
-
logging.error(f"Error fetching data from Plex: {e}")
|
| 1890 |
-
return None
|
| 1891 |
-
except Exception as e:
|
| 1892 |
-
logging.error(f"An unexpected error occurred: {e}")
|
| 1893 |
-
return None
|
| 1894 |
-
|
| 1895 |
-
def get_plex_file_name(itemid: str, server_ip: str, plex_token: str) -> str:
|
| 1896 |
-
"""Gets the full path to a file from the Plex server.
|
| 1897 |
-
Args:
|
| 1898 |
-
itemid: The ID of the item in the Plex library.
|
| 1899 |
-
server_ip: The IP address of the Plex server.
|
| 1900 |
-
plex_token: The Plex token.
|
| 1901 |
-
Returns:
|
| 1902 |
-
The full path to the file.
|
| 1903 |
-
"""
|
| 1904 |
-
|
| 1905 |
-
url = f"{server_ip}/library/metadata/{itemid}"
|
| 1906 |
-
|
| 1907 |
-
headers = {
|
| 1908 |
-
"X-Plex-Token": plex_token,
|
| 1909 |
-
}
|
| 1910 |
-
|
| 1911 |
-
response = requests.get(url, headers=headers)
|
| 1912 |
-
|
| 1913 |
-
if response.status_code == 200:
|
| 1914 |
-
root = ET.fromstring(response.content)
|
| 1915 |
-
fullpath = root.find(".//Part").attrib['file']
|
| 1916 |
-
return fullpath
|
| 1917 |
-
else:
|
| 1918 |
-
raise Exception(f"Error: {response.status_code}")
|
| 1919 |
-
|
| 1920 |
-
def refresh_plex_metadata(itemid: str, server_ip: str, plex_token: str) -> None:
|
| 1921 |
-
"""
|
| 1922 |
-
Refreshes the metadata of a Plex library item.
|
| 1923 |
-
|
| 1924 |
-
Args:
|
| 1925 |
-
itemid: The ID of the item in the Plex library whose metadata needs to be refreshed.
|
| 1926 |
-
server_ip: The IP address of the Plex server.
|
| 1927 |
-
plex_token: The Plex token used for authentication.
|
| 1928 |
-
|
| 1929 |
-
Raises:
|
| 1930 |
-
Exception: If the server does not respond with a successful status code.
|
| 1931 |
-
"""
|
| 1932 |
-
|
| 1933 |
-
# Plex API endpoint to refresh metadata for a specific item
|
| 1934 |
-
url = f"{server_ip}/library/metadata/{itemid}/refresh"
|
| 1935 |
-
|
| 1936 |
-
# Headers to include the Plex token for authentication
|
| 1937 |
-
headers = {
|
| 1938 |
-
"X-Plex-Token": plex_token,
|
| 1939 |
-
}
|
| 1940 |
-
|
| 1941 |
-
# Sending the PUT request to refresh metadata
|
| 1942 |
-
response = requests.put(url, headers=headers)
|
| 1943 |
-
|
| 1944 |
-
# Check if the request was successful
|
| 1945 |
-
if response.status_code == 200:
|
| 1946 |
-
logging.info("Metadata refresh initiated successfully.")
|
| 1947 |
-
else:
|
| 1948 |
-
raise Exception(f"Error refreshing metadata: {response.status_code}")
|
| 1949 |
-
|
| 1950 |
-
def refresh_jellyfin_metadata(itemid: str, server_ip: str, jellyfin_token: str) -> None:
|
| 1951 |
-
"""
|
| 1952 |
-
Refreshes the metadata of a Jellyfin library item.
|
| 1953 |
-
|
| 1954 |
-
Args:
|
| 1955 |
-
itemid: The ID of the item in the Jellyfin library whose metadata needs to be refreshed.
|
| 1956 |
-
server_ip: The IP address of the Jellyfin server.
|
| 1957 |
-
jellyfin_token: The Jellyfin token used for authentication.
|
| 1958 |
-
|
| 1959 |
-
Raises:
|
| 1960 |
-
Exception: If the server does not respond with a successful status code.
|
| 1961 |
-
"""
|
| 1962 |
-
|
| 1963 |
-
# Jellyfin API endpoint to refresh metadata for a specific item
|
| 1964 |
-
url = f"{server_ip}/Items/{itemid}/Refresh?MetadataRefreshMode=FullRefresh"
|
| 1965 |
-
|
| 1966 |
-
# Headers to include the Jellyfin token for authentication
|
| 1967 |
-
headers = {
|
| 1968 |
-
"Authorization": f"MediaBrowser Token={jellyfin_token}",
|
| 1969 |
-
}
|
| 1970 |
-
|
| 1971 |
-
# Cheap way to get the admin user id, and save it for later use.
|
| 1972 |
-
users = json.loads(requests.get(f"{server_ip}/Users", headers=headers).content)
|
| 1973 |
-
jellyfin_admin = get_jellyfin_admin(users)
|
| 1974 |
-
|
| 1975 |
-
response = requests.get(f"{server_ip}/Users/{jellyfin_admin}/Items/{itemid}/Refresh", headers=headers)
|
| 1976 |
-
|
| 1977 |
-
# Sending the PUT request to refresh metadata
|
| 1978 |
-
response = requests.post(url, headers=headers)
|
| 1979 |
-
|
| 1980 |
-
# Check if the request was successful
|
| 1981 |
-
if response.status_code == 204:
|
| 1982 |
-
logging.info("Metadata refresh queued successfully.")
|
| 1983 |
-
else:
|
| 1984 |
-
raise Exception(f"Error refreshing metadata: {response.status_code}")
|
| 1985 |
-
|
| 1986 |
-
|
| 1987 |
-
def get_jellyfin_file_name(item_id: str, jellyfin_url: str, jellyfin_token: str) -> str:
|
| 1988 |
-
"""Gets the full path to a file from the Jellyfin server.
|
| 1989 |
-
Args:
|
| 1990 |
-
jellyfin_url: The URL of the Jellyfin server.
|
| 1991 |
-
jellyfin_token: The Jellyfin token.
|
| 1992 |
-
item_id: The ID of the item in the Jellyfin library.
|
| 1993 |
-
Returns:
|
| 1994 |
-
The full path to the file.
|
| 1995 |
-
"""
|
| 1996 |
-
|
| 1997 |
-
headers = {
|
| 1998 |
-
"Authorization": f"MediaBrowser Token={jellyfin_token}",
|
| 1999 |
-
}
|
| 2000 |
-
|
| 2001 |
-
# Cheap way to get the admin user id, and save it for later use.
|
| 2002 |
-
users = json.loads(requests.get(f"{jellyfin_url}/Users", headers=headers).content)
|
| 2003 |
-
jellyfin_admin = get_jellyfin_admin(users)
|
| 2004 |
-
|
| 2005 |
-
response = requests.get(f"{jellyfin_url}/Users/{jellyfin_admin}/Items/{item_id}", headers=headers)
|
| 2006 |
-
|
| 2007 |
-
if response.status_code == 200:
|
| 2008 |
-
file_name = json.loads(response.content)['Path']
|
| 2009 |
-
return file_name
|
| 2010 |
-
else:
|
| 2011 |
-
raise Exception(f"Error: {response.status_code}")
|
| 2012 |
-
|
| 2013 |
-
def get_jellyfin_admin(users):
|
| 2014 |
-
for user in users:
|
| 2015 |
-
if user["Policy"]["IsAdministrator"]:
|
| 2016 |
-
return user["Id"]
|
| 2017 |
-
|
| 2018 |
-
raise Exception("Unable to find administrator user in Jellyfin")
|
| 2019 |
-
|
| 2020 |
-
def has_audio(file_path):
|
| 2021 |
-
try:
|
| 2022 |
-
if not is_valid_path(file_path):
|
| 2023 |
-
return False
|
| 2024 |
-
|
| 2025 |
-
if not (has_video_extension(file_path) or has_audio_extension(file_path)):
|
| 2026 |
-
# logging.debug(f"{file_path} is an not a video or audio file, skipping processing. skipping processing")
|
| 2027 |
-
return False
|
| 2028 |
-
|
| 2029 |
-
with av.open(file_path) as container:
|
| 2030 |
-
# Check for an audio stream and ensure it has a valid codec
|
| 2031 |
-
for stream in container.streams:
|
| 2032 |
-
if stream.type == 'audio':
|
| 2033 |
-
# Check if the stream has a codec and if it is valid
|
| 2034 |
-
if stream.codec_context and stream.codec_context.name != 'none':
|
| 2035 |
-
return True
|
| 2036 |
-
else:
|
| 2037 |
-
logging.debug(f"Unsupported or missing codec for audio stream in {file_path}")
|
| 2038 |
-
return False
|
| 2039 |
-
|
| 2040 |
-
except (av.FFmpegError, UnicodeDecodeError):
|
| 2041 |
-
logging.debug(f"Error processing file {file_path}")
|
| 2042 |
-
return False
|
| 2043 |
-
|
| 2044 |
-
def is_valid_path(file_path):
|
| 2045 |
-
# Check if the path is a file
|
| 2046 |
-
if not os.path.isfile(file_path):
|
| 2047 |
-
# If it's not a file, check if it's a directory
|
| 2048 |
-
if not os.path.isdir(file_path):
|
| 2049 |
-
logging.warning(f"{file_path} is neither a file nor a directory. Are your volumes correct?")
|
| 2050 |
-
return False
|
| 2051 |
-
else:
|
| 2052 |
-
logging.debug(f"{file_path} is a directory, skipping processing as a file.")
|
| 2053 |
-
return False
|
| 2054 |
-
else:
|
| 2055 |
-
return True
|
| 2056 |
-
|
| 2057 |
-
def has_video_extension(file_name):
|
| 2058 |
-
file_extension = os.path.splitext(file_name)[1].lower() # Get the file extension
|
| 2059 |
-
return file_extension in VIDEO_EXTENSIONS
|
| 2060 |
-
|
| 2061 |
-
def has_audio_extension(file_name):
|
| 2062 |
-
file_extension = os.path.splitext(file_name)[1].lower() # Get the file extension
|
| 2063 |
-
return file_extension in AUDIO_EXTENSIONS
|
| 2064 |
-
|
| 2065 |
-
|
| 2066 |
-
def path_mapping(fullpath):
|
| 2067 |
-
if use_path_mapping:
|
| 2068 |
-
logging.debug("Updated path: " + fullpath.replace(path_mapping_from, path_mapping_to))
|
| 2069 |
-
return fullpath.replace(path_mapping_from, path_mapping_to)
|
| 2070 |
-
return fullpath
|
| 2071 |
-
|
| 2072 |
-
def is_file_stable(file_path, wait_time=2, check_intervals=3):
|
| 2073 |
-
"""Returns True if the file size is stable for a given number of checks."""
|
| 2074 |
-
if not os.path.exists(file_path):
|
| 2075 |
-
return False
|
| 2076 |
-
|
| 2077 |
-
previous_size = -1
|
| 2078 |
-
for _ in range(check_intervals):
|
| 2079 |
-
try:
|
| 2080 |
-
current_size = os.path.getsize(file_path)
|
| 2081 |
-
except OSError:
|
| 2082 |
-
return False # File might still be inaccessible
|
| 2083 |
-
|
| 2084 |
-
if current_size == previous_size:
|
| 2085 |
-
return True # File is stable
|
| 2086 |
-
previous_size = current_size
|
| 2087 |
-
time.sleep(wait_time)
|
| 2088 |
-
|
| 2089 |
-
return False # File is still changing
|
| 2090 |
-
|
| 2091 |
-
if monitor:
|
| 2092 |
-
# Define a handler class that will process new files
|
| 2093 |
-
class NewFileHandler(FileSystemEventHandler):
|
| 2094 |
-
def create_subtitle(self, event):
|
| 2095 |
-
# Only process if it's a file
|
| 2096 |
-
if not event.is_directory:
|
| 2097 |
-
file_path = event.src_path
|
| 2098 |
-
if has_audio(file_path):
|
| 2099 |
-
logging.info(f"File: {path_mapping(file_path)} was added")
|
| 2100 |
-
gen_subtitles_queue(path_mapping(file_path), transcribe_or_translate)
|
| 2101 |
-
|
| 2102 |
-
def handle_event(self, event):
|
| 2103 |
-
"""Wait for stability before processing the file."""
|
| 2104 |
-
file_path = event.src_path
|
| 2105 |
-
if is_file_stable(file_path):
|
| 2106 |
-
self.create_subtitle(event)
|
| 2107 |
-
|
| 2108 |
-
def on_created(self, event):
|
| 2109 |
-
time.sleep(5) # Extra buffer time for new files
|
| 2110 |
-
self.handle_event(event)
|
| 2111 |
-
|
| 2112 |
-
def on_modified(self, event):
|
| 2113 |
-
self.handle_event(event)
|
| 2114 |
-
|
| 2115 |
-
def transcribe_existing(transcribe_folders, forceLanguage : LanguageCode | None = None):
|
| 2116 |
-
transcribe_folders = transcribe_folders.split("|")
|
| 2117 |
-
logging.info("Starting to search folders to see if we need to create subtitles.")
|
| 2118 |
-
logging.debug("The folders are:")
|
| 2119 |
-
for path in transcribe_folders:
|
| 2120 |
-
logging.debug(path)
|
| 2121 |
-
for root, dirs, files in os.walk(path):
|
| 2122 |
-
for file in files:
|
| 2123 |
-
file_path = os.path.join(root, file)
|
| 2124 |
-
gen_subtitles_queue(path_mapping(file_path), transcribe_or_translate, forceLanguage)
|
| 2125 |
-
# if the path specified was actually a single file and not a folder, process it
|
| 2126 |
-
if os.path.isfile(path):
|
| 2127 |
-
if has_audio(path):
|
| 2128 |
-
gen_subtitles_queue(path_mapping(path), transcribe_or_translate, forceLanguage)
|
| 2129 |
-
# Set up the observer to watch for new files
|
| 2130 |
-
if monitor:
|
| 2131 |
-
observer = Observer()
|
| 2132 |
-
for path in transcribe_folders:
|
| 2133 |
-
if os.path.isdir(path):
|
| 2134 |
-
handler = NewFileHandler()
|
| 2135 |
-
observer.schedule(handler, path, recursive=True)
|
| 2136 |
-
observer.start()
|
| 2137 |
-
logging.info("Finished searching and queueing files for transcription. Now watching for new files.")
|
| 2138 |
-
|
| 2139 |
-
|
| 2140 |
-
if __name__ == "__main__":
|
| 2141 |
-
import uvicorn
|
| 2142 |
-
logging.info(f"Subgen v{subgen_version}")
|
| 2143 |
-
logging.info(f"Threads: {str(whisper_threads)}, Concurrent transcriptions: {str(concurrent_transcriptions)}")
|
| 2144 |
-
logging.info(f"Transcribe device: {transcribe_device}, Model: {whisper_model}")
|
| 2145 |
-
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
|
| 2146 |
-
if transcribe_folders:
|
| 2147 |
-
transcribe_existing(transcribe_folders)
|
| 2148 |
-
uvicorn.run("__main__:app", host="0.0.0.0", port=int(webhookport), reload=reload_script_on_change, use_colors=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subgen.xml
DELETED
|
@@ -1,56 +0,0 @@
|
|
| 1 |
-
<?xml version="1.0"?>
|
| 2 |
-
<Container version="2">
|
| 3 |
-
<Name>subgen</Name>
|
| 4 |
-
<ExtraParams>--gpus all</ExtraParams>
|
| 5 |
-
<Beta>false</Beta>
|
| 6 |
-
<Category>CATEGORY:</Category>
|
| 7 |
-
<Repository>mccloud/subgen</Repository>
|
| 8 |
-
<Registry>https://github.com/McCloudS/subgen</Registry>
|
| 9 |
-
<DonateText>If you appreciate my work, then please consider donating</DonateText>
|
| 10 |
-
<DonateLink>https://www.paypal.com/donate/?hosted_button_id=SU4QQP6LH5PF6</DonateLink>
|
| 11 |
-
<DonateImg>https://www.paypal.com/en_US/i/btn/btn_donate_SM.gif</DonateImg>
|
| 12 |
-
<Network>bridge</Network>
|
| 13 |
-
<Privileged>false</Privileged>
|
| 14 |
-
<Support>https://github.com/McCloudS/subgen/issues</Support>
|
| 15 |
-
<Shell>bash</Shell>
|
| 16 |
-
<GitHub>https://github.com/McCloudS/subgen</GitHub>
|
| 17 |
-
<ReadMe>https://github.com/McCloudS/subgen/blob/main/README.md</ReadMe>
|
| 18 |
-
<Project>https://github.com/McCloudS/subgen</Project>
|
| 19 |
-
<Overview>subgen will transcribe your personal media on a Plex, Emby, or Jellyfin server to create subtitles (.srt) from audio/video files, it can also be used as a Whisper Provider in Bazarr</Overview>
|
| 20 |
-
<WebUI>http://[IP]:[PORT:9000]/docs</WebUI>
|
| 21 |
-
<TemplateURL>https://github.com/McCloudS/subgen/blob/main/subgen.xml</TemplateURL>
|
| 22 |
-
<Icon>https://raw.githubusercontent.com/McCloudS/subgen/main/icon.png</Icon>
|
| 23 |
-
<Date>2024-03-23</Date>
|
| 24 |
-
<Changes></Changes>
|
| 25 |
-
<Config Name="Port: Webhook Port" Target="9000" Default="9000" Mode="tcp" Description="This is the port for the webhook" Type="Port" Display="always" Required="true" Mask="false"/>
|
| 26 |
-
<Config Name="Path: /subgen" Target="/subgen" Default="/mnt/user/appdata/subgen" Mode="rw" Description="This is the container path to your configuration files." Type="Path" Display="always" Required="true" Mask="false"/>
|
| 27 |
-
<Config Name="Variable: TRANSCRIBE_DEVICE" Target="TRANSCRIBE_DEVICE" Default="gpu" Description="Can transcribe via gpu (Cuda only) or cpu. Takes option of 'cpu', 'gpu', 'cuda'." Type="Variable" Display="always" Required="false" Mask="false"/>
|
| 28 |
-
<Config Name="Variable: WHISPER_MODEL" Target="WHISPER_MODEL" Default="medium" Description="Can be:'tiny', 'tiny.en', 'base', 'base.en', 'small', 'small.en', 'medium', 'medium.en', 'large-v1','large-v2', 'large-v3', 'large', 'distil-large-v2', 'distil-medium.en', 'distil-small.en'" Type="Variable" Display="always" Required="false" Mask="false"/>
|
| 29 |
-
<Config Name="Variable: CONCURRENT_TRANSCRIPTIONS" Target="CONCURRENT_TRANSCRIPTIONS" Default="2" Description="Number of files it will transcribe in parallel" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 30 |
-
<Config Name="Variable: WHISPER_THREADS" Target="WHISPER_THREADS" Default="4" Description="number of threads to use during computation" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 31 |
-
<Config Name="Variable: MODEL_PATH" Target="MODEL_PATH" Default="./models" Description="This is where the WHISPER_MODEL will be stored. This defaults to placing it where you execute the script in the folder 'models'" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 32 |
-
<Config Name="Variable: PROCADDEDMEDIA" Target="PROCADDEDMEDIA" Default="True" Description="will gen subtitles for all media added regardless of existing external/embedded subtitles (based off of SKIPIFINTERNALSUBLANG)" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 33 |
-
<Config Name="Variable: PROCMEDIAONPLAY" Target="PROCMEDIAONPLAY" Default="True" Description="will gen subtitles for all played media regardless of existing external/embedded subtitles (based off of SKIPIFINTERNALSUBLANG)" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 34 |
-
<Config Name="Variable: NAMESUBLANG" Target="NAMESUBLANG" Default="aa" Description="allows you to pick what it will name the subtitle. Instead of using EN, I'm using AA, so it doesn't mix with exiting external EN subs, and AA will populate higher on the list in Plex." Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 35 |
-
<Config Name="Variable: SKIPIFINTERNALSUBLANG" Target="SKIPIFINTERNALSUBLANG" Default="eng" Description="Will not generate a subtitle if the file has an internal sub matching the 3 letter code of this variable (See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 36 |
-
<Config Name="Variable: WORD_LEVEL_HIGHLIGHT" Target="WORD_LEVEL_HIGHLIGHT" Default="False" Description="Highlights each words as it's spoken in the subtitle. See example video @ https://github.com/jianfch/stable-ts" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 37 |
-
<Config Name="Variable: PLEXSERVER" Target="PLEXSERVER" Default="http://plex:32400" Description="This needs to be set to your local plex server address/port" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 38 |
-
<Config Name="Variable: PLEXTOKEN" Target="PLEXTOKEN" Default="token here" Description="This needs to be set to your plex token found by https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 39 |
-
<Config Name="Variable: JELLYFINSERVER" Target="JELLYFINSERVER" Default="http://jellyfin:8096" Description="Set to your Jellyfin server address/port" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 40 |
-
<Config Name="Variable: JELLYFINTOKEN" Target="JELLYFINTOKEN" Default="token here" Description="Generate a token inside the Jellyfin interface" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 41 |
-
<Config Name="Variable: WEBHOOKPORT" Target="WEBHOOKPORT" Default="9000" Description="Change this if you need a different port for your webhook" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 42 |
-
<Config Name="Variable: TRANSCRIBE_FOLDERS" Target="TRANSCRIBE_FOLDERS" Default="" Description="Takes a pipe '|' separated list (For example: /tv|/movies|/familyvideos) and iterates through and adds those files to be queued for subtitle generation if they don't have internal subtitles" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 43 |
-
<Config Name="Variable: TRANSCRIBE_OR_TRANSLATE" Target="TRANSCRIBE_OR_TRANSLATE" Default="transcribe" Description="Takes either 'transcribe' or 'translate'. Transcribe will transcribe the audio in the same language as the input. Translate will transcribe and translate into English." Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 44 |
-
<Config Name="Variable: COMPUTE_TYPE" Target="COMPUTE_TYPE" Default="auto" Description="Set compute-type using the following information: https://github.com/OpenNMT/CTranslate2/blob/master/docs/quantization.md" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 45 |
-
<Config Name="Variable: DEBUG" Target="DEBUG" Default="True" Description="Provides some debug data that can be helpful to troubleshoot path mapping and other issues." Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 46 |
-
<Config Name="Variable: FORCE_DETECTED_LANGUAGE_TO" Target="FORCE_DETECTED_LANGUAGE_TO" Default="" Description="This is to force the model to a language instead of the detected one, takes a 2 letter language code. For example, your audio is French but keeps detecting as English, you would set it to 'fr'" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 47 |
-
<Config Name="Variable: CLEAR_VRAM_ON_COMPLETE" Target="CLEAR_VRAM_ON_COMPLETE" Default="False" Description="This will delete the model and do garbage collection when queue is empty. Good if you need to use the VRAM for something else." Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 48 |
-
<Config Name="Variable: UPDATE" Target="UPDATE" Default="True" Description="Will pull latest subgen.py from the repository if True. False will use the original subgen.py built into the Docker image. Standalone users can use this with launcher.py to get updates." Type="Variable" Display="always" Required="false" Mask="false"/>
|
| 49 |
-
<Config Name="Variable: APPEND" Target="APPEND" Default="False" Description="Will add the following at the end of a subtitle: 'Transcribed by whisperAI with faster-whisper ({whisper_model}) on {datetime.now()}'" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 50 |
-
<Config Name="Variable: MONITOR" Target="MONITOR" Default="False" Description="Will monitor TRANSCRIBE_FOLDERS for real-time changes to see if we need to generate subtitles" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 51 |
-
<Config Name="Variable: USE_MODEL_PROMPT" Target="USE_MODEL_PROMPT" Default="False" Description="When set to True, will use the default prompt stored in greetings_translations 'Hello, welcome to my lecture.' to try and force the use of punctuation in transcriptions that don't." Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 52 |
-
<Config Name="Variable: CUSTOM_MODEL_PROMPT" Target="CUSTOM_MODEL_PROMPT" Default="" Description="If USE_MODEL_PROMPT is True, you can override the default prompt (See: https://medium.com/axinc-ai/prompt-engineering-in-whisper-6bb18003562d for great examples)." Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 53 |
-
<Config Name="Variable: LRC_FOR_AUDIO_FILES" Target="LRC_FOR_AUDIO_FILES" Default="True" Description="Will generate LRC (instead of SRT) files for filetypes: '.mp3', '.flac', '.wav', '.alac', '.ape', '.ogg', '.wma', '.m4a', '.m4b', '.aac', '.aiff'" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 54 |
-
<Config Name="Variable: CUSTOM_REGROUP" Target="CUSTOM_REGROUP" Default="cm_sl=84_sl=42++++++1" Description="Attempts to regroup some of the segments to make a cleaner looking subtitle. See Issue #68 for discussion. Set to blank if you want to use Stable-TS default regroups algorithm of cm_sp=,* /,_sg=.5_mg=.3+3_sp=.* /。/?/?'" Type="Variable" Display="advanced" Required="false" Mask="false"/>
|
| 55 |
-
|
| 56 |
-
</Container>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|