-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEasyUrlLoader.py
70 lines (56 loc) · 2.4 KB
/
EasyUrlLoader.py
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
67
68
69
70
import os
import cv2
import yt_dlp
import numpy as np
class EasyUrlLoader:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"url": ("STRING", {"default": "https://www.Fancy.com/watch?v=your_video_url", "label": "Video URL"})
}
}
RETURN_TYPES = ("STRING",) # Return type changed to STRING for video file path
FUNCTION = "download_video"
CATEGORY = "Custom Nodes"
def download_video(self, url):
try:
output_path = './custom_nodes/comfyui-easyurlloader/downloads'
os.makedirs(output_path, exist_ok=True)
ydl_opts = {
'format': 'bestvideo[height<=2160]+bestaudio/best[height<=2160]/best[height<=2160]',
'outtmpl': f'{output_path}/%(title)s.%(ext)s',
'merge_output_format': 'mp4',
'noplaylist': True,
}
video_path = None
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(url, download=True)
video_path = ydl.prepare_filename(info)
except yt_dlp.DownloadError as e:
raise RuntimeError(f"Error downloading video: {e}")
except Exception as e:
raise RuntimeError(f"Unexpected error: {e}")
if not video_path or not os.path.exists(video_path):
raise FileNotFoundError(f'Video file not found: {video_path}')
batch_size = 100
cap = cv2.VideoCapture(video_path)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
processed_frames = 0
while processed_frames < frame_count:
frames = []
for _ in range(batch_size):
success, frame = cap.read()
if not success:
break
frames.append(frame)
if frames:
processed_frames += len(frames)
cap.release()
return (video_path,)
except Exception as e:
print(f'Error processing video: {e}')
return ("Error: Unable to download video",)
NODE_CLASS_MAPPINGS = {"EasyUrlLoader": EasyUrlLoader}
NODE_DISPLAY_NAME_MAPPINGS = {"EasyUrlLoader": "EasyUrl Loader"}