Spaces:
				
			
			
	
			
			
		Build error
		
	
	
	
			
			
	
	
	
	
		
		
		Build error
		
	Commit 
							
							·
						
						e5b6f8d
	
1
								Parent(s):
							
							9283501
								
Upload 3 files
Browse files- helper_funcs/display_progress.py +92 -0
- helper_funcs/help_Nekmo_ffmpeg.py +160 -0
- helper_funcs/help_uploadbot.py +49 -0
    	
        helper_funcs/display_progress.py
    ADDED
    
    | @@ -0,0 +1,92 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            #!/usr/bin/env python3
         | 
| 2 | 
            +
            # -*- coding: utf-8 -*-
         | 
| 3 | 
            +
            # (c) Shrimadhav U K
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # the logging things
         | 
| 6 | 
            +
            import logging
         | 
| 7 | 
            +
            logging.basicConfig(level=logging.DEBUG,
         | 
| 8 | 
            +
                                format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
         | 
| 9 | 
            +
            logger = logging.getLogger(__name__)
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            import math
         | 
| 12 | 
            +
            import os
         | 
| 13 | 
            +
            import time
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            # the secret configuration specific things
         | 
| 16 | 
            +
            if bool(os.environ.get("WEBHOOK", False)):
         | 
| 17 | 
            +
                from sample_config import Config
         | 
| 18 | 
            +
            else:
         | 
| 19 | 
            +
                from config import Config
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            # the Strings used for this "thing"
         | 
| 22 | 
            +
            from translation import Translation
         | 
| 23 | 
            +
             | 
| 24 | 
            +
             | 
| 25 | 
            +
            async def progress_for_pyrogram(
         | 
| 26 | 
            +
                current,
         | 
| 27 | 
            +
                total,
         | 
| 28 | 
            +
                ud_type,
         | 
| 29 | 
            +
                message,
         | 
| 30 | 
            +
                start
         | 
| 31 | 
            +
            ):
         | 
| 32 | 
            +
                now = time.time()
         | 
| 33 | 
            +
                diff = now - start
         | 
| 34 | 
            +
                if round(diff % 10.00) == 0 or current == total:
         | 
| 35 | 
            +
                    # if round(current / total * 100, 0) % 5 == 0:
         | 
| 36 | 
            +
                    percentage = current * 100 / total
         | 
| 37 | 
            +
                    speed = current / diff
         | 
| 38 | 
            +
                    elapsed_time = round(diff) * 1000
         | 
| 39 | 
            +
                    time_to_completion = round((total - current) / speed) * 1000
         | 
| 40 | 
            +
                    estimated_total_time = elapsed_time + time_to_completion
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                    elapsed_time = TimeFormatter(milliseconds=elapsed_time)
         | 
| 43 | 
            +
                    estimated_total_time = TimeFormatter(milliseconds=estimated_total_time)
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                    progress = "[{0}{1}] \nP: {2}%\n".format(
         | 
| 46 | 
            +
                        ''.join(["█" for i in range(math.floor(percentage / 5))]),
         | 
| 47 | 
            +
                        ''.join(["░" for i in range(20 - math.floor(percentage / 5))]),
         | 
| 48 | 
            +
                        round(percentage, 2))
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                    tmp = progress + "{0} of {1}\nSpeed: {2}/s\nETA: {3}\n".format(
         | 
| 51 | 
            +
                        humanbytes(current),
         | 
| 52 | 
            +
                        humanbytes(total),
         | 
| 53 | 
            +
                        humanbytes(speed),
         | 
| 54 | 
            +
                        # elapsed_time if elapsed_time != '' else "0 s",
         | 
| 55 | 
            +
                        estimated_total_time if estimated_total_time != '' else "0 s"
         | 
| 56 | 
            +
                    )
         | 
| 57 | 
            +
                    try:
         | 
| 58 | 
            +
                        await message.edit(
         | 
| 59 | 
            +
                            text="{}\n {}".format(
         | 
| 60 | 
            +
                                ud_type,
         | 
| 61 | 
            +
                                tmp
         | 
| 62 | 
            +
                            )
         | 
| 63 | 
            +
                        )
         | 
| 64 | 
            +
                    except:
         | 
| 65 | 
            +
                        pass
         | 
| 66 | 
            +
             | 
| 67 | 
            +
             | 
| 68 | 
            +
            def humanbytes(size):
         | 
| 69 | 
            +
                # https://stackoverflow.com/a/49361727/4723940
         | 
| 70 | 
            +
                # 2**10 = 1024
         | 
| 71 | 
            +
                if not size:
         | 
| 72 | 
            +
                    return ""
         | 
| 73 | 
            +
                power = 2**10
         | 
| 74 | 
            +
                n = 0
         | 
| 75 | 
            +
                Dic_powerN = {0: ' ', 1: 'Ki', 2: 'Mi', 3: 'Gi', 4: 'Ti'}
         | 
| 76 | 
            +
                while size > power:
         | 
| 77 | 
            +
                    size /= power
         | 
| 78 | 
            +
                    n += 1
         | 
| 79 | 
            +
                return str(round(size, 2)) + " " + Dic_powerN[n] + 'B'
         | 
| 80 | 
            +
             | 
| 81 | 
            +
             | 
| 82 | 
            +
            def TimeFormatter(milliseconds: int) -> str:
         | 
| 83 | 
            +
                seconds, milliseconds = divmod(int(milliseconds), 1000)
         | 
| 84 | 
            +
                minutes, seconds = divmod(seconds, 60)
         | 
| 85 | 
            +
                hours, minutes = divmod(minutes, 60)
         | 
| 86 | 
            +
                days, hours = divmod(hours, 24)
         | 
| 87 | 
            +
                tmp = ((str(days) + "d, ") if days else "") + \
         | 
| 88 | 
            +
                    ((str(hours) + "h, ") if hours else "") + \
         | 
| 89 | 
            +
                    ((str(minutes) + "m, ") if minutes else "") + \
         | 
| 90 | 
            +
                    ((str(seconds) + "s, ") if seconds else "") + \
         | 
| 91 | 
            +
                    ((str(milliseconds) + "ms, ") if milliseconds else "")
         | 
| 92 | 
            +
                return tmp[:-2]
         | 
    	
        helper_funcs/help_Nekmo_ffmpeg.py
    ADDED
    
    | @@ -0,0 +1,160 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            #!/usr/bin/env python3
         | 
| 2 | 
            +
            # -*- coding: utf-8 -*-
         | 
| 3 | 
            +
            # (c) Shrimadhav U K
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # the logging things
         | 
| 6 | 
            +
            import logging
         | 
| 7 | 
            +
            logging.basicConfig(level=logging.DEBUG,
         | 
| 8 | 
            +
                                format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
         | 
| 9 | 
            +
            logger = logging.getLogger(__name__)
         | 
| 10 | 
            +
             | 
| 11 | 
            +
             | 
| 12 | 
            +
            import asyncio
         | 
| 13 | 
            +
            import os
         | 
| 14 | 
            +
            import time
         | 
| 15 | 
            +
            from hachoir.metadata import extractMetadata
         | 
| 16 | 
            +
            from hachoir.parser import createParser
         | 
| 17 | 
            +
             | 
| 18 | 
            +
             | 
| 19 | 
            +
            async def place_water_mark(input_file, output_file, water_mark_file):
         | 
| 20 | 
            +
                watermarked_file = output_file + ".watermark.png"
         | 
| 21 | 
            +
                metadata = extractMetadata(createParser(input_file))
         | 
| 22 | 
            +
                width = metadata.get("width")
         | 
| 23 | 
            +
                # https://stackoverflow.com/a/34547184/4723940
         | 
| 24 | 
            +
                shrink_watermark_file_genertor_command = [
         | 
| 25 | 
            +
                    "ffmpeg",
         | 
| 26 | 
            +
                    "-i", water_mark_file,
         | 
| 27 | 
            +
                    "-y -v quiet",
         | 
| 28 | 
            +
                    "-vf",
         | 
| 29 | 
            +
                    "scale={}*0.5:-1".format(width),
         | 
| 30 | 
            +
                    watermarked_file
         | 
| 31 | 
            +
                ]
         | 
| 32 | 
            +
                # print(shrink_watermark_file_genertor_command)
         | 
| 33 | 
            +
                process = await asyncio.create_subprocess_exec(
         | 
| 34 | 
            +
                    *shrink_watermark_file_genertor_command,
         | 
| 35 | 
            +
                    # stdout must a pipe to be accessible as process.stdout
         | 
| 36 | 
            +
                    stdout=asyncio.subprocess.PIPE,
         | 
| 37 | 
            +
                    stderr=asyncio.subprocess.PIPE,
         | 
| 38 | 
            +
                )
         | 
| 39 | 
            +
                # Wait for the subprocess to finish
         | 
| 40 | 
            +
                stdout, stderr = await process.communicate()
         | 
| 41 | 
            +
                e_response = stderr.decode().strip()
         | 
| 42 | 
            +
                t_response = stdout.decode().strip()
         | 
| 43 | 
            +
                commands_to_execute = [
         | 
| 44 | 
            +
                    "ffmpeg",
         | 
| 45 | 
            +
                    "-i", input_file,
         | 
| 46 | 
            +
                    "-i", watermarked_file,
         | 
| 47 | 
            +
                    "-filter_complex",
         | 
| 48 | 
            +
                    # https://stackoverflow.com/a/16235519
         | 
| 49 | 
            +
                    # "\"[0:0] scale=400:225 [wm]; [wm][1:0] overlay=305:0 [out]\"",
         | 
| 50 | 
            +
                    # "-map \"[out]\" -b:v 896k -r 20 -an ",
         | 
| 51 | 
            +
                    "\"overlay=(main_w-overlay_w):(main_h-overlay_h)\"",
         | 
| 52 | 
            +
                    # "-vf \"drawtext=text='@FFMovingPictureExpertGroupBOT':x=W-(W/2):y=H-(H/2):fontfile=" + Config.FONT_FILE + ":fontsize=12:fontcolor=white:shadowcolor=black:shadowx=5:shadowy=5\"",
         | 
| 53 | 
            +
                    output_file
         | 
| 54 | 
            +
                ]
         | 
| 55 | 
            +
                # print(commands_to_execute)
         | 
| 56 | 
            +
                process = await asyncio.create_subprocess_exec(
         | 
| 57 | 
            +
                    *commands_to_execute,
         | 
| 58 | 
            +
                    # stdout must a pipe to be accessible as process.stdout
         | 
| 59 | 
            +
                    stdout=asyncio.subprocess.PIPE,
         | 
| 60 | 
            +
                    stderr=asyncio.subprocess.PIPE,
         | 
| 61 | 
            +
                )
         | 
| 62 | 
            +
                # Wait for the subprocess to finish
         | 
| 63 | 
            +
                stdout, stderr = await process.communicate()
         | 
| 64 | 
            +
                e_response = stderr.decode().strip()
         | 
| 65 | 
            +
                t_response = stdout.decode().strip()
         | 
| 66 | 
            +
                return output_file
         | 
| 67 | 
            +
             | 
| 68 | 
            +
             | 
| 69 | 
            +
            async def take_screen_shot(video_file, output_directory, ttl):
         | 
| 70 | 
            +
                # https://stackoverflow.com/a/13891070/4723940
         | 
| 71 | 
            +
                out_put_file_name = output_directory + \
         | 
| 72 | 
            +
                    "/" + str(time.time()) + ".jpg"
         | 
| 73 | 
            +
                file_genertor_command = [
         | 
| 74 | 
            +
                    "ffmpeg",
         | 
| 75 | 
            +
                    "-ss",
         | 
| 76 | 
            +
                    str(ttl),
         | 
| 77 | 
            +
                    "-i",
         | 
| 78 | 
            +
                    video_file,
         | 
| 79 | 
            +
                    "-vframes",
         | 
| 80 | 
            +
                    "1",
         | 
| 81 | 
            +
                    out_put_file_name
         | 
| 82 | 
            +
                ]
         | 
| 83 | 
            +
                # width = "90"
         | 
| 84 | 
            +
                process = await asyncio.create_subprocess_exec(
         | 
| 85 | 
            +
                    *file_genertor_command,
         | 
| 86 | 
            +
                    # stdout must a pipe to be accessible as process.stdout
         | 
| 87 | 
            +
                    stdout=asyncio.subprocess.PIPE,
         | 
| 88 | 
            +
                    stderr=asyncio.subprocess.PIPE,
         | 
| 89 | 
            +
                )
         | 
| 90 | 
            +
                # Wait for the subprocess to finish
         | 
| 91 | 
            +
                stdout, stderr = await process.communicate()
         | 
| 92 | 
            +
                e_response = stderr.decode().strip()
         | 
| 93 | 
            +
                t_response = stdout.decode().strip()
         | 
| 94 | 
            +
                if os.path.lexists(out_put_file_name):
         | 
| 95 | 
            +
                    return out_put_file_name
         | 
| 96 | 
            +
                else:
         | 
| 97 | 
            +
                    return None
         | 
| 98 | 
            +
             | 
| 99 | 
            +
            # https://github.com/Nekmo/telegram-upload/blob/master/telegram_upload/video.py#L26
         | 
| 100 | 
            +
             | 
| 101 | 
            +
            async def cult_small_video(video_file, output_directory, start_time, end_time):
         | 
| 102 | 
            +
                # https://stackoverflow.com/a/13891070/4723940
         | 
| 103 | 
            +
                out_put_file_name = output_directory + \
         | 
| 104 | 
            +
                    "/" + str(round(time.time())) + ".mp4"
         | 
| 105 | 
            +
                file_genertor_command = [
         | 
| 106 | 
            +
                    "ffmpeg",
         | 
| 107 | 
            +
                    "-i",
         | 
| 108 | 
            +
                    video_file,
         | 
| 109 | 
            +
                    "-ss",
         | 
| 110 | 
            +
                    start_time,
         | 
| 111 | 
            +
                    "-to",
         | 
| 112 | 
            +
                    end_time,
         | 
| 113 | 
            +
                    "-async",
         | 
| 114 | 
            +
                    "1",
         | 
| 115 | 
            +
                    "-strict",
         | 
| 116 | 
            +
                    "-2",
         | 
| 117 | 
            +
                    out_put_file_name
         | 
| 118 | 
            +
                ]
         | 
| 119 | 
            +
                process = await asyncio.create_subprocess_exec(
         | 
| 120 | 
            +
                    *file_genertor_command,
         | 
| 121 | 
            +
                    # stdout must a pipe to be accessible as process.stdout
         | 
| 122 | 
            +
                    stdout=asyncio.subprocess.PIPE,
         | 
| 123 | 
            +
                    stderr=asyncio.subprocess.PIPE,
         | 
| 124 | 
            +
                )
         | 
| 125 | 
            +
                # Wait for the subprocess to finish
         | 
| 126 | 
            +
                stdout, stderr = await process.communicate()
         | 
| 127 | 
            +
                e_response = stderr.decode().strip()
         | 
| 128 | 
            +
                t_response = stdout.decode().strip()
         | 
| 129 | 
            +
                if os.path.lexists(out_put_file_name):
         | 
| 130 | 
            +
                    return out_put_file_name
         | 
| 131 | 
            +
                else:
         | 
| 132 | 
            +
                    return None
         | 
| 133 | 
            +
             | 
| 134 | 
            +
             | 
| 135 | 
            +
            async def generate_screen_shots(
         | 
| 136 | 
            +
                video_file,
         | 
| 137 | 
            +
                output_directory,
         | 
| 138 | 
            +
                is_watermarkable,
         | 
| 139 | 
            +
                wf,
         | 
| 140 | 
            +
                min_duration,
         | 
| 141 | 
            +
                no_of_photos
         | 
| 142 | 
            +
            ):
         | 
| 143 | 
            +
                metadata = extractMetadata(createParser(video_file))
         | 
| 144 | 
            +
                duration = 0
         | 
| 145 | 
            +
                if metadata is not None:
         | 
| 146 | 
            +
                    if metadata.has("duration"):
         | 
| 147 | 
            +
                        duration = metadata.get('duration').seconds
         | 
| 148 | 
            +
                if duration > min_duration:
         | 
| 149 | 
            +
                    images = []
         | 
| 150 | 
            +
                    ttl_step = duration // no_of_photos
         | 
| 151 | 
            +
                    current_ttl = ttl_step
         | 
| 152 | 
            +
                    for looper in range(0, no_of_photos):
         | 
| 153 | 
            +
                        ss_img = await take_screen_shot(video_file, output_directory, current_ttl)
         | 
| 154 | 
            +
                        current_ttl = current_ttl + ttl_step
         | 
| 155 | 
            +
                        if is_watermarkable:
         | 
| 156 | 
            +
                            ss_img = await place_water_mark(ss_img, output_directory + "/" + str(time.time()) + ".jpg", wf)
         | 
| 157 | 
            +
                        images.append(ss_img)
         | 
| 158 | 
            +
                    return images
         | 
| 159 | 
            +
                else:
         | 
| 160 | 
            +
                    return None
         | 
    	
        helper_funcs/help_uploadbot.py
    ADDED
    
    | @@ -0,0 +1,49 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            #!/usr/bin/env python3
         | 
| 2 | 
            +
            # -*- coding: utf-8 -*-
         | 
| 3 | 
            +
            # (c) Shrimadhav U K
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # the logging things
         | 
| 6 | 
            +
            import logging
         | 
| 7 | 
            +
            logging.basicConfig(level=logging.DEBUG,
         | 
| 8 | 
            +
                                format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
         | 
| 9 | 
            +
            logger = logging.getLogger(__name__)
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            import os
         | 
| 12 | 
            +
            import requests
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            def DetectFileSize(url):
         | 
| 15 | 
            +
                r = requests.get(url, allow_redirects=True, stream=True)
         | 
| 16 | 
            +
                total_size = int(r.headers.get("content-length", 0))
         | 
| 17 | 
            +
                return total_size
         | 
| 18 | 
            +
             | 
| 19 | 
            +
             | 
| 20 | 
            +
            def DownLoadFile(url, file_name, chunk_size, client, ud_type, message_id, chat_id):
         | 
| 21 | 
            +
                if os.path.exists(file_name):
         | 
| 22 | 
            +
                    os.remove(file_name)
         | 
| 23 | 
            +
                if not url:
         | 
| 24 | 
            +
                    return file_name
         | 
| 25 | 
            +
                r = requests.get(url, allow_redirects=True, stream=True)
         | 
| 26 | 
            +
                # https://stackoverflow.com/a/47342052/4723940
         | 
| 27 | 
            +
                total_size = int(r.headers.get("content-length", 0))
         | 
| 28 | 
            +
                downloaded_size = 0
         | 
| 29 | 
            +
                with open(file_name, 'wb') as fd:
         | 
| 30 | 
            +
                    for chunk in r.iter_content(chunk_size=chunk_size):
         | 
| 31 | 
            +
                        if chunk:
         | 
| 32 | 
            +
                            fd.write(chunk)
         | 
| 33 | 
            +
                            downloaded_size += chunk_size
         | 
| 34 | 
            +
                        if client is not None:
         | 
| 35 | 
            +
                            if ((total_size // downloaded_size) % 5) == 0:
         | 
| 36 | 
            +
                                time.sleep(0.3)
         | 
| 37 | 
            +
                                try:
         | 
| 38 | 
            +
                                    client.edit_message_text(
         | 
| 39 | 
            +
                                        chat_id,
         | 
| 40 | 
            +
                                        message_id,
         | 
| 41 | 
            +
                                        text="{}: {} of {}".format(
         | 
| 42 | 
            +
                                            ud_type,
         | 
| 43 | 
            +
                                            humanbytes(downloaded_size),
         | 
| 44 | 
            +
                                            humanbytes(total_size)
         | 
| 45 | 
            +
                                        )
         | 
| 46 | 
            +
                                    )
         | 
| 47 | 
            +
                                except:
         | 
| 48 | 
            +
                                    pass
         | 
| 49 | 
            +
                return file_name
         |