Compare commits

12 Commits
21 changed files with 1146 additions and 299 deletions
+3
View File
@@ -6,3 +6,6 @@ save
__pycache__ __pycache__
twitch_secrets.json twitch_secrets.json
.vscode/settings.json .vscode/settings.json
download
output.log
*.mp4
+127
View File
@@ -0,0 +1,127 @@
#!/usr/bin/env python3
import database
import youtube_short
import transcribe_video
import twitch_chat_vod
DB = None
def build_chat_video():
chats = DB.get_unuploaded_chats()
for chat in chats:
(
id,
title,
created_at,
view_count,
duration,
url,
thumbnail_url,
game_id,
game_name,
stream_id,
creator_name,
clip_is,
downloaded,
uploaded_yt,
uploaded_yt_chats,
uploaded_yt_shorts,
) = chat
print("====================================================")
print(f"🚀 Chat Video: {title}")
print("====================================================")
twitch_chat_vod.combine_twitch_vod_and_chat(f"download/videos/{id}/{id}.mp4", "side-by-side")
print("====================================================")
print(f"✅ Processing: Chat Video Done.")
print("====================================================")
def build_transcribe():
unuploadeds = DB.get_unuploaded()
for unuploaded in unuploadeds:
(
id,
title,
created_at,
view_count,
duration,
url,
thumbnail_url,
game_id,
game_name,
stream_id,
creator_name,
clip_is,
downloaded,
uploaded_yt,
uploaded_yt_chats,
uploaded_yt_shorts,
) = unuploaded
if clip_is:
target_dir = f"download/clips"
else:
target_dir = f"download/videos"
print("====================================================")
print(f"🚀 Transcribe: {title}")
print("====================================================")
transcribe_video.transcribe_to_srt(f"{target_dir}/{id}/{id}.mp4")
print("====================================================")
print(f"✅ Processing: Transcribing Done.")
print("====================================================")
def build_shorts():
shorts = DB.get_unuploaded_shorts()
top_txt = "TeamPGP Live on Twitch...\n Every Friday and Sunday @7:30 ET.\n twitch.tv/teampgp"
for short in shorts:
(
id,
title,
created_at,
view_count,
duration,
url,
thumbnail_url,
game_id,
game_name,
stream_id,
creator_name,
clip_is,
downloaded,
uploaded_yt,
uploaded_yt_chats,
uploaded_yt_shorts,
) = short
target_dir = f"download/clips/{id}/{id}.mp4"
print("====================================================")
print(f"🚀 Processing: Clip to Youtube Short {title}")
print("====================================================")
youtube_short.fit_to_9_16_letterbox(target_dir, top_txt, f"Clipped By: {creator_name}.")
print("====================================================")
print(f"✅ Processing: Clip to Youtube Short Done.")
print("====================================================")
def main():
global DB
DB = database.Database()
build_shorts()
build_transcribe()
build_chat_video()
if __name__ == "__main__":
main()
+45 -15
View File
@@ -5,19 +5,18 @@ from pathlib import Path
from typing import Any from typing import Any
class Database: class Database:
def __init__(self): def __init__(self, db_path: str = "database.db"):
self.columns = "id, title, created_at, view_count, duration, url, thumbnail_url, game_id, game_name, stream_id, creator_name, clip_is, downloaded, uploaded_yt, uploaded_yt_chats, uploaded_yt_shorts" self.columns = "id, title, created_at, view_count, duration, url, thumbnail_url, game_id, game_name, stream_id, creator_name, clip_is, downloaded, uploaded_yt, uploaded_yt_chats, uploaded_yt_shorts"
# IMPORTANT Must check if database.db exists before connecting to it. file_exists = Path(db_path).is_file()
file_exists = Path("database.db").is_file()
self.conn = sqlite3.connect("database.db") self.conn = sqlite3.connect(db_path)
self.cursor = self.conn.cursor() self.cursor = self.conn.cursor()
if not file_exists: if not file_exists:
self.create_database() self.create_database()
def __del__(self): def __exit__(self):
# Destructors are unpredictable in Python; explicitly close when done instead # Destructors are unpredictable in Python; explicitly close when done instead
try: try:
self.close_database() self.close_database()
@@ -35,7 +34,7 @@ class Database:
duration TEXT NOT NULL, duration TEXT NOT NULL,
url TEXT NOT NULL, url TEXT NOT NULL,
thumbnail_url TEXT NOT NULL, thumbnail_url TEXT NOT NULL,
game_id INTEGER NOT NULL, game_id TEXT NOT NULL,
game_name TEXT NOT NULL, game_name TEXT NOT NULL,
stream_id TEXT NOT NULL, stream_id TEXT NOT NULL,
creator_name TEXT NOT NULL, creator_name TEXT NOT NULL,
@@ -55,10 +54,10 @@ class Database:
self.conn.commit() self.conn.commit()
self.conn.close() self.conn.close()
def __mark_as(self, record_id: str, set_sql: str): def __mark_as(self, record_id: str, set_row: str, mark: str = "1" ):
self.cursor.execute( self.cursor.execute(
f"UPDATE twitch_videos SET {set_sql} = 1 WHERE id = ?", f"UPDATE twitch_videos SET {set_row} = ? WHERE id = ?",
(record_id,)) (mark, record_id))
self.conn.commit() self.conn.commit()
def mark_as_uploaded_shorts(self, record_id: str): def mark_as_uploaded_shorts(self, record_id: str):
@@ -77,18 +76,22 @@ class Database:
"""Flags a specific row record to downloaded (1).""" """Flags a specific row record to downloaded (1)."""
self.__mark_as(record_id, "downloaded") self.__mark_as(record_id, "downloaded")
def __get_unuploaded(self, set_sql: str) -> list[Any]: def unmark_as_download(self, record_id: str):
"""Flags a specific row record to downloaded (0)."""
self.__mark_as(record_id, "downloaded", "0")
def __get_unuploaded(self, set_row: str, also: str = "") -> list[Any]:
"""Retrieve all rows that were download but not uploaded""" """Retrieve all rows that were download but not uploaded"""
self.cursor.execute(f"SELECT {self.columns} FROM twitch_videos WHERE downloaded = 1 AND {set_sql} = 0") self.cursor.execute(f"SELECT {self.columns} FROM twitch_videos WHERE downloaded = 1 AND {set_row} = 0 {also}")
return self.cursor.fetchall() return self.cursor.fetchall()
def get_unuploaded_shorts(self) -> list[Any]: def get_unuploaded_shorts(self) -> list[Any]:
"""Retrieve all rows that were download but not uploaded_yt_shorts""" """Retrieve all rows that were download but not uploaded_yt_shorts"""
return self.__get_unuploaded("uploaded_yt_shorts") return self.__get_unuploaded("uploaded_yt_shorts", "AND clip_is = 1")
def get_unuploaded_chats(self) -> list[Any]: def get_unuploaded_chats(self) -> list[Any]:
"""Retrieve all rows that were download but not uploaded_yt_chats""" """Retrieve all rows that were download but not uploaded_yt_chats"""
return self.__get_unuploaded("uploaded_yt_chats") return self.__get_unuploaded("uploaded_yt_chats", "AND clip_is = 0")
def get_unuploaded(self) -> list[Any]: def get_unuploaded(self) -> list[Any]:
"""Retrieve all rows that were download but not uploaded_yt""" """Retrieve all rows that were download but not uploaded_yt"""
@@ -99,6 +102,20 @@ class Database:
self.cursor.execute(f"SELECT {self.columns} FROM twitch_videos WHERE downloaded = 0") self.cursor.execute(f"SELECT {self.columns} FROM twitch_videos WHERE downloaded = 0")
return self.cursor.fetchall() return self.cursor.fetchall()
def get_download(self) -> list[Any]:
"""Retrieves all rows that were downloaded"""
self.cursor.execute(f"SELECT {self.columns} FROM twitch_videos WHERE downloaded = 1")
return self.cursor.fetchall()
def get_clips(self) -> list[Any]:
"""Retrieves all rows that are clip_is"""
self.cursor.execute(f"SELECT {self.columns} FROM twitch_videos WHERE clip_is = 1")
return self.cursor.fetchall()
def get_vods(self) -> list[Any]:
self.cursor.execute(f"SELECT {self.columns} FROM twitch_videos WHERE clip_is = 0")
return self.cursor.fetchall()
def insert_video_record( def insert_video_record(
self, id: str, title: str, created_at: str, view_count: int, duration: str, self, id: str, title: str, created_at: str, view_count: int, duration: str,
url: str, thumbnail_url: str, game_id: int, game_name: str, stream_id: str, url: str, thumbnail_url: str, game_id: int, game_name: str, stream_id: str,
@@ -112,10 +129,23 @@ class Database:
# Explicitly defining columns removes the security risk and column-count bug # Explicitly defining columns removes the security risk and column-count bug
query = """ query = """
INSERT OR IGNORE INTO twitch_videos ( INSERT INTO twitch_videos (
id, title, created_at, view_count, duration, url, thumbnail_url, id, title, created_at, view_count, duration, url, thumbnail_url,
game_id, game_name, stream_id, creator_name, clip_is game_id, game_name, stream_id, creator_name, clip_is
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
title = excluded.title,
created_at = excluded.created_at,
view_count = excluded.view_count,
duration = excluded.duration,
url = excluded.url,
thumbnail_url = excluded.thumbnail_url,
game_id = excluded.game_id,
game_name = excluded.game_name,
stream_id = excluded.stream_id,
creator_name = excluded.creator_name,
clip_is = excluded.clip_is
WHERE excluded.duration != twitch_videos.duration
""" """
values = ( values = (
@@ -128,5 +158,5 @@ class Database:
self.conn.commit() self.conn.commit()
except Exception as e: except Exception as e:
# Prevent silent failures if the database connection drops # Prevent silent failures if the database connection drops
print(f"Database insertion failed: {e}") print(f"Database insertion failed: {e}")
self.conn.rollback() self.conn.rollback()
+6 -6
View File
@@ -38,8 +38,8 @@ def get_my_uploads_playlist_id(youtube):
request = youtube.channels().list(part="contentDetails", mine=True) request = youtube.channels().list(part="contentDetails", mine=True)
response = request.execute() response = request.execute()
if "items" in response and len(response["items"]) > 0: if "items" in response and len(response['items']) > 0:
return response["items"][0]["contentDetails"]["relatedPlaylists"]["uploads"] return response['items'][0]['contentDetails']['relatedPlaylists']['uploads']
else: else:
print("No channel found for these credentials.") print("No channel found for these credentials.")
return None return None
@@ -70,7 +70,7 @@ def scan_channel_videos_for_tag(youtube, uploads_playlist_id: str, target_tag: s
playlist_response = playlist_request.execute() playlist_response = playlist_request.execute()
video_ids_batch = [ video_ids_batch = [
item["snippet"]["resourceId"]["videoId"] item['snippet']['resourceId']['videoId']
for item in playlist_response.get("items", []) for item in playlist_response.get("items", [])
] ]
@@ -87,10 +87,10 @@ def scan_channel_videos_for_tag(youtube, uploads_playlist_id: str, target_tag: s
video_response = video_request.execute() video_response = video_request.execute()
for video in video_response.get("items", []): for video in video_response.get("items", []):
title = video["snippet"]["title"] title = video['snippet']['title']
video_id = video["id"] video_id = video['id']
# Tags are optional fields on YouTube; default to an empty list if absent # Tags are optional fields on YouTube; default to an empty list if absent
tags = video["snippet"].get("tags", []) tags = video['snippet'].get("tags", [])
# Normalize tags to lowercase for clean matching evaluation # Normalize tags to lowercase for clean matching evaluation
tags_lower = [tag.lower() for tag in tags] tags_lower = [tag.lower() for tag in tags]
+51 -12
View File
@@ -1,17 +1,56 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys
import shlex
import subprocess import subprocess
def run_command(command: str): def run_command(cmd_str: str, progress_prefix: str = "Progress", look_for: list = ["frame=", "time=", "fps=", "Rendering frame"]) -> bool:
"""Executes a Linux command, waits for completion, and returns output.""" """Runs a system command, streams its output live, and reports errors on failure."""
try: args = shlex.split(cmd_str)
# shell=True allows running full command strings with pipes/wildcards
# text=True returns strings instead of bytes
result = subprocess.run(
command, shell=True, check=True, capture_output=True, text=True
)
return {"success": True, "stdout": result.stdout, "stderr": result.stderr} # Redirect stderr to stdout to catch all logging/progress in one stream
process = subprocess.Popen(
args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
print(f"Executing: {cmd_str[:90]}...")
except subprocess.CalledProcessError as e: # Maintain a small buffer history to display context if a crash occurs
# Handles errors if the Linux command returns a non-zero exit code output_history = []
return {"success": False, "stdout": e.stdout, "stderr": e.stderr}
# Stream the output live to the terminal
while True:
line = process.stdout.readline()
if not line and process.poll() is not None:
break
if line:
clean_line = line.strip()
output_history.append(clean_line) # Keep history for error reporting
# Keep history slim by only keeping the last 20 lines
if len(output_history) > 20:
output_history.pop(0)
# Only print updates that show progress metrics to keep terminal clean
if any(metric in clean_line for metric in look_for):
sys.stdout.write(f"\r[{progress_prefix}] {clean_line}")
sys.stdout.flush()
elif "Error" in clean_line or "failed" in clean_line:
print(f"\n[Alert] {clean_line}")
print("\n") # New line after process finishes
# Evaluate success status
success = (process.returncode == 0)
if not success:
print(f"❌ Command failed with exit code: {process.returncode}")
print("--- Technical Error Details (Last 5 lines of output) ---")
# Print the last 5 captured lines to show the exact point of failure
for error_line in output_history[-5:]:
print(f" > {error_line}")
print("---------------------------------------------------------")
return success
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env python3
import asyncio
import twitch_video_info
import twitch_download_videos
import twitch_download_thumbnails
import build_videos
if __name__ == "__main__":
# Get Twitch Video Info
asyncio.run(twitch_video_info.main())
# Download Twitch Videos
twitch_download_videos.main()
twitch_download_thumbnails.main()
#Build
build_videos.main()
+67
View File
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
import sqlite3
# Connect to your database file
db_name = "database.db" # Change to your actual file name
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
try:
# 1. Create a temporary table with the new text-based game_id
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS twitch_videos_temp (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
created_at TEXT NOT NULL,
view_count INTEGER NOT NULL,
duration TEXT NOT NULL,
url TEXT NOT NULL,
thumbnail_url TEXT NOT NULL,
game_id TEXT NOT NULL,
game_name TEXT NOT NULL,
stream_id TEXT NOT NULL,
creator_name TEXT NOT NULL,
clip_is BOOLEAN NOT NULL DEFAULT 0,
downloaded BOOLEAN NOT NULL DEFAULT 0,
uploaded_yt BOOLEAN NOT NULL DEFAULT 0,
uploaded_yt_chats BOOLEAN NOT NULL DEFAULT 0,
uploaded_yt_shorts BOOLEAN NOT NULL DEFAULT 0
)
"""
)
# 2. Copy and convert data to the temporary table
cursor.execute(
"""
INSERT INTO twitch_videos_temp (
id, title, created_at, view_count, duration, url, thumbnail_url,
game_id, game_name, stream_id, creator_name, clip_is, downloaded,
uploaded_yt, uploaded_yt_chats, uploaded_yt_shorts
)
SELECT
id, title, created_at, view_count, duration, url, thumbnail_url,
CAST(game_id AS TEXT), game_name, stream_id, creator_name, clip_is, downloaded,
uploaded_yt, uploaded_yt_chats, uploaded_yt_shorts
FROM twitch_videos
"""
)
# 3. Drop the old table configuration
cursor.execute("DROP TABLE twitch_videos")
# 4. Rename the temporary table to your exact original table name
cursor.execute("ALTER TABLE twitch_videos_temp RENAME TO twitch_videos")
# Commit changes if everything succeeded
conn.commit()
print("Migration successful! 'twitch_videos' table updated.")
except sqlite3.Error as e:
# Roll back changes if an error occurs
conn.rollback()
print(f"An error occurred: {e}")
finally:
# Close database connection
conn.close()
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
import database
DB = None
def redownload_clips():
clips = DB.get_clips()
for clip in clips:
# Unpack variables clearly
(
id,
title,
created_at,
view_count,
duration,
url,
thumbnail_url,
game_id,
game_name,
stream_id,
creator_name,
clip_is,
downloaded,
uploaded_yt,
uploaded_yt_chats,
uploaded_yt_shorts,
) = clip
DB.unmark_as_download(id)
if __name__ == "__main__":
DB = database.Database()
redownload_clips()
+72
View File
@@ -0,0 +1,72 @@
import os
import database
DB = None
def remove_files():
datas = DB.get_download()
top_txt = "TeamPGP Live on Twitch...\n Every Friday and Sunday @7:30 ET.\n twitch.tv/teampgp"
for data in datas:
(
id,
title,
created_at,
view_count,
duration,
url,
thumbnail_url,
game_id,
game_name,
stream_id,
creator_name,
clip_is,
downloaded,
uploaded_yt,
uploaded_yt_chats,
uploaded_yt_shorts,
) = data
if clip_is == 0:
video_path = f"download/videos/{id}/{id}.mp4"
else:
video_path = f"download/clips/{id}/{id}.mp4"
temp_chat = video_path.replace(".mp4", "_temp_chat.mp4")
temp_with_chat = video_path.replace(".mp4", "_temp_with_chat.mp4")
video_chat = video_path.replace(".mp4", "_with_chat.mp4")
video_chatSS = video_path.replace(".mp4", "_SS_with_chat.mp4")
video_chat_over = video_path.replace(".mp4", "_over_with_chat.mp4")
mask_path = video_path.replace(".mp4", "_temp_chat_mask.mp4")
shorts_gaussian_9_16 = ""#video_path.replace(".mp4", "_gaussian_9_16.mp4")
shorts_9_16 = ""#video_path.replace(".mp4", "_9_16.mp4")
srt_file = ""#video_path.replace(".mp4", ".srt")
files = [temp_chat, temp_with_chat, video_chatSS, video_chat_over, mask_path, shorts_gaussian_9_16, shorts_9_16, srt_file, video_chat]
for file in files:
if os.path.exists(file):
if file == video_path:
print("❌ Error: trying to delete import file.")
return
else:
os.remove(file)
def redownload():
DB.unmark_as_download("2840110867")
def main():
global DB
DB = database.Database()
remove_files()
redownload()
if __name__ == "__main__":
main()
+41 -28
View File
@@ -1,38 +1,51 @@
aiohappyeyeballs==2.7.1
aiohttp==3.14.2
aiosignal==1.4.0
anyio==4.14.2
attrs==26.1.0 attrs==26.1.0
beautifulsoup4==4.14.3 beautifulsoup4==4.15.0
certifi==2026.2.25 certifi==2026.7.22
cffi==2.0.0 cffi==2.1.0
chardet==5.2.0 charset-normalizer==3.4.9
charset-normalizer==3.4.7 chat-downloader==0.2.8
click==8.4.2 click==8.4.2
colorama==0.4.6 colorlog==6.12.0
cryptography==49.0.0 cryptography==49.0.0
cuda-bindings==13.3.1 cuda-bindings==13.3.1
cuda-pathfinder==1.5.6 cuda-pathfinder==1.6.0
cuda-toolkit==13.0.3.0 cuda-toolkit==13.0.3.0
decorator==5.3.1 decorator==5.3.1
defusedxml==0.7.1 defusedxml==0.7.1
docopt==0.6.2 docstring_parser==0.18.0
filelock==3.31.0 enum-tools==0.13.0
filelock==3.32.0
frozenlist==1.8.0
fsspec==2026.6.0 fsspec==2026.6.0
google-api-core==2.31.0 git-filter-repo==2.47.0
google==3.0.0
google-api-core==2.32.0
google-api-python-client==2.198.0 google-api-python-client==2.198.0
google-auth==2.56.0 google-auth==2.56.2
google-auth-httplib2==0.4.0 google-auth-httplib2==0.4.0
google-auth-oauthlib==1.4.0 google-auth-oauthlib==1.4.0
googleapis-common-protos==1.75.0 googleapis-common-protos==1.75.0
h11==0.16.0 h11==0.16.0
httpcore==1.0.9
httplib2==0.32.0 httplib2==0.32.0
idna==3.11 httpx==0.28.1
ImageIO==2.37.3 idna==3.18
ImageIO==2.37.4
imageio-ffmpeg==0.6.0 imageio-ffmpeg==0.6.0
isodate==0.7.2
Jinja2==3.1.6 Jinja2==3.1.6
joblib==1.5.3 joblib==1.5.3
llvmlite==0.48.0 llvmlite==0.48.0
lxml==6.1.1
MarkupSafe==3.0.3 MarkupSafe==3.0.3
more-itertools==11.1.0 more-itertools==11.1.0
moviepy==2.2.1 moviepy==2.2.1
mpmath==1.3.0 mpmath==1.3.0
multidict==6.7.1
networkx==3.6.1 networkx==3.6.1
nltk==3.10.0 nltk==3.10.0
numba==0.66.0 numba==0.66.0
@@ -55,42 +68,42 @@ nvidia-nvtx==13.0.85
oauthlib==3.3.1 oauthlib==3.3.1
openai-whisper==20250625 openai-whisper==20250625
outcome==1.3.0.post0 outcome==1.3.0.post0
packaging==26.2
pillow==11.3.0 pillow==11.3.0
pipreqs==0.4.13 pip_system_certs==5.3
proglog==0.1.12 proglog==0.1.12
propcache==0.5.2
proto-plus==1.28.1 proto-plus==1.28.1
protobuf==7.35.1 protobuf==7.35.1
pyasn1==0.6.4 pyasn1==0.6.4
pyasn1_modules==0.4.2 pyasn1_modules==0.4.2
pycountry==26.2.16
pycparser==3.0 pycparser==3.0
pycryptodome==3.23.0
Pygments==2.20.0
pyparsing==3.3.2 pyparsing==3.3.2
PySocks==1.7.1 PySocks==1.7.1
python-apt==3.0.0 python-dateutil==2.9.0.post0
python-debian==1.0.1
python-debianbts==4.1.1
python-dotenv==1.2.2 python-dotenv==1.2.2
regex==2026.7.19 regex==2026.7.19
reportbug==13.2.0 requests==2.34.2
requests==2.33.1
requests-oauthlib==2.0.0 requests-oauthlib==2.0.0
selenium==4.43.0
setuptools==83.0.0 setuptools==83.0.0
six==1.17.0
sniffio==1.3.1 sniffio==1.3.1
sortedcontainers==2.4.0 sortedcontainers==2.4.0
soupsieve==2.8.3 soupsieve==2.9.1
streamlink==8.4.0
sympy==1.14.0 sympy==1.14.0
tiktoken==0.13.0 tiktoken==0.13.0
torch==2.13.0 torch==2.13.0
tqdm==4.67.3 tqdm==4.69.0
trio==0.33.0 trio==0.33.0
trio-websocket==0.12.2 trio-websocket==0.12.2
triton==3.7.1 triton==3.7.1
typing_extensions==4.15.0 twitchAPI==4.5.0
typing_extensions==4.16.0
uritemplate==4.2.0 uritemplate==4.2.0
urllib3==2.6.3 urllib3==2.7.0
webdriver-manager==4.0.2
websocket-client==1.9.0 websocket-client==1.9.0
wheel==0.46.1
wsproto==1.3.2 wsproto==1.3.2
yarg==0.1.10 yarl==1.24.5
+107 -49
View File
@@ -1,61 +1,119 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import sys
import shutil
import subprocess import subprocess
import whisper from pathlib import Path
import linux from faster_whisper import WhisperModel
from moviepy import VideoFileClip from faster_whisper.utils import format_timestamp
from whisper.utils import get_writer
model = whisper.load_model("base") # Prevent OpenMP thread conflicts from crashing the script
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
def extract_audio(video_path: str, audio_temp_path: str): # 5 minutes per chunk (300 seconds) keeps RAM usage low and stable
# Create a temporary path for a sanitized copy of the video CHUNK_DURATION_SEC = 300
sanitized_video_path = video_path.replace(".mp4", "_clean.mp4")
print("🔧 Initializing C++ Engine...")
model = WhisperModel(
"base",
device="cpu",
compute_type="int8",
cpu_threads=0, # Let CTranslate2 auto-detect safe core counts
num_workers=1
)
print("✅ C++ Model loaded successfully.")
def extract_audio_and_chunk(video_path: str, output_dir: Path) -> list:
"""Extracts and splits audio into 5-minute chunks using a single FFmpeg pass."""
print("🚀 Extracting and chunking audio with FFmpeg...")
output_dir.mkdir(parents=True, exist_ok=True)
print("Sanitizing video metadata for MoviePy parser...") # Segment format output: chunk_000.wav, chunk_001.wav, etc.
# -map_chapters -1 removes chapter layouts that break the parser. chunk_pattern = str(output_dir / "chunk_%03d.wav")
# -sn strips text/subtitle streams that crash MoviePy.
# -c copy copies video and audio instantly without quality loss.
linux.run_command(f"ffmpeg -y -i {video_path} -map_chapters -1 -sn -c copy {sanitized_video_path}")
print("Extracting uncompressed WAV audio...") command = [
"ffmpeg", "-y", "-i", video_path,
"-vn", "-ac", "1", "-ar", "16000",
"-acodec", "pcm_s16le", "-sn", "-map_chapters", "-1",
"-f", "segment", "-segment_time", str(CHUNK_DURATION_SEC),
chunk_pattern
]
result = subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
if result.returncode != 0:
print(f"❌ FFmpeg Error Output:\n{result.stderr}")
raise RuntimeError("FFmpeg extraction and chunking failed.")
# Return sorted list of generated chunk files
return sorted(list(output_dir.glob("chunk_*.wav")))
def transcribe_to_srt(video_path: str, force: bool = False):
video_path_obj = Path(video_path)
srt_path = video_path_obj.with_suffix(".srt")
temp_dir = video_path_obj.parent / f"temp_chunks_{video_path_obj.stem}"
if srt_path.exists():
if not force:
print(f"❌ Error: Transcription file already exists: {srt_path}")
return
else:
srt_path.unlink()
try: try:
# Load the sanitized file instead of the raw Twitch clip # Step 1: Split audio into bite-sized pieces
with VideoFileClip(sanitized_video_path) as video: audio_chunks = extract_audio_and_chunk(str(video_path_obj), temp_dir)
video.audio.write_audiofile( if not audio_chunks:
audio_temp_path, print("❌ Error: No audio chunks were generated.")
fps=16000, return
codec="pcm_s16le",
ffmpeg_params=["-ac", "1"] print(f"📦 Successfully split audio into {len(audio_chunks)} chunks.")
) print("🎙️ Starting safe chunk-by-chunk transcription...")
global_segment_index = 1
with open(srt_path, "w", encoding="utf-8") as srt_file:
for chunk_idx, chunk_path in enumerate(audio_chunks):
# Calculate the time offset for the current chunk
time_offset = chunk_idx * CHUNK_DURATION_SEC
print(f"\n⏳ Processing chunk {chunk_idx + 1}/{len(audio_chunks)} ({chunk_path.name})...")
segments_generator, info = model.transcribe(
str(chunk_path),
beam_size=1,
vad_filter=True,
temperature=0.0
)
# Consume chunk generator and shift timestamps instantly
for segment in segments_generator:
# Shift timestamps relative to the original video timeline
actual_start = segment.start + time_offset
actual_end = segment.end + time_offset
start_str = format_timestamp(actual_start, always_include_hours=True)
end_str = format_timestamp(actual_end, always_include_hours=True)
srt_file.write(f"{global_segment_index}\n{start_str} --> {end_str}\n{segment.text.strip()}\n\n")
global_segment_index += 1
# Free up space as we go by deleting the processed chunk
chunk_path.unlink()
print(f"\n✅ All chunks combined! SRT subtitle file saved in: {srt_path}")
except Exception as e:
print(f"\n❌ Execution Error: {e}")
finally: finally:
# Always clean up the temporary sanitized video on Windows 11 # Clean up the temporary folder entirely
if os.path.exists(sanitized_video_path): if temp_dir.exists():
os.remove(sanitized_video_path) shutil.rmtree(temp_dir)
def transcribe_to_srt(audio_path: str, output_directory: str, output_filename: str):
print("Transcribing audio...")
result = model.transcribe(audio_path)
print("Creating SRT file...")
srt_writer = get_writer("srt", output_directory)
srt_writer(result, output_filename, {})
print(f"SRT subtitle file saved in: {output_directory}")
if os.path.exists(audio_path):
os.remove(audio_path)
if __name__ == "__main__": if __name__ == "__main__":
video_path = "my_video.mp4" target_video = "download/videos/2813112936/2813112936.mp4"
audio_temp_path = "temp_audio.wav" # Changed extension to .wav if not os.path.exists(target_video):
print(f"❌ System Error: Target video file does not exist at path: {target_video}")
output_dir = os.getcwd() else:
output_prefix = "my_video_subtitles" transcribe_to_srt(target_video, force=True)
extract_audio(video_path, audio_temp_path)
transcribe_to_srt(audio_temp_path, output_dir, output_prefix)
if os.path.exists(audio_temp_path):
os.remove(audio_temp_path)
+150
View File
@@ -0,0 +1,150 @@
#!/usr/bin/env python3
import os
import cv2
import linux
def get_video_height(video_path: str) -> int:
# Open the video file
video = cv2.VideoCapture(video_path)
# Get the height property
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Always release the video object
video.release()
return height
def combine_twitch_vod_and_chat(vod_path: str, layout: str = "side-by-side", force: bool = False) -> bool:
"""
Renders Twitch chat JSON to video and combines it with the source VOD.
Provides real-time terminal feedback for all processing steps.
"""
threads = 8
video_height = 1080
video_path = vod_path
video_chat = ""
chat_path = video_path.replace(".mp4", "_chat.json")
temp_with_chat = video_path.replace(".mp4", "_temp_with_chat.mp4")
temp_chat = video_path.replace(".mp4", "_temp_chat.mp4")
video_chat_SS = video_path.replace(".mp4", "_SS_with_chat.mp4")
video_chat_over = video_path.replace(".mp4", "_over_with_chat.mp4")
mask_path = video_path.replace(".mp4", "_temp_chat_mask.mp4")
# Step 1: Pre-flight checks
if not os.path.exists(video_path):
print(f"❌ Error: Source video not found: {video_path}")
return False
if not os.path.exists(chat_path):
print(f"❌ Error: Chat file not found: {chat_path}")
return False
if os.path.exists(temp_with_chat):
os.remove(temp_with_chat)
if layout == "side-by-side":
video_chat = video_chat_SS
elif layout == "overlay":
video_chat = video_chat_over
else:
return False
if os.path.exists(video_chat):
if not force:
print(f"❌ Error: Chat video already exists: {video_chat}")
return False
else:
os.remove(video_chat)
if os.path.exists(temp_chat):
os.remove(temp_chat)
if os.path.exists(mask_path):
os.remove(mask_path)
# Step 2: Render Chat to Video
print("====================================================")
print("🚀 STEP 1: Rendering Chat JSON to Video Layer")
print("====================================================")
video_height = get_video_height(vod_path)
chat_cmd = (
f"TwitchDownloaderCLI chatrender "
f"-i {chat_path} "
f"-w 400 -h {video_height} "
f"--collision Overwrite "
f"--temp-path download/temp "
f"--font-size 20 "
f"--background-color #00000000 "
f"-o {temp_chat}"
)
if layout == "overlay":
chat_cmd = (f"{chat_cmd} --generate-mask ")
if not os.path.exists(temp_chat):
chat_success = linux.run_command(chat_cmd, look_for=["[STATUS]"])
if chat_success:
print("✅ Success: TwitchDownloaderCLI render chat video.")
else:
print("❌ Error: TwitchDownloaderCLI failed to render chat video.")
os.remove(temp_chat)
os.remove(mask_path)
return False
# Step 3: Combine Video and Chat using FFmpeg
print("====================================================")
print(f"🚀 STEP 2: Merging VOD and Chat Layout ({layout})")
print("====================================================")
# -preset superfast speeds up the 3+ hour encoding process significantly
# -map 0:a? safely includes audio if it exists, without breaking on silent VODs
if layout == "side-by-side":
ffmpeg_cmd = (
f"ffmpeg -y -i {video_path} -i {temp_chat} "
f"-filter_complex '[1:v]scale=-1:ih[scaled_chat];[0:v][scaled_chat]hstack=inputs=2[v]' "
f"-map '[v]' -map 0:a? -c:v libx264 -crf 18 -preset slow -c:a copy "
f"-threads {threads} {temp_with_chat}"
)
elif layout == "overlay":
ffmpeg_cmd = (
f"ffmpeg -y -i {video_path} -i {temp_chat} -i {mask_path} "
f"-filter_complex '[1:v][2:v]alphamerge[masked_chat];[0:v][masked_chat]overlay=x=0:y=10[v]' "
f"-map '[v]' -map 0:a? -c:v libx264 -crf 18 -preset slow -c:a copy "
f"-threads {threads} {temp_with_chat}"
)
else:
raise ValueError("Invalid layout choice. Choose 'side-by-side' or 'overlay'.")
ffmpeg_success = linux.run_command(ffmpeg_cmd, progress_prefix="FFmpeg Merge")
# Step 4: Final verification and cleanup
if ffmpeg_success and os.path.exists(temp_with_chat):
os.rename(temp_with_chat, video_chat)
print("====================================================")
print(f"🎉 SUCCESS: Video processing complete!")
print(f"📁 Output Saved: {video_chat}")
print("====================================================")
# Clean up the massive temporary chat video to save storage space
if os.path.exists(temp_chat):
print("🧹 Cleaning up temporary chat render video...")
os.remove(temp_chat)
if os.path.exists(mask_path):
os.remove(mask_path)
if os.path.exists(temp_with_chat):
os.remove(temp_with_chat)
return True
else:
print("❌ Error: FFmpeg failed to merge the video streams.")
if os.path.exists(video_chat):
os.remove(video_chat)
if os.path.exists(temp_with_chat):
os.remove(temp_with_chat)
return False
if __name__ == "__main__":
combine_twitch_vod_and_chat("download/videos/2813112936/2813112936.mp4", "overlay", True)
+4 -4
View File
@@ -58,11 +58,11 @@ def download():
output = linux.run_command(f"TwitchDownloaderCLI videodownload --id {record_id} -o save/videos/{record_id}/{record_id}.mp4 --collision Overwrite") output = linux.run_command(f"TwitchDownloaderCLI videodownload --id {record_id} -o save/videos/{record_id}/{record_id}.mp4 --collision Overwrite")
output2 = linux.run_command(f"TwitchDownloaderCLI chatdownload --id {record_id} -o save/videos/{record_id}/{record_id}_chat.json -E --collision Overwrite") output2 = linux.run_command(f"TwitchDownloaderCLI chatdownload --id {record_id} -o save/videos/{record_id}/{record_id}_chat.json -E --collision Overwrite")
time.sleep(1) time.sleep(1)
if output["success"] is True: if output['success'] is True:
print(f"ID: {record_id} | Date: {record_date} | Game: {game_name} | Title: {title} | was successful") print(f"ID: {record_id} | Date: {record_date} | Game: {game_name} | Title: {title} | was successful")
DB.mark_as_downloaded(record_id) DB.mark_as_downloaded(record_id)
else: else:
print(f"ID: {record_id} | Download Process failed. {output["stdout"]}. Error: {output["stderr"]}") print(f"ID: {record_id} | Download Process failed. {output['stdout']}. Error: {output['stderr']}")
elif DB.table == "clips": elif DB.table == "clips":
for slug, record_date, title, game_name, clip_by, views, downloaded, uploaded_yt, uploaded_shorts_yt in undownloaded: for slug, record_date, title, game_name, clip_by, views, downloaded, uploaded_yt, uploaded_shorts_yt in undownloaded:
@@ -72,11 +72,11 @@ def download():
output = linux.run_command(f"TwitchDownloaderCLI clipdownload --id {slug} -o save/clips/{slug}/{slug}.mp4 --collision Overwrite") output = linux.run_command(f"TwitchDownloaderCLI clipdownload --id {slug} -o save/clips/{slug}/{slug}.mp4 --collision Overwrite")
time.sleep(1) time.sleep(1)
if output["success"] is True: if output['success'] is True:
print(f"Slug: {slug} | Was successfully downloaded.") print(f"Slug: {slug} | Was successfully downloaded.")
DB.mark_as_downloaded(slug) DB.mark_as_downloaded(slug)
else: else:
print(f"Slug: {slug} | Download Process failed. {output["stdout"]}. Error: {output["stderr"]}") print(f"Slug: {slug} | Download Process failed. {output['stdout']}. Error: {output['stderr']}")
print(f"Finished Downloading {DB.table}...") print(f"Finished Downloading {DB.table}...")
+3 -3
View File
@@ -66,11 +66,11 @@ def download_clips():
# Uses standard clipdownload directive # Uses standard clipdownload directive
output = run_linux_command(f"TwitchDownloaderCLI clipdownload --id {slug} -o save/clips/{slug}/{slug}.mp4") output = run_linux_command(f"TwitchDownloaderCLI clipdownload --id {slug} -o save/clips/{slug}/{slug}.mp4")
if output["success"] is True: if output['success'] is True:
print(f"Slug: {slug} | Was successfully downloaded.") print(f"Slug: {slug} | Was successfully downloaded.")
DB.mark_as_downloaded(slug) DB.mark_as_downloaded(slug)
else: else:
print(f"Slug: {slug} | Download Process failed. {output["stdout"]}. Error: {output["stderr"]}") print(f"Slug: {slug} | Download Process failed. {output['stdout']}. Error: {output['stderr']}")
print("Finished Downloading Clips...") print("Finished Downloading Clips...")
@@ -94,7 +94,7 @@ def upload_clips():
tags.extend(top_hashtags({slug})) tags.extend(top_hashtags({slug}))
#output = uploader.upload_video(file_path, title, categoryId, description, privatcyStatus, tags) output = uploader.upload_video(file_path, title, categoryId, description, privatcyStatus, tags)
if output is True: if output is True:
print(f"Slug: {slug} | Was successfully uploaded.") print(f"Slug: {slug} | Was successfully uploaded.")
+162
View File
@@ -0,0 +1,162 @@
import os
import json
import asyncio
import requests
from twitchAPI.twitch import Twitch
from twitchAPI.helper import first
import database
# 1. Fill in your credentials from the Twitch Developer Console
SECRETS = None
TWITCH = None
USER = None
CHANNEL_NAME = "teampgp"
async def get_twitch():
global SECRETS, TWITCH, USER
if SECRETS is None:
with open('twitch_secrets.json', 'r') as f:
SECRETS = json.load(f)
if TWITCH is None:
TWITCH = await Twitch(SECRETS['client_id'], SECRETS['client_secret'])
if USER is None:
USER = await first(TWITCH.get_users(logins=[CHANNEL_NAME]))
if not USER:
print("User not found.")
async def download_live_thumbnail(twitch_client, streamer_username: str, w: int = 1920, h: int = 1080):
"""Fetches and saves the live stream thumbnail for an active broadcast."""
print(f"🔎 Checking live status for: {streamer_username}...")
# Query the live streams endpoint
stream_generator = twitch_client.get_streams(user_logins=[streamer_username])
stream_data = await first(stream_generator)
if not stream_data:
print(f"❌ User '{streamer_username}' is offline. Live thumbnails require an active stream.")
return
# Twitch API live streams use the {width} and {height} format
raw_url = stream_data.thumbnail_url
clean_url = raw_url.replace('{width}', str(w)).replace('{height}', str(h))
filename = f"live_{streamer_username}_{w}x{h}.jpg"
save_image(clean_url, filename)
async def download_vod_thumbnail(twitch_client, vod_id: str, w: int = 1920, h: int = 1080):
"""Fetches and saves a thumbnail from a past broadcast VOD ID."""
print(f"🔎 Searching for VOD ID: {vod_id}...")
# Query the videos endpoint
video_generator = twitch_client.get_videos(vod_id)
video_data = await first(video_generator)
filename = f"download/videos/{vod_id}/{vod_id}_{w}x{h}.jpg"
if os.path.exists(filename):
return
if not video_data:
print(f"❌ VOD ID {vod_id} could not be found.")
return
# Twitch VOD endpoints typically format string tokens as %{width} and %{height}
raw_url = video_data.thumbnail_url
if not raw_url:
print("❌ This VOD does not have an available thumbnail.")
return
clean_url = raw_url.replace('%{width}', str(w)).replace('%{height}', str(h))
#filename = f"vod_{vod_id}_{w}x{h}.jpg"
save_image(clean_url, filename)
async def download_clip_thumbnail(clip_id: str, url: str):
print(f"🔎 Searching for Clip ID: {clip_id}...")
filename = f"download/clips/{clip_id}/{clip_id}.jpg"
if os.path.exists(filename):
return
save_image(url, filename, False)
def save_image(url: str, filename: str, stream: bool = True):
"""Helper function to stream image bytes directly to a file."""
try:
response = requests.get(url, stream)
if response.status_code == 200:
with open(filename, 'wb') as file:
if stream is True:
for chunk in response.iter_content(1024):
file.write(chunk)
else:
file.write(response.content)
print(f"✅ Success! Saved as: {filename}")
else:
print(f"❌ Download failed. HTTP Status: {response.status_code}")
except Exception as e:
print(f"❌ An error occurred during file writing: {e}")
async def main():
# Initialize connection & automatically authorize the App token
await get_twitch()
twitch = TWITCH # Twitch(APP_ID, APP_SECRET)
# --- OPTION A: Download Live Thumbnail ---
# Target user must be streaming live right now
target_streamer = CHANNEL_NAME
#await download_live_thumbnail(twitch, target_streamer, 1920, 1080)
# --- OPTION B: Download Past Broadcast VOD Thumbnail ---
# Extract the ID sequence from your target video link
# target_vod = "2145678901"
db = database.Database()
vods = db.get_vods()
for vod in vods:
(
id,
title,
created_at,
view_count,
duration,
url,
thumbnail_url,
game_id,
game_name,
stream_id,
creator_name,
clip_is,
downloaded,
uploaded_yt,
uploaded_yt_chats,
uploaded_yt_shorts,
) = vod
await download_vod_thumbnail(twitch, id, 1920, 1080)
clips = db.get_clips()
for clip in clips:
(
id,
title,
created_at,
view_count,
duration,
url,
thumbnail_url,
game_id,
game_name,
stream_id,
creator_name,
clip_is,
downloaded,
uploaded_yt,
uploaded_yt_chats,
uploaded_yt_shorts,
) = clip
await download_clip_thumbnail(id, thumbnail_url)
if __name__ == '__main__':
asyncio.run(main())
+81 -22
View File
@@ -1,8 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import requests
import os
import time
import csv import csv
import subprocess
from datetime import datetime, timedelta, timezone
import linux import linux
from database import Database from database import Database
@@ -19,31 +18,91 @@ def write_csv(data, file_name):
writer.writerows(data) writer.writerows(data)
def download(): def download():
"""Find all undownload videos and download them.""" """Find all undownloaded videos and clips, and download them safely."""
undownloaded = DB.get_undownloaded() undownloaded = DB.get_undownloaded()
print(f"Download...") print("Starting downloads...")
for id, title, created_at, view_count, duration, url, thumbnail_url, game_id, game_name, stream_id, creator_name, clip_is, downloaded, uploaded_yt, uploaded_yt_chats, uploaded_yt_shorts in undownloaded: for row in undownloaded:
data = [[id, title, created_at, view_count, duration, url, thumbnail_url, game_id, game_name, stream_id, creator_name, clip_is, downloaded, uploaded_yt, uploaded_yt_chats, uploaded_yt_shorts]] # Unpack variables clearly
print(f"ID: {id} | Date: {created_at} | Game: {game_name} | Title: {title}") (
if not clip_is: id,
output = linux.run_command(f"TwitchDownloaderCLI videodownload --id {id} -o download/videos/{id}/{id}.mp4 --collision Overwrite") title,
output2 = linux.run_command(f"TwitchDownloaderCLI chatdownload --id {id} -o download/videos/{id}/{id}_chat.json -E --collision Overwrite") created_at,
write_csv(data, f"download/videos/{id}/{id}.csv") view_count,
elif clip_is: duration,
output = linux.run_command(f"TwitchDownloaderCLI clipdownload --id {id} -o download/clips/{id}/{id}.mp4 --collision Overwrite") url,
write_csv(data, f"download/clips/{id}/{id}.csv") thumbnail_url,
game_id,
game_name,
stream_id,
creator_name,
clip_is,
downloaded,
uploaded_yt,
uploaded_yt_chats,
uploaded_yt_shorts,
) = row
time.sleep(1) # 1. Define your data's timestamp (Example: April 10, 2026, at 10:00 AM)
#datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%SZ")
data_timestamp = datetime.fromisoformat(created_at)
if output["success"] is True: # 2. Get the exact current date and time
print(f"ID: {id} | Date: {created_at} | Game: {game_name} | Title: {title} | was successful") current_time = datetime.now(timezone.utc)
# 3. Calculate the difference between the two times
time_difference = current_time - data_timestamp
# 4. Check if the difference is greater than 24 hours
if time_difference > timedelta(hours=24):
print("The data is more than 24 hours old.")
else:
#lets wait 24 hours befor downloading
print("The data is less than 24 hours old.")
continue
data = [list(row)]
print(
f"ID: {id} | Date: {created_at} | Game: {game_name} | Title: {title}"
)
# 1. Define paths and isolate base directory
if clip_is:
target_dir = f"download/clips/{id}"
cmd = f"TwitchDownloaderCLI clipdownload --id {id} -o {target_dir}/{id}.mp4 --collision Overwrite --temp-path download/temp"
else:
target_dir = f"download/videos/{id}"
# FIXED: Removed the duplicated command string combined with '&&'
cmd = f"TwitchDownloaderCLI videodownload --id {id} -o {target_dir}/{id}.mp4 --collision Overwrite --threads 2 --temp-path download/temp"
csv_file = f"{target_dir}/{id}.csv"
# 3. FIXED: Use the live streaming function to prevent Out-Of-Memory crashes
output = linux.run_command(cmd, look_for=["[STATUS]"])
output2 = linux.run_command(f"TwitchDownloaderCLI chatdownload --id {id} -o {target_dir}/{id}_chat.json -E --collision Overwrite --temp-path download/temp", look_for=["[STATUS]"])
if output:
# Only write CSV and update database if download actually completed
write_csv(data, csv_file)
print(f"✅ Success: TwitchDownloaderCLI video. {target_dir}")
DB.mark_as_downloaded(id) DB.mark_as_downloaded(id)
else: else:
print(f"ID: {id} | Download Process failed. {output["stdout"]}. Error: {output["stderr"]}") print(f"ID: {id} | Download Process failed.")
#print(f"❌ Reason/Error: {output.get('error', 'Unknown Error')}")
#if output.get("stderr"):
# print(f"Details: {output['stderr']}")
print(f"Finished Downloading...") # Clear temporary chunk clutter immediately if a VOD crashes out
if not clip_is:
print("Flushing temporary crash chunks...")
subprocess.run("rm -rf download/temp/*", shell=True)
print("Finished Downloading Pipeline.")
def main():
global DB
DB = Database()
download()
if __name__ == "__main__": if __name__ == "__main__":
DB = Database() main()
download()
+2 -7
View File
@@ -99,7 +99,7 @@ async def get_streamer_vods():
# Safely convert game_id to integer if possible, otherwise default to 0 # Safely convert game_id to integer if possible, otherwise default to 0
try: try:
clean_game_id = int(game_id) clean_game_id = game_id
except ValueError: except ValueError:
clean_game_id = 0 clean_game_id = 0
@@ -132,11 +132,6 @@ async def get_streamer_clips():
async for c in clip_generator: async for c in clip_generator:
game_name = await get_game_name_by_id(c.game_id) game_name = await get_game_name_by_id(c.game_id)
try:
clean_game_id = int(c.game_id)
except (ValueError, TypeError):
clean_game_id = 0
clip_data = { clip_data = {
"id": c.id, "id": c.id,
@@ -146,7 +141,7 @@ async def get_streamer_clips():
"duration": c.duration, "duration": c.duration,
"url": c.url, "url": c.url,
"thumbnail_url": c.thumbnail_url, "thumbnail_url": c.thumbnail_url,
"game_id": clean_game_id, "game_id": c.game_id,
"game_name": game_name, "game_name": game_name,
"stream_id": "0", "stream_id": "0",
"creator_name": c.creator_name, "creator_name": c.creator_name,
+1 -1
View File
@@ -78,7 +78,7 @@ def upload_video(file_path: str, title: str, category: CategoryId, description:
try: try:
# Check if file exists # Check if file exists
if not os.path.exists(file_path): if not os.path.exists(file_path):
print(f"Error: File not found: {file_path}") print(f"Error: File not found: {file_path}")
return False return False
# Load credentials # Load credentials
+1 -1
View File
@@ -183,7 +183,7 @@ def get_clip_slugs(channel_name):
return clip_slugs return clip_slugs
except Exception as e: except Exception as e:
print(f"An unexpected error occurred: {e}") print(f"An unexpected error occurred: {e}")
return [] return []
if __name__ == "__main__": if __name__ == "__main__":
+48 -22
View File
@@ -1,15 +1,18 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import re import re
import json
import nltk
import string import string
from collections import Counter from collections import Counter
from nltk.corpus import stopwords from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize from nltk.tokenize import word_tokenize
import nltk
# Download necessary NLTK data # Download necessary NLTK data modules
nltk.download('punkt', quiet=True) nltk.download('punkt', quiet=True)
nltk.download('stopwords', quiet=True) nltk.download('stopwords', quiet=True)
nltk.download('punkt_tab', quiet=True) nltk.download('punkt_tab', quiet=True)
nltk.download('averaged_perceptron_tagger', quiet=True) # Required for POS tagging
nltk.download('averaged_perceptron_tagger_eng', quiet=True)
def extract_text_from_srt(file_path: str): def extract_text_from_srt(file_path: str):
with open(file_path, 'r', encoding='utf-8') as file: with open(file_path, 'r', encoding='utf-8') as file:
@@ -19,32 +22,55 @@ def extract_text_from_srt(file_path: str):
clean_text = re.sub(r'\d+', '', clean_text) clean_text = re.sub(r'\d+', '', clean_text)
return clean_text return clean_text
def get_top_hashtags(srt_file_path: str, top_n: int = 10): def extract_text_from_json(file_path: str):
raw_text = extract_text_from_srt(srt_file_path) with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
messages = []
for comment in data.get("comments", []):
message_text = comment.get("message", {}).get("body", "")
messages.append(message_text)
# Lowercase and remove punctuation # Return a single merged string of all chat text
raw_text = raw_text.lower() return " ".join(messages)
raw_text = raw_text.translate(str.maketrans('', '', string.punctuation))
def get_top_nouns(file_path: str, top_n: int = 10):
# 1. Extract raw text
if file_path.endswith(".srt"):
raw_text = extract_text_from_srt(file_path)
else:
raw_text = extract_text_from_json(file_path)
# 2. Basic cleanup (Keep original case for proper noun accuracy)
# Strip basic punctuation but leave words intact
clean_text = raw_text.translate(str.maketrans('', '', string.punctuation))
# Tokenize and remove stopwords # 3. Tokenize words
words = word_tokenize(raw_text) words = word_tokenize(clean_text)
# 4. Part-of-Speech Tagging
tagged_words = nltk.pos_tag(words)
# 5. Filter for Nouns (NN = Singular Noun, NNP = Proper Noun, NNS = Plural Noun)
stop_words = set(stopwords.words('english')) stop_words = set(stopwords.words('english'))
nouns = []
# Filter for alphabetical words longer than 3 characters that aren't stop words for word, tag in tagged_words:
filtered_words = [ word_lower = word.lower()
word for word in words # Filter out short fragments and standard stopwords
if word.isalpha() and word not in stop_words and len(word) > 3 if tag in ['NN', 'NNP', 'NNS'] and len(word_lower) > 2 and word_lower not in stop_words:
] nouns.append(word_lower)
# 6. Count frequencies
noun_counts = Counter(nouns)
return [noun for noun, count in noun_counts.most_common(top_n)]
# Get frequency and create hashtags
word_counts = Counter(filtered_words)
top_words = word_counts.most_common(top_n)
hashtags = [f"{word[0]}" for word in top_words]
return hashtags
if __name__ == "__main__": if __name__ == "__main__":
# Example usage # Example usage
# Replace 'your_video.srt' with the path to your file # Replace 'your_video.srt' with the path to your file
results = get_top_hashtags('your_video.srt', top_n=10) results = get_top_nouns('download/videos/2813112936/2813112936.srt', top_n=10)
print("Trending Hashtags:", results) print("Trending Hashtags SRT:", results)
results = get_top_nouns("download/videos/2813112936/2813112936_chat.json", top_n=10)
print("Trending Hashtags JSON:", results)
+120 -129
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import os
import sys import json
import linux import linux
import tempfile import tempfile
from pathlib import Path from pathlib import Path
@@ -11,142 +11,133 @@ from pathlib import Path
import numpy as np import numpy as np
from PIL import Image, ImageFilter from PIL import Image, ImageFilter
def apply_gaussian_blur(frame, radius: int = 30): def get_video_info(input_path: Path) -> tuple:
""" """Uses ffprobe to instantly read input video dimensions and frame rate."""
Transforms a single NumPy array frame using PIL's true GaussianBlur filter. cmd = f"ffprobe -v error -select_streams v:0 -show_entries stream=width,height,r_frame_rate -of json {input_path}"
""" # Run command and capture output (assumes linux.run_command prints or you use subprocess)
# Convert numpy array to PIL Image import subprocess
image = Image.fromarray(frame) result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
# Apply high-quality true Gaussian Blur #linux.run_command(cmd)
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=radius)) try:
# Return back as a numpy array for MoviePy data = json.loads(result.stdout)
return np.array(blurred_image) stream = data['streams'][0]
w = int(stream['width'])
h = int(stream['height'])
# Convert fractional FPS string (e.g. "60/1" or "30000/1001") to float
fps_parts = stream['r_frame_rate'].split('/')
fps = float(fps_parts[0]) / float(fps_parts[1]) if len(fps_parts) > 1 else float(fps_parts[0])
return w, h, fps
except Exception:
return 1920, 1080, 60.0 # Safe defaults if probe fails
def fit_to_9_16_letterbox(input_path: str, top_text: str = "TOP TEXT", bottom_text: str = "BOTTOM TEXT", use_blur: bool = True): def fit_to_9_16_letterbox(input_path: str, top_text: str = "TOP TEXT", bottom_text: str = "BOTTOM TEXT", use_blur: bool = True, force: bool = False):
threads = "8"
input_file = Path(input_path) input_file = Path(input_path)
output_suffix = "gaussian_9_16" if use_blur else "black_9_16" output_suffix = "gaussian_9_16" if use_blur else "black_9_16"
output_path = input_file.parent / f"{input_file.stem}_{output_suffix}{input_file.suffix}" output_path = input_file.parent / f"{input_file.stem}_{output_suffix}{input_file.suffix}"
print("🧼 Sanitizing video metadata streams inside an automated safe context...") if os.path.exists(output_path):
with tempfile.NamedTemporaryFile(suffix=input_file.suffix, delete=False) as temp_file: if not force:
temp_path = temp_file.name print(f"❌ Error: Short video already exists: {output_path}")
return
else:
os.remove(output_path)
bg_scaled = None # 1. Probe input metadata instantly
bg_cropped = None orig_w, orig_h, fps = get_video_info(input_file)
background_layer = None canvas_w = 1080
canvas_h = 1920
print("✍️ Generating text overlay graphics via MoviePy...")
# Render static images for text instead of running a video context
title_clip = TextClip(
text=top_text, font_size=55, color="white", font="DejaVuSans-Bold",
text_align="center", size=(canvas_w - 100, 300), method="caption"
)
bottom_clip = TextClip(
text=bottom_text, font_size=55, color="white", font="DejaVuSans-Bold",
text_align="center", size=(canvas_w - 100, 300), method="caption"
)
# Save text layers to temporary PNGs
top_png = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
bottom_png = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
title_clip.save_frame(top_png)
bottom_clip.save_frame(bottom_png)
title_clip.close()
bottom_clip.close()
print("🎬 Dispatching compilation workload to FFmpeg filtergraph...")
# 2. Build the complex FFmpeg filtergraph
# [0:v] is the raw input video stream
filter_complex = []
if use_blur:
# Scale height to 1920, crop center 1080x1920, apply fast boxblur (power of 3 approximates Gaussian)
filter_complex.append(
f"[0:v]scale=-1:{canvas_h},crop={canvas_w}:{canvas_h}:(iw-{canvas_w})/2:0,boxblur=luma_radius=35:luma_power=3[bg];"
)
else:
# Generate a pure black background canvas matching video frame specs
filter_complex.append(
f"color=c=black:s={canvas_w}x{canvas_h}:r={fps}[bg];"
)
# Scale the foreground video to a clean 1080 width, keeping aspect ratio
filter_complex.append(
f"[0:v]scale={canvas_w}:-1[fg];"
)
# Layer composition chain:
# Overlay 1: Put scaled foreground onto background (centered vertically)
filter_complex.append(
f"[bg][fg]overlay=0:(H-h)/2[tmp1];"
)
# Overlay 2: Drop top text asset onto position Y=180
filter_complex.append(
f"[tmp1][1:v]overlay=(W-w)/2:180[tmp2];"
)
# Overlay 3: Drop bottom text asset onto position Y=1430
filter_complex.append(
f"[tmp2][2:v]overlay=(W-w)/2:1430[finalv]"
)
filter_graph = "".join(filter_complex)
# 3. Execute the native assembly command
# -map_chapters -1 -sn: Strips unnecessary metadata chunks instantly
# -c:a copy: Safely pulls original digital audio directly without decompression cycles
# -threads 0: Forces FFmpeg to auto-consume all available processing cores
ffmpeg_cmd = (
f'ffmpeg -y -v error -i "{input_file}" -i "{top_png}" -i "{bottom_png}" '
f'-filter_complex "{filter_graph}" '
f'-map "[finalv]" -map 0:a? -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p '
f'-c:a copy -map_chapters -1 -sn -threads {threads} "{output_path}"'
)
try: try:
linux.run_command(f"ffmpeg -y -i {input_file} -map_chapters -1 -sn -c copy {temp_path}") linux.run_command(ffmpeg_cmd)
print(f"🎉 High-speed processing complete! Video saved to: {output_path}")
# Load video
clip = VideoFileClip(temp_path)
# Standard vertical 9:16 canvas sizes
canvas_w = 1080
canvas_h = 1920
# Background logic
if use_blur:
print("📐 Scaling, cropping, and blurring background layer...")
bg_scaled = clip.resized(height=canvas_h)
bg_cropped = bg_scaled.cropped(width=canvas_w, x_center=bg_scaled.w / 2)
background_layer = bg_cropped.transform(lambda gf, t: apply_gaussian_blur(gf(t), radius=35))
else:
print("⚫ Creating solid black background canvas...")
background_layer = ColorClip(size=(canvas_w, canvas_h), color=(0, 0, 0), duration=clip.duration)
print("📐 Shrinking foreground video width to fit the 1080 wide canvas...")
foreground_clip = clip.resized(width=canvas_w)
print("✍️ Creating multi-line top title text clip...")
# FIXED: Added 'method="caption"' and increased vertical size to 300
title_clip = TextClip(
text=top_text,
font_size=55, # Slightly smaller to accommodate paragraphs comfortably
color="white",
font="DejaVuSans-Bold",
text_align="center",
size=(canvas_w - 100, 300), # Subtracted 100px for safety margins on left/right edges
method="caption", # Forces text to wrap cleanly onto a new line
duration=clip.duration
)
# Position adjusted to center the taller 300px box in the upper section
positioned_top_text = title_clip.with_position(("center", 180))
print("✍️ Creating multi-line bottom text clip...")
# FIXED: Added 'method="caption"' and increased vertical size to 300
bottom_clip = TextClip(
text=bottom_text,
font_size=55,
color="white",
font="DejaVuSans-Bold",
text_align="center",
size=(canvas_w - 100, 300), # Left/right margins included
method="caption", # Forces text to wrap cleanly onto a new line
duration=clip.duration
)
# Position adjusted to center the taller 300px box in the lower section
positioned_bottom_text = bottom_clip.with_position(("center", 1430))
# Composite layers
final_clip = CompositeVideoClip(
[
background_layer,
foreground_clip.with_position("center"),
positioned_top_text,
positioned_bottom_text
]
).with_audio(clip.audio)
print("🎬 Rendering final vertical composition...")
final_clip.write_videofile(
str(output_path),
codec="libx264",
audio_codec="aac",
fps=clip.fps
)
# Clean up file locks safely
clip.close()
if bg_scaled: bg_scaled.close()
if bg_cropped: bg_cropped.close()
background_layer.close()
foreground_clip.close()
title_clip.close()
bottom_clip.close()
final_clip.close()
finally: finally:
temp_file_path = Path(temp_path) # Clean up temporary PNG picture files safely
if temp_file_path.exists(): for path in (top_png, bottom_png):
temp_file_path.unlink() if os.path.exists(path):
os.unlink(path)
print(f"🎉 Text overlay video saved to: {output_path}")
def main():
# Set up the command-line argument parser
parser = argparse.ArgumentParser(description="TeamPGP Clip Processing and Upload Pipeline")
# Add optional arguments
parser.add_argument('--convert', type=str, metavar='CLIP_PATH', help='Path to a video file to convert to a 9:16 Short')
parser.add_argument('--upload', action='store_true', help='Process and upload pending Shorts in the database to YouTube')
args = parser.parse_args()
# If no flags are provided, show help text and exit
if not args.convert and not args.upload:
parser.print_help()
sys.exit("\n❌ Error: You must provide at least one action flag (--convert or --upload).")
# Execute conversion step if path is provided
if args.convert:
clip_by = "joelmckinney"
#convert_to_short(args.convert)
fit_to_9_16_letterbox(args.convert, "TeamPGP Live on Twitch...\n Every Friday and Sunday @7:30 ET.\n twitch.tv/teampgp", f"Clipped By: {clip_by}.", True)
# Execute database upload step if flag is provided
if args.upload:
upload_shorts()
if __name__ == "__main__": if __name__ == "__main__":
main() import time
start_time = time.perf_counter()
creator_name = "greenskiesbluegrass"
top_txt = "TeamPGP Live on Twitch...\n Every Friday and Sunday @7:30 ET.\n twitch.tv/teampgp"
bottom_txt = f"Clipped By: {creator_name}."
fit_to_9_16_letterbox("download/clips/AbnegateAgitatedGrassPJSalt/AbnegateAgitatedGrassPJSalt.mp4", top_txt, bottom_txt, True, True)
end_time = time.perf_counter()
execution_time = end_time - start_time
print(f"The function took {execution_time:.6f} seconds to complete.")