|
1 | 1 | using System.Collections.Concurrent; |
2 | 2 | using System.Text.Json; |
3 | 3 | using System.Text.RegularExpressions; |
4 | | -using MetadataExtractor; |
5 | | -using MetadataExtractor.Formats.Jpeg; |
6 | | -using MetadataExtractor.Formats.Png; |
7 | 4 | using Directory = MetadataExtractor.Directory; |
8 | 5 | using Dir = System.IO.Directory; |
9 | 6 | using System.Globalization; |
10 | | -using System.Reflection.Metadata.Ecma335; |
11 | | -using MetadataExtractor.Formats.WebP; |
12 | 7 | using Diffusion.Common; |
13 | 8 | using System.Text; |
14 | | -using System.Security.Cryptography; |
15 | 9 | using SixLabors.ImageSharp; |
| 10 | +using MetadataExtractor; |
| 11 | +using MetadataExtractor.Formats.Jpeg; |
| 12 | +using MetadataExtractor.Formats.Png; |
| 13 | +using MetadataExtractor.Formats.WebP; |
| 14 | +using MetadataExtractor.Formats.QuickTime; |
16 | 15 |
|
17 | 16 | namespace Diffusion.IO; |
18 | 17 |
|
@@ -51,6 +50,7 @@ public enum FileType |
51 | 50 | PNG, |
52 | 51 | JPEG, |
53 | 52 | WebP, |
| 53 | + MP4, |
54 | 54 | Other, |
55 | 55 | } |
56 | 56 |
|
@@ -154,11 +154,23 @@ private static FileType GetFileType(Stream stream) |
154 | 154 | // Some PNGs are JPEGs in disguise, read the magic to make sure we have the correct file type |
155 | 155 | var fileType = GetFileType(stream); |
156 | 156 |
|
| 157 | + if (fileType == FileType.Other) |
| 158 | + { |
| 159 | + if (Path.GetExtension(file).ToLowerInvariant() == ".mp4") |
| 160 | + { |
| 161 | + fileType = FileType.MP4; |
| 162 | + } |
| 163 | + } |
| 164 | + |
157 | 165 | stream.Seek(0, SeekOrigin.Begin); |
158 | 166 |
|
159 | 167 | // Now, attempt to read the metadata |
160 | 168 | switch (fileType) |
161 | 169 | { |
| 170 | + case FileType.MP4: |
| 171 | + { |
| 172 | + break; |
| 173 | + } |
162 | 174 | case FileType.PNG: |
163 | 175 | { |
164 | 176 | IEnumerable<Directory> directories = PngMetadataReader.ReadMetadata(stream); |
@@ -255,7 +267,7 @@ private static FileType GetFileType(Stream stream) |
255 | 267 | { |
256 | 268 | var isJson = tag.Description.Substring("prompt: ".Length).Trim().StartsWith("{"); |
257 | 269 | format = isJson ? MetaFormat.ComfyUI : MetaFormat.EasyDiffusion; |
258 | | - var tempParameters = isJson ? ReadComfyUIParameters(file, tag.Description) : ReadEasyDiffusionParameters(file, directories); |
| 270 | + var tempParameters = isJson ? ReadComfyUIParameters(tag.Description) : ReadEasyDiffusionParameters(file, directories); |
259 | 271 |
|
260 | 272 | if (fileParameters == null) |
261 | 273 | { |
@@ -400,6 +412,25 @@ private static FileType GetFileType(Stream stream) |
400 | 412 | _ => fileParameters |
401 | 413 | }; |
402 | 414 | } |
| 415 | + else |
| 416 | + { |
| 417 | + if (tag.Description.StartsWith("{\"prompt\":")) |
| 418 | + { |
| 419 | + format = MetaFormat.ComfyUI; |
| 420 | + var tempParameters = ReadComfyUIParameters(tag.Description, true); |
| 421 | + |
| 422 | + if (fileParameters == null) |
| 423 | + { |
| 424 | + fileParameters = tempParameters; |
| 425 | + } |
| 426 | + else |
| 427 | + { |
| 428 | + fileParameters.WorkflowId = tempParameters.WorkflowId; |
| 429 | + fileParameters.Workflow = tempParameters.Workflow; |
| 430 | + fileParameters.Nodes = tempParameters.Nodes; |
| 431 | + } |
| 432 | + } |
| 433 | + } |
403 | 434 | break; |
404 | 435 | } |
405 | 436 | } |
@@ -495,7 +526,7 @@ private static FileType GetFileType(Stream stream) |
495 | 526 |
|
496 | 527 | fileParameters.Width = width; |
497 | 528 | fileParameters.Height = height; |
498 | | - |
| 529 | + |
499 | 530 | return fileParameters; |
500 | 531 | } |
501 | 532 |
|
@@ -699,27 +730,39 @@ private static int GetHashCode(JsonElement node) |
699 | 730 |
|
700 | 731 |
|
701 | 732 |
|
702 | | - private static FileParameters ReadComfyUIParameters(string file, string description) |
| 733 | + private static FileParameters ReadComfyUIParameters(string description, bool isProperJson = false) |
703 | 734 | { |
704 | 735 | var fp = new FileParameters(); |
705 | 736 |
|
706 | 737 | try |
707 | 738 | { |
708 | | - var json = description.Substring("prompt: ".Length); |
| 739 | + if (!isProperJson) |
| 740 | + { |
| 741 | + var json = description.Substring("prompt: ".Length); |
| 742 | + |
| 743 | + // fix for errant nodes |
| 744 | + json = json.Replace("NaN", "null"); |
| 745 | + |
| 746 | + fp.Workflow = json; |
709 | 747 |
|
710 | | - // fix for errant nodes |
711 | | - json = json.Replace("NaN", "null"); |
| 748 | + var root = JsonDocument.Parse(json); |
| 749 | + fp.WorkflowId = GetHashCode(root.RootElement).ToString("X"); |
712 | 750 |
|
713 | | - fp.Workflow = json; |
| 751 | + var parser = new ComfyUIParser(); |
| 752 | + var pnodes = parser.Parse(fp.WorkflowId, fp.Workflow); |
714 | 753 |
|
715 | | - var root = JsonDocument.Parse(json); |
| 754 | + fp.Nodes = pnodes; |
| 755 | + } |
| 756 | + else |
| 757 | + { |
| 758 | + fp.Workflow = description; |
716 | 759 |
|
717 | | - fp.WorkflowId = GetHashCode(root.RootElement).ToString("X"); |
| 760 | + var parser = new ComfyUIParser(); |
| 761 | + var pnodes = parser.Parse(fp.WorkflowId, fp.Workflow); |
718 | 762 |
|
719 | | - var parser = new ComfyUIParser(); |
720 | | - var pnodes = parser.Parse(fp.WorkflowId, fp.Workflow); |
| 763 | + fp.Nodes = pnodes; |
| 764 | + } |
721 | 765 |
|
722 | | - fp.Nodes = pnodes; |
723 | 766 |
|
724 | 767 | return fp; |
725 | 768 | } |
|
0 commit comments