yizhoucp commited on
Commit
7cc362f
·
verified ·
1 Parent(s): 876c6e8

Delete custom_nodes/baiyang_h3_s3_uploader.py

Browse files
custom_nodes/baiyang_h3_s3_uploader.py DELETED
@@ -1,264 +0,0 @@
1
- import json
2
- import os
3
- import shutil
4
- import subprocess
5
- import time
6
- from pathlib import Path
7
-
8
- import boto3
9
- import requests
10
- import folder_paths
11
-
12
-
13
- class BaiYangH3S3VideoUploader:
14
- """Upload the final H3 video only after verifying its audio stream."""
15
-
16
- CALLBACK_TIMEOUT_SECONDS = 10
17
- CALLBACK_RETRY_TIMES = 3
18
- CALLBACK_RETRY_INTERVAL_SECONDS = 2
19
- FILE_WAIT_SECONDS = 20
20
- FFPROBE_TIMEOUT_SECONDS = 60
21
-
22
- @classmethod
23
- def INPUT_TYPES(cls):
24
- return {
25
- "required": {
26
- "api_url": ("STRING", {"default": ""}),
27
- "ali_env_mark": ("STRING", {"default": ""}),
28
- "manual_job_id": ("STRING", {"default": ""}),
29
- "aws_access_key": ("STRING", {"default": ""}),
30
- "aws_secret_key": ("STRING", {"default": ""}),
31
- "bucket_name": ("STRING", {"default": ""}),
32
- "region_name": ("STRING", {"default": "us-east-1"}),
33
- "endpoint_url": ("STRING", {"default": ""}),
34
- "s3_folder": ("STRING", {"default": "prod/comfyui"}),
35
- "cdn_domain": ("STRING", {"default": ""}),
36
- },
37
- "optional": {"filenames": ("*",)},
38
- }
39
-
40
- RETURN_TYPES = ()
41
- FUNCTION = "upload_and_notify"
42
- OUTPUT_NODE = True
43
- CATEGORY = "BaiYang_Tools/H3"
44
-
45
- @staticmethod
46
- def _setting(value, *environment_names):
47
- text = str(value or "").strip()
48
- if text.startswith("${") and text.endswith("}"):
49
- text = ""
50
- if text:
51
- return text
52
- return next(
53
- (os.environ[name].strip() for name in environment_names
54
- if os.environ.get(name, "").strip()),
55
- "",
56
- )
57
-
58
- @staticmethod
59
- def _find_video_path(filenames):
60
- output_root = Path(folder_paths.get_output_directory())
61
- temp_root = Path(folder_paths.get_temp_directory())
62
- default_root = output_root
63
- if (isinstance(filenames, (list, tuple)) and len(filenames) == 2
64
- and filenames[0] is False):
65
- default_root = temp_root
66
- candidates = []
67
-
68
- def collect(value, root=default_root):
69
- if isinstance(value, str) and value.lower().endswith(".mp4"):
70
- path = Path(value)
71
- candidates.append(path if path.is_absolute() else root / path)
72
- elif isinstance(value, (list, tuple)):
73
- for child in value:
74
- collect(child, root)
75
- elif isinstance(value, dict):
76
- if isinstance(value.get("filename"), str):
77
- file_root = temp_root if value.get("type") == "temp" else root
78
- collect(str(Path(value.get("subfolder") or "") / value["filename"]), file_root)
79
- else:
80
- for child in value.values():
81
- collect(child, root)
82
-
83
- collect(filenames)
84
- unique = list(dict.fromkeys(candidates))
85
- if not unique:
86
- raise ValueError("H3 S3: no MP4 path supplied by VHS_VideoCombine")
87
- audio_paths = [path for path in unique if path.name.lower().endswith("-audio.mp4")]
88
- # Do not fall back to an intermediate file if the final file is missing.
89
- return (audio_paths or unique)[-1]
90
-
91
- @classmethod
92
- def _wait_until_stable(cls, path):
93
- deadline = time.monotonic() + cls.FILE_WAIT_SECONDS
94
- last = None
95
- stable_count = 0
96
- while time.monotonic() < deadline:
97
- try:
98
- stat = path.stat()
99
- current = (stat.st_size, stat.st_mtime_ns)
100
- if path.is_file() and stat.st_size > 0 and current == last:
101
- stable_count += 1
102
- if stable_count >= 2:
103
- return
104
- else:
105
- stable_count = 0
106
- last = current
107
- except FileNotFoundError:
108
- last = None
109
- stable_count = 0
110
- time.sleep(0.5)
111
- raise RuntimeError("H3 S3: final video is missing, empty, or still being written")
112
-
113
- @staticmethod
114
- def _file_identity(path):
115
- stat = path.stat()
116
- return (stat.st_dev, stat.st_ino, stat.st_size, stat.st_mtime_ns)
117
-
118
- @classmethod
119
- def _verify_audio(cls, path):
120
- executable = shutil.which("ffprobe")
121
- if executable is None:
122
- raise RuntimeError("H3 S3: ffprobe is required to verify audio")
123
- try:
124
- result = subprocess.run(
125
- [
126
- executable, "-v", "error", "-count_packets",
127
- "-show_entries", "stream=codec_type,codec_name,nb_read_packets",
128
- "-of", "json", str(path),
129
- ],
130
- capture_output=True, text=True, timeout=cls.FFPROBE_TIMEOUT_SECONDS,
131
- check=True,
132
- )
133
- streams = json.loads(result.stdout).get("streams", [])
134
- except (subprocess.SubprocessError, OSError, ValueError) as error:
135
- raise RuntimeError(
136
- "H3 S3: ffprobe could not verify the final video; upload blocked"
137
- ) from error
138
-
139
- def has_packets(stream):
140
- try:
141
- return int(stream.get("nb_read_packets", 0)) > 0
142
- except (TypeError, ValueError):
143
- return False
144
-
145
- video_streams = [
146
- stream for stream in streams
147
- if stream.get("codec_type") == "video" and has_packets(stream)
148
- ]
149
- audio_streams = [
150
- stream for stream in streams
151
- if stream.get("codec_type") == "audio" and has_packets(stream)
152
- ]
153
- if not video_streams or not audio_streams:
154
- raise RuntimeError(
155
- "H3 S3: final MP4 must contain video and audio packets; "
156
- "refusing to upload a silent intermediate video"
157
- )
158
- return {
159
- "video_codec": video_streams[0].get("codec_name"),
160
- "audio_codec": audio_streams[0].get("codec_name"),
161
- "audio_packets": int(audio_streams[0]["nb_read_packets"]),
162
- }
163
-
164
- def _notify(self, api_url, ali_env_mark, payload):
165
- headers = {"Content-Type": "application/json"}
166
- if ali_env_mark:
167
- headers["ali-env-mark"] = ali_env_mark
168
- last_error = ""
169
- for attempt in range(1, self.CALLBACK_RETRY_TIMES + 1):
170
- try:
171
- response = requests.post(
172
- api_url, json=payload, headers=headers,
173
- timeout=self.CALLBACK_TIMEOUT_SECONDS,
174
- allow_redirects=False,
175
- )
176
- if 200 <= response.status_code < 300:
177
- return
178
- last_error = f"HTTP {response.status_code}"
179
- except requests.RequestException as error:
180
- last_error = type(error).__name__
181
- if attempt < self.CALLBACK_RETRY_TIMES:
182
- time.sleep(self.CALLBACK_RETRY_INTERVAL_SECONDS)
183
- raise RuntimeError(
184
- f"H3 S3: callback failed after {self.CALLBACK_RETRY_TIMES} attempts "
185
- f"({last_error})"
186
- )
187
-
188
- def upload_and_notify(
189
- self, api_url, ali_env_mark, manual_job_id, aws_access_key, aws_secret_key,
190
- bucket_name, region_name, endpoint_url, s3_folder, cdn_domain, filenames=None,
191
- ):
192
- job_id = self._setting(manual_job_id, "BAIYANG_MANUAL_JOB_ID")
193
- api_url = self._setting(api_url, "BAIYANG_API_URL")
194
- ali_env_mark = self._setting(ali_env_mark, "BAIYANG_ALI_ENV_MARK")
195
- aws_access_key = self._setting(aws_access_key, "BAIYANG_AWS_ACCESS_KEY", "AWS_ACCESS_KEY_ID")
196
- aws_secret_key = self._setting(aws_secret_key, "BAIYANG_AWS_SECRET_KEY", "AWS_SECRET_ACCESS_KEY")
197
- aws_session_token = self._setting("", "BAIYANG_AWS_SESSION_TOKEN", "AWS_SESSION_TOKEN")
198
- bucket_name = self._setting(bucket_name, "BAIYANG_S3_BUCKET")
199
- region_name = self._setting(region_name, "BAIYANG_S3_REGION", "AWS_DEFAULT_REGION")
200
- endpoint_url = self._setting(endpoint_url, "BAIYANG_S3_ENDPOINT")
201
- s3_folder = self._setting(s3_folder, "BAIYANG_S3_FOLDER")
202
- cdn_domain = self._setting(cdn_domain, "BAIYANG_CDN_DOMAIN")
203
- if not job_id:
204
- raise ValueError("H3 S3: manual_job_id is required; inject the backend task ID")
205
- required = {
206
- "api_url": api_url, "aws_access_key": aws_access_key,
207
- "aws_secret_key": aws_secret_key, "bucket_name": bucket_name,
208
- "region_name": region_name, "endpoint_url": endpoint_url,
209
- "s3_folder": s3_folder, "cdn_domain": cdn_domain,
210
- }
211
- missing = [name for name, value in required.items() if not value]
212
- if missing:
213
- raise ValueError("H3 S3: missing uploader fields: " + ", ".join(missing))
214
-
215
- try:
216
- target_path = self._find_video_path(filenames)
217
- self._wait_until_stable(target_path)
218
- identity = self._file_identity(target_path)
219
- media = self._verify_audio(target_path)
220
- if identity != self._file_identity(target_path):
221
- raise RuntimeError("H3 S3: video changed during audio verification")
222
- client_kwargs = {
223
- "aws_access_key_id": aws_access_key,
224
- "aws_secret_access_key": aws_secret_key,
225
- "region_name": region_name,
226
- "endpoint_url": endpoint_url,
227
- }
228
- if aws_session_token:
229
- client_kwargs["aws_session_token"] = aws_session_token
230
- s3_key = f"{s3_folder.strip('/')}/{job_id}.mp4"
231
- s3_client = boto3.client("s3", **client_kwargs)
232
- print("[BaiYangH3S3VideoUploader] verified final video: " + json.dumps(
233
- {"filename": target_path.name, "size": identity[2], **media},
234
- ensure_ascii=False,
235
- ))
236
- s3_client.upload_file(
237
- str(target_path), bucket_name, s3_key,
238
- ExtraArgs={"ContentType": "video/mp4"},
239
- )
240
- if identity != self._file_identity(target_path):
241
- raise RuntimeError("H3 S3: video changed during upload; success withheld")
242
- except Exception:
243
- try:
244
- self._notify(api_url, ali_env_mark, {"promptId": job_id, "result": "false"})
245
- except Exception as callback_error:
246
- print("[BaiYangH3S3VideoUploader] failure callback failed: "
247
- + type(callback_error).__name__)
248
- raise
249
-
250
- video_url = f"{cdn_domain.rstrip('/')}/{s3_key}"
251
- try:
252
- self._notify(api_url, ali_env_mark, {
253
- "promptId": job_id, "result": "true", "videoUrl": video_url,
254
- })
255
- except RuntimeError as error:
256
- raise RuntimeError("H3 S3: video uploaded, but success " + str(error)) from error
257
- print("[BaiYangH3S3VideoUploader] upload and callback succeeded: " + json.dumps(
258
- {"promptId": job_id, "videoUrl": video_url}, ensure_ascii=False,
259
- ))
260
- return ()
261
-
262
-
263
- NODE_CLASS_MAPPINGS = {"BaiYangH3S3VideoUploader": BaiYangH3S3VideoUploader}
264
- NODE_DISPLAY_NAME_MAPPINGS = {"BaiYangH3S3VideoUploader": "H3 S3 上传(验证音轨)"}