Spaces:
Sleeping
Sleeping
File size: 2,134 Bytes
8d120bf 11c4fb6 8d120bf c52f09b 11c4fb6 8d120bf fdd892b 11c4fb6 8d120bf 11c4fb6 8d120bf 8f5637c fdd892b 8d120bf fdd892b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
from tempfile import mkdtemp
from tkinter import Y
from yt_dlp import YoutubeDL
import yt_dlp
from yt_dlp.postprocessor import PostProcessor
class FilenameCollectorPP(PostProcessor):
def __init__(self):
super(FilenameCollectorPP, self).__init__(None)
self.filenames = []
def run(self, information):
self.filenames.append(information["filepath"])
return [], information
def downloadUrl(url: str, maxDuration: int = None):
try:
return _performDownload(url, maxDuration=maxDuration)
except yt_dlp.utils.DownloadError as e:
# In case of an OS error, try again with a different output template
if e.msg and e.msg.find("[Errno 36] File name too long") >= 0:
return _performDownload(url, maxDuration=maxDuration, outputTemplate="%(title).10s %(id)s.%(ext)s")
pass
def _performDownload(url: str, maxDuration: int = None, outputTemplate: str = None):
destinationDirectory = mkdtemp()
ydl_opts = {
"format": "bestaudio/best",
'playlist_items': '1',
'paths': {
'home': destinationDirectory
}
}
# Add output template if specified
if outputTemplate:
ydl_opts['outtmpl'] = outputTemplate
filename_collector = FilenameCollectorPP()
with YoutubeDL(ydl_opts) as ydl:
if maxDuration and maxDuration > 0:
info = ydl.extract_info(url, download=False)
duration = info['duration']
if duration >= maxDuration:
raise ExceededMaximumDuration(videoDuration=duration, maxDuration=maxDuration, message="Video is too long")
ydl.add_post_processor(filename_collector)
ydl.download([url])
if len(filename_collector.filenames) <= 0:
raise Exception("Cannot download " + url)
result = filename_collector.filenames[0]
print("Downloaded " + result)
return result
class ExceededMaximumDuration(Exception):
def __init__(self, videoDuration, maxDuration, message):
self.videoDuration = videoDuration
self.maxDuration = maxDuration
super().__init__(message) |