telegram-video-downloader/vid_utils.py

116 lines
4.1 KiB
Python
Raw Normal View History

2017-08-06 15:19:51 +02:00
import os
2017-08-09 14:55:08 +02:00
from glob import glob, escape
2017-08-06 15:19:51 +02:00
from subprocess import Popen, PIPE
2017-08-09 14:55:08 +02:00
from contextlib import contextmanager
from telegram import InlineKeyboardButton
2021-06-19 20:35:07 +02:00
from db import VidDatabase
class BadLink(Exception):
pass
2017-08-09 14:55:08 +02:00
class Video:
2021-06-19 20:35:07 +02:00
def __init__(self, link=None, vid=None, init_keyboard=False):
self.db = VidDatabase("viddb.sqlite3")
if not self.db.is_valid:
# Database file not present
# Create a new database
self.db.create()
if vid is None and link is not None:
self.link = link
self.file_name = None
elif vid is not None and link is None:
self.link, self.code = self.db.select_vid(vid)
else:
raise Exception('what is going on?')
2017-08-19 18:44:56 +02:00
if init_keyboard:
self.formats = self.get_formats()
self.keyboard = self.generate_keyboard()
2017-08-09 14:55:08 +02:00
2017-08-19 18:44:56 +02:00
def get_formats(self):
2021-06-19 20:35:07 +02:00
formats = {}
2017-08-09 14:55:08 +02:00
2021-06-19 20:35:07 +02:00
p = Popen(["youtube-dl", "-F", self.link], stdout=PIPE, stderr=PIPE).communicate()
it = iter(str(p[0], 'utf-8').split('\n')) # iterator of output lines
2017-08-09 14:55:08 +02:00
try:
2021-06-19 20:35:07 +02:00
while "code extension" not in next(it):
pass # Remove garbage lines
except StopIteration:
2021-06-19 20:35:07 +02:00
raise BadLink("youtube-dl couldn't download the link you provided") # Isn't a valid youtube link
2017-08-09 14:55:08 +02:00
while True:
try:
line = next(it)
if not line:
2021-06-19 20:35:07 +02:00
raise StopIteration # Usually the last line is empty
2017-08-09 14:55:08 +02:00
if "video only" in line:
2021-06-19 20:35:07 +02:00
continue # I don't need video without audio
2017-08-09 14:55:08 +02:00
except StopIteration:
break
else:
format_code, extension, resolution, *_ = line.strip().split()
2021-06-19 20:35:07 +02:00
key = '{},{}'.format(extension, resolution)
index = self.db.insert_vid(self.link, format_code)
formats[key] = index
2017-08-09 14:55:08 +02:00
return formats
def generate_keyboard(self):
""" Generate a list of InlineKeyboardButton of resolutions """
kb = []
2021-06-19 20:35:07 +02:00
for key in self.formats.keys():
cb = "{}".format(self.formats[key])
kb.append([InlineKeyboardButton(key, callback_data=cb)])
2017-08-09 14:55:08 +02:00
return kb
2021-06-19 20:35:07 +02:00
def download(self):
p = Popen(["youtube-dl", "-f", self.code, self.link], stdout=PIPE, stderr=PIPE).communicate()
2017-08-09 14:55:08 +02:00
for line in str(p[0], 'utf-8').split('\n'):
if "[download] Destination:" in line:
2021-06-19 20:35:07 +02:00
self.file_name = line[24:] # name of the file
2017-08-09 14:55:08 +02:00
def check_dimension(self):
if os.path.getsize(self.file_name) > 50 * 1024 * 1023:
2021-06-19 20:35:07 +02:00
Popen(["split", "-b", "49M", self.file_name, self.file_name])
2017-08-09 14:55:08 +02:00
os.remove(self.file_name)
return glob(escape(self.file_name) + '*')
@contextmanager
def send(self):
2021-06-19 20:35:07 +02:00
files = self.check_dimension() # split if size >= 50MB
2017-08-09 14:55:08 +02:00
yield files
2021-06-19 20:35:07 +02:00
for f in files: # removing old files
2017-08-09 14:55:08 +02:00
os.remove(f)
2017-08-06 15:19:51 +02:00
2017-08-09 14:55:08 +02:00
#__________________________OLD STUFFS, TOUCH CAREFULLY__________________________
2017-08-06 15:19:51 +02:00
# 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
cmd = "avconv -i %s" % filepath
p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
di = p.communicate()
for line in di:
if line.rfind(b"Duration") > 0:
duration = str(re.findall(b"Duration: (\d+:\d+:[\d.]+)", line)[0])
return 3600 * int(duration[2: 4]) + 60 * int(duration[5: 7]) + int(duration[8: 10])
def check_dimension(f): # if f is bigger than 50MB split it in subvideos
if os.path.getsize(f) > 50 * 1024 * 1023:
duration = get_duration(f)
for i in range(0, duration, 180):
start = strftime("%H:%M:%S", strptime('{0} {1} {2}'.format(i // 3600, (i // 60) % 60, i % 60), "%H %M %S")) # TODO this is not pythonic code!
os.system("""avconv -i '{0}' -vcodec copy -acodec copy -ss {1} -t {2} 'part_{3}.mp4'""".format(f, start, 180, (i // 180) % 180))
os.remove(f) # delete original file
'''