Update streamlit_app.py
Browse files- streamlit_app.py +27 -110
streamlit_app.py
CHANGED
|
@@ -11,11 +11,7 @@ from TikTokLive import TikTokLiveClient
|
|
| 11 |
from TikTokLive.events import CommentEvent, DisconnectEvent
|
| 12 |
from TikTokLive.client.errors import UserOfflineError, UserNotFoundError
|
| 13 |
|
| 14 |
-
# ----------
|
| 15 |
-
# إذا لم تدخل وكيلًا يدويًا، سيتم استخدام هذا الوسيط المجاني.
|
| 16 |
-
AUTO_PROXY = "https://cors-anywhere.herokuapp.com/"
|
| 17 |
-
|
| 18 |
-
# ---------- تحميل الخط ----------
|
| 19 |
FONT_PATH = "Palestine.ttf"
|
| 20 |
FONT_URL = "https://github.com/mokhtarbsaid/rare-arabic-fonts/raw/main/palestine/palestine-regular.ttf"
|
| 21 |
|
|
@@ -34,6 +30,7 @@ MAX_COMMENTS = 8
|
|
| 34 |
lock = threading.Lock()
|
| 35 |
|
| 36 |
def create_comment_overlay():
|
|
|
|
| 37 |
width, height = 720, 1280
|
| 38 |
img = Image.new("RGBA", (width, height), (0, 0, 0, 0))
|
| 39 |
draw = ImageDraw.Draw(img)
|
|
@@ -57,16 +54,12 @@ def create_comment_overlay():
|
|
| 57 |
return buf.read()
|
| 58 |
|
| 59 |
# ---------- عميل تعليقات تيك توك ----------
|
| 60 |
-
def run_tiktok_client(username
|
| 61 |
while True:
|
| 62 |
try:
|
| 63 |
loop = asyncio.new_event_loop()
|
| 64 |
asyncio.set_event_loop(loop)
|
| 65 |
-
|
| 66 |
-
if proxy:
|
| 67 |
-
client_params["web_proxy"] = proxy
|
| 68 |
-
client_params["ws_proxy"] = proxy
|
| 69 |
-
client = TikTokLiveClient(**client_params)
|
| 70 |
|
| 71 |
@client.on(CommentEvent)
|
| 72 |
async def on_comment(event: CommentEvent):
|
|
@@ -85,105 +78,29 @@ def run_tiktok_client(username, proxy=None):
|
|
| 85 |
pass
|
| 86 |
time.sleep(10)
|
| 87 |
|
| 88 |
-
# ----------
|
| 89 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
try:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
async def fetch():
|
| 95 |
-
await client._init_http_client()
|
| 96 |
-
if not await client.is_live():
|
| 97 |
-
return None
|
| 98 |
-
room_info = await client._http_client.get_room_info()
|
| 99 |
-
stream_data = room_info['data']['stream_url']
|
| 100 |
-
return stream_data.get('hls_pull_url') or stream_data.get('rtmp_pull_url')
|
| 101 |
-
return loop.run_until_complete(fetch())
|
| 102 |
-
except:
|
| 103 |
-
return None
|
| 104 |
-
|
| 105 |
-
# ---------- تشغيل البث ----------
|
| 106 |
-
def start_streaming(yt_key, tiktok_username, proxy=None):
|
| 107 |
-
# تحديد الوكيل النهائي (يدوي > تلقائي)
|
| 108 |
-
effective_proxy = proxy if proxy else AUTO_PROXY
|
| 109 |
-
stream_url = None
|
| 110 |
-
|
| 111 |
-
# المحاولة 1: streamlink مع الوكيل
|
| 112 |
-
try:
|
| 113 |
-
sl_cmd = [
|
| 114 |
-
"streamlink",
|
| 115 |
-
f"https://www.tiktok.com/@{tiktok_username}/live",
|
| 116 |
-
"best", "--stdout",
|
| 117 |
-
"--retry-open", "1", "--retry-max", "1",
|
| 118 |
-
]
|
| 119 |
-
if effective_proxy:
|
| 120 |
-
sl_cmd += ["--http-proxy", effective_proxy, "--https-proxy", effective_proxy]
|
| 121 |
-
|
| 122 |
-
sl_test = subprocess.run(sl_cmd, capture_output=True, text=True, timeout=15)
|
| 123 |
-
if sl_test.returncode == 0 and sl_test.stdout:
|
| 124 |
-
stream_url = "streamlink"
|
| 125 |
-
except:
|
| 126 |
-
pass
|
| 127 |
-
|
| 128 |
-
if stream_url != "streamlink":
|
| 129 |
-
api_url = get_stream_url_from_api(tiktok_username, effective_proxy)
|
| 130 |
-
if api_url:
|
| 131 |
-
stream_url = api_url
|
| 132 |
-
else:
|
| 133 |
-
print("[ERROR] فشل جلب رابط البث حتى مع الوكيل. جرب وكيلاً آخر.")
|
| 134 |
-
return
|
| 135 |
-
|
| 136 |
-
print("[INFO] تم جلب البث، جاري بدء ffmpeg...")
|
| 137 |
-
|
| 138 |
-
if stream_url == "streamlink":
|
| 139 |
-
sl_process = subprocess.Popen(sl_cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
| 140 |
-
else:
|
| 141 |
-
sl_process = None
|
| 142 |
-
|
| 143 |
-
ffmpeg_cmd = [
|
| 144 |
-
"ffmpeg",
|
| 145 |
-
"-thread_queue_size", "8192",
|
| 146 |
-
"-i", "pipe:0" if sl_process else stream_url,
|
| 147 |
-
"-f", "image2pipe", "-r", "2", "-i", "pipe:1",
|
| 148 |
-
"-filter_complex",
|
| 149 |
-
"[0:v]scale=720:1280[vid];[vid][1:v]overlay=0:0",
|
| 150 |
-
"-map", "[outv]", "-map", "0:a?",
|
| 151 |
-
"-c:v", "libx264", "-preset", "ultrafast", "-tune", "zerolatency",
|
| 152 |
-
"-b:v", "1500k", "-maxrate", "2000k", "-bufsize", "3000k", "-g", "30",
|
| 153 |
-
"-c:a", "aac", "-ar", "48000", "-b:a", "192k", "-ac", "2",
|
| 154 |
-
"-f", "flv",
|
| 155 |
-
f"rtmp://a.rtmp.youtube.com/live2/{yt_key}"
|
| 156 |
-
]
|
| 157 |
-
|
| 158 |
-
try:
|
| 159 |
-
ffmpeg_process = subprocess.Popen(ffmpeg_cmd, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 160 |
-
|
| 161 |
-
def feed_overlay():
|
| 162 |
-
while ffmpeg_process.poll() is None:
|
| 163 |
-
try:
|
| 164 |
-
ffmpeg_process.stdin.write(create_comment_overlay())
|
| 165 |
-
ffmpeg_process.stdin.flush()
|
| 166 |
-
except:
|
| 167 |
-
break
|
| 168 |
-
time.sleep(0.5)
|
| 169 |
-
ffmpeg_process.stdin.close()
|
| 170 |
-
|
| 171 |
-
threading.Thread(target=feed_overlay, daemon=True).start()
|
| 172 |
-
|
| 173 |
-
if sl_process:
|
| 174 |
-
while ffmpeg_process.poll() is None:
|
| 175 |
-
data = sl_process.stdout.read(8192)
|
| 176 |
-
if not data:
|
| 177 |
-
break
|
| 178 |
-
ffmpeg_process.stdin.write(data)
|
| 179 |
-
sl_process.stdout.close()
|
| 180 |
-
sl_process.wait()
|
| 181 |
-
else:
|
| 182 |
-
ffmpeg_process.wait()
|
| 183 |
-
|
| 184 |
print("[INFO] توقف البث")
|
| 185 |
except Exception as e:
|
| 186 |
-
print(f"[ERROR] فشل تشغيل
|
| 187 |
|
| 188 |
# ---------- واجهة المستخدم ----------
|
| 189 |
st.set_page_config(page_title="بث تيك توك مع تعليقات")
|
|
@@ -192,17 +109,17 @@ st.title("📱 بث تيك توك إلى يوتيوب مع تعليقات متح
|
|
| 192 |
with st.form(key="stream_form"):
|
| 193 |
tiktok_user = st.text_input("معرف مستخدم تيك توك (بدون @)").strip()
|
| 194 |
yt_stream_key = st.text_input("مفتاح بث يوتيوب (RTMP Key)", type="password").strip()
|
| 195 |
-
proxy = st.text_input("خادم وكيل (Proxy) - اتركه فارغاً لتجربة التلقائي", "").strip()
|
| 196 |
submit_button = st.form_submit_button(label="ابدأ البث الآن")
|
| 197 |
|
| 198 |
if submit_button:
|
| 199 |
if not tiktok_user or not yt_stream_key:
|
| 200 |
st.error("يرجى إدخال اسم المستخدم ومفتاح البث.")
|
| 201 |
else:
|
| 202 |
-
threading.Thread(target=run_tiktok_client, args=(tiktok_user,
|
| 203 |
-
threading.Thread(target=start_streaming, args=(yt_stream_key, tiktok_user
|
| 204 |
st.success("تم بدء العمليات في الخلفية. انتظر ظهور البث على يوتيوب.")
|
| 205 |
|
|
|
|
| 206 |
st.subheader("💬 أحدث التعليقات")
|
| 207 |
placeholder = st.empty()
|
| 208 |
while True:
|
|
|
|
| 11 |
from TikTokLive.events import CommentEvent, DisconnectEvent
|
| 12 |
from TikTokLive.client.errors import UserOfflineError, UserNotFoundError
|
| 13 |
|
| 14 |
+
# ---------- تحميل الخط تلقائياً ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
FONT_PATH = "Palestine.ttf"
|
| 16 |
FONT_URL = "https://github.com/mokhtarbsaid/rare-arabic-fonts/raw/main/palestine/palestine-regular.ttf"
|
| 17 |
|
|
|
|
| 30 |
lock = threading.Lock()
|
| 31 |
|
| 32 |
def create_comment_overlay():
|
| 33 |
+
"""توليد صورة شفافة للتعليقات"""
|
| 34 |
width, height = 720, 1280
|
| 35 |
img = Image.new("RGBA", (width, height), (0, 0, 0, 0))
|
| 36 |
draw = ImageDraw.Draw(img)
|
|
|
|
| 54 |
return buf.read()
|
| 55 |
|
| 56 |
# ---------- عميل تعليقات تيك توك ----------
|
| 57 |
+
def run_tiktok_client(username):
|
| 58 |
while True:
|
| 59 |
try:
|
| 60 |
loop = asyncio.new_event_loop()
|
| 61 |
asyncio.set_event_loop(loop)
|
| 62 |
+
client = TikTokLiveClient(unique_id=f"@{username}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
@client.on(CommentEvent)
|
| 65 |
async def on_comment(event: CommentEvent):
|
|
|
|
| 78 |
pass
|
| 79 |
time.sleep(10)
|
| 80 |
|
| 81 |
+
# ---------- تشغيل البث (نفس طريقة Tk.test الناجحة) ----------
|
| 82 |
+
def start_streaming(yt_key, tiktok_username):
|
| 83 |
+
# 1. أمر streamlink (مثل Tk.test تماماً)
|
| 84 |
+
streamlink_cmd = f"streamlink https://www.tiktok.com/@{tiktok_username}/live best --hls-live-edge 3 --stdout"
|
| 85 |
+
|
| 86 |
+
# 2. أمر ffmpeg (مع الحفاظ على الجودة مثل Tk.test)
|
| 87 |
+
ffmpeg_cmd = (
|
| 88 |
+
f"ffmpeg -thread_queue_size 8192 -i pipe:0 "
|
| 89 |
+
f"-c:v copy " # نسخ الفيديو بدون إعادة ترميز (مثل Tk.test)
|
| 90 |
+
f"-c:a aac -ar 48000 -b:a 192k -ac 2 "
|
| 91 |
+
f"-f flv rtmp://a.rtmp.youtube.com/live2/{yt_key}"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# 3. دمج الأمرين (مثل Tk.test)
|
| 95 |
+
full_cmd = f"{streamlink_cmd} | {ffmpeg_cmd}"
|
| 96 |
+
|
| 97 |
try:
|
| 98 |
+
print(f"[INFO] بدء البث من @{tiktok_username} إلى يوتيوب...")
|
| 99 |
+
process = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 100 |
+
process.wait()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
print("[INFO] توقف البث")
|
| 102 |
except Exception as e:
|
| 103 |
+
print(f"[ERROR] فشل تشغيل البث: {e}")
|
| 104 |
|
| 105 |
# ---------- واجهة المستخدم ----------
|
| 106 |
st.set_page_config(page_title="بث تيك توك مع تعليقات")
|
|
|
|
| 109 |
with st.form(key="stream_form"):
|
| 110 |
tiktok_user = st.text_input("معرف مستخدم تيك توك (بدون @)").strip()
|
| 111 |
yt_stream_key = st.text_input("مفتاح بث يوتيوب (RTMP Key)", type="password").strip()
|
|
|
|
| 112 |
submit_button = st.form_submit_button(label="ابدأ البث الآن")
|
| 113 |
|
| 114 |
if submit_button:
|
| 115 |
if not tiktok_user or not yt_stream_key:
|
| 116 |
st.error("يرجى إدخال اسم المستخدم ومفتاح البث.")
|
| 117 |
else:
|
| 118 |
+
threading.Thread(target=run_tiktok_client, args=(tiktok_user,), daemon=True).start()
|
| 119 |
+
threading.Thread(target=start_streaming, args=(yt_stream_key, tiktok_user), daemon=True).start()
|
| 120 |
st.success("تم بدء العمليات في الخلفية. انتظر ظهور البث على يوتيوب.")
|
| 121 |
|
| 122 |
+
# عرض التعليقات الحية
|
| 123 |
st.subheader("💬 أحدث التعليقات")
|
| 124 |
placeholder = st.empty()
|
| 125 |
while True:
|