29 lines
955 B
Python
29 lines
955 B
Python
from pytube import YouTube
|
|
import random
|
|
import string
|
|
import os
|
|
|
|
def convert_video(link, media_format):
|
|
def randmname(size=6, chars=string.ascii_lowercase + string.digits):
|
|
return ''.join(random.choice(chars) for _ in range(size))
|
|
rand_filename = f"{randmname(25)}.{media_format.lower()}"
|
|
|
|
yt = YouTube(link)
|
|
if media_format == "MP4":
|
|
yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename=rand_filename, output_path="files/ytdl")
|
|
elif media_format == "MP3":
|
|
yt.streams.filter(only_audio=True).first().download(filename=rand_filename, output_path="files/ytdl")
|
|
|
|
cdn_url = {
|
|
"url": f"https://cdn.oki.cx/files/ytdl/{rand_filename}",
|
|
"title": yt.title,
|
|
"thumbnail": f"{yt.thumbnail_url}",
|
|
"filename": rand_filename
|
|
}
|
|
|
|
return cdn_url
|
|
|
|
def delete_file(filename):
|
|
os.remove(f"files/ytdl/{filename}")
|
|
|