forked from yashoswalyo/MERGE-BOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
921 lines (863 loc) · 31.8 KB
/
bot.py
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
import asyncio
import os
import shutil
import string
import time
import shutil, psutil
import pyrogram
from hachoir import metadata
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
from PIL import Image
from pyrogram import Client, filters
from pyrogram.errors import MessageNotModified
from pyrogram.errors.rpc_error import UnknownError
from pyrogram.errors.exceptions.flood_420 import FloodWait
from pyrogram.types import (CallbackQuery, InlineKeyboardButton,InlineKeyboardMarkup, Message)
from pyromod import listen
from config import Config
from helpers import database
from helpers.display_progress import progress_for_pyrogram
from helpers.ffmpeg import MergeSub, MergeVideo, MergeSubNew
from helpers.uploader import uploadVideo
from helpers.utils import get_readable_time, get_readable_file_size
from helpers.rclone_upload import rclone_driver, rclone_upload
botStartTime = time.time()
mergeApp = Client(
session_name="merge-bot",
api_hash=Config.API_HASH,
api_id=Config.API_ID,
bot_token=Config.BOT_TOKEN,
workers=300,
app_version="3.0+yash-multiSubsSupport"
)
if os.path.exists('./downloads') == False:
os.makedirs('./downloads')
queueDB={}
formatDB={}
replyDB={}
@mergeApp.on_message( filters.command(['login']) & filters.private & ~filters.edited )
async def allowUser(c:Client, m: Message):
passwd = m.text.split()[-1]
if passwd == Config.PASSWORD:
await database.allowUser(uid=m.from_user.id)
await m.reply_text(
text=f"**Login passed ✅,**\n ⚡ Now you can you me!!",
quote=True
)
else:
await m.reply_text(
text=f"**Login failed ❌,**\n 🛡️ Unfortunately you can't use me\n\nContact: 🈲 @{Config.OWNER_USERNAME}",
quote=True
)
return
@mergeApp.on_message(filters.command(['stats']) & filters.private & filters.user(Config.OWNER))
async def stats_handler(c:Client, m:Message):
currentTime = get_readable_time(time.time() - botStartTime)
total, used, free = shutil.disk_usage('.')
total = get_readable_file_size(total)
used = get_readable_file_size(used)
free = get_readable_file_size(free)
sent = get_readable_file_size(psutil.net_io_counters().bytes_sent)
recv = get_readable_file_size(psutil.net_io_counters().bytes_recv)
cpuUsage = psutil.cpu_percent(interval=0.5)
memory = psutil.virtual_memory().percent
disk = psutil.disk_usage('/').percent
stats = f'<b>「 💠 BOT STATISTICS 」</b>\n' \
f'<b></b>\n' \
f'<b>⏳ Bot Uptime : {currentTime}</b>\n' \
f'<b>💾 Total Disk Space : {total}</b>\n' \
f'<b>📀 Total Used Space : {used}</b>\n' \
f'<b>💿 Total Free Space : {free}</b>\n' \
f'<b>🔺 Total Upload : {sent}</b>\n' \
f'<b>🔻 Total Download : {recv}</b>\n' \
f'<b>🖥 CPU : {cpuUsage}%</b>\n' \
f'<b>⚙️ RAM : {memory}%</b>\n' \
f'<b>💿 DISK : {disk}%</b>'
await m.reply_text(stats,quote=True)
@mergeApp.on_message(filters.command(['broadcast']) & filters.private & filters.user(Config.OWNER))
async def broadcast_handler(c:Client, m:Message):
msg = m.reply_to_message
userList = await database.broadcast()
len = userList.collection.count_documents({})
for i in range(len):
try:
await msg.copy(chat_id=userList[i]['_id'])
except FloodWait as e:
await asyncio.sleep(e.x)
await msg.copy(chat_id=userList[i]['_id'])
except Exception:
await database.deleteUser(userList[i]['_id'])
pass
print(f"Message sent to {userList[i]['name']} ")
await asyncio.sleep(2)
await m.reply_text(
text="🤓 __Broadcast completed sucessfully__",
quote=True
)
@mergeApp.on_message(filters.command(['start']) & filters.private & ~filters.edited)
async def start_handler(c: Client, m: Message):
await database.addUser(uid=m.from_user.id,fname=m.from_user.first_name, lname=m.from_user.last_name)
if await database.allowedUser(uid=m.from_user.id) is False:
res = await m.reply_text(
text=f"Hi **{m.from_user.first_name}**\n\n 🛡️ Unfortunately you can't use me\n\n**Contact: 🈲 @{Config.OWNER_USERNAME}** ",
quote=True
)
return
res = await m.reply_text(
text=f"Hi **{m.from_user.first_name}**\n\n ⚡ I am a file/video merger bot\n\n😎 I can merge Telegram files!, And upload it to telegram\n\n**Owner: 🈲 @{Config.OWNER_USERNAME}** ",
quote=True
)
@mergeApp.on_message((filters.document | filters.video) & filters.private & ~filters.edited)
async def video_handler(c: Client, m: Message):
if await database.allowedUser(uid=m.from_user.id) is False:
res = await m.reply_text(
text=f"Hi **{m.from_user.first_name}**\n\n 🛡️ Unfortunately you can't use me\n\n**Contact: 🈲 @{Config.OWNER_USERNAME}** ",
quote=True
)
return
input_ = f"downloads/{str(m.from_user.id)}/input.txt"
if os.path.exists(input_):
await m.reply_text("Sorry Bro,\nAlready One process in Progress!\nDon't Spam.")
return
media = m.video or m.document
currentFileNameExt = media.file_name.rsplit(sep='.')[-1].lower()
if media.file_name is None:
await m.reply_text('File Not Found')
return
if media.file_name.rsplit(sep='.')[-1].lower() in 'conf':
await m.reply_text(
text="**💾 Config file found, Do you want to save it?**",
reply_markup = InlineKeyboardMarkup(
[
[
InlineKeyboardButton("✅ Yes", callback_data=f"rclone_save"),
InlineKeyboardButton("❌ No", callback_data='rclone_discard')
]
]
),
quote=True
)
return
if currentFileNameExt == 'srt':
queueDB.get(m.from_user.id)['videos'].append(m.message_id)
queueDB.get(m.from_user.id)['subtitles'].append(None)
button = await MakeButtons(c,m,queueDB)
button.remove([InlineKeyboardButton("🔗 Merge Now", callback_data="merge")])
button.remove([InlineKeyboardButton("💥 Clear Files", callback_data="cancel")])
button.append([InlineKeyboardButton("🔗 Merge Subtitles", callback_data="mergeSubtitles")])
button.append([InlineKeyboardButton("💥 Clear Files", callback_data="cancel")])
await m.reply_text(
text="You send a subtitle file. Do you want to merge it?",
quote=True,
reply_markup= InlineKeyboardMarkup(button)
)
formatDB.update({m.from_user.id: currentFileNameExt})
return
if queueDB.get(m.from_user.id, None) is None:
formatDB.update({m.from_user.id: currentFileNameExt})
if (formatDB.get(m.from_user.id, None) is not None) and (currentFileNameExt != formatDB.get(m.from_user.id)):
await m.reply_text(f"First you sent a {formatDB.get(m.from_user.id).upper()} file so now send only that type of file.", quote=True)
return
if currentFileNameExt not in ['mkv','mp4','webm']:
await m.reply_text("This Video Format not Allowed!\nOnly send MP4 or MKV or WEBM.", quote=True)
return
editable = await m.reply_text("Please Wait ...", quote=True)
MessageText = "Okay,\nNow Send Me Next Video or Press **Merge Now** Button!"
if queueDB.get(m.from_user.id, None) is None:
queueDB.update({m.from_user.id: {'videos':[],'subtitles':[]}})
if (len(queueDB.get(m.from_user.id)['videos']) >= 0) and (len(queueDB.get(m.from_user.id)['videos'])<10 ):
queueDB.get(m.from_user.id)['videos'].append(m.message_id)
queueDB.get(m.from_user.id)['subtitles'].append(None)
print(queueDB.get(m.from_user.id)['videos'], queueDB.get(m.from_user.id)['subtitles'])
if len(queueDB.get(m.from_user.id)['videos']) == 1:
await editable.edit(
'**Send me some more videos to merge them into single file**',parse_mode='markdown'
)
return
if queueDB.get(m.from_user.id, None)['videos'] is None:
formatDB.update({m.from_user.id: media.file_name.split(sep='.')[-1].lower()})
if replyDB.get(m.from_user.id, None) is not None:
await c.delete_messages(chat_id=m.chat.id, message_ids=replyDB.get(m.from_user.id))
if len(queueDB.get(m.from_user.id)['videos']) == 10:
MessageText = "Okay Unkil, Now Just Press **Merge Now** Button Plox!"
markup = await MakeButtons(c, m, queueDB)
reply_ = await editable.edit(
text=MessageText,
reply_markup=InlineKeyboardMarkup(markup)
)
replyDB.update({m.from_user.id: reply_.message_id})
elif len(queueDB.get(m.from_user.id)['videos']) > 10:
markup = await MakeButtons(c,m,queueDB)
await editable.text(
"Max 10 videos allowed",
reply_markup=InlineKeyboardMarkup(markup)
)
@mergeApp.on_message(filters.photo & filters.private & ~filters.edited)
async def photo_handler(c: Client,m: Message):
if await database.allowedUser(uid=m.from_user.id) is False:
res = await m.reply_text(
text=f"Hi **{m.from_user.first_name}**\n\n 🛡️ Unfortunately you can't use me\n\n**Contact: 🈲 @{Config.OWNER_USERNAME}** ",
quote=True
)
return
thumbnail = m.photo.file_id
msg = await m.reply_text('Saving Thumbnail. . . .',quote=True)
await database.saveThumb(m.from_user.id,thumbnail)
LOCATION = f'./downloads/{m.from_user.id}_thumb.jpg'
await c.download_media(
message=m,
file_name=LOCATION
)
await msg.edit_text(
text="✅ Custom Thumbnail Saved!"
)
@mergeApp.on_message(filters.command(['help']) & filters.private & ~filters.edited)
async def help_msg(c: Client, m: Message):
await m.reply_text(
text='''**Follow These Steps:
1) Send me the custom thumbnail (optional).
2) Send two or more Your Videos Which you want to merge
3) After sending all files select merge options
4) Select the upload mode.
5) Select rename if you want to give custom file name else press default**''',
quote=True,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Close 🔐", callback_data="close")
]
]
)
)
@mergeApp.on_message( filters.command(['about']) & filters.private & ~filters.edited )
async def about_handler(c:Client,m:Message):
await m.reply_text(
text='''
- **WHAT'S NEW:**
+ Upload to drive using your own rclone config
+ Merged video preserves all streams of the first video you send (i.e. all audiotracks/subtitles)
- **FEATURES:**
+ Merge Upto 10 videos in one
+ Upload as document/video
+ Custom thumbnail support
+ Users can login to bot using password
+ Owner can broadcast message to all users
''',
quote=True,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Developer", url="https://t.me/yashoswalyo")
],
[
InlineKeyboardButton("Source Code", url="https://github.com/yashoswalyo/MERGE-BOT"),
InlineKeyboardButton("Deployed By", url=f"https://t.me/{Config.OWNER_USERNAME}")
]
]
)
)
@mergeApp.on_message(filters.command(['showthumbnail']) & filters.private & ~filters.edited)
async def show_thumbnail(c:Client ,m: Message):
try:
thumb_id = await database.getThumb(m.from_user.id)
LOCATION = f'./downloads/{m.from_user.id}_thumb.jpg'
await c.download_media(message=str(thumb_id),file_name=LOCATION)
if os.path.exists(LOCATION) is False:
await m.reply_text(text='❌ Custom thumbnail not found',quote=True)
else:
await m.reply_photo(photo=LOCATION, caption='🖼️ Your custom thumbnail', quote=True)
except Exception as err:
await m.reply_text(text='❌ Custom thumbnail not found',quote=True)
@mergeApp.on_message(filters.command(['deletethumbnail']) & filters.private & ~filters.edited)
async def delete_thumbnail(c: Client,m: Message):
try:
thumb_id = await database.getThumb(m.from_user.id)
LOCATION = f'./downloads/{m.from_user.id}_thumb.jpg'
await c.download_media(message=str(thumb_id),file_name=LOCATION)
if os.path.exists(LOCATION) is False:
await m.reply_text(text='❌ Custom thumbnail not found',quote=True)
else:
await database.delThumb(m.from_user.id)
os.remove(LOCATION)
await m.reply_text('✅ Deleted Sucessfully',quote=True)
except Exception as err:
await m.reply_text(text='❌ Custom thumbnail not found',quote=True)
@mergeApp.on_callback_query()
async def callback(c: Client, cb: CallbackQuery):
if cb.data == 'merge':
await cb.message.edit(
text='Where do you want to upload?',
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton('📤 To Telegram', callback_data = 'to_telegram'),
InlineKeyboardButton('🌫️ To Drive', callback_data = 'to_drive')
]
]
)
)
return
elif cb.data == "mergeSubtitles":
Config.upload_to_drive.update({f'{cb.from_user.id}':False})
await cb.message.edit(
text='How do yo want to upload file',
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton('🎞️ Video', callback_data='videoS'),
InlineKeyboardButton('📁 File', callback_data='documentS')
]
]
)
)
return
elif cb.data == 'to_drive':
try:
urc = await database.getUserRcloneConfig(cb.from_user.id)
await c.download_media(message=urc,file_name=f"userdata/{cb.from_user.id}/rclone.conf")
except Exception as err:
await cb.message.reply_text("Rclone not Found, Unable to upload to drive")
if os.path.exists(f"userdata/{cb.from_user.id}/rclone.conf") is False:
await cb.message.delete()
await delete_all(root=f"downloads/{cb.from_user.id}/")
queueDB.update({cb.from_user.id: {'videos':[],'subtitles':[]}})
formatDB.update({cb.from_user.id: None})
return
Config.upload_to_drive.update({f'{cb.from_user.id}':True})
await cb.message.edit(
text="Okay I'll upload to drive\nDo you want to rename? Default file name is **[@yashoswalyo]_merged.mkv**",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton('👆 Default', callback_data='rename_NO'),
InlineKeyboardButton('✍️ Rename', callback_data='rename_YES')
]
]
)
)
return
elif cb.data == 'to_telegram':
Config.upload_to_drive.update({f'{cb.from_user.id}':False})
await cb.message.edit(
text='How do yo want to upload file',
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton('🎞️ Video', callback_data='video'),
InlineKeyboardButton('📁 File', callback_data='document')
]
]
)
)
return
elif cb.data == 'document':
Config.upload_as_doc.update({f'{cb.from_user.id}':True})
await cb.message.edit(
text='Do you want to rename? Default file name is **[@yashoswalyo]_merged.mkv**',
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton('👆 Default', callback_data='rename_NO'),
InlineKeyboardButton('✍️ Rename', callback_data='rename_YES')
]
]
)
)
return
elif cb.data == 'video':
Config.upload_as_doc.update({f'{cb.from_user.id}':False})
await cb.message.edit(
text='Do you want to rename? Default file name is **[@yashoswalyo]_merged.mkv**',
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton('👆 Default', callback_data='rename_NO'),
InlineKeyboardButton('✍️ Rename', callback_data='rename_YES')
]
]
)
)
return
elif cb.data == 'documentS':
Config.upload_as_doc.update({f'{cb.from_user.id}':True})
await cb.message.edit(
text='Do you want to rename? Default file name is **[@yashoswalyo]_softmuxed_video.mkv**',
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton('👆 Default', callback_data='renameS_NO'),
InlineKeyboardButton('✍️ Rename', callback_data='renameS_YES')
]
]
)
)
return
elif cb.data == 'videoS':
Config.upload_as_doc.update({f'{cb.from_user.id}':False})
await cb.message.edit(
text=f"Do you want to rename? Default file name is **[@yashoswalyo]_softmuxed_video.mkv**",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton('👆 Default', callback_data='renameS_NO'),
InlineKeyboardButton('✍️ Rename', callback_data='renameS_YES')
]
]
)
)
return
elif cb.data.startswith('rclone_'):
if 'save' in cb.data:
fileId = cb.message.reply_to_message.document.file_id
print(fileId)
await c.download_media(
message=cb.message.reply_to_message,
file_name=f"./userdata/{cb.from_user.id}/rclone.conf"
)
await database.addUserRcloneConfig(cb, fileId)
else:
await cb.message.delete()
return
elif cb.data.startswith('rename_'):
if 'YES' in cb.data:
await cb.message.edit(
'Current filename: **[@yashoswalyo]_merged.mkv**\n\nSend me new file name without extension: You have 1 minute',
parse_mode='markdown'
)
res: Message = await c.listen( cb.message.chat.id,filters=filters.text, timeout=60 )
if res.text :
new_file_name = f"./downloads/{str(cb.from_user.id)}/{res.text.replace(' ','.')}.mkv"
await res.delete(True)
await mergeNow(c,cb,new_file_name)
return
if 'NO' in cb.data:
await mergeNow(c,cb,new_file_name = f"./downloads/{str(cb.from_user.id)}/[@yashoswalyo]_merged.mkv")
elif cb.data.startswith('renameS_'):
if 'YES' in cb.data:
await cb.message.edit(
'Current filename: **[@yashoswalyo]_softmuxed_video.mkv**\n\nSend me new file name without extension: You have 1 minute ',
parse_mode='markdown'
)
res: Message = await c.listen( cb.message.chat.id,filters=filters.text, timeout=300 )
if res.text :
new_file_name = f"./downloads/{str(cb.from_user.id)}/{res.text.replace(' ','.')}.mkv"
await res.delete(True)
await mergeSub(c,cb,new_file_name)
if 'NO' in cb.data:
await mergeSub(c,cb,new_file_name = f"./downloads/{str(cb.from_user.id)}/[@yashoswalyo]_softmuxed_video.mkv")
elif cb.data == 'cancel':
print("beta branch")
await delete_all(root=f"downloads/{cb.from_user.id}/")
queueDB.update({cb.from_user.id: {'videos':[],'subtitles':[]}})
formatDB.update({cb.from_user.id: None})
await cb.message.edit("Sucessfully Cancelled")
await asyncio.sleep(5)
await cb.message.delete(True)
return
elif cb.data == 'close':
await cb.message.delete(True)
elif cb.data.startswith('showFileName_'):
id = int(cb.data.rsplit("_",1)[-1])
print(queueDB.get(cb.from_user.id)['videos'],queueDB.get(cb.from_user.id)['subtitles'])
sIndex = queueDB.get(cb.from_user.id)['videos'].index(id)
m = await c.get_messages(chat_id=cb.message.chat.id,message_ids=id)
if queueDB.get(cb.from_user.id)['subtitles'][sIndex] is None:
try:
await cb.message.edit(
text=f"File Name: {m.video.file_name}",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("❌ Remove",callback_data=f"removeFile_{str(m.message_id)}"),
InlineKeyboardButton("📜 Add Subtitle", callback_data=f"addSub_{str(sIndex)}")
],
[InlineKeyboardButton("🔙 Back", callback_data="back")]
]
)
)
except:
await cb.message.edit(
text=f"File Name: {m.document.file_name}",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("❌ Remove",callback_data=f"removeFile_{str(m.message_id)}"),
InlineKeyboardButton("📜 Add Subtitle", callback_data=f"addSub_{str(sIndex)}")
],
[InlineKeyboardButton("🔙 Back", callback_data="back")]
]
)
)
return
else:
sMessId = queueDB.get(cb.from_user.id)['subtitles'][sIndex]
s = await c.get_messages(chat_id=cb.message.chat.id,message_ids=sMessId)
try:
await cb.message.edit(
text=f"File Name: {m.video.file_name}\n\nSubtitles: {s.document.file_name}",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("❌ Remove File",callback_data=f"removeFile_{str(m.message_id)}"),
InlineKeyboardButton("❌ Remove Subtitle", callback_data=f"removeSub_{str(sIndex)}")
],
[InlineKeyboardButton("🔙 Back", callback_data="back")]
]
)
)
except:
await cb.message.edit(
text=f"File Name: {m.document.file_name}\n\nSubtitles: {s.document.file_name}",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("❌ Remove File",callback_data=f"removeFile_{str(m.message_id)}"),
InlineKeyboardButton("❌ Remove Subtitle", callback_data=f"removeSub_{str(sIndex)}")
],
[InlineKeyboardButton("🔙 Back", callback_data="back")]
]
)
)
return
elif cb.data.startswith('addSub_'):
sIndex = int(cb.data.split(sep="_")[1])
vMessId = queueDB.get(cb.from_user.id)["videos"][sIndex]
rmess = await cb.message.edit(text=f"Send me a subtitle file, you have 1 minute",
reply_markup=InlineKeyboardMarkup(
[
[InlineKeyboardButton("🔙 Back", callback_data=f"showFileName_{vMessId}")]
]
)
)
subs:Message = await c.listen(cb.message.chat.id,filters="filters.document",timeout=60)
if subs is not None:
media = subs.document or subs.video
if media.file_name.rsplit(".")[-1] not in "srt":
await subs.reply_text(text=f"Please go back first",
reply_markup=InlineKeyboardMarkup(
[
[InlineKeyboardButton("🔙 Back", callback_data=f"showFileName_{vMessId}")]
]
),
quote=True
)
return
queueDB.get(cb.from_user.id)["subtitles"][sIndex] = subs.message_id
await subs.reply_text(f"Added {subs.document.file_name}",
reply_markup=InlineKeyboardMarkup(
[
[InlineKeyboardButton("🔙 Back", callback_data=f"showFileName_{vMessId}")]
]
),
quote=True
)
await rmess.delete(True)
print("Added sub to list")
return
elif cb.data.startswith('removeSub_'):
sIndex = int(cb.data.rsplit("_")[-1])
vMessId = queueDB.get(cb.from_user.id)["videos"][sIndex]
queueDB.get(cb.from_user.id)["subtitles"][sIndex] = None
await cb.message.edit(text=f"Subtitle Removed Now go back or send next video",
reply_markup=InlineKeyboardMarkup(
[
[InlineKeyboardButton("🔙 Back", callback_data=f"showFileName_{vMessId}")]
]
)
)
print('Sub removed from list')
return
elif cb.data == 'back':
await showQueue(c,cb)
return
elif cb.data.startswith('removeFile_'):
sIndex = queueDB.get(cb.from_user.id)['videos'].index(int(cb.data.split("_", 1)[-1]))
queueDB.get(cb.from_user.id)['videos'].remove(int(cb.data.split("_", 1)[-1]))
await showQueue(c,cb)
return
async def showQueue(c:Client, cb: CallbackQuery):
try:
markup = await MakeButtons(c,cb.message,queueDB)
await cb.message.edit(
text="Okay,\nNow Send Me Next Video or Press **Merge Now** Button!",
reply_markup=InlineKeyboardMarkup(markup)
)
except ValueError:
await cb.message.edit('Send Some more videos')
return
async def mergeSub(c:Client,cb:CallbackQuery,new_file_name:str):
print()
omess = cb.message.reply_to_message
vid_list = list()
await cb.message.edit('⭕ Processing...')
duration = 0
list_message_ids = queueDB.get(cb.from_user.id)["videos"]
list_message_ids.sort()
if list_message_ids is None:
await cb.answer("Queue Empty",show_alert=True)
await cb.message.delete(True)
return
if not os.path.exists(f'./downloads/{str(cb.from_user.id)}/'):
os.makedirs(f'./downloads/{str(cb.from_user.id)}/')
for i in (await c.get_messages(chat_id=cb.from_user.id,message_ids=list_message_ids)):
media = i.video or i.document
await cb.message.edit(f'📥 Starting Download of ... {media.file_name}')
print(f'📥 Starting Download of ... {media.file_name}')
await asyncio.sleep(5)
file_dl_path = None
try:
c_time = time.time()
file_dl_path = await c.download_media(
message=i.document,
file_name=f"./downloads/{str(cb.from_user.id)}/{str(i.message_id)}/",
progress=progress_for_pyrogram,
progress_args=(
f"🚀 Downloading: {media.file_name}",
cb.message,
c_time
)
)
await cb.message.edit(f"Downloaded Sucessfully:- {media.file_name}")
print(f"Downloaded Sucessfully:- {media.file_name}")
await asyncio.sleep(3)
except Exception as downloadErr:
print(f"Failed to download Error: {downloadErr}")
queueDB.get(cb.from_user.id)['videos'].remove(i.message_id)
await cb.message.edit("❗File Skipped!")
await asyncio.sleep(3)
await cb.message.delete(True)
continue
vid_list.append(f"{file_dl_path}")
subbed_video = await MergeSubNew(filePath=vid_list[0], subPath=vid_list[1],user_id=cb.from_user.id, vid_list=vid_list)
_cache = list()
if subbed_video is None:
await cb.message.edit("❌ Failed to add subs video !")
await delete_all(root=f'./downloads/{str(cb.from_user.id)}')
queueDB.update({cb.from_user.id: {'videos':[],'subtitles':[]}})
formatDB.update({cb.from_user.id: None})
return
await cb.message.edit("✅ Sucessfully Muxed Video !")
print(f"Video muxed for: {cb.from_user.first_name} ")
await asyncio.sleep(3)
file_size = os.path.getsize(subbed_video)
os.rename(subbed_video,new_file_name)
await cb.message.edit(f"🔄 Renaming Video to\n **{new_file_name.rsplit('/',1)[-1]}**")
await asyncio.sleep(2)
merged_video_path = new_file_name
if file_size > 2044723200:
await cb.message.edit("Video is Larger than 2GB Can't Upload")
await delete_all(root=f'./downloads/{str(cb.from_user.id)}')
queueDB.update({cb.from_user.id: {'videos':[],'subtitles':[]}})
formatDB.update({cb.from_user.id: None})
return
await cb.message.edit("🎥 Extracting Video Data ...")
duration = 1
width = 100
height = 100
try:
metadata = extractMetadata(createParser(merged_video_path))
if metadata.has("duration"):
duration = metadata.get("duration").seconds
if metadata.has("width"):
width = metadata.get("width")
if metadata.has("height"):
height = metadata.get("height")
except:
await delete_all(root=f'./downloads/{str(cb.from_user.id)}')
queueDB.update({cb.from_user.id: {'videos':[],'subtitles':[]}})
formatDB.update({cb.from_user.id: None})
await cb.message.edit("⭕ Merged Video is corrupted")
return
video_thumbnail = f'./downloads/{str(cb.from_user.id)}_thumb.jpg'
if os.path.exists(video_thumbnail) is False:
video_thumbnail=f"./assets/default_thumb.jpg"
else:
Image.open(video_thumbnail).convert("RGB").save(video_thumbnail)
img = Image.open(video_thumbnail)
# img.resize(width,height)
img.save(video_thumbnail,"JPEG")
await uploadVideo(
c=c,
cb=cb,
merged_video_path=merged_video_path,
width=width,
height=height,
duration=duration,
video_thumbnail=video_thumbnail,
file_size=os.path.getsize(merged_video_path),
upload_mode=Config.upload_as_doc[f'{cb.from_user.id}']
)
await cb.message.delete(True)
await delete_all(root=f'./downloads/{str(cb.from_user.id)}')
queueDB.update({cb.from_user.id: {'videos':[],'subtitles':[]}})
formatDB.update({cb.from_user.id: None})
return
async def mergeNow(c:Client, cb:CallbackQuery,new_file_name: str):
omess = cb.message.reply_to_message
# print(omess.message_id)
vid_list = list()
sub_list = list()
sIndex = 0
await cb.message.edit('⭕ Processing...')
duration = 0
list_message_ids = queueDB.get(cb.from_user.id)["videos"]
list_message_ids.sort()
list_subtitle_ids = queueDB.get(cb.from_user.id)["subtitles"]
# list_subtitle_ids.sort()
print(list_message_ids,list_subtitle_ids)
if list_message_ids is None:
await cb.answer("Queue Empty",show_alert=True)
await cb.message.delete(True)
return
if not os.path.exists(f'./downloads/{str(cb.from_user.id)}/'):
os.makedirs(f'./downloads/{str(cb.from_user.id)}/')
input_ = f"./downloads/{str(cb.from_user.id)}/input.txt"
for i in (await c.get_messages(chat_id=cb.from_user.id,message_ids=list_message_ids)):
media = i.video or i.document
await cb.message.edit(f'📥 Starting Download of ... {media.file_name}')
print(f'📥 Starting Download of ... {media.file_name}')
time.sleep(5)
file_dl_path = None
sub_dl_path = None
try:
c_time = time.time()
file_dl_path = await c.download_media(
message=media,
file_name=f"./downloads/{str(cb.from_user.id)}/{str(i.message_id)}/",
progress=progress_for_pyrogram,
progress_args=(
f"🚀 Downloading: {media.file_name}",
cb.message,
c_time
)
)
except Exception as downloadErr:
print(f"Failed to download Error: {downloadErr}")
queueDB.get(cb.from_user.id)["video"].remove(i.message_id)
await cb.message.edit("❗File Skipped!")
time.sleep(4)
continue
except UnknownError as e:
print("e")
pass
await cb.message.edit(f"Downloaded Sucessfully:- {media.file_name}")
print(f"Downloaded Sucessfully:- {media.file_name}")
time.sleep(5)
if list_subtitle_ids[sIndex] is not None:
a = await c.get_messages(chat_id=cb.from_user.id,message_ids=list_subtitle_ids[sIndex])
sub_dl_path = await c.download_media(message=a,file_name=f"./downloads/{str(cb.from_user.id)}/{str(a.message_id)}/")
print("Got sub",a.document.file_name)
file_dl_path = await MergeSub(file_dl_path,sub_dl_path,cb.from_user.id)
print("Added subs")
sIndex += 1
metadata = extractMetadata(createParser(file_dl_path))
try:
if metadata.has("duration"):
duration += metadata.get('duration').seconds
vid_list.append(f"file '{file_dl_path}'")
except:
await delete_all(root=f'./downloads/{str(cb.from_user.id)}')
queueDB.update({cb.from_user.id: {"videos":[],"subtitles":[]}})
formatDB.update({cb.from_user.id: None})
await cb.message.edit('⚠️ Video is corrupted')
return
_cache = list()
for i in range(len(vid_list)):
if vid_list[i] not in _cache:
_cache.append(vid_list[i])
vid_list = _cache
await cb.message.edit(f"🔀 Trying to merge videos ...")
with open(input_,'w') as _list:
_list.write("\n".join(vid_list))
merged_video_path = await MergeVideo(
input_file=input_,
user_id=cb.from_user.id,
message=cb.message,
format_='mkv'
)
if merged_video_path is None:
await cb.message.edit("❌ Failed to merge video !")
await delete_all(root=f'./downloads/{str(cb.from_user.id)}')
queueDB.update({cb.from_user.id: {"videos":[],"subtitles":[]}})
formatDB.update({cb.from_user.id: None})
return
await cb.message.edit("✅ Sucessfully Merged Video !")
print(f"Video merged for: {cb.from_user.first_name} ")
await asyncio.sleep(3)
file_size = os.path.getsize(merged_video_path)
os.rename(merged_video_path,new_file_name)
await cb.message.edit(f"🔄 Renamed Merged Video to\n **{new_file_name.rsplit('/',1)[-1]}**")
await asyncio.sleep(1)
merged_video_path = new_file_name
if Config.upload_to_drive[f'{cb.from_user.id}']:
await rclone_driver(omess,cb,merged_video_path)
await delete_all(root=f'./downloads/{str(cb.from_user.id)}')
queueDB.update({cb.from_user.id: {"videos":[],"subtitles":[]}})
formatDB.update({cb.from_user.id: None})
return
if file_size > 2044723200:
await cb.message.edit("Video is Larger than 2GB Can't Upload")
await delete_all(root=f'./downloads/{str(cb.from_user.id)}')
queueDB.update({cb.from_user.id: {"videos":[],"subtitles":[]}})
formatDB.update({cb.from_user.id: None})
return
await cb.message.edit("🎥 Extracting Video Data ...")
duration = 1
width = 100
height = 100
try:
metadata = extractMetadata(createParser(merged_video_path))
if metadata.has("duration"):
duration = metadata.get("duration").seconds
if metadata.has("width"):
width = metadata.get("width")
if metadata.has("height"):
height = metadata.get("height")
except:
await delete_all(root=f'./downloads/{str(cb.from_user.id)}')
queueDB.update({cb.from_user.id: {"videos":[],"subtitles":[]}})
formatDB.update({cb.from_user.id: None})
await cb.message.edit("⭕ Merged Video is corrupted")
return
video_thumbnail = f'./downloads/{str(cb.from_user.id)}_thumb.jpg'
if os.path.exists(video_thumbnail) is False:
video_thumbnail=f"./assets/default_thumb.jpg"
else:
Image.open(video_thumbnail).convert("RGB").save(video_thumbnail)
img = Image.open(video_thumbnail)
# img.resize(width,height)
img.save(video_thumbnail,"JPEG")
await uploadVideo(
c=c,
cb=cb,
merged_video_path=merged_video_path,
width=width,
height=height,
duration=duration,
video_thumbnail=video_thumbnail,
file_size=os.path.getsize(merged_video_path),
upload_mode=Config.upload_as_doc[f'{cb.from_user.id}']
)
await cb.message.delete(True)
await delete_all(root=f'./downloads/{str(cb.from_user.id)}')
queueDB.update({cb.from_user.id: {"videos":[],"subtitles":[]}})
formatDB.update({cb.from_user.id: None})
return
async def delete_all(root):
try:
shutil.rmtree(root)
except Exception as e:
print(e)
async def MakeButtons(bot: Client, m: Message, db: dict):
markup = []
for i in (await bot.get_messages(chat_id=m.chat.id, message_ids=db.get(m.chat.id)['videos'])):
media = i.video or i.document or None
if media is None:
continue
else:
markup.append([InlineKeyboardButton(f"{media.file_name}", callback_data=f"showFileName_{i.message_id}")])
markup.append([InlineKeyboardButton("🔗 Merge Now", callback_data="merge")])
markup.append([InlineKeyboardButton("💥 Clear Files", callback_data="cancel")])
return markup
mergeApp.run()