chatspace / app.py
Yak-2026's picture
Update app.py
0b7b0f2 verified
Raw
History Blame Contribute Delete
5.55 kB
import os
import subprocess
import threading
import gradio as gr
import time
from PIL import Image
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
streaming_thread = None
overlay_thread = None
is_streaming = False
ffmpeg_process = None
driver = None
OVERLAY_IMAGE = "full_chat.png"
# 🎯 ඔයා ලබාදුන් නිවැරදි විස්තර කෝඩ් එක ඇතුලටම දැම්මා මචං
LIVE_CHAT_URL = "https://studio.youtube.com/live_chat?is_popout=1&v=zX7iYrKOY84"
STREAM_KEY = "9mxh-05bs-aez1-3tcx-b302"
def real_chat_full_screen_worker():
global is_streaming, driver
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--window-size=1080,1920") # 1080x1920 Full Screen
try:
driver = webdriver.Chrome(options=chrome_options)
driver.get(LIVE_CHAT_URL)
print("✅ සැබෑ Live Chat එකට සාර්ථකව සම්බන්ධ වුණා!")
time.sleep(6) # චැට් එක ලෝඩ් වෙන්න ප්‍රමාණවත් වෙලාවක් දෙනවා
except Exception as e:
print(f"Browser Error: {e}")
while is_streaming:
try:
if driver:
# 📸 චැට් එක වයිට් තීම් එකෙන්ම ෆුල් ස්ක්‍රීන් ස්ක්‍රීන්ෂොට් එකක් ගන්නවා
driver.save_screenshot(OVERLAY_IMAGE)
except Exception as e:
print(f"Screenshot Error: {e}")
time.sleep(1) # හැම තත්පරයකම සජීවීව අප්ඩේට් වෙනවා
def start_auto_stream():
global is_streaming, streaming_thread, overlay_thread
if is_streaming:
return "ஏற்கனவே Stream එකක් දුවනවා! කලින් එක නවත්තලා ඉන්න."
is_streaming = True
# 1. Full Screen Chat Capture Start
overlay_thread = threading.Thread(target=real_chat_full_screen_worker)
overlay_thread.start()
# 2. FFmpeg Pure Image Stream Start
streaming_thread = threading.Thread(target=stream_worker)
streaming_thread.start()
return "🚀 සියලුම දත්ත ඇතුලත් කර කෝඩ් එක ඔටෝමැටිකවම ලයිව් එක පටන් ගත්තා! කරුණාකර YouTube Studio බලන්න."
def stop_stream():
global is_streaming, ffmpeg_process, driver
is_streaming = False
if driver:
try: driver.quit()
except: pass
if ffmpeg_process:
ffmpeg_process.terminate()
subprocess.run(["pkill", "-f", "ffmpeg"])
subprocess.run(["pkill", "-f", "chrome"])
if os.path.exists(OVERLAY_IMAGE):
try: os.remove(OVERLAY_IMAGE)
except: pass
return "ලයිව් ස්ට්‍රීම් එක සම්පූර්ණයෙන්ම නැවැත්තුවා!"
def stream_worker():
global is_streaming, ffmpeg_process
try:
hls_url = f"https://a.upload.youtube.com/http_upload_hls?cid={STREAM_KEY}&copy=0&file=live.m3u8"
# 🎯 CPU එකේ බර අඩු කරලා, 1.00x ස්පීඩ් එකෙන්ම බෆරින් නැතුව දුවන්න -pix_fmt yuv420p එක දැම්මා මචං
ffmpeg_cmd = (
f"ffmpeg -re -loop 1 -i {OVERLAY_IMAGE} -f lavfi -i anullsrc=cl=stereo:r=44100 "
f"-vcodec libx264 -pix_fmt yuv420p -preset ultrafast -r 30 -g 60 -b:v 2500k -maxrate 2500k -bufsize 5000k "
f"-acodec aac -b:a 128k -ar 44100 -shortest "
f"-f hls -hls_time 2 -hls_list_size 5 -hls_flags delete_segments -method POST '{hls_url}'"
)
# පළමු ස්ක්‍රීන්ෂොට් එක හැදෙනකන් තත්පර 7ක් ඉන්නවා
time.sleep(7)
while is_streaming:
if os.path.exists(OVERLAY_IMAGE):
print("FFmpeg Full Screen Chat එක 1.00x වේගයෙන් යූටියුබ් එකට තල්ලු කරනවා...")
ffmpeg_process = subprocess.Popen(ffmpeg_cmd, shell=True)
ffmpeg_process.wait()
time.sleep(2)
except Exception as e:
print("Stream Error:", str(e))
is_streaming = False
with gr.Blocks(title="24/7 Auto Full Screen Chat") as demo:
gr.Markdown("## 📊 24/7 Pure Full-Screen Live Chat Streamer (Auto Configuration)")
gr.Markdown("ලින්ක් සහ ස්ට්‍රීම් කී සියල්ල කෝඩ් එක ඇතුලෙන්ම ඔටෝ සෙට් කර සකසන ලද අවසන් ස්ථාවර සංස්කරණය.")
status_output = gr.Textbox(label="Status (තත්ත්වය)", value="ස්ට්‍රීම් එක ආරම්භ කිරීමට සූදානම්...")
with gr.Row():
start_btn = gr.Button("🔴 Start Auto Live Stream", variant="primary")
stop_btn = gr.Button("⏹️ Stop Stream", variant="stop")
start_btn.click(start_auto_stream, outputs=status_output)
stop_btn.click(stop_stream, outputs=status_output)
demo.launch()