update version 1.0
This commit is contained in:
		
							parent
							
								
									68d809f54e
								
							
						
					
					
						commit
						5a6ebb8eaf
					
				
							
								
								
									
										76
									
								
								vid_utils.py
									
									
									
									
									
								
							
							
						
						
									
										76
									
								
								vid_utils.py
									
									
									
									
									
								
							@ -1,18 +1,80 @@
 | 
				
			|||||||
import re
 | 
					import re
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
from glob import glob
 | 
					from glob import glob, escape
 | 
				
			||||||
from subprocess import Popen, PIPE
 | 
					from subprocess import Popen, PIPE
 | 
				
			||||||
from time import strftime, strptime
 | 
					from time import strftime, strptime, sleep
 | 
				
			||||||
 | 
					from contextlib import contextmanager
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from telegram import InlineKeyboardButton
 | 
				
			||||||
# many of these imports serve the commented code...
 | 
					# many of these imports serve the commented code...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# this is the hard-split version (files need to be concatenated...)
 | 
					class Video:
 | 
				
			||||||
def check_dimension(f):
 | 
					    def __init__(self, link, chat_id):
 | 
				
			||||||
    """ If f is larger than 50MB it divides it into files up to 45MB """
 | 
					        self.link = link
 | 
				
			||||||
    if os.path.getsize(f) > 50 * 1024 * 1023:
 | 
					        self.chat_id = chat_id
 | 
				
			||||||
        os.system('split -b 49MB "{0}" "{1}"'.format(f, f))
 | 
					        self.formats = self.get_formats()
 | 
				
			||||||
 | 
					        self.keyboard = self.generate_keyboard()
 | 
				
			||||||
 | 
					        self.file_name = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_formats(self, link=None):
 | 
				
			||||||
 | 
					        formats = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        cmd = "youtube-dl -F {}".format(self.link)
 | 
				
			||||||
 | 
					        p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate()
 | 
				
			||||||
 | 
					        it = iter(str(p[0], 'utf-8').split('\n')) # iterator of output lines
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        while "code  extension" not in next(it): pass # Remove garbage lines
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        while True:
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                line = next(it)
 | 
				
			||||||
 | 
					                if not line:
 | 
				
			||||||
 | 
					                    raise StopIteration # Usually the last line is empty
 | 
				
			||||||
 | 
					                if "video only" in line:
 | 
				
			||||||
 | 
					                    continue # I don't need video without audio
 | 
				
			||||||
 | 
					            except StopIteration:
 | 
				
			||||||
 | 
					                break
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                format_code, extension, resolution, *_ = line.strip().split()
 | 
				
			||||||
 | 
					                formats.append([format_code, extension, resolution])
 | 
				
			||||||
 | 
					        return formats
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def generate_keyboard(self):
 | 
				
			||||||
 | 
					        """ Generate a list of InlineKeyboardButton of resolutions """
 | 
				
			||||||
 | 
					        kb = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for code, extension, resolution in self.formats:
 | 
				
			||||||
 | 
					            kb.append([InlineKeyboardButton("{0}, {1}".format(extension, resolution),
 | 
				
			||||||
 | 
					                                               callback_data=code)])
 | 
				
			||||||
 | 
					        return kb
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def download(self, resolution_code):
 | 
				
			||||||
 | 
					        cmd = "youtube-dl -f {0} {1}".format(resolution_code, self.link)
 | 
				
			||||||
 | 
					        p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for line in str(p[0], 'utf-8').split('\n'):
 | 
				
			||||||
 | 
					            if "[download] Destination:" in line:
 | 
				
			||||||
 | 
					                self.file_name = line[24:] # name of the file
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def check_dimension(self):
 | 
				
			||||||
 | 
					        if os.path.getsize(self.file_name) > 50 * 1024 * 1023:
 | 
				
			||||||
 | 
					            os.system('split -b 49M "{0}" "{1}"'.format(self.file_name, self.file_name))
 | 
				
			||||||
 | 
					            os.remove(self.file_name)
 | 
				
			||||||
 | 
					        return glob(escape(self.file_name) + '*')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @contextmanager
 | 
				
			||||||
 | 
					    def send(self):
 | 
				
			||||||
 | 
					        files = self.check_dimension() # split if size >= 50MB
 | 
				
			||||||
 | 
					        yield files
 | 
				
			||||||
 | 
					        for f in files: #removing old files
 | 
				
			||||||
            os.remove(f)
 | 
					            os.remove(f)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#__________________________OLD STUFFS, TOUCH CAREFULLY__________________________
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# this is the soft-split version, require avconv, but the audio isn't synchronized, avconv's problems :(
 | 
					# this is the soft-split version, require avconv, but the audio isn't synchronized, avconv's problems :(
 | 
				
			||||||
'''
 | 
					'''
 | 
				
			||||||
def get_duration(filepath): # get duration in seconds
 | 
					def get_duration(filepath): # get duration in seconds
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user