-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_processor.py
More file actions
1644 lines (1464 loc) · 71.6 KB
/
Copy pathqueue_processor.py
File metadata and controls
1644 lines (1464 loc) · 71.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import gc
import ctypes
import asyncio
import time
import logging
import glob
import itertools
import secrets
from dataclasses import dataclass, field
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from config import load_config, check_disk_space, check_ffmpeg, DOWNLOAD_DIR, get_ffmpeg_command, get_proxy_list, get_cookie_file
from downloader import (
download_content, get_video_info, get_playlist_info,
probe_live_state, restart_warp_proxy,
)
from uploader import (
upload_video_streaming,
upload_audio_streaming,
split_video,
crop_to_square,
UploadRetryableError,
UploadPermanentError,
)
from handlers import cancelled_tasks, stopped_tasks
from telegram_utils import tg_retry
logger = logging.getLogger(__name__)
# Live recordings run outside the request queue processor and need explicit
# tracking so maintenance never starts while they are active.
active_live_tasks = set()
# The live-edge recorder is only a temporary safety net while the archive from
# the beginning proves it can keep recording. After this interval, keeping
# both recorders would only create duplicate uploads.
LIVE_FROM_START_STABILITY_SECONDS = 10 * 60
LIVE_FROM_START_MAX_DATA_GAP_SECONDS = 30
# Upload retries are deliberately separate from the download queue. A Telegram
# flood-control delay must never keep every later download waiting behind it.
UPLOAD_RETRY_WINDOW_SECONDS = 24 * 60 * 60
UPLOAD_RETRY_MAX_DELAY_SECONDS = 15 * 60
_upload_retry_queue = None
_upload_retry_wakeup = None
_upload_retry_jobs = {}
_upload_retry_counter = itertools.count()
_upload_retry_tasks = set()
@dataclass
class UploadJob:
"""An in-memory, resumable upload of one media item or its split parts."""
application: object
chat_id: int
source_file_path: str
files_to_upload: list
title: str
url: str
audio_only: bool = False
update_status_func: object = None
channel_name: str = None
reply_to_message_id: int = None
thumb_path: str = None
allow_audio_download: bool = True
job_id: str = field(default_factory=lambda: secrets.token_urlsafe(8))
next_part_index: int = 0
retry_deadline: float = None
retry_count: int = 0
retry_generation: int = 0
running: bool = False
completed: bool = False
auto_retry_expired: bool = False
def _safe_remove(path):
if not path:
return
try:
if os.path.exists(path):
os.remove(path)
except Exception as exc:
logger.warning("Failed to remove temporary upload file %s: %s", path, exc)
async def _update_upload_job_status(job, text, show_retry=False):
"""Update the request's one status message, where one is available."""
if not job.update_status_func:
return
try:
if show_retry:
await job.update_status_func(text, force=True, retry_job_id=job.job_id)
else:
await job.update_status_func(text, force=True)
except TypeError:
# Live and older call sites do not expose inline keyboard support.
await job.update_status_func(text, force=True)
except Exception as exc:
logger.warning("Failed to update upload status for %s: %s", job.job_id, exc)
def _retry_delay(error, retry_count):
if isinstance(error, UploadRetryableError) and error.retry_after is not None:
return max(0, error.retry_after)
return min(60 * (2 ** min(retry_count, 4)), UPLOAD_RETRY_MAX_DELAY_SECONDS)
async def _schedule_upload_retry(job, error):
"""Keep failed media on disk and schedule its next background attempt."""
global _upload_retry_queue, _upload_retry_wakeup
if isinstance(error, UploadPermanentError):
job.auto_retry_expired = True
await _update_upload_job_status(
job,
(
f"❌ Upload rejected at part {job.next_part_index + 1}/{len(job.files_to_upload)}: {error}\n"
"Files are kept; correct the problem, then use Retry upload now."
),
show_retry=True,
)
return
now = time.monotonic()
if job.retry_deadline is None:
job.retry_deadline = now + UPLOAD_RETRY_WINDOW_SECONDS
if now >= job.retry_deadline:
job.auto_retry_expired = True
await _update_upload_job_status(
job,
(
f"❌ Upload paused at part {job.next_part_index + 1}/{len(job.files_to_upload)}.\n"
"Automatic retries stopped after 24 hours; files are kept."
),
show_retry=True,
)
return
job.retry_count += 1
job.auto_retry_expired = False
delay = _retry_delay(error, job.retry_count - 1)
due_at = min(now + delay, job.retry_deadline)
job.retry_generation += 1
if _upload_retry_queue is None:
logger.error("Upload retry worker is unavailable; keeping job %s for manual retry", job.job_id)
await _update_upload_job_status(
job,
f"❌ Upload failed for part {job.next_part_index + 1}/{len(job.files_to_upload)}: {error}\nFiles are kept.",
show_retry=True,
)
return
await _upload_retry_queue.put((due_at, next(_upload_retry_counter), job.job_id, job.retry_generation))
if _upload_retry_wakeup:
_upload_retry_wakeup.set()
await _update_upload_job_status(
job,
(
f"⚠️ Upload failed for part {job.next_part_index + 1}/{len(job.files_to_upload)}: {error}\n"
f"⏳ Retrying automatically in {int(max(0, due_at - now))} seconds; files are kept."
),
show_retry=True,
)
def _caption_for_upload_part(job, part_index):
if job.channel_name:
caption = f"{job.channel_name}\n{job.title}\n{job.url}"
else:
caption = f"{job.title}\n{job.url}"
if len(job.files_to_upload) > 1:
if job.channel_name:
caption = f"{job.channel_name}\n{job.title} (Part {part_index + 1}/{len(job.files_to_upload)})\n{job.url}"
else:
caption = f"{job.title} (Part {part_index + 1}/{len(job.files_to_upload)})\n{job.url}"
return caption
def _upload_reply_markups(url, allow_audio_download=True):
"""Build the optional audio-download button for non-live uploads only."""
if not allow_audio_download:
return None, None
audio_cb_data = f"audio:{url}"
if len(audio_cb_data.encode('utf-8')) > 64:
return None, None
return (
{"inline_keyboard": [[{"text": "🎵 Download Audio", "callback_data": audio_cb_data}]]},
InlineKeyboardMarkup([[InlineKeyboardButton("🎵 Download Audio", callback_data=audio_cb_data)]]),
)
async def _upload_one_part(job, part_index):
"""Send exactly one part; raw uploads perform their own bounded retries."""
file_path = job.files_to_upload[part_index]
if not os.path.exists(file_path):
raise UploadPermanentError(f"Upload file is missing: {file_path}")
config = load_config()
api_url = config.get('api_url', '')
bot_token = config.get('bot_token', '')
is_local_api = api_url and 'api.telegram.org' not in api_url
caption = _caption_for_upload_part(job, part_index)
if job.audio_only:
if is_local_api:
await upload_audio_streaming(
bot_token, api_url, job.chat_id, file_path, job.title, caption,
reply_to_message_id=job.reply_to_message_id, thumb_path=job.thumb_path,
max_retries=1,
)
return
with open(file_path, 'rb') as media_fh:
thumb_fh = None
try:
if job.thumb_path and os.path.exists(job.thumb_path):
thumb_fh = open(crop_to_square(job.thumb_path), 'rb')
await tg_retry(
job.application.bot.send_audio,
chat_id=job.chat_id,
audio=media_fh,
title=job.title,
caption=caption,
reply_to_message_id=job.reply_to_message_id,
thumbnail=thumb_fh,
)
finally:
if thumb_fh:
thumb_fh.close()
return
raw_markup, telegram_markup = _upload_reply_markups(
job.url, allow_audio_download=job.allow_audio_download
)
if is_local_api:
await upload_video_streaming(
bot_token, api_url, job.chat_id, file_path, caption, raw_markup,
reply_to_message_id=job.reply_to_message_id, thumb_path=job.thumb_path,
max_retries=1,
)
return
with open(file_path, 'rb') as media_fh:
thumb_fh = None
try:
if job.thumb_path and os.path.exists(job.thumb_path):
thumb_fh = open(crop_to_square(job.thumb_path), 'rb')
await tg_retry(
job.application.bot.send_video,
chat_id=job.chat_id,
video=media_fh,
caption=caption,
supports_streaming=True,
reply_markup=telegram_markup,
reply_to_message_id=job.reply_to_message_id,
thumbnail=thumb_fh,
)
finally:
if thumb_fh:
thumb_fh.close()
def _cleanup_completed_upload(job):
"""Delete artifacts only after every part has reached Telegram."""
paths = set(job.files_to_upload)
paths.add(job.source_file_path)
if job.thumb_path:
paths.add(job.thumb_path)
for path in paths:
_safe_remove(path)
async def _execute_upload_job(job):
"""Run a job from its first unfinished part through completion or deferral."""
try:
while job.next_part_index < len(job.files_to_upload):
part_number = job.next_part_index + 1
await _update_upload_job_status(
job,
f"⬆️ Uploading {'audio' if job.audio_only else f'part {part_number}/{len(job.files_to_upload)}'}...",
)
try:
await _upload_one_part(job, job.next_part_index)
except Exception as exc:
logger.error("Upload failed for job %s part %s: %s", job.job_id, part_number, exc)
await _schedule_upload_retry(job, exc)
return False
job.next_part_index += 1
await _update_upload_job_status(job, "🧹 Cleaning up...", show_retry=False)
_cleanup_completed_upload(job)
job.completed = True
_upload_retry_jobs.pop(job.job_id, None)
await _update_upload_job_status(job, "✅ Upload complete.", show_retry=False)
return True
finally:
job.running = False
async def _run_upload_job(job):
if job.running or job.completed:
return False
job.running = True
return await _execute_upload_job(job)
async def process_upload_retry_queue():
"""Run one deferred upload at a time, leaving the download queue unblocked."""
logger.info("Upload retry worker started.")
while True:
due_at, _, job_id, generation = await _upload_retry_queue.get()
try:
wait_seconds = due_at - time.monotonic()
requeued_for_earlier_job = False
if wait_seconds > 0:
try:
# A newly scheduled job may have an earlier due time than
# this one. Wake and put this entry back so PriorityQueue
# can choose the right job instead of sleeping past it.
await asyncio.wait_for(_upload_retry_wakeup.wait(), timeout=wait_seconds)
_upload_retry_wakeup.clear()
await _upload_retry_queue.put((due_at, next(_upload_retry_counter), job_id, generation))
requeued_for_earlier_job = True
except asyncio.TimeoutError:
pass
if requeued_for_earlier_job:
continue
job = _upload_retry_jobs.get(job_id)
if not job or job.completed or job.running or job.retry_generation != generation:
continue
await _run_upload_job(job)
except asyncio.CancelledError:
raise
except Exception as exc:
logger.exception("Upload retry worker error for %s: %s", job_id, exc)
finally:
_upload_retry_queue.task_done()
def start_upload_retry_worker():
"""Create the in-memory retry queue and return its single worker task."""
global _upload_retry_queue, _upload_retry_wakeup
_upload_retry_queue = asyncio.PriorityQueue()
_upload_retry_wakeup = asyncio.Event()
return asyncio.create_task(process_upload_retry_queue())
async def stop_upload_retry_tasks():
"""Stop user-triggered immediate retry tasks during bot shutdown."""
tasks = list(_upload_retry_tasks)
for task in tasks:
if not task.done():
task.cancel()
if tasks:
await asyncio.gather(*tasks, return_exceptions=True)
async def retry_upload_job(job_id):
"""Request an immediate retry from the status-message callback."""
job = _upload_retry_jobs.get(job_id)
if not job or job.completed:
return False, "This upload is no longer available."
if job.running:
return False, "Upload retry is already in progress."
# Invalidate a delayed queue entry. The direct task starts now rather
# than waiting for the retry worker to wake for its old due time.
job.retry_generation += 1
job.auto_retry_expired = False
# Mark it running before yielding so double-taps cannot start two uploads
# of the same Telegram part.
job.running = True
task = asyncio.create_task(_execute_upload_job(job))
_upload_retry_tasks.add(task)
task.add_done_callback(_upload_retry_tasks.discard)
return True, "Retrying upload now."
def has_active_downloads(request_queue, playlist_queue):
"""Return whether a queued, processing, uploading, or live task exists."""
request_active = getattr(request_queue, '_unfinished_tasks', 0)
playlist_active = getattr(playlist_queue, '_unfinished_tasks', 0)
retry_upload_active = any(not job.completed for job in _upload_retry_jobs.values())
return bool(request_active or playlist_active or retry_upload_active or active_live_tasks)
async def process_live_stream_tracked(*args):
"""Track a detached live recording for daily maintenance decisions."""
task_id = args[5]
active_live_tasks.add(task_id)
try:
await process_live_stream(*args)
finally:
active_live_tasks.discard(task_id)
def _free_memory():
"""Force garbage collection and release memory back to OS via glibc malloc_trim."""
gc.collect()
try:
ctypes.CDLL("libc.so.6").malloc_trim(0)
except Exception:
pass
def _cleanup_partial_downloads():
"""Remove .part files and orphaned thumbnails from downloads directory."""
for f in glob.glob(os.path.join(DOWNLOAD_DIR, "*.part")):
try: os.remove(f)
except: pass
for ext in ('*.jpg', '*.webp', '*.jpeg'):
for f in glob.glob(os.path.join(DOWNLOAD_DIR, ext)):
base = os.path.splitext(f)[0]
has_video = any(os.path.exists(base + v) for v in ('.mp4', '.mkv', '.webm', '.m4a', '.mp3'))
if not has_video:
try: os.remove(f)
except: pass
async def handle_upload(application, chat_id, file_path, title, url, audio_only=False, update_status_func=None, channel_name=None, reply_to_message_id=None, thumb_path=None, allow_audio_download=True):
"""Upload media now, or retain it and defer retries after a failure.
Returns ``True`` only when all parts are delivered and cleanup is complete.
A ``False`` result means the retry worker owns the retained files.
"""
try:
if audio_only:
files_to_upload = [file_path]
else:
if update_status_func:
await update_status_func("✂️ Checking file size...", force=True)
loop = asyncio.get_running_loop()
if not check_ffmpeg():
files_to_upload = [file_path]
else:
files_to_upload = await loop.run_in_executor(None, split_video, file_path)
job = UploadJob(
application=application,
chat_id=chat_id,
source_file_path=file_path,
files_to_upload=files_to_upload,
title=title,
url=url,
audio_only=audio_only,
update_status_func=update_status_func,
channel_name=channel_name,
reply_to_message_id=reply_to_message_id,
thumb_path=thumb_path,
allow_audio_download=allow_audio_download,
)
_upload_retry_jobs[job.job_id] = job
completed = await _run_upload_job(job)
return completed
except Exception as e:
logger.error(f"Error in handle_upload: {e}")
error_text = f"🔥 Upload error: {e}"
if update_status_func:
await update_status_func(error_text, force=True)
else:
await tg_retry(application.bot.send_message, chat_id=chat_id, text=error_text)
# Never remove media on an upload failure. If construction failed
# before a job was registered, this is still safer than data loss.
return False
finally:
_free_memory()
async def process_queue(application, request_queue):
"""Main queue processor for single video downloads."""
logger.info("Queue processor started.")
while True:
task = await request_queue.get()
try:
status_msg_passed = None
is_live = False
channel_name = None
expected_live_video_id = None
if len(task) == 8:
chat_id, url, message_id, max_height, status_msg_passed, channel_name, is_live, expected_live_video_id = task
elif len(task) == 7:
chat_id, url, message_id, max_height, status_msg_passed, channel_name, is_live = task
elif len(task) == 6:
chat_id, url, message_id, max_height, status_msg_passed, channel_name = task
elif len(task) == 5:
chat_id, url, message_id, max_height, status_msg_passed = task
elif len(task) == 4:
chat_id, url, message_id, max_height = task
else:
chat_id, url, message_id = task
max_height = 1080
audio_only = (max_height in (-1, -2))
audio_format = 'mp3' if max_height == -2 else 'm4a'
if audio_only:
max_height = 1080
task_id = f"{chat_id}_{message_id}_{int(time.time())}"
status_msg = status_msg_passed
last_edit_time = 0
async def update_status_msg(text, force=False, show_cancel=False, retry_job_id=None):
nonlocal status_msg, last_edit_time
now = time.time()
if not force and (now - last_edit_time < 20):
return
try:
keyboard = []
if show_cancel:
keyboard.append([InlineKeyboardButton("❌ Cancel", callback_data=f"cancel:{task_id}")])
if retry_job_id:
keyboard.append([
InlineKeyboardButton("🔁 Retry upload now", callback_data=f"retryupload:{retry_job_id}")
])
reply_markup = InlineKeyboardMarkup(keyboard) if keyboard else None
if status_msg:
if status_msg.text != text:
await tg_retry(status_msg.edit_text, text, reply_markup=reply_markup)
last_edit_time = now
else:
status_msg = await tg_retry(application.bot.send_message,
chat_id=chat_id, text=text, reply_to_message_id=message_id, reply_markup=reply_markup
)
last_edit_time = now
except Exception as e:
logger.warning(f"Failed to update status: {e}")
# Initial Live Detection (from queue flag)
if is_live:
asyncio.create_task(process_live_stream_tracked(application, chat_id, url, message_id, status_msg, task_id, update_status_msg, channel_name, expected_live_video_id))
continue
await update_status_msg(f"🚀 Processing: {url}", force=True, show_cancel=True)
# Info extraction and secondary Live Detection
await update_status_msg("📊 Checking video info...", force=True, show_cancel=True)
video_info = {}
try:
loop = asyncio.get_running_loop()
# 45s timeout for extraction to avoid blocking the queue permanently
video_info = await asyncio.wait_for(
loop.run_in_executor(None, lambda: get_video_info(url)),
timeout=45
)
# If info extraction reveals it IS a live stream, handle it
if video_info.get('is_live'):
logger.info(f"URL detected as LIVE during info check: {url}")
channel_name = channel_name or video_info.get('uploader') or video_info.get('title', 'Live')
asyncio.create_task(process_live_stream_tracked(application, chat_id, url, message_id, status_msg, task_id, update_status_msg, channel_name, video_info.get('id')))
continue
except asyncio.TimeoutError:
logger.warning(f"Timeout checking info for {url}, proceeding with defaults")
except Exception as e:
logger.error(f"Error checking video info: {e}")
# Disk space check
config = load_config()
max_disk_gb = config.get('max_disk_gb', 0)
if max_disk_gb > 0:
estimated_mb = video_info.get('filesize_mb', 0)
if estimated_mb > 0:
can_download, remaining_gb = check_disk_space(estimated_mb)
if not can_download:
await update_status_msg(f"❌ Low disk space! Need {estimated_mb/1024:.1f}GB, have {remaining_gb:.1f}GB.", force=True)
continue
if task_id in cancelled_tasks:
if status_msg:
try: await tg_retry(status_msg.delete)
except: pass
cancelled_tasks.discard(task_id)
continue
loop = asyncio.get_running_loop()
def progress_cb(d):
if task_id in cancelled_tasks: raise Exception("Download cancelled")
if d['status'] == 'downloading':
p = d.get('_percent_str', '0%')
eta = d.get('_eta_str', '?')
mode = f"🎵 Audio {audio_format.upper()}" if audio_only else f"{max_height}p"
asyncio.run_coroutine_threadsafe(update_status_msg(f"⬇️ Downloading ({mode}): {p}\nETA: {eta}", show_cancel=True), loop)
# Download
try:
file_path, title, video_id, thumb_path = await loop.run_in_executor(
None,
lambda: download_content(url, progress_cb, audio_only=audio_only, audio_format=audio_format, max_height=max_height, task_id=task_id, cancelled_tasks=cancelled_tasks)
)
# Upload using helper
upload_completed = await handle_upload(
application, chat_id, file_path, title, url, audio_only,
update_status_msg, channel_name, message_id, thumb_path,
)
if not upload_completed:
# The retry worker owns the files and the status message.
# Do not delete either while it is attempting recovery.
continue
except Exception as e:
# Cleanup potential partial files on failure
logger.error(f"Download failed for {url}: {e}")
_cleanup_partial_downloads()
await update_status_msg(f"❌ Download failed: {e}", force=True)
continue
# Delete the progress/status message upon completion
if status_msg:
try:
await tg_retry(status_msg.delete)
except Exception as e:
logger.warning(f"Failed to delete status message: {e}")
except Exception as e:
logger.error(f"Error in process_queue: {e}")
await update_status_msg(f"🔥 Error: {e}", force=True)
finally:
request_queue.task_done()
_free_memory()
async def _kill_process(process, task_id):
"""Gracefully stop process: SIGINT → SIGTERM → SIGKILL."""
import signal
try:
process.send_signal(signal.SIGINT)
await asyncio.wait_for(process.wait(), timeout=20)
logger.info(f"[LIVE:{task_id}] Process stopped gracefully via SIGINT")
except asyncio.TimeoutError:
logger.warning(f"[LIVE:{task_id}] SIGINT timeout, sending SIGTERM")
try:
process.terminate()
await asyncio.wait_for(process.wait(), timeout=15)
logger.info(f"[LIVE:{task_id}] Process terminated via SIGTERM")
except asyncio.TimeoutError:
logger.warning(f"[LIVE:{task_id}] SIGTERM timeout, sending SIGKILL")
try:
process.kill()
await asyncio.wait_for(process.wait(), timeout=10)
except Exception:
logger.error(f"[LIVE:{task_id}] SIGKILL also failed, process may be orphaned")
except Exception as e:
logger.error(f"[LIVE:{task_id}] _kill_process error: {e}", exc_info=True)
async def process_live_stream(application, chat_id, url, message_id, status_msg, task_id, update_status_msg, channel_name, expected_live_video_id=None):
"""Use the live edge as a temporary backup while archiving from the start.
streamlink protects the live edge when YouTube has no VOD/DVR available,
while yt-dlp's independent --live-from-start process captures the archive.
Once the archive is healthy for ten minutes, stop and discard the duplicate
live-edge recording.
"""
SEGMENT_SIZE_BYTES = 1900 * 1024 * 1024 # 1.9GB per segment
logger.info(f"[LIVE:{task_id}] START url={url}, chat_id={chat_id}, channel={channel_name}")
fromstart_upload_tasks = []
fromstart_stable = asyncio.Event()
def _make_keyboard():
return InlineKeyboardMarkup([[
InlineKeyboardButton("⏹ Stop & Upload", callback_data=f"stoplive:{task_id}"),
InlineKeyboardButton("❌ Cancel", callback_data=f"cancel:{task_id}"),
]])
async def live_status(text):
nonlocal status_msg
try:
keyboard = _make_keyboard()
logger.info(f"[LIVE:{task_id}] live_status: '{text}'")
if status_msg:
if status_msg.text != text:
await tg_retry(status_msg.edit_text, text, reply_markup=keyboard)
else:
status_msg = await tg_retry(
application.bot.send_message,
chat_id=chat_id, text=text, reply_to_message_id=message_id, reply_markup=keyboard
)
except Exception as e:
logger.error(f"[LIVE:{task_id}] live_status failed: {e}", exc_info=True)
def _build_record_cmd(output_path, proxy=None):
"""streamlink command for live recording — writes continuously to file."""
cmd = [
'streamlink',
'--force',
'--loglevel', 'warning',
'--ffmpeg-ffmpeg', get_ffmpeg_command(),
'-o', output_path,
]
if proxy:
cmd += ['--http-proxy', proxy]
cmd += [url, 'best']
return cmd
def _build_fromstart_cmd(proxy=None):
"""yt-dlp command for from-start download, outputs to stdout."""
cmd = [
'yt-dlp',
'--no-part',
'--no-check-certificates',
'--no-playlist',
'--hls-use-mpegts',
'--live-from-start',
'--ffmpeg-location', get_ffmpeg_command(),
'--socket-timeout', '30',
'--retries', '10',
'--fragment-retries', '10',
'-o', '-',
]
cookie_file = get_cookie_file()
if cookie_file:
cmd += ['--cookies', cookie_file]
if proxy:
cmd += ['--proxy', proxy]
cmd.append(url)
return cmd
async def _download_from_start():
"""Archive from the beginning without interrupting the live-edge recorder.
A missing YouTube DVR/VOD is expected for some streams. In that case
this worker exits quietly and leaves the streamlink recording untouched.
"""
bg_id = f"{task_id}_fromstart"
logger.info(f"[LIVE:{bg_id}] Auto from-start archive starting (pipe mode)")
proxy_list = get_proxy_list()
proc = None
for proxy in proxy_list:
cmd = _build_fromstart_cmd(proxy)
logger.info(f"[LIVE:{bg_id}] cmd: {' '.join(cmd[:8])}...")
try:
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.DEVNULL,
)
logger.info(f"[LIVE:{bg_id}] pid={proc.pid}")
break
except Exception as e:
logger.error(f"[LIVE:{bg_id}] Spawn failed: {e}", exc_info=True)
continue
if proc is None:
logger.error(f"[LIVE:{bg_id}] All proxies failed to spawn")
await live_status(
"⚠️ From-start archive could not be started.\n"
"🔴 Continuing to record from the current live position."
)
return False
seg_num = 0
seg_file = None
seg_path = None
seg_bytes = 0
total_bytes = 0
start_time = time.time()
first_data_time = None
last_data_time = None
got_data = False
cancelled = False
try:
while True:
# Check signals
if task_id in cancelled_tasks:
cancelled = True
await _kill_process(proc, bg_id)
logger.info(f"[LIVE:{bg_id}] Cancelled")
return False
if task_id in stopped_tasks:
logger.info(f"[LIVE:{bg_id}] Stop signal received")
await _kill_process(proc, bg_id)
break
# Read chunk from pipe (non-blocking with timeout)
try:
chunk = await asyncio.wait_for(proc.stdout.read(1024 * 1024), timeout=5)
except asyncio.TimeoutError:
# No data yet — check if process died
if proc.returncode is not None:
break
# VOD unavailable check
elapsed = time.time() - start_time
if elapsed > 90 and not got_data:
logger.warning(f"[LIVE:{bg_id}] No data after {elapsed:.0f}s, VOD likely unavailable")
await _kill_process(proc, bg_id)
return False
if (
first_data_time is not None
and last_data_time is not None
and time.monotonic() - last_data_time > LIVE_FROM_START_MAX_DATA_GAP_SECONDS
):
logger.warning(
f"[LIVE:{bg_id}] No archive data for over "
f"{LIVE_FROM_START_MAX_DATA_GAP_SECONDS}s; resetting stability timer"
)
first_data_time = None
continue
if not chunk:
# EOF — yt-dlp finished
break
got_data = True
data_time = time.monotonic()
if first_data_time is None:
first_data_time = data_time
elif (
not fromstart_stable.is_set()
and data_time - first_data_time >= LIVE_FROM_START_STABILITY_SECONDS
):
fromstart_stable.set()
logger.info(
f"[LIVE:{bg_id}] From-start archive stable for "
f"{LIVE_FROM_START_STABILITY_SECONDS}s; retiring live-edge backup"
)
last_data_time = data_time
total_bytes += len(chunk)
# Open new segment file if needed
if seg_file is None:
seg_num += 1
seg_path = os.path.join(DOWNLOAD_DIR, f"live_{bg_id}_seg{seg_num:03d}.ts")
seg_file = open(seg_path, 'wb')
seg_bytes = 0
seg_file.write(chunk)
seg_bytes += len(chunk)
# Segment full — close, remux+upload, start next
if seg_bytes >= SEGMENT_SIZE_BYTES:
seg_file.close()
seg_file = None
seg_mp4 = os.path.join(DOWNLOAD_DIR, f"live_{bg_id}_seg{seg_num:03d}.mp4")
logger.info(f"[LIVE:{bg_id}] Segment {seg_num} complete: {seg_bytes/(1024*1024):.1f}MB (total: {total_bytes/(1024*1024):.0f}MB)")
fromstart_upload_tasks.append(
asyncio.create_task(_remux_and_upload_bg(bg_id, seg_path, seg_mp4, seg_num))
)
except Exception as e:
logger.error(f"[LIVE:{bg_id}] Pipe read error: {e}", exc_info=True)
finally:
# Close last segment and upload if it has data
if seg_file:
seg_file.close()
if not cancelled and seg_bytes > 1024:
seg_mp4 = os.path.join(DOWNLOAD_DIR, f"live_{bg_id}_seg{seg_num:03d}.mp4")
logger.info(f"[LIVE:{bg_id}] Final segment {seg_num}: {seg_bytes/(1024*1024):.1f}MB (total: {total_bytes/(1024*1024):.0f}MB)")
fromstart_upload_tasks.append(
asyncio.create_task(
_remux_and_upload_bg(bg_id, seg_path, seg_mp4, seg_num, is_final=True)
)
)
else:
try: os.remove(seg_path)
except: pass
elif seg_path and os.path.exists(seg_path) and os.path.getsize(seg_path) > 1024:
# Edge case: segment was closed by size limit but we need to mark last uploaded as final
pass
# Wait for proc to finish if still running
if proc.returncode is None:
try:
await asyncio.wait_for(proc.wait(), timeout=5)
except asyncio.TimeoutError:
await _kill_process(proc, bg_id)
if proc.returncode not in (None, 0) and not cancelled and not got_data:
logger.warning(
f"[LIVE:{bg_id}] From-start process exited rc={proc.returncode} before producing data"
)
await live_status(
"⚠️ From-start archive is unavailable (DVR/VOD is not enabled).\n"
"🔴 Continuing to record from the current live position."
)
if fromstart_upload_tasks:
if cancelled:
for upload_task in fromstart_upload_tasks:
upload_task.cancel()
await asyncio.gather(*fromstart_upload_tasks, return_exceptions=True)
_cleanup_live_files(bg_id)
logger.info(f"[LIVE:{bg_id}] Complete. Segments: {seg_num}, Total: {total_bytes/(1024*1024):.1f}MB")
return got_data
async def _remux_and_upload_bg(bg_id, ts_path, mp4_path, seg_num, is_final=False):
"""Remux a from-start segment and upload."""
try:
ts_size = os.path.getsize(ts_path) if os.path.exists(ts_path) else 0
if ts_size == 0:
try: os.remove(ts_path)
except: pass
return
logger.info(f"[LIVE:{bg_id}] Remuxing seg {seg_num}: {ts_size/(1024*1024):.1f}MB")
remux = await asyncio.create_subprocess_exec(
get_ffmpeg_command(), '-y',
'-err_detect', 'ignore_err',
'-fflags', '+genpts+discardcorrupt',
'-i', ts_path,
'-c', 'copy', '-movflags', '+faststart', mp4_path,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE,
)
await remux.stderr.read()
await remux.wait()
if remux.returncode != 0:
remux2 = await asyncio.create_subprocess_exec(
get_ffmpeg_command(), '-y',
'-err_detect', 'ignore_err',
'-fflags', '+genpts+discardcorrupt',
'-i', ts_path,
'-c', 'copy', mp4_path,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.DEVNULL,
)
await remux2.wait()
try: os.remove(ts_path)
except: pass
if os.path.exists(mp4_path) and os.path.getsize(mp4_path) > 0:
title = f"⏪ {channel_name} - From Start Part {seg_num}"
if is_final:
# This worker can EOF because its proxy vanished. The
# controller owns end-of-stream confirmation, so never
# label an archive fragment as the live stream's end.
title += " (Archive final segment)"
logger.info(f"[LIVE:{bg_id}] Uploading seg {seg_num}: {os.path.getsize(mp4_path)/(1024*1024):.1f}MB")
await handle_upload(
application, chat_id, mp4_path, title, url, False, None,
channel_name, message_id, allow_audio_download=False,
)
logger.info(f"[LIVE:{bg_id}] Upload done seg {seg_num}")
else:
logger.error(f"[LIVE:{bg_id}] Remux produced no output for seg {seg_num}")
except Exception as e:
logger.error(f"[LIVE:{bg_id}] Remux/upload seg {seg_num} error: {e}", exc_info=True)
async def _concat_parts(part_files, output_ts):
"""Concatenate multiple .ts part files using binary concat (TS is designed for this)."""
if len(part_files) == 1:
os.rename(part_files[0], output_ts)
return True
try:
with open(output_ts, 'wb') as out:
for p in part_files:
if os.path.exists(p) and os.path.getsize(p) > 0:
with open(p, 'rb') as inp:
while True:
chunk = inp.read(8 * 1024 * 1024)
if not chunk:
break
out.write(chunk)
if os.path.exists(output_ts) and os.path.getsize(output_ts) > 0:
for p in part_files:
try: os.remove(p)
except: pass
return True
else:
logger.error(f"[LIVE:{task_id}] binary concat produced empty file")
try: os.remove(output_ts)
except: pass
return False
except Exception as e:
logger.error(f"[LIVE:{task_id}] binary concat error: {e}")
try: os.remove(output_ts)
except: pass
return False
async def _remux_and_upload(ts_path, mp4_path, seg_num, is_final=False, completion_reason='confirmed_end'):
"""Background task: remux .ts to .mp4 and upload."""
try:
ts_size = os.path.getsize(ts_path) if os.path.exists(ts_path) else 0
if ts_size == 0:
try: os.remove(ts_path)
except: pass
return
logger.info(f"[LIVE:{task_id}] BG remux seg {seg_num}: {ts_size/(1024*1024):.1f}MB")
remux = await asyncio.create_subprocess_exec(
get_ffmpeg_command(), '-y',
'-err_detect', 'ignore_err',
'-fflags', '+genpts+discardcorrupt',
'-i', ts_path,
'-c', 'copy', '-movflags', '+faststart', mp4_path,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE,
)
await remux.stderr.read()
await remux.wait()
if remux.returncode != 0:
remux2 = await asyncio.create_subprocess_exec(
get_ffmpeg_command(), '-y',
'-err_detect', 'ignore_err',
'-fflags', '+genpts+discardcorrupt',