text: from pyrogram import Client, filters
  from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
  import os
  import shutil
  import time
  import math
  from pyromod import listen
  import re
  import asyncio
  import youtube_dl
  import pysubs2
  import datetime
  
  if "BOT_TOKEN" in os.environ:
      BOT_TOKEN = os.environ.get("BOT_TOKEN")
      API_ID = os.environ.get("API_ID")
      API_HASH = os.environ.get("API_HASH")
  else:
      BOT_TOKEN = "2097075744:AAEoJN9kufairh1wNseDI-N_eXqvxcnZSfw"
      API_ID = "4328913"
      API_HASH = "3230ec801f78a517c9a2ad6bebb7f7b4"
  
  YTDL_REGEX = (r"^((?:https?:)?\/\/)"
                r"?((?:www|m)\.)"
                r"?((?:youtube\.com|youtu\.be|xvideos\.com|pornhub\.com"
                r"|xhamster\.com|xnxx\.com))"
                r"(\/)([-a-zA-Z0-9()@:%_\+.~#?&//=]*)([\w\-]+)(\S+)?$")
  
  progress_pattern = re.compile(
      r'(frame|fps|size|time|bitrate|speed)\s*\=\s*(\S+)'
  )
  
  def parse_progress(line):
      items = {
          key: value for key, value in progress_pattern.findall(line)
      }
      if not items:
          return None
      return items
  
  async def readlines(stream):
      pattern = re.compile(br'[\r\n]+')
  
      data = bytearray()
      while not stream.at_eof():
          lines = pattern.split(data)
          data[:] = lines.pop(-1)
  
          for line in lines:
              yield line
  
          data.extend(await stream.read(1024))
  
  async def read_stderr(start, msg, process):
      async for line in readlines(process.stderr):
              line = line.decode('utf-8')
              progress = parse_progress(line)
              if progress:
                  #Progress bar logic
                  now = time.time()
                  diff = start-now
                  text = 'PROGRESS\n'
                  text += 'Size : {}\n'.format(progress['size'])
                  text += 'Time : {}\n'.format(progress['time'])
                  text += 'Speed : {}\n'.format(progress['speed'])
  
                  if round(diff % 5)==0:
                      try:
                          await msg.edit( text )
                      except:
                          pass
  
  async def progress_bar(current, total, text, message, start):
  
      now = time.time()
      diff = now-start
      if round(diff % 10) == 0 or current == total:
          percentage = current*100/total
          speed = current/diff
          elapsed_time = round(diff)*1000
          eta = round((total-current)/speed)*1000
          ett = eta + elapsed_time
  
          elapsed_time = TimeFormatter(elapsed_time)
          ett = TimeFormatter(ett)
  
          progress = "[{0}{1}] \n\n🔹Progress: {2}%\n".format(
              ''.join(["◼️" for i in range(math.floor(percentage / 5))]),
              ''.join(["◻️" for i in range(20 - math.floor(percentage / 5))]),
              round(percentage, 2))
  
          tmp = progress + "{0} of {1}\n\n️🔹Speed: {2}/s\n\n🔹ETA: {3}\n".format(
              humanbytes(current),
              humanbytes(total),
              humanbytes(speed),
              # elapsed_time if elapsed_time != '' else "0 s",
              ett if ett != '' else "0 s"
          )
  
          try :
              await message.edit(
                  text = '{}.\n{}'.format(text, tmp)
              )
          except:
              pass
  
  def humanbytes(size):
      # https://stackoverflow.com/a/49361727/4723940
      # 2**10 = 1024
      if not size:
          return ""
      power = 2**10
      n = 0
      Dic_powerN = {0: ' ', 1: 'Ki', 2: 'Mi', 3: 'Gi', 4: 'Ti'}
      while size > power:
          size /= power
          n += 1
      return str(round(size, 2)) + " " + Dic_powerN[n] + 'B'
  
  
  def TimeFormatter(milliseconds: int) -> str:
      seconds, milliseconds = divmod(int(milliseconds), 1000)
      minutes, seconds = divmod(seconds, 60)
      hours, minutes = divmod(minutes, 60)
      days, hours = divmod(hours, 24)
      tmp = ((str(days) + "d, ") if days else "") + \
          ((str(hours) + "h, ") if hours else "") + \
          ((str(minutes) + "m, ") if minutes else "") + \
          ((str(seconds) + "s, ") if seconds else "") + \
          ((str(milliseconds) + "ms, ") if milliseconds else "")
      return tmp[:-2]
  
  
  Bot = Client(
      "Bot",
      bot_token = BOT_TOKEN,
      api_id = int(API_ID),
      api_hash = API_HASH
  )
  
  
  START_TXT = """
  Hi {}, I'm Forward Tag Remover bot.\n\nForward me some messages, i will remove forward tag from them.\nAlso can do it in channels.
  """
  
  START_BTN = InlineKeyboardMarkup(
          [[
          InlineKeyboardButton('Source Code', url='https://github.com/samadii/ChannelForwardTagRemover'),
          ]]
      )
  
  
  @Bot.on_message(filters.command(["start"]))
  async def start(bot, update):
      text = START_TXT.format(update.from_user.mention)
      reply_markup = START_BTN
      await update.reply_text(
          text=text,
          disable_web_page_preview=True,
          reply_markup=reply_markup
      )
  
  sub = None
  vid = None
  Url = None
  """
  @Bot.on_message(filters.private & filters.video)
  async def vide(client, message):
      global vid
      global sub
      if vid != None:
          return
      chat_id = message.from_user.id
      start_time = time.time()
      downloading = await client.send_message(chat_id, 'Downloading your File!')
      video = await client.download_media(
          message = message,
          file_name = "temp/vid1.mkv",
          progress = progress_bar,
          progress_args = (
              'Initializing',
              downloading,
              start_time
              )
          )
  
      if video is None:
          return client.edit_message_text(
              text = 'Downloading Failed!',
              chat_id = chat_id,
              message_id = downloading.message_id
          )
      cut = await bot.ask(message.chat.id,'زمان آغار و پایان برش؟', filters=filters.text)
      os.system(f"ffmpeg -ss {cut.text.split()[0]} -i temp/vid1.mkv -ss 00:01:00 -t {cut.text.split()[1]} -c copy temp/vid2.mkv")
      merge = await bot.ask(message.chat.id,'ویدیویی که میخوای ادغام شه؟بفرس', filters=filters.video)
      await client.download_media(message=message, file_name="temp/vid3.mkv")
      subprocess.call(
          ['ffmpeg', '-f', 'concat', '-i', 'temp/inputs.txt', '-c', 'copy', 'temp/out.mkv']
      )
      if (sub == None) or (vid == None):
          return
      text = 'Your File is Being Hard Subbed. This might take a long time!'
      sent_msg = await client.send_message(chat_id, text)
  
      command = [
              'ffmpeg','-hide_banner',
              '-i','temp/out.mkv',
              '-vf',"subtitles=temp/sub.ass"+":fontsdir=temp:force_style='Fontname=B Titr,Fontsize=28,PrimaryColour=&H0000FFFF,Shadow=0'", #,SecondaryColour=&H0300FFFF'", #,OutlineColour=&H00000000,BackColour=&H02000000,ScaleX=100,ScaleY=100,BorderStyle=1,Outline=1,Alignment=2,MarginL=10,MarginR=10,MarginV=10,Encoding=1'",
              '-c:v','h264',
              '-map','0:v:0',
              '-map','0:a:0?',
              #'-crf','18',
              '-preset','faster',
              '-y','temp/final.mkv'
              ]
      process = await asyncio.create_subprocess_exec(
              *command,
              # stdout must a pipe to be accessible as process.stdout
              stdout=asyncio.subprocess.PIPE,
              stderr=asyncio.subprocess.PIPE,
              )
      await asyncio.wait([
              read_stderr(start,msg, process),
              process.wait(),
          ])
      if process.returncode == 0:
          await message.reply('Muxing  Completed Successfully!')
      else:
          return await message.reply('An Error occured while Muxing!')
      text = 'Your File is Being Hard Subbed. This might take a long time!'
      sent_msg = await client.send_message(chat_id, text)
      start_time = time.time()
      try:
          await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 720, width = 1280, file_name = re.sub(r'\S*0p\S*|\S*0P\S*', '720P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"720P.mkv")
          text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
          await sent_msg.edit(text)
      except Exception as e:
          print(e)
          await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
      sent_msg = await client.send_message(chat_id, text)
      start_time = time.time()
      try:
          await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 300, width = 536, file_name = re.sub(r'\S*0p\S*|\S*0P\S*', '240P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"240P.mkv")
          
          text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
          await sent_msg.edit(text)
      except Exception as e:
          print(e)
          await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
      sent_msg = await client.send_message(chat_id, text)
      start_time = time.time()
      try:
          await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 480, width = 856, file_name = re.sub(r'\S*0p\S*|\S*0P\S*', '480P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"480P.mkv")
          
          text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
          await sent_msg.edit(text)
      except Exception as e:
          print(e)
          await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
      sent_msg = await client.send_message(chat_id, text)
      start_time = time.time()
      try:
          await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 1080, width = 1920, file_name = re.sub(r'\S*0p\S*|\S*0P\S*', '1080P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"1080P.mkv")
          text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
          await sent_msg.edit(text)
      except Exception as e:
          print(e)
          await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
  
      sub = None
      vid = None
  """
  @Bot.on_message(filters.private & filters.document)
  async def subts(client, message):
      global sub
      #global vid
      #global Url
      #if sub != None:
      #return
      ext = message.document.file_name.split('.').pop()
      if not ext in ['ass','srt']:
          return await message.reply("document just should be .ass or .srt!")
      ext = message.document.file_name.rsplit(".", 1)[1]
      chat_id = message.from_user.id
      start_time = time.time()
      downloading = await client.send_message(chat_id, 'Downloading your File!')
      Sub = await client.download_media(
          message = message,
          file_name = "temp/sub1."+ext,
          progress = progress_bar,
          progress_args = (
              'Initializing',
              downloading,
              start_time
              )
          )
  
      if Sub is None:
          return client.edit_message_text(
              text = 'Downloading Failed!',
              chat_id = chat_id,
              message_id = downloading.message_id
          )
      os.system(f"ffmpeg -i temp/sub1.{ext} temp/subt.ass")
      subs = pysubs2.load("temp/subt.ass", encoding="utf-8")
      for line in subs:
          if (not line.text.__contains__("color")) and (not line.text.__contains__("macvin")):
              line.text = line.text + "\\N{\\b1\\c&H0080ff&}t.me/dlmacvin_new{\\c}{\\b0}"
          
          if "color" in line.text:
              line.text = line.text.split('color')[0] + "{\\b1\\c&H0080ff&}t.me/dlmacvin_new{\\c}{\\b0}"
      subs.save("temp/sub.ass")
      try:
          os.remove("temp/subt.ass")
      except:
          pass
      
      await client.edit_message_text(
          text = "لینک یوتیوب ویدیو؟بفرس",
          chat_id = chat_id,
          message_id = downloading.message_id
      )
      sub = message.document.file_name
      """
      if vid != None and sub != None:
          text = 'Your File is Being Hard Subbed. This might take a long time!'
          sent_msg = await client.send_message(chat_id, text)
          start_time = time.time()
          try:
              await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 720, width = 1280, file_name = re.sub(r'\S*0p\S*|\S*0P\S*', '720P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"720P.mkv")
              text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
              await sent_msg.edit(text)
          except Exception as e:
              print(e)
              await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
          sent_msg = await client.send_message(chat_id, text)
          start_time = time.time()
          try:
              await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 300, width = 536, file_name = re.sub(r'\S*0p\S*|\S*0P\S*', '240P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"240P.mkv")
              text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
              await sent_msg.edit(text)
          except Exception as e:
              print(e)
              await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
          sent_msg = await client.send_message(chat_id, text)
          start_time = time.time()
          try:
              await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 480, width = 856, file_name = re.sub(r'\S*0p\S*|\S*0P\S*', '480P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"480P.mkv")
  
              text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
              await sent_msg.edit(text)
          except Exception as e:
              print(e)
              await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
          sent_msg = await client.send_message(chat_id, text)
          start_time = time.time()
          try:
              await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 1080, width = 1920, file_name = re.sub(r'\S*0p\S*|\S*0P\S*', '1080P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"1080P.mkv")
              text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
              await sent_msg.edit(text)
          except Exception as e:
              print(e)
              await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
          sub = None
          vid = None
      """
  
  
  @Bot.on_message(filters.private & filters.regex(YTDL_REGEX))
  async def urlss(client, message):
      global sub
      #global Url
      #if Url != None:
      #return
      chat_id = message.from_user.id
  
      """
      save_filename = None
  
      if "|" in message.text:
          if len(message.text.split('|')) ==2 :
              save_filename = message.text.split('|')[1].strip()
              url = message.text.split('|')[0].strip()
      else :
          url = message.text.strip()
  
      if save_filename and len(save_filename)>60:
          return
      r = requests.get(url, stream=True, allow_redirects=True)
      if save_filename is None :
          if 'content-disposition' in r.headers.keys() :
              regx = 'filename="(.*?)"'
              res = re.search(regx, str(r.headers))
              if res :
                  save_filename = res.group(1)
              else :
                  #removing argumets from url!
                  if '?' in url:
                      url = ''.join(url.split('?')[0:-1])
                  save_filename = url.split('/')[-1]
                  save_filename = unquote(save_filename)
          else :
              if '?' in url:
                  url = ''.join(url.split('?')[0:-1])
              save_filename = url.split('/')[-1]
              save_filename = unquote(save_filename)
  
      sent_msg = await client.send_message(chat_id, 'Preparing Your Download')
      ext = save_filename.split('.')[-1]
      
      size = None
      if 'content-length' in r.headers.keys() :
          size = int(r.headers['content-length'])
  
      if not size :
          return
      if size>(2*1000*1000*1000) :
          return
  
      current = 0
      start = time.time()
  
      with requests.get(url, stream=True, allow_redirects=True) as r :
          with open(os.path.join("temp","vid1.mkv"), 'wb') as f :
              for chunk in r.iter_content(chunk_size=1024*1024) :
                  if (chunk) :
                      written = f.write(chunk)
                      #current += 1024*1024
                      current += written
                      await progress_bar(current, size, 'Downloading Your File!', sent_msg, start)
      """
      
      Url = message.text
      cut = await client.ask(message.chat.id,'زمان آغار و پایان برش؟', filters=filters.text)
      #os.system(f"ffmpeg -ss {cut.text.split()[0]} -i temp/vid1.mkv -ss 00:01:00 -t {cut.text.split()[1]} -c copy temp/vid2.mkv")
      merge = await client.ask(message.chat.id,'ویدیویی که میخوای ادغام شه؟بفرس', filters=filters.video)
      mess = await message.reply("cutting and merging...")
      await client.download_media(message=merge.video, file_name="temp/vid3.mp4")
      #os.system("ffmpeg -i temp/vid4.mp4 -ar 44100 temp/vid3.mp4")
      """
      A = cut.text.split()[0]
      F = cut.text.split()[1]
      try:
          B = int(sum(x * int(t) for x, t in zip([1, 60, 3600], reversed(A.split(".")[0].split(":"))))) + merge.video.duration
          From = str(datetime.timedelta(seconds = B)) + "." + A.split(".")[1]
      except:
          B = int(sum(x * int(t) for x, t in zip([1, 60, 3600], reversed(A.split(":"))))) + merge.video.duration
          From = str(datetime.timedelta(seconds = B))
      try:
          K = int(sum(x * int(t) for x, t in zip([1, 60, 3600], reversed(F.split(".")[0].split(":"))))) + merge.video.duration
          To = str(datetime.timedelta(seconds = K)) + "." + F.split(".")[1]
      except:
          K  = int(sum(x * int(t) for x, t in zip([1, 60, 3600], reversed(F.split(":"))))) + merge.video.duration
          To = str(datetime.timedelta(seconds = K))
      """
      # for 1080p
      ytdl_opts = {
          'format': 'best[height<=1080]',
          'geo_bypass':True,
          'nocheckcertificate':True,
          'videoformat':'mp4',
          'outtmpl': 'temp/vid1.mp4'
      }
      with youtube_dl.YoutubeDL(ytdl_opts) as ytdl:
          info = ytdl.extract_info(Url, download=True)
      #await message.reply_video(video='temp/vid1.mp4')
      #print(float(info['duration']))
      #os.system('"C:\\Program Files\\MKVToolNix\\mkvmerge.exe" -o temp/vid2.mkv temp/vid1.mkv +temp/vid3.mkv')
      #os.system(f"ffmpeg -i temp/vid1.mp4 -ss {cut.text.split()[0]} -to {cut.text.split()[1]} temp/vid2.mp4")
      
      file_generator_command = [
          "ffmpeg",
          "-i",
          "temp/vid1.mp4",
          "-ss",
          cut.text.split()[0],
          "-to",
          cut.text.split()[1],
          "-async",
          "1",
          "-strict",
          "-2",
          "temp/vid2.mp4"
      ]
      process = await asyncio.create_subprocess_exec(
          *file_generator_command,
          stdout=asyncio.subprocess.PIPE,
          stderr=asyncio.subprocess.PIPE,
      )
      time.sleep(5)
      os.system('ffmpeg -i temp/vid2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp/vid2.ts')
  
      os.system('ffmpeg -i temp/vid3.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp/vid3.ts')
  
      os.system('ffmpeg -i "concat:temp/vid2.ts|temp/vid3.ts" -c copy -bsf:a aac_adtstoasc temp/out.mp4')
  
      #os.system(f"ffmpeg -ss {cut.text.split()[0]} -i temp/vid1.mkv -c copy -t {cut.text.split()[1]} -avoid_negative_ts make_zero -fflags +genpts temp/segment.mkv")
      # os.system("ffmpeg -f concat -i temp/segments.txt -c copy temp/vid2.mkv")
      #ffmpeg_extract_subclip("temp/vid1.mp4", 1.100, 10.900, targetname="temp/vid2.mp4")
      time.sleep(5)
      text = 'Your File is Being Hard Subbed. This might take a long time!'
      sent_msg = await client.send_message(chat_id, text)
      await mess.delete()
  
  
  
  
      command = [
              'ffmpeg','-hide_banner',
              '-i','temp/out.mp4',
              '-vf',"subtitles=temp/sub.ass"+":fontsdir=temp/font:force_style='Fontname=B Titr,Fontsize=28,PrimaryColour=&H0000FFFF,Shadow=0'", #,SecondaryColour=&H0300FFFF'", #,OutlineColour=&H00000000,BackColour=&H02000000,ScaleX=100,ScaleY=100,BorderStyle=1,Outline=1,Alignment=2,MarginL=10,MarginR=10,MarginV=10,Encoding=1'",
              '-c:v','h264',
              '-b:v','1500k',
              '-preset','faster',
              '-y','temp/final.mp4'
              ]
      process = await asyncio.create_subprocess_exec(
              *command,
              # stdout must a pipe to be accessible as process.stdout
              stdout=asyncio.subprocess.PIPE,
              stderr=asyncio.subprocess.PIPE,
              )
      # os.system('''ffmpeg -hide_banner -i temp/out.mp4 -vf "subtitles=temp/sub.ass:fontsdir=temp/font:force_style='Fontname=B Titr,Fontsize=28,PrimaryColour=&H0000FFFF,Shadow=0'" -c:v h264 -map 0:v:0 -map 0:a:0? -preset faster -y temp/final.mp4''')
      time.sleep(5)
      os.system('ffmpeg -i temp/final.mp4 -vcodec copy -b:a 128k temp/final.mkv')
      time.sleep(5)
      name = re.sub(r'\S*0p\S*|\S*0P\S*', '1080P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"1080P.mkv"
      try:
          #await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 1080, width = 1920, file_name = name)
          await client.send_video(chat_id, video = 'temp/final.mkv', file_name = name)
          await sent_msg.delete()
      except Exception as e:
          print(e)
          await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
      return
      shutil.copyfile("temp/final.mkv", "files/"+name)
      
      # for 720p
      ytdl_opts = {
          'format': 'bestvideo[height<=720]+bestaudio/best[height<=720]',
          'postprocessors': [{
              'key': 'FFmpegVideoConvertor',
              'preferedformat': 'mkv'},],
          'outtmpl': 'temp/vid1'+'.%(ext)s'
      }
      with youtube_dl.YoutubeDL(ytdl_opts) as ytdl:
          ytdl.extract_info(Url, download=True)
  
      os.system(f"ffmpeg -ss {cut.text.split()[0]} -i temp/vid1.mkv -ss 00:01:00 -t {cut.text.split()[1]} -c copy temp/vid2.mkv")
      subprocess.call(
          ['ffmpeg', '-f', 'concat', '-i', 'temp/inputs.txt', '-c', 'copy', 'temp/out.mkv']
      )
  
      text = 'Your File is Being Hard Subbed. This might take a long time!'
      sent_msg = await client.send_message(chat_id, text)
  
      command = [
              'ffmpeg','-hide_banner',
              '-i','temp/out.mkv',
              '-vf',"subtitles=temp/sub.ass"+":fontsdir=temp:force_style='Fontname=B Titr,Fontsize=28,PrimaryColour=&H0000FFFF,Shadow=0'", #,SecondaryColour=&H0300FFFF'", #,OutlineColour=&H00000000,BackColour=&H02000000,ScaleX=100,ScaleY=100,BorderStyle=1,Outline=1,Alignment=2,MarginL=10,MarginR=10,MarginV=10,Encoding=1'",
              '-c:v','h264',
              '-map','0:v:0',
              '-map','0:a:0?',
              #'-crf','18',
              '-preset','faster',
              '-y','temp/final.mkv'
              ]
      process = await asyncio.create_subprocess_exec(
              *command,
              # stdout must a pipe to be accessible as process.stdout
              stdout=asyncio.subprocess.PIPE,
              stderr=asyncio.subprocess.PIPE,
              )
      await asyncio.wait([
              read_stderr(start,msg, process),
              process.wait(),
          ])
      if process.returncode == 0:
          await message.reply('Muxing  Completed Successfully!')
      else:
          return await message.reply('An Error occured while Muxing!')
      text = 'Your File is Being Hard Subbed. This might take a long time!'
      sent_msg = await client.send_message(chat_id, text)
      start_time = time.time()
      name = re.sub(r'\S*0p\S*|\S*0P\S*', '720P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"720P.mkv"
      try:
          await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 720, width = 1280, file_name = name)
          text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
          await sent_msg.edit(text)
      except Exception as e:
          print(e)
          await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
      shutil.copyfile("temp/final.mkv", "files/"+name)
  
      # for 240p
      ytdl_opts = {
          'format': 'bestvideo[height<=240]+bestaudio/best[height<=240]',
          'postprocessors': [{
              'key': 'FFmpegVideoConvertor',
              'preferedformat': 'mkv'},],
          'outtmpl': 'temp/vid1'+'.%(ext)s'
      }
      with youtube_dl.YoutubeDL(ytdl_opts) as ytdl:
          ytdl.extract_info(Url, download=True)
  
      os.system(f"ffmpeg -ss {cut.text.split()[0]} -i temp/vid1.mkv -ss 00:01:00 -t {cut.text.split()[1]} -c copy temp/vid2.mkv")
      subprocess.call(
          ['ffmpeg', '-f', 'concat', '-i', 'temp/inputs.txt', '-c', 'copy', 'temp/out.mkv']
      )
      text = 'Your File is Being Hard Subbed. This might take a long time!'
      sent_msg = await client.send_message(chat_id, text)
      
      """
      command = [
              'ffmpeg','-hide_banner',
              '-i','temp/out.mkv',
              '-vf',"subtitles=temp/sub.ass"+":fontsdir=temp:force_style='Fontname=B Titr,Fontsize=28,PrimaryColour=&H0000FFFF,Shadow=0'", #,SecondaryColour=&H0300FFFF'", #,OutlineColour=&H00000000,BackColour=&H02000000,ScaleX=100,ScaleY=100,BorderStyle=1,Outline=1,Alignment=2,MarginL=10,MarginR=10,MarginV=10,Encoding=1'",
              '-c:v','h264',
              '-map','0:v:0',
              '-map','0:a:0?',
              #'-crf','18',
              '-preset','faster',
              '-y','temp/final.mkv'
              ]
      process = await asyncio.create_subprocess_exec(
              *command,
              # stdout must a pipe to be accessible as process.stdout
              stdout=asyncio.subprocess.PIPE,
              stderr=asyncio.subprocess.PIPE,
              )
      await asyncio.wait([
              read_stderr(start,msg, process),
              process.wait(),
          ])
      if process.returncode == 0:
          await message.reply('Muxing  Completed Successfully!')
      else:
          return await message.reply('An Error occured while Muxing!')
      text = 'Your File is Being Hard Subbed. This might take a long time!'
      sent_msg = await client.send_message(chat_id, text)
      start_time = time.time()
      """
      name = re.sub(r'\S*0p\S*|\S*0P\S*', '240P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"240P.mkv"
      try:
          await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 300, width = 536, file_name = name)
          #text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
          #await sent_msg.edit(text)
      except Exception as e:
          print(e)
          await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
      shutil.copyfile("temp/final.mkv", "files/"+name)
  
      # for 480p
      ytdl_opts = {
          'format': 'bestvideo[height<=480]+bestaudio/best[height<=480]',
          'postprocessors': [{
              'key': 'FFmpegVideoConvertor',
              'preferedformat': 'mkv'},],
          'outtmpl': 'temp/vid1'+'.%(ext)s'
      }
      with youtube_dl.YoutubeDL(ytdl_opts) as ytdl:
          ytdl.extract_info(Url, download=True)
  
      os.system(f"ffmpeg -ss {cut.text.split()[0]} -i temp/vid1.mkv -ss 00:01:00 -t {cut.text.split()[1]} -c copy temp/vid2.mkv")
      subprocess.call(
          ['ffmpeg', '-f', 'concat', '-i', 'temp/inputs.txt', '-c', 'copy', 'temp/out.mkv']
      )
  
      text = 'Your File is Being Hard Subbed. This might take a long time!'
      sent_msg = await client.send_message(chat_id, text)
  
      command = [
              'ffmpeg','-hide_banner',
              '-i','temp/out.mkv',
              '-vf',"subtitles=temp/sub.ass"+":fontsdir=temp:force_style='Fontname=B Titr,Fontsize=28,PrimaryColour=&H0000FFFF,Shadow=0'", #,SecondaryColour=&H0300FFFF'", #,OutlineColour=&H00000000,BackColour=&H02000000,ScaleX=100,ScaleY=100,BorderStyle=1,Outline=1,Alignment=2,MarginL=10,MarginR=10,MarginV=10,Encoding=1'",
              '-c:v','h264',
              '-map','0:v:0',
              '-map','0:a:0?',
              #'-crf','18',
              '-preset','faster',
              '-y','temp/final.mkv'
              ]
      process = await asyncio.create_subprocess_exec(
              *command,
              # stdout must a pipe to be accessible as process.stdout
              stdout=asyncio.subprocess.PIPE,
              stderr=asyncio.subprocess.PIPE,
              )
      await asyncio.wait([
              read_stderr(start,msg, process),
              process.wait(),
          ])
      if process.returncode == 0:
          await message.reply('Muxing  Completed Successfully!')
      else:
          return await message.reply('An Error occured while Muxing!')
      text = 'Your File is Being Hard Subbed. This might take a long time!'
      sent_msg = await client.send_message(chat_id, text)
      start_time = time.time()
      name = re.sub(r'\S*0p\S*|\S*0P\S*', '480P', sub+'.mkv').replace('.ass', '').replace('.srt', '') if sub.__contains__('0p') or sub.__contains__('0P') else sub.replace('.ass', '').replace('.srt', '')+"480P.mkv"
      try:
          await client.send_video(chat_id, progress = progress_bar, progress_args = ('Uploading your File!',sent_msg,start_time), video = 'temp/final.mkv', height = 480, width = 856, file_name = name)
          text = 'File Successfully Uploaded!\nTotal Time taken : {} seconds'.format(round(time.time()-start_time))
          await sent_msg.edit(text)
      except Exception as e:
          print(e)
          await client.send_message(chat_id, 'An error occured while uploading the file!\nCheck logs for details of the error!')
      shutil.copyfile("temp/final.mkv", "files/"+name)
      for i in range(80):
          shutil.copy2('files/'+name, f'files/{i}{name}')
  
  
  
  
  
  Bot.run()
  