-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbot.py
More file actions
1027 lines (979 loc) · 47.5 KB
/
bot.py
File metadata and controls
1027 lines (979 loc) · 47.5 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 discord
import subprocess
import shlex
from discord.ext import commands, tasks
from discord import app_commands
# from discord import ui
import logging
import os
import json
import asyncio
import aiohttp
import random as rand
import folder
import datetime
import shutil
import downloader
from ffprobe import FFprobe
import humanize
import sqlite3
from urlextract import URLExtract
import parse
from catboxpy.catbox import CatboxClient
from dpy_paginator import paginate
from yt_dlp import YoutubeDL
import re
youguessedit = input('Run IHTX Bot? (Y/N)')
if youguessedit.upper() == 'Y':
print('Sent message to run.')
else:
print('Ok.')
exit()
global conn, c
conn = sqlite3.connect('templates.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS templates (
guild_id INTEGER,
name TEXT,
content TEXT,
user_id INTEGER,
PRIMARY KEY (guild_id, name)
)
''')
conn.commit()
prefix = ';'
theme = 0x6100FF
invitelink = "https://discord.com/oauth2/authorize?client_id=1417512460784504953&permissions=274878023680&integration_type=0&scope=bot"
bottoken = "YOUR_BOT_TOKEN_HERE" # place your bot token here
UPLOAD_DIR = "./uploads"
folder.makeDir(UPLOAD_DIR)
folder.clear(UPLOAD_DIR,True)
folder.makeDir(f"{UPLOAD_DIR}/ihtx")
# os.makedirs(UPLOAD_DIR, exist_ok=True)
FILE_EXTENSION = ".ts"
ADDITIONAL_COMPATIBILITY = ""
handler = logging.FileHandler(filename='discord.log',encoding='utf-8',mode='w')
intents = discord.Intents.all()
bot = commands.Bot(command_prefix=prefix, intents=intents)
statuses = ["for AMTVE","everyone","NotSoBot","TADC","Robot 64","SMG4","Numberblocks"]
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}!')
await bot.tree.sync() # Syncs all global commands
# For guild-specific commands, use: await bot.tree.sync(guild=discord.Object(id=YOUR_GUILD_ID))
print("Commands synced!")
change_status.start()
print('Status changed.')
@bot.event
async def on_guild_join(guild):
print(f"Joined new guild: {guild.name} (ID: {guild.id})")
icon = discord.File("icon.png", filename="icon.png")
embeder = discord.Embed(title=f"Thank you for inviting {bot.user.name} to {guild.name}.",description=f"{bot.user.name} is a I hate the X video editing bot. I hate the X is where a effect gets repeated by a amount of times. Say {prefix}help for all commands. It uses FFmpeg, so you have to have some FFmpeg knowledge to use this bot properly.",color=theme,timestamp=datetime.datetime.now())
embeder.set_footer(text=bot.user.name, icon_url="attachment://icon.png")
if guild.system_channel:
await guild.system_channel.send(embed=embeder,file=icon)
else:
# If no system channel is found, try to send to the first available text channel
for channel in guild.text_channels:
try:
await channel.send(embed=embeder,file=icon)
break # Send to the first available channel and break the loop
except discord.Forbidden:
continue # Bot doesn't have permission to send messages in this channel
@tasks.loop(seconds=10.0)
async def change_status():
await bot.change_presence(activity=discord.Activity(name=rand.choice(statuses),type=discord.ActivityType.watching),status=discord.Status.online)
# start of commands
bot.remove_command('help') # removes default help
class MyHelpCommand(commands.HelpCommand):
async def send_bot_help(self, mapping):
icon = discord.File("icon.png", filename="icon.png")
embed = discord.Embed(title=f"{bot.user.name} Help", description="Here are my commands:",color=theme)
for cog, commands in mapping.items():
if cog:
name = cog.qualified_name
else:
name = "No Category"
command_signatures = [self.get_command_signature(c) for c in commands]
for i, v in enumerate(commands):
if v.brief:
command_signatures[i] = command_signatures[i]+f" - {v.brief}"
if command_signatures:
embed.add_field(name=name, value="\n".join(command_signatures), inline=False)
embed.set_footer(text=f"{len(commands)} commands | "+bot.user.name, icon_url="attachment://icon.png")
await self.get_destination().send(embed=embed,file=icon)
async def send_cog_help(self, cog):
icon = discord.File("icon.png", filename="icon.png")
embed = discord.Embed(title=cog.qualified_name,color=theme)
if cog.description:
desc = cog.description
else:
desc = "-# _No description provided._"
commands = ""
for i, v in enumerate(cog.get_commands()):
commands += f"- {self.get_command_signature(v)}\n"
embed.add_field(name="Description",value=desc)
embed.add_field(name="Commands",value=commands)
embed.set_footer(text=bot.user.name, icon_url="attachment://icon.png")
await self.get_destination().send(embed=embed,file=icon)
async def send_command_help(self, command):
icon = discord.File("icon.png", filename="icon.png")
embed = discord.Embed(title=self.get_command_signature(command),color=theme)
if command.cog:
cogname = command.cog.qualified_name
else:
cogname = "No Category"
if command.help:
commandhelp = command.help
else:
commandhelp = "No help provided."
embed.add_field(name="Help",value=commandhelp,inline=False)
embed.add_field(name="Category",value=cogname,inline=True)
embed.add_field(name="Usage",value=command.usage,inline=True)
embed.set_footer(text=bot.user.name, icon_url="attachment://icon.png")
await self.get_destination().send(embed=embed,file=icon)
async def send_command_help(self, command):
icon = discord.File(f"icon.png", filename="icon.png")
embed = discord.Embed(title=self.get_command_signature(command),color=theme)
if command.cog:
cogname = command.cog.qualified_name
else:
cogname = "No Category"
if command.help:
commandhelp = command.help
else:
commandhelp = "No help provided."
embed.add_field(name="Help",value=commandhelp,inline=False)
embed.add_field(name="Category",value=cogname,inline=True)
embed.add_field(name="Usage",value=command.usage,inline=True)
embed.set_footer(text=bot.user.name, icon_url="attachment://icon.png")
await self.get_destination().send(embed=embed,file=icon)
bot.help_command = MyHelpCommand()
class FileLayout(discord.ui.LayoutView):
def __init__(self, file:discord.File, filename:str, timeout=60):
super().__init__()
self.file = file
self.filename = filename
container = discord.ui.Container(discord.ui.MediaGallery(discord.MediaGalleryItem(self.file)),accent_color=theme)
container.add_item(discord.ui.Separator())
probed = FFprobe(self.filename)
container.add_item(discord.ui.TextDisplay(f'-# {probed["streams"][0]["width"]}x{probed["streams"][0]["height"]}, {probed["streams"][0]["nb_frames"]} frames, {humanize.naturalsize(int(probed["format"]["size"]))}'))
self.container = container
def make(self):
t = discord.ui.LayoutView()
t.add_item(item=self.container)
return t
class FileLayoutURL(discord.ui.LayoutView):
def __init__(self, url:str, filename:str, timeout=60):
super().__init__()
self.url = url
self.filename = filename
container = discord.ui.Container(discord.ui.MediaGallery(discord.MediaGalleryItem(self.url)),accent_color=theme)
container.add_item(discord.ui.Separator())
probed = FFprobe(self.filename)
container.add_item(discord.ui.TextDisplay(f'-# {probed["streams"][0]["width"]}x{probed["streams"][0]["height"]}, {probed["streams"][0]["nb_frames"]} frames, {humanize.naturalsize(int(probed["format"]["size"]))}'))
self.container = container
def make(self):
t = discord.ui.LayoutView()
t.add_item(item=self.container)
return t
class FileLayout2(discord.ui.LayoutView):
def __init__(self, file:discord.File, filename:str, thing:dict, timeout=60):
super().__init__()
self.file = file
self.filename = filename
self.thing = thing
container = discord.ui.Container(discord.ui.MediaGallery(discord.MediaGalleryItem(self.file)),accent_color=theme)
container.add_item(discord.ui.Separator())
probed = FFprobe(self.filename)
container.add_item(discord.ui.TextDisplay(f'-# {probed["streams"][0]["width"]}x{probed["streams"][0]["height"]}, {probed["streams"][0]["nb_frames"]} frames, {humanize.naturalsize(int(probed["format"]["size"]))}'))
container.add_item(discord.ui.Separator())
stat = "-# "
for k, v in thing.items():
stat += f"{k}: {v}, "
stat = stat[:len(stat)-2]
container.add_item(discord.ui.TextDisplay(stat))
self.container = container
def make(self):
t = discord.ui.LayoutView()
t.add_item(item=self.container)
return t
def FileBiggerThan(mb:float,filename:str):
filesize_bytes = os.path.getsize(filename)
mb = mb * 1024 * 1024 # 8 MB converted to bytes
return filesize_bytes > mb
def catbox_upload(filename:str):
client = CatboxClient()
upload = client.upload(filename)
return upload
def getyoutubeid(url: str) -> str | None:
"""Extracts the YouTube video ID from a URL using regex."""
pattern = r'(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})'
match = re.search(pattern, url)
if match:
return match.group(1)
return None
@bot.command()
async def suggest(ctx):
await ctx.send("[Suggest any ideas here.](https://amtve.straw.page/suggestihtxbot)")
@bot.command()
async def command(ctx,*,command):
"""
Execute a FFmpeg command on a video file, works on reply.
Example usage:
;command -vf "scale=320:240"
"""
loadMessage = await ctx.reply("> <a:load:1417523157824442599> Loading Video (1/3)", mention_author=False)
file_names = [os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4"),os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{ctx.author.id}.mp4")]
for i, v in enumerate(file_names):
if os.path.exists(v):
await loadMessage.edit(content="> :warning: You still have a process still running. Please wait.")
return
attachment_url = None
# Check for attachments or replied media
if ctx.message.reference:
referenced_message = await ctx.channel.fetch_message(ctx.message.reference.message_id)
if referenced_message.attachments:
attachment_url = referenced_message.attachments[0].url
else:
extractor = URLExtract()
urls = extractor.find_urls(referenced_message.content)
if urls:
attachment_url = urls[0]
if not referenced_message.components == []:
attachment_url = referenced_message.components[0].children[0].items[0].media.url
if not attachment_url and ctx.message.attachments:
attachment_url = ctx.message.attachments[0].url
if not attachment_url:
await loadMessage.edit(content="> :x: Please provide a video attachment or link.")
return
else:
await loadMessage.edit(content="> <a:load:1417523157824442599> Downloading Video (2/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
try:
# Download the file
file_name = os.path.join(UPLOAD_DIR, f"input_{ctx.author.id}.mp4")
await downloader.from_url(attachment_url,file_name)
probet = FFprobe(file_name)
w = probet["streams"][0]["width"]
h = probet["streams"][0]["height"]
fc = probet["streams"][0]["nb_frames"]
await loadMessage.edit(content="> <a:load:1417523157824442599> Processing Video (3/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
command = command.replace("$w",str(w))
command = command.replace("$h",str(h))
command = command.replace("$fc",str(fc))
sanitized_command = shlex.split(command)
# Prepare output file
output_file = os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4")
if not any(arg.endswith(".mp4") for arg in sanitized_command):
sanitized_command.append(output_file)
# Run FFmpeg command asynchronously
process = await asyncio.create_subprocess_exec(
"ffmpeg",
"-i", file_name,
*sanitized_command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = await process.communicate()
if os.path.exists(output_file):
file = discord.File(output_file)
view = FileLayout(file, output_file)
# "Command executed successfully."
await loadMessage.delete()
await asyncio.sleep(0.5)
await ctx.reply(file=file,view=view.make())
os.remove(output_file)
else:
await loadMessage.edit(content=f"> :x: Error executing command:\n```{stderr.decode()}```")
if os.path.exists(file_name):
os.remove(file_name)
except Exception as e:
if os.path.exists(file_name):
os.remove(file_name)
await loadMessage.edit(content=f"> :x: An error occurred: {str(e)}")
@bot.command()
async def sync(ctx):
"""
Execute a FFmpeg command on a video file, works on reply.
Usage: ;sync
"""
loadMessage = await ctx.reply("> <a:load:1417523157824442599> Loading Video (1/3)", mention_author=False)
file_names = [os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4"),os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{ctx.author.id}.mp4")]
for i, v in enumerate(file_names):
if os.path.exists(v):
await loadMessage.edit(content="> :warning: You still have a process still running. Please wait.")
return
attachment_url = None
# Check for attachments or replied media
if ctx.message.reference:
referenced_message = await ctx.channel.fetch_message(ctx.message.reference.message_id)
if referenced_message.attachments:
attachment_url = referenced_message.attachments[0].url
else:
extractor = URLExtract()
urls = extractor.find_urls(referenced_message.content)
if urls:
attachment_url = urls[0]
if not referenced_message.components == []:
attachment_url = referenced_message.components[0].children[0].items[0].media.url
if not attachment_url and ctx.message.attachments:
attachment_url = ctx.message.attachments[0].url
if not attachment_url:
await loadMessage.edit(content="> :x: Please provide a video attachment or link.")
return
else:
await loadMessage.edit(content="> <a:load:1417523157824442599> Downloading Video (2/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
try:
# Download the file
file_name = os.path.join(UPLOAD_DIR, f"input_{ctx.author.id}.mp4")
await downloader.from_url(attachment_url,file_name)
probet = FFprobe(file_name)
vdur = float(probet["streams"][0]["duration"])
adur = float(probet["streams"][1]["duration"])
fps = probet["streams"][0]["r_frame_rate"]
speed = str(vdur/adur)
diff = str(vdur-adur)
await loadMessage.edit(content="> <a:load:1417523157824442599> Processing Video (3/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
sanitized_command = shlex.split(f"-vf setpts=1/{speed}*PTS,fps={fps}")
# Prepare output file
output_file = os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4")
if not any(arg.endswith(".mp4") for arg in sanitized_command):
sanitized_command.append(output_file)
# Run FFmpeg command asynchronously
process = await asyncio.create_subprocess_exec(
"ffmpeg",
"-i", file_name,
*sanitized_command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = await process.communicate()
if os.path.exists(output_file):
file = discord.File(output_file)
huhg = {}
huhg["Video"] = adur
huhg["Audio"] = vdur
huhg["Speed used"] = speed
huhg["Difference"] = diff
view = FileLayout2(file, output_file, huhg)
# "Command executed successfully."
await loadMessage.delete()
await asyncio.sleep(0.5)
await ctx.reply(file=file,view=view.make())
os.remove(output_file)
else:
await loadMessage.edit(content=f"> :x: Error executing command:\n```{stderr.decode()}```")
if os.path.exists(file_name):
os.remove(file_name)
except Exception as e:
if os.path.exists(file_name):
os.remove(file_name)
await loadMessage.edit(content=f"> :x: An error occurred: {str(e)}")
@bot.command(aliases=["fix","fixvideo"])
async def reencode(ctx):
"""
Re-encodes a video. So it can work with bot commands properly.
Usage: ;reencode, ;fix
"""
loadMessage = await ctx.reply("> <a:load:1417523157824442599> Loading Video (1/3)", mention_author=False)
file_names = [os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4"),os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{ctx.author.id}.mp4")]
for i, v in enumerate(file_names):
if os.path.exists(v):
await loadMessage.edit(content="> :warning: You still have a process still running. Please wait.")
return
attachment_url = None
# Check for attachments or replied media
if ctx.message.reference:
referenced_message = await ctx.channel.fetch_message(ctx.message.reference.message_id)
if referenced_message.attachments:
attachment_url = referenced_message.attachments[0].url
else:
extractor = URLExtract()
urls = extractor.find_urls(referenced_message.content)
if urls:
attachment_url = urls[0]
if not referenced_message.components == []:
attachment_url = referenced_message.components[0].children[0].items[0].media.url
if not attachment_url and ctx.message.attachments:
attachment_url = ctx.message.attachments[0].url
if not attachment_url:
async for message in ctx.channel.history(limit=50):
if message.attachments:
attachment_url = message.attachments[0].url
break
if not attachment_url:
await loadMessage.edit(content="> :x: Please provide a video attachment or link.")
return
else:
await loadMessage.edit(content="> <a:load:1417523157824442599> Downloading Video (2/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
try:
# Download the file
file_name = os.path.join(UPLOAD_DIR, f"input_{ctx.author.id}.mp4")
await downloader.from_url(attachment_url,file_name)
await loadMessage.edit(content="> <a:load:1417523157824442599> Reencoding Video (3/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
sanitized_command = shlex.split("-c:v libx264 -crf 23 -preset medium -c:a aac -b:a 192k")
# Prepare output file
output_file = os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4")
if not any(arg.endswith(".mp4") for arg in sanitized_command):
sanitized_command.append(output_file)
# Run FFmpeg command asynchronously
process = await asyncio.create_subprocess_exec(
"ffmpeg",
"-i", file_name,
*sanitized_command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = await process.communicate()
if os.path.exists(output_file):
file = discord.File(output_file)
view = FileLayout(file, output_file)
await loadMessage.delete()
await asyncio.sleep(0.5)
await ctx.reply(file=file,view=view.make())
os.remove(output_file)
else:
await loadMessage.edit(content=f"> :x: Error executing command:\n```{stderr.decode()}```")
if os.path.exists(file_name):
os.remove(file_name)
except Exception as e:
if os.path.exists(file_name):
os.remove(file_name)
await loadMessage.edit(content=f"> :x: An error occurred: {str(e)}")
async def FFrun(i,co,o,time):
try:
# subprocess.run(shlex.split(f"ffmpeg -i {i} {co} -t {time} {o}"),check=True,capture_output=True)
splitter = shlex.split(co)
proc = await asyncio.create_subprocess_exec("ffmpeg","-i",i,*splitter,"-t",str(time),"-b:v","5000k",o,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout, stderr = await proc.communicate()
if proc.returncode != 0:
print(f"Subprocess failed with exit code {proc.returncode}")
print(f"Stderr: {stderr.decode().strip()}")
# else:
# print(f"Subprocess completed successfully.")
# print(f"Stdout: {stdout.decode().strip()}")
except Exception as e:
print(str(e))
async def FFconcat(items,out):
conc = "|".join(items)
try:
# subprocess.run(shlex.split(f'ffmpeg -i "concat:{conc}" {out}'),check=True,capture_output=True)
proc = await asyncio.create_subprocess_exec("ffmpeg","-i",f"concat:{conc}",out,"-b:v","5000k","-c","copy",stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout, stderr = await proc.communicate()
if proc.returncode != 0:
print(f"Subprocess failed with exit code {proc.returncode}")
print(f"Stderr: {stderr.decode().strip()}")
# else:
# print(f"Subprocess completed successfully.")
# print(f"Stdout: {stdout.decode().strip()}")
except Exception as e:
print(str(e))
@bot.command()
async def ihtx(ctx,powers,time,usetemplate:bool,*,command):
loadMessage = await ctx.reply("> <a:load:1417523157824442599> Loading Video (1/3)", mention_author=False)
file_names = [os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4"),os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{ctx.author.id}.mp4")]
for i, v in enumerate(file_names):
if os.path.exists(v):
await loadMessage.edit(content="> :warning: You still have a process still running. Please wait.")
return
attachment_url = None
# Check for attachments or replied media
if ctx.message.reference:
referenced_message = await ctx.channel.fetch_message(ctx.message.reference.message_id)
if referenced_message.attachments:
attachment_url = referenced_message.attachments[0].url
else:
extractor = URLExtract()
urls = extractor.find_urls(referenced_message.content)
if urls:
attachment_url = urls[0]
if not referenced_message.components == []:
attachment_url = referenced_message.components[0].children[0].items[0].media.url
if not attachment_url and ctx.message.attachments:
attachment_url = ctx.message.attachments[0].url
if not attachment_url:
async for message in ctx.channel.history(limit=50):
if message.attachments:
attachment_url = message.attachments[0].url
break
if not attachment_url:
await loadMessage.edit(content="> :x: Please provide a video attachment or link.")
return
else:
await loadMessage.edit(content="> <a:load:1417523157824442599> Downloading Video (2/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
try:
# Download the file
file_name = os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{ctx.author.id}.mp4")
await downloader.from_url(attachment_url,file_name)
await loadMessage.edit(content="> <a:load:1417523157824442599> Processing Video (3/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
if usetemplate == False:
sanitized_command = shlex.split(command)
probet = FFprobe(file_name)
w = probet["streams"][0]["width"]
h = probet["streams"][0]["height"]
fc = probet["streams"][0]["nb_frames"]
vidlen = probet["streams"][0]["duration"]
com = command.replace("$w",str(w))
com = com.replace("$h",str(h))
com = com.replace("$fc",str(fc))
com = com.replace("$d",str(vidlen))
time = str(vidlen) if time == "vidlen" else time
# Prepare output file
output_file = os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"ihtx_custom_{ctx.author.id}.mp4")
powers = int(powers)
await FFrun(file_name,com,os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"{ctx.author.id}_1{FILE_EXTENSION}"),time)
files = []
for i in range(powers):
files.append(os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"{ctx.author.id}_{i+1}{FILE_EXTENSION}"))
for i, v in enumerate(files):
await FFrun(v,com,os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"{ctx.author.id}_{i+2}{FILE_EXTENSION}"),time)
await FFconcat(files,output_file)
for i in range(powers+1):
if os.path.exists(os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"{ctx.author.id}_{i+1}{FILE_EXTENSION}")):
os.remove(os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"{ctx.author.id}_{i+1}{FILE_EXTENSION}"))
# if os.path.exists(output_file):
# file = discord.File(output_file)
# view = FileLayout(file, output_file)
# "Command executed successfully."
# await loadMessage.delete()
# await asyncio.sleep(0.5)
# await ctx.reply(file=file,view=view.make())
# os.remove(output_file)
# os.remove(file_name)
# else:
# os.remove(file_name)
# await loadMessage.edit(content="> :x: Error executing command")
else:
c.execute('SELECT content FROM templates WHERE guild_id = ? AND name = ?', (ctx.guild.id, command))
result = c.fetchone()
if result:
probet = FFprobe(file_name)
w = probet["streams"][0]["width"]
h = probet["streams"][0]["height"]
fc = probet["streams"][0]["nb_frames"]
vidlen = probet["streams"][0]["duration"]
com = result[0].replace("$w",str(w))
com = com.replace("$h",str(h))
com = com.replace("$fc",str(fc))
com = com.replace("$d",str(vidlen))
time = str(vidlen) if time == "vidlen" else time
# Prepare output file
output_file = os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"ihtx_custom_{ctx.author.id}.mp4")
powers = int(powers)
await FFrun(file_name,com,os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"{ctx.author.id}_1{FILE_EXTENSION}"),time)
files = []
for i in range(powers):
files.append(os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"{ctx.author.id}_{i+1}{FILE_EXTENSION}"))
for i, v in enumerate(files):
await FFrun(v,com,os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"{ctx.author.id}_{i+2}{FILE_EXTENSION}"),time)
await FFconcat(files,output_file)
for i in range(powers+1):
if os.path.exists(os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"{ctx.author.id}_{i+1}{FILE_EXTENSION}")):
os.remove(os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"{ctx.author.id}_{i+1}{FILE_EXTENSION}"))
else:
os.remove(file_name)
await loadMessage.edit(content="> :x: Template does not exist.")
if os.path.exists(output_file):
if not FileBiggerThan(10.0,output_file):
file = discord.File(output_file)
view = FileLayout(file, output_file)
await loadMessage.delete()
await asyncio.sleep(0.5)
await ctx.reply(file=file,view=view.make())
os.remove(output_file)
os.remove(file_name)
else:
catbox_url = catbox_upload(output_file)
await loadMessage.delete()
await asyncio.sleep(0.5)
await ctx.reply(f"> Since the video was over 10MB it got uploaded over to Catbox.\n{catbox_url}")
os.remove(output_file)
os.remove(file_name)
else:
os.remove(file_name)
await loadMessage.edit(content="> :x: Error executing command")
except Exception as e:
if os.path.exists(file_name):
os.remove(file_name)
await loadMessage.edit(content=f"> :x: An error occurred: {str(e)}")
@bot.command(aliases=["fe,fec,first,first_export_custom"])
async def first_export(ctx,time):
"""
Trims the video to the first export by a specified duration.
"""
loadMessage = await ctx.reply("> <a:load:1417523157824442599> Loading Video (1/3)", mention_author=False)
file_names = [os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4"),os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{ctx.author.id}.mp4")]
for i, v in enumerate(file_names):
if os.path.exists(v):
await loadMessage.edit(content="> :warning: You still have a process still running. Please wait.")
return
attachment_url = None
# Check for attachments or replied media
if ctx.message.reference:
referenced_message = await ctx.channel.fetch_message(ctx.message.reference.message_id)
if referenced_message.attachments:
attachment_url = referenced_message.attachments[0].url
if not referenced_message.components == []:
attachment_url = referenced_message.components[0].children[0].items[0].media.url
if not attachment_url and ctx.message.attachments:
attachment_url = ctx.message.attachments[0].url
if not attachment_url:
await loadMessage.edit(content="> :x: Please provide a video attachment or link.")
return
else:
await loadMessage.edit(content="> <a:load:1417523157824442599> Downloading Video (2/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
try:
# Download the file
file_name = os.path.join(UPLOAD_DIR, f"input_{ctx.author.id}.mp4")
await downloader.from_url(attachment_url,file_name)
await loadMessage.edit(content="> <a:load:1417523157824442599> Processing Video (3/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
output_file = os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4")
subprocess.run(shlex.split(f"ffmpeg -i {file_name} -t {time} {output_file}"),check=True,capture_output=True)
if os.path.exists(output_file):
file = discord.File(output_file)
view = FileLayout(file, output_file)
await loadMessage.delete()
await asyncio.sleep(0.5)
await ctx.reply(file=file,view=view.make())
os.remove(output_file)
os.remove(file_name)
else:
os.remove(file_name)
await loadMessage.edit(content="> :x: Error executing command")
except Exception as e:
await loadMessage.edit(content=f"> :x: An error occurred: {str(e)}")
@bot.command(aliases=["le","lec","last","last_export_custom"])
async def last_export(ctx,time):
"""
Trims the video to the last export by a specified duration.
"""
loadMessage = await ctx.reply("> <a:load:1417523157824442599> Loading Video (1/3)", mention_author=False)
file_names = [os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4"),os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{ctx.author.id}.mp4")]
for i, v in enumerate(file_names):
if os.path.exists(v):
await loadMessage.edit(content="> :warning: You still have a process still running. Please wait.")
return
attachment_url = None
# Check for attachments or replied media
if ctx.message.reference:
referenced_message = await ctx.channel.fetch_message(ctx.message.reference.message_id)
if referenced_message.attachments:
attachment_url = referenced_message.attachments[0].url
if not referenced_message.components == []:
attachment_url = referenced_message.components[0].children[0].items[0].media.url
if not attachment_url and ctx.message.attachments:
attachment_url = ctx.message.attachments[0].url
if not attachment_url:
await loadMessage.edit(content="> :x: Please provide a video attachment or link.")
return
else:
await loadMessage.edit(content="> <a:load:1417523157824442599> Downloading Video (2/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
try:
# Download the file
file_name = os.path.join(UPLOAD_DIR, f"input_{ctx.author.id}.mp4")
await downloader.from_url(attachment_url,file_name)
await loadMessage.edit(content="> <a:load:1417523157824442599> Processing Video (3/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
output_file = os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4")
subprocess.run(shlex.split(f"ffmpeg -i {file_name} -vf reverse,trim=0:{time},reverse -af areverse,atrim=0:{time},areverse {output_file}"),check=True,capture_output=True)
if os.path.exists(output_file):
file = discord.File(output_file)
view = FileLayout(file, output_file)
await loadMessage.delete()
await asyncio.sleep(0.5)
await ctx.reply(file=file,view=view.make())
os.remove(output_file)
os.remove(file_name)
else:
os.remove(file_name)
await loadMessage.edit(content="> :x: Error executing command")
except Exception as e:
await loadMessage.edit(content=f"> :x: An error occurred: {str(e)}")
@bot.command()
async def invite(ctx):
"""
Invite the bot to your server or use anywhere!
"""
await ctx.reply(f"[Click here to invite the bot anywhere!]({invitelink})",mention_author=False)
@bot.command()
async def interpolate(ctx):
"""
Interpolates a video to 60 FPS.
"""
loadMessage = await ctx.reply("> <a:load:1417523157824442599> Loading Video (1/3)", mention_author=False)
file_names = [os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4"),os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{ctx.author.id}.mp4")]
for i, v in enumerate(file_names):
if os.path.exists(v):
await loadMessage.edit(content="> :warning: You still have a process still running. Please wait.")
return
attachment_url = None
# Check for attachments or replied media
if ctx.message.reference:
referenced_message = await ctx.channel.fetch_message(ctx.message.reference.message_id)
if referenced_message.attachments:
attachment_url = referenced_message.attachments[0].url
if not referenced_message.components == []:
attachment_url = referenced_message.components[0].children[0].items[0].media.url
if not attachment_url and ctx.message.attachments:
attachment_url = ctx.message.attachments[0].url
if not attachment_url:
await loadMessage.edit(content="> :x: Please provide a video attachment or link.")
return
else:
await loadMessage.edit(content="> <a:load:1417523157824442599> Downloading Video (2/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
try:
# Download the file
file_name = os.path.join(UPLOAD_DIR, f"input_{ctx.author.id}.mp4")
await downloader.from_url(attachment_url,file_name)
await loadMessage.edit(content="> <a:load:1417523157824442599> Processing Video (3/3)",allowed_mentions=discord.AllowedMentions(replied_user=False))
output_file = os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4")
subprocess.run(shlex.split(f'ffmpeg -i {file_name} -vf "fps=30,minterpolate=mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=60" {output_file}'),check=True,capture_output=True)
if os.path.exists(output_file):
file = discord.File(output_file)
view = FileLayout(file, output_file)
await loadMessage.delete()
await asyncio.sleep(0.5)
await ctx.reply(file=file,view=view.make())
os.remove(output_file)
os.remove(file_name)
else:
os.remove(file_name)
await loadMessage.edit(content="> :x: Error executing command")
except Exception as e:
await loadMessage.edit(content=f"> :x: An error occurred: {str(e)}")
@bot.command(name='createtemplate',aliases=["addtemplate"])
async def create_template(ctx, name: str, *, content: str):
try:
# Store the user's ID
c.execute('INSERT INTO templates (guild_id, name, content, user_id) VALUES (?, ?, ?, ?)', (ctx.guild.id, name, content, ctx.author.id))
conn.commit()
await ctx.send(f'> Successfully created template `{name}`')
except sqlite3.IntegrityError:
await ctx.send(f'> A template with the name `{name}` already exists.')
@bot.command()
async def template(ctx, name: str, *, args: str=""):
c.execute('SELECT content FROM templates WHERE guild_id = ? AND name = ?', (ctx.guild.id, name))
result = c.fetchone()
if result:
processed_content = parse.TemplateParse(result[0],args)
await ctx.send(processed_content)
else:
await ctx.send(f'Template `{name}` not found.')
@bot.command()
async def viewtemplate(ctx, name: str):
c.execute('SELECT content FROM templates WHERE guild_id = ? AND name = ?', (ctx.guild.id, name))
result = c.fetchone()
if result:
embed = discord.Embed(title=name,description=f"```{result[0]}```",color=theme)
await ctx.send(embed=embed)
else:
await ctx.send(f'Template `{name}` not found.')
@bot.command(name='edittemplate')
async def edit_template(ctx, name: str, *, new_content: str):
# The WHERE clause now includes user_id
c.execute('UPDATE templates SET content = ? WHERE guild_id = ? AND name = ? AND user_id = ?', (new_content, ctx.guild.id, name, ctx.author.id))
conn.commit()
if c.rowcount > 0:
await ctx.send(f'Template `{name}` updated successfully!')
else:
# Check if the template exists at all to provide better feedback
c.execute('SELECT user_id FROM templates WHERE guild_id = ? AND name = ?', (ctx.guild.id, name))
result = c.fetchone()
if result:
await ctx.send(f'You are not the owner of the template `{name}`.')
else:
await ctx.send(f'Could not find a template named `{name}` to update.')
@bot.command(name='removetemplate')
async def remove_template(ctx, name: str):
# The DELETE statement now includes the user_id for an ownership check
c.execute('DELETE FROM templates WHERE guild_id = ? AND name = ? AND user_id = ?', (ctx.guild.id, name, ctx.author.id))
conn.commit()
if c.rowcount > 0:
await ctx.send(f'Template `{name}` removed successfully!')
else:
# Check if the template exists at all to provide better feedback
c.execute('SELECT user_id FROM templates WHERE guild_id = ? AND name = ?', (ctx.guild.id, name))
result = c.fetchone()
if result:
await ctx.send(f'You are not the owner of the template `{name}`.')
else:
await ctx.send(f'Could not find a template named `{name}` to remove.')
@bot.command(name='templatelist')
async def template_list(ctx, user: discord.Member = None):
"""Lists templates for the current server or a specific user."""
embed = discord.Embed(title="Server Templates", color=theme)
if user:
# Fetch templates for a specific user
c.execute('SELECT name FROM templates WHERE guild_id = ? AND user_id = ?', (ctx.guild.id, user.id))
user_templates = c.fetchall()
if user_templates:
# Format the user's templates into a readable string
template_names = [f"`{t[0]}`" for t in user_templates]
embed.add_field(name=f"Templates by {user.display_name}", value=", ".join(template_names), inline=False)
else:
embed.add_field(name=f"Templates by {user.display_name}", value="No templates found for this user.", inline=False)
else:
# Fetch all templates for the server
c.execute('SELECT name, user_id FROM templates WHERE guild_id = ?', (ctx.guild.id,))
all_templates = c.fetchall()
if all_templates:
user_templates_map = {}
for name, user_id in all_templates:
if user_id not in user_templates_map:
user_templates_map[user_id] = []
user_templates_map[user_id].append(f"`{name}`")
# Add fields for each user's templates
for user_id, templates in user_templates_map.items():
member = ctx.guild.get_member(user_id)
user_name = member.display_name if member else f"User ID: {user_id}"
embed.add_field(name=f"Templates by {user_name}", value=", ".join(templates), inline=False)
else:
embed.description = "No templates found for this server."
await ctx.reply(embed=embed,mention_author=False)
@bot.command(aliases=["dl","ytdl"])
async def download(ctx, url:str):
"""
Downloads YouTube videos.
Usage:
;download https://youtu.be/8gklmIIyOVQ
;download https://youtube.com/watch?v=90U0dYQDft0
"""
loadMessage = await ctx.reply("> <a:load:1417523157824442599> Loading Video (1/2)", mention_author=False)
file_names = [os.path.join(UPLOAD_DIR, f"output_{ctx.author.id}.mp4"),os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{ctx.author.id}.mp4")]
for i, v in enumerate(file_names):
if os.path.exists(v):
await loadMessage.edit(content="> :warning: You still have a process still running. Please wait.")
return
attachment_url = None
# Check for attachments or replied media
if ctx.message.reference:
referenced_message = await ctx.channel.fetch_message(ctx.message.reference.message_id)
extractor = URLExtract()
urls = extractor.find_urls(referenced_message.content)
if urls:
attachment_url = urls[0]
if not attachment_url:
attachment_url = url
if not attachment_url:
await loadMessage.edit(content="> :x: Please provide a YouTube link.")
return
else:
await loadMessage.edit(content="> <a:load:1417523157824442599> Downloading Video (2/2)",allowed_mentions=discord.AllowedMentions(replied_user=False))
opts = {'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best','outtmpl': 'downloads/youtube-%(id)s.%(ext)s'}
try:
with YoutubeDL(opts) as ydl:
await asyncio.to_thread(ydl.download,[url])
if (yid := getyoutubeid(url)):
print(yid)
formatted = f"./downloads/youtube-{yid}.mp4"
if os.path.exists(formatted):
file = discord.File(formatted)
view = FileLayout(file, formatted)
await loadMessage.delete()
await asyncio.sleep(0.5)
await ctx.reply(file=file,view=view.make())
os.remove(formatted)
else:
await loadMessage.edit(content="> :x: Error locating file.")
if os.path.exists(formatted):
os.remove(formatted)
except Exception as e:
await loadMessage.edit(content=f"Error: {e}")
# end of commands
# start of slash commands
@bot.tree.command(name="invite",description="sends a invite to the current channel to invite the bot.")
async def invite(interaction: discord.Interaction, hide:bool=False):
await interaction.response.send_message(f"[Click here to invite the bot anywhere!]({invitelink})",ephemeral=hide)
@bot.tree.command(name="command",description="burps")
async def ffmpeg(i:discord.Interaction,video:discord.Attachment,command:str):
"""
Execute an FFmpeg command on a Discord video link, uploaded file, or reply.
Example usage: !ffmpeg -vf "scale=320:240"
"""
await i.response.defer()
attachment_url = video.url
# Check for attachments or replied media
try:
# Download the file
file_name = os.path.join(UPLOAD_DIR, f"input_{i.user.id}.mp4")
async with aiohttp.ClientSession() as session:
async with session.get(attachment_url) as response:
if response.status == 200:
with open(file_name, "wb") as f:
f.write(await response.read())
else:
await i.followup.send("Failed to download the video.")
return
sanitized_command = shlex.split(command)
# Prepare output file
output_file = os.path.join(UPLOAD_DIR, f"output_{i.user.id}.mp4")
if not any(arg.endswith(".mp4") for arg in sanitized_command):
sanitized_command.append(output_file)
# Run FFmpeg command asynchronously
process = await asyncio.create_subprocess_exec(
"ffmpeg",
"-i", file_name,
*sanitized_command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = await process.communicate()
if os.path.exists(output_file):
file = discord.File(output_file)
view = FileLayout(file, output_file)
await i.followup.send(file=file,view=view.make())
os.remove(output_file)
else:
await i.followup.send(f"Error executing command:\n```{stderr.decode()}```")
if os.path.exists(file_name):
os.remove(file_name)
except Exception as e:
await i.followup.send(f"An error occurred: {str(e)}")
@bot.tree.command(name="ihtx",description="does an ihtx on a video with a command and time and powers.")
async def ihtx(interaction:discord.Interaction,video:discord.Attachment,command:str,powers:app_commands.Range[int,1,100],time:float=0.5):
await interaction.response.defer()
attachment_url = video.url
file_names = [os.path.join(UPLOAD_DIR, f"output_{interaction.user.id}.mp4"),os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{interaction.user.id}.mp4")]
for i, v in enumerate(file_names):
if os.path.exists(v):
await interaction.followup.send("> :warning: You still have a process still running. Please wait.")
return
try:
# Download the file
file_name = os.path.join(os.path.join(UPLOAD_DIR,"ihtx"), f"input_{interaction.user.id}.mp4")
await downloader.from_url(attachment_url,file_name)
probet = FFprobe(file_name)
w = probet["streams"][0]["width"]
h = probet["streams"][0]["height"]
fc = probet["streams"][0]["nb_frames"]
command = command.replace("$w",str(w))
command = command.replace("$h",str(h))
command = command.replace("$fc",str(fc))
sanitized_command = shlex.split(command)