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89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import os
import re
import cv2
import sys
import json
import torch
import datetime
import itertools
import subprocess
import folder_paths
import numpy as np
from string import Template
from pathlib import Path
from PIL import Image, ExifTags
from PIL.PngImagePlugin import PngInfo
from .ffmpeg import ffmpeg_path, gifski_path
from ..utils import tensor_to_bytes, tensor_to_shorts, requeue_workflow



def gen_format_widgets(video_format):
    for k in video_format:
        if k.endswith("_pass"):
            for i in range(len(video_format[k])):
                if isinstance(video_format[k][i], list):
                    item = [video_format[k][i]]
                    yield item
                    video_format[k][i] = item[0]
        else:
            if isinstance(video_format[k], list):
                item = [video_format[k]]
                yield item
                video_format[k] = item[0]
               
def get_format_widget_defaults(format_name):
    video_format_path = folder_paths.get_full_path("VHS_video_formats", format_name + ".json")
    with open(video_format_path, 'r') as stream:
        video_format = json.load(stream)
    results = {}
    for w in gen_format_widgets(video_format):
        if len(w[0]) > 2 and 'default' in w[0][2]:
            default = w[0][2]['default']
        else:
            if type(w[0][1]) is list:
                default = w[0][1][0]
            else:
                #NOTE: This doesn't respect max/min, but should be good enough as a fallback to a fallback to a fallback
                default = {"BOOLEAN": False, "INT": 0, "FLOAT": 0, "STRING": ""}[w[0][1]]
        results[w[0][0]] = default
    return results
 
def get_video_formats():
    formats = []
    for format_name in folder_paths.get_filename_list("VHS_video_formats"):
        format_name = format_name[:-5]
        formats.append("video/" + format_name)
    return formats

def gifski_process(args, video_format, file_path, env):
    frame_data = yield
    with subprocess.Popen(args + video_format['main_pass'] + ['-f', 'yuv4mpegpipe', '-'],
                          stderr=subprocess.PIPE, stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE, env=env) as procff:
        with subprocess.Popen([gifski_path] + video_format['gifski_pass']
                              + ['-q', '-o', file_path, '-'], stderr=subprocess.PIPE,
                              stdin=procff.stdout, stdout=subprocess.PIPE,
                              env=env) as procgs:
            try:
                while frame_data is not None:
                    procff.stdin.write(frame_data)
                    frame_data = yield
                procff.stdin.flush()
                procff.stdin.close()
                resff = procff.stderr.read()
                resgs = procgs.stderr.read()
                outgs = procgs.stdout.read()
            except BrokenPipeError as e:
                procff.stdin.close()
                resff = procff.stderr.read()
                resgs = procgs.stderr.read()
                raise Exception("An error occurred while creating gifski output\n" \
                        + "Make sure you are using gifski --version >=1.32.0\nffmpeg: " \
                        + resff.decode("utf-8") + '\ngifski: ' + resgs.decode("utf-8"))
    if len(resff) > 0:
        print(resff.decode("utf-8"), end="", file=sys.stderr)
    if len(resgs) > 0:
        print(resgs.decode("utf-8"), end="", file=sys.stderr)
    #should always be empty as the quiet flag is passed
    if len(outgs) > 0:
        print(outgs.decode("utf-8"))

