56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
import os
|
|
|
|
from app.core.config import settings
|
|
from app.core.uploader import uploader_backend
|
|
from app.utils.uploader import S3UploadBackend
|
|
from app.schemas import DownloadRequest, DownloadResponse, DownloadResponseV2
|
|
|
|
|
|
class YouTubeService:
|
|
@staticmethod
|
|
def download(data: DownloadRequest):
|
|
from app.utils.youtube import YtDlpManager
|
|
|
|
manager = YtDlpManager(str(data.url), uploader_backend)
|
|
uploader_backend.key_prefix = f"{manager.id}@{data.quality}@"
|
|
video_url = manager.download_video(data.quality)
|
|
audio_url = manager.download_audio()
|
|
return DownloadResponse(video=video_url, audio=audio_url)
|
|
|
|
@staticmethod
|
|
def download_v2(data: DownloadRequest):
|
|
filepath = None
|
|
try:
|
|
from app.utils.youtubeV2 import YtDlpManager
|
|
|
|
manager = YtDlpManager(str(data.url))
|
|
best_audio = manager.best_audio()
|
|
best_video = manager.best_video(data.quality)
|
|
filepath = manager.download(best_video, best_audio)
|
|
upload_backend = S3UploadBackend(f"{manager.id}@{data.quality}@")
|
|
video_url = upload_backend.upload(os.path.basename(filepath), filepath)
|
|
return DownloadResponseV2(video=video_url)
|
|
finally:
|
|
if filepath:
|
|
os.remove(filepath)
|
|
|
|
@staticmethod
|
|
def resolutions(url: str):
|
|
from app.utils.youtubeV2 import YtDlpManager
|
|
manager = YtDlpManager(url)
|
|
return manager.resolutions
|
|
|
|
@staticmethod
|
|
def filesize(data: DownloadRequest):
|
|
from app.utils.youtubeV2 import YtDlpManager
|
|
manager = YtDlpManager((data.url))
|
|
video_size = manager.best_video(data.quality).filesize
|
|
audio_size = manager.best_audio().filesize
|
|
return {"filesize": video_size + audio_size}
|
|
|
|
|
|
class Files:
|
|
@staticmethod
|
|
def list():
|
|
return [f for f in os.listdir(settings.MEDIA_DIR) if os.path.isfile(f)]
|