def ffmpeg_process(args, video_format, video_metadata, file_path, env):

    res = None
    frame_data = yield
    total_frames_output = 0
    if video_format.get('save_metadata', 'False') != 'False':
        os.makedirs(folder_paths.get_temp_directory(), exist_ok=True)
        metadata = json.dumps(video_metadata)
        metadata_path = os.path.join(folder_paths.get_temp_directory(), "metadata.txt")
        #metadata from file should  escape = ; # \ and newline
        metadata = metadata.replace("\\","\\\\")
        metadata = metadata.replace(";","\\;")
        metadata = metadata.replace("#","\\#")
        metadata = metadata.replace("=","\\=")
        metadata = metadata.replace("\n","\\\n")
        metadata = "comment=" + metadata
        with open(metadata_path, "w") as f:
            f.write(";FFMETADATA1\n")
            f.write(metadata)
        m_args = args[:1] + ["-i", metadata_path] + args[1:] + ["-metadata", "creation_time=now"]
        print(f'ffmpeg: {m_args}')
        with subprocess.Popen(m_args + [file_path], stderr=subprocess.PIPE,
                              stdin=subprocess.PIPE, env=env) as proc:
            try:
                while frame_data is not None:
                    proc.stdin.write(frame_data)
                    #TODO: skip flush for increased speed
                    frame_data = yield
                    total_frames_output+=1
                proc.stdin.flush()
                proc.stdin.close()
                res = proc.stderr.read()
            except BrokenPipeError as e:
                err = proc.stderr.read()
                #Check if output file exists. If it does, the re-execution
                #will also fail. This obscures the cause of the error
                #and seems to never occur concurrent to the metadata issue
                if os.path.exists(file_path):
                    raise Exception("An error occurred in the ffmpeg subprocess:\n" \
                            + err.decode("utf-8"))
                #Res was not set
                print(err.decode("utf-8"), end="", file=sys.stderr)
                print("An error occurred when saving with metadata")
    if res != b'':
        with subprocess.Popen(args + [file_path], stderr=subprocess.PIPE,
                              stdin=subprocess.PIPE, env=env) as proc:
            try:
                while frame_data is not None:
                    proc.stdin.write(frame_data)
                    frame_data = yield
                    total_frames_output+=1
                proc.stdin.flush()
                proc.stdin.close()
                res = proc.stderr.read()
            except BrokenPipeError as e:
                res = proc.stderr.read()
                raise Exception("An error occurred in the ffmpeg subprocess:\n" \
                        + res.decode("utf-8"))
    yield total_frames_output
    if len(res) > 0:
        print(res.decode("utf-8"), end="", file=sys.stderr)
        
def to_pingpong(inp):
    if not hasattr(inp, "__getitem__"):
        inp = list(inp)
    yield from inp
    for i in range(len(inp)-2,0,-1):
        yield inp[i]
        
def apply_format_widgets(format_name, kwargs):
    video_format_path = folder_paths.get_full_path("VHS_video_formats", format_name + ".json")
    with open(video_format_path, 'r') as stream:
        video_format = json.load(stream)

    for w in gen_format_widgets(video_format):
        assert(w[0][0] in kwargs)
        if len(w[0]) > 3:
            w[0] = Template(w[0][3]).substitute(val=kwargs[w[0][0]])
        else:
            w[0] = str(kwargs[w[0][0]])
    return video_format

class SaveVideoNode:
    @classmethod
    def INPUT_TYPES(s):
        ffmpeg_formats = get_video_formats()
        return {
            "required": {
                "path": ("STRING", {"multiline": True, "dynamicPrompts": False}),
                "format": (ffmpeg_formats,),
                "quality": ([100, 95, 90, 85, 80, 75, 70, 60, 50], {"default": 100}),
                "pingpong": ("BOOLEAN", {"default": False}),
            },
            "optional": {
                "images": ("IMAGE",),
                "audio": ("AUDIO",),
                "frame_rate": ("INT,FLOAT", { "default": 25.0, "step": 1.0, "min": 1.0, "max": 60.0 }),
                "meta_batch": ("BatchManager",),
            },
            "hidden": {
                "prompt": "PROMPT",
                "unique_id": "UNIQUE_ID"
            },
        }

    RETURN_TYPES = ()
    CATEGORY = "tbox/Video"
    FUNCTION = "save_video"
    OUTPUT_NODE = True
    
    def save_video(
        self,
        path,
        frame_rate=25,
        images=None,
        format="video/h264-mp4",
        quality=85,
        pingpong=False,
        audio=None,
        prompt=None,
        meta_batch=None,
        unique_id=None,
        manual_format_widgets=None,
    ):
        if images is None:
            return {}
        if isinstance(images, torch.Tensor) and images.size(0) == 0:
            return {}
        
        if frame_rate < 1:
            frame_rate = 1
        elif frame_rate > 120:
            frame_rate = 120
            
        num_frames = len(images)

        first_image = images[0]
        images = iter(images)
        
        file_path = os.path.abspath(path.split('\n')[0])
        output_dir = os.path.dirname(file_path)
        filename = os.path.basename(file_path)
        name, extension = os.path.splitext(filename)
       
        output_process = None

        video_metadata = {}
        if prompt is not None:
            video_metadata["prompt"] = prompt
            
        if meta_batch is not None and unique_id in meta_batch.outputs:
            (counter, output_process) = meta_batch.outputs[unique_id]
        else:
            counter = 0 
            output_process = None    

        format_type, format_ext = format.split("/")

        # Use ffmpeg to save a video
        if ffmpeg_path is None:
            raise ProcessLookupError(f"ffmpeg is required for video outputs and could not be found.\nIn order to use video outputs, you must either:\n- Install imageio-ffmpeg with pip,\n- Place a ffmpeg executable in {os.path.abspath('')}, or\n- Install ffmpeg and add it to the system path.")

        #Acquire additional format_widget values
        kwargs = None
        if manual_format_widgets is None:
            if prompt is not None:
                kwargs = prompt[unique_id]['inputs']
            else:
                manual_format_widgets = {}
        
        if kwargs is None:
            kwargs = get_format_widget_defaults(format_ext)
            missing = {}
            for k in kwargs.keys():
                if k in manual_format_widgets:
                    kwargs[k] = manual_format_widgets[k]
                else:
                    missing[k] = kwargs[k]
            if len(missing) > 0:
                print("Extra format values were not provided, the following defaults will be used: " + str(kwargs) + "\nThis is likely due to usage of ComfyUI-to-python. These values can be manually set by supplying a manual_format_widgets argument")
    
        video_format = apply_format_widgets(format_ext, kwargs)
        has_alpha = first_image.shape[-1] == 4
        dim_alignment = video_format.get("dim_alignment", 8)
        if (first_image.shape[1] % dim_alignment) or (first_image.shape[0] % dim_alignment):
            #output frames must be padded
            to_pad = (-first_image.shape[1] % dim_alignment,
                        -first_image.shape[0] % dim_alignment)
            padding = (to_pad[0]//2, to_pad[0] - to_pad[0]//2,
                        to_pad[1]//2, to_pad[1] - to_pad[1]//2)
            padfunc = torch.nn.ReplicationPad2d(padding)
            def pad(image):
                image = image.permute((2,0,1))#HWC to CHW
                padded = padfunc(image.to(dtype=torch.float32))
                return padded.permute((1,2,0))
            images = map(pad, images)
            new_dims = (-first_image.shape[1] % dim_alignment + first_image.shape[1],
                        -first_image.shape[0] % dim_alignment + first_image.shape[0])
            dimensions = f"{new_dims[0]}x{new_dims[1]}"
            print(f"Output images were not of valid resolution and have had padding applied: {dimensions}")
        else:
            dimensions = f"{first_image.shape[1]}x{first_image.shape[0]}"

        if pingpong:
            if meta_batch is not None:
                print("pingpong is incompatible with batched output")
            images = to_pingpong(images)
        
        images = map(tensor_to_bytes, images)
        if has_alpha:
            i_pix_fmt = 'rgba'
        else:
            i_pix_fmt = 'rgb24'
                
        args = [ffmpeg_path, "-v", "error", "-f", "rawvideo", "-pix_fmt", i_pix_fmt,
                "-s", dimensions, "-r", str(frame_rate), "-i", "-"]

        images = map(lambda x: x.tobytes(), images)
        env=os.environ.copy()
        if  "environment" in video_format:
            env.update(video_format["environment"])

        if "pre_pass" in video_format:
            images = [b''.join(images)]
            os.makedirs(folder_paths.get_temp_directory(), exist_ok=True)
            pre_pass_args = args[:13] + video_format['pre_pass']
            try:
                subprocess.run(pre_pass_args, input=images[0], env=env,
                                capture_output=True, check=True)
            except subprocess.CalledProcessError as e:
                raise Exception("An error occurred in the ffmpeg prepass:\n" \
                        + e.stderr.decode("utf-8"))
        if "inputs_main_pass" in video_format:
            args = args[:13] + video_format['inputs_main_pass'] + args[13:]

        if output_process is None:
            args += video_format['main_pass'] 
            output_process = ffmpeg_process(args, video_format, video_metadata, file_path, env)
            #Proceed to first yield
            output_process.send(None)
            if meta_batch is not None:
                meta_batch.outputs[unique_id] = (0, output_process)

        for image in images:
            output_process.send(image)
        if meta_batch is not None:
            requeue_workflow((meta_batch.unique_id, not meta_batch.has_closed_inputs))
        if meta_batch is None or meta_batch.has_closed_inputs:
            #Close pipe and wait for termination.
            try:
                total_frames_output = output_process.send(None)
                output_process.send(None)
            except StopIteration:
                pass
            if meta_batch is not None:
                meta_batch.outputs.pop(unique_id)
                #if len(meta_batch.outputs) == 0:
                #    meta_batch.reset()
        else:
            return {}

        a_waveform = None
        if audio is not None:
            try:
                #safely check if audio produced by VHS_LoadVideo actually exists
                a_waveform = audio['waveform']
            except:
                print(f'save audio >> not waveform')    
                pass
        if a_waveform is not None:
            # Create audio file if input was provided
            output_file_with_audio = f"{name}-audio{extension}"
            output_file_with_audio_path = os.path.join(output_dir, output_file_with_audio)
            if "audio_pass" not in video_format:
                print("Selected video format does not have explicit audio support")
                video_format["audio_pass"] = ["-c:a", "libopus"]


            # FFmpeg command with audio re-encoding
            #TODO: expose audio quality options if format widgets makes it in
            #Reconsider forcing apad/shortest
            channels = audio['waveform'].size(1)
            min_audio_dur = total_frames_output / frame_rate + 1
            mux_args = [ffmpeg_path, "-v", "error", "-i", file_path,
                        "-ar", str(audio['sample_rate']), "-ac", str(channels),
                        "-y","-f", "f32le", "-i", "-", "-c:v", "copy"] \
                        + video_format["audio_pass"] \
                        + ["-af", "apad=whole_dur="+str(min_audio_dur),
                            "-shortest", output_file_with_audio_path]

            audio_data = audio['waveform'].squeeze(0).transpose(0,1) \
                    .numpy().tobytes()
            try:
                res = subprocess.run(mux_args, input=audio_data,
                                        env=env, capture_output=True, check=True)
                if res.returncode == 0:
                    self.replace_file(output_file_with_audio_path, file_path)
            except subprocess.CalledProcessError as e:
                raise Exception("An error occured in the ffmpeg subprocess:\n" \
                        + e.stderr.decode("utf-8"))
            if res.stderr:
                print(res.stderr.decode("utf-8"), end="", file=sys.stderr)


        return {}
    
    @classmethod
    def VALIDATE_INPUTS(self, format, **kwargs):
        return True

    def replace_file(self, audio_path, file_path):
        try:
            # 删除 file_path 文件(如果存在)
            if os.path.exists(file_path):
                os.remove(file_path)
                print(f"Deleted file: {file_path}")
            else:
                print(f"File not found, skipping deletion: {file_path}")
            
            # 将 output_file_with_audio_path 重命名为 file_path
            os.rename(audio_path, file_path)
            print(f"Renamed {audio_path} to {file_path}")
        except Exception as e:
            print(f"An error occurred: {e}")