From 7167956a7644ef7cd90729576b7fd7d6d8249364 Mon Sep 17 00:00:00 2001 From: adroitwhiz Date: Tue, 26 Jan 2021 21:30:04 -0500 Subject: [PATCH] Stop using ffprobe to read GIF frame delay (#57) * Stop using ffprobe to read GIF delay * Default image delay to 0 --- natives/blur.cc | 2 +- natives/blurple.cc | 2 +- natives/caption.cc | 4 ++-- natives/caption2.cc | 4 ++-- natives/circle.cc | 2 +- natives/crop.cc | 2 +- natives/explode.cc | 2 +- natives/flag.cc | 2 +- natives/flip.cc | 2 +- natives/freeze.cc | 2 +- natives/gamexplain.cc | 2 +- natives/globe.cc | 2 +- natives/invert.cc | 2 +- natives/leak.cc | 2 +- natives/magik.cc | 2 +- natives/meme.cc | 2 +- natives/mirror.cc | 4 ++-- natives/misc.cc | 2 +- natives/motivate.cc | 4 ++-- natives/resize.cc | 2 +- natives/reverse.cc | 2 +- natives/scott.cc | 4 ++-- natives/speed.cc | 21 +++++++++++++++++---- natives/spin.cc | 2 +- natives/tile.cc | 4 ++-- natives/trump.cc | 4 ++-- natives/wall.cc | 2 +- natives/watermark.cc | 4 ++-- natives/wdt.cc | 4 ++-- utils/image-runner.js | 3 --- 30 files changed, 54 insertions(+), 44 deletions(-) diff --git a/natives/blur.cc b/natives/blur.cc index 376fa45..3401377 100644 --- a/natives/blur.cc +++ b/natives/blur.cc @@ -51,7 +51,7 @@ Napi::Value Blur(const Napi::CallbackInfo &info) string path = obj.Get("path").As().Utf8Value(); bool sharp = obj.Get("sharp").As().Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; BlurWorker* blurWorker = new BlurWorker(cb, path, sharp, type, delay); blurWorker->Queue(); diff --git a/natives/blurple.cc b/natives/blurple.cc index 3480225..61e98b8 100644 --- a/natives/blurple.cc +++ b/natives/blurple.cc @@ -49,7 +49,7 @@ Napi::Value Blurple(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; BlurpleWorker* blurpleWorker = new BlurpleWorker(cb, path, type, delay); blurpleWorker->Queue(); diff --git a/natives/caption.cc b/natives/caption.cc index 05bac61..0e7928c 100644 --- a/natives/caption.cc +++ b/natives/caption.cc @@ -40,11 +40,11 @@ class CaptionWorker : public Napi::AsyncWorker { appendImages(&appended, images.begin(), images.end(), true); appended.repage(); appended.magick(type); + appended.animationDelay(delay == 0 ? image.animationDelay() : delay); captioned.push_back(appended); } optimizeImageLayers(&result, captioned.begin(), captioned.end()); - if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); writeImages(result.begin(), result.end(), &blob); } @@ -67,7 +67,7 @@ Napi::Value Caption(const Napi::CallbackInfo &info) string path = obj.Get("path").As().Utf8Value(); string caption = obj.Get("caption").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; CaptionWorker* captionWorker = new CaptionWorker(cb, caption, path, type, delay); captionWorker->Queue(); diff --git a/natives/caption2.cc b/natives/caption2.cc index 5b52b7b..e0135c3 100644 --- a/natives/caption2.cc +++ b/natives/caption2.cc @@ -39,11 +39,11 @@ class CaptionTwoWorker : public Napi::AsyncWorker { appendImages(&appended, images.begin(), images.end(), true); appended.repage(); appended.magick(type); + appended.animationDelay(delay == 0 ? image.animationDelay() : delay); captioned.push_back(appended); } optimizeImageLayers(&result, captioned.begin(), captioned.end()); - if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); writeImages(result.begin(), result.end(), &blob); } @@ -66,7 +66,7 @@ Napi::Value CaptionTwo(const Napi::CallbackInfo &info) string path = obj.Get("path").As().Utf8Value(); string caption = obj.Get("caption").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; CaptionTwoWorker* captionTwoWorker = new CaptionTwoWorker(cb, caption, path, type, delay); captionTwoWorker->Queue(); diff --git a/natives/circle.cc b/natives/circle.cc index 6e13258..fdb2d88 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -48,7 +48,7 @@ Napi::Value Circle(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; CircleWorker* circleWorker = new CircleWorker(cb, path, type, delay); circleWorker->Queue(); diff --git a/natives/crop.cc b/natives/crop.cc index 86f5801..7ee0085 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -49,7 +49,7 @@ Napi::Value Crop(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; CropWorker* blurWorker = new CropWorker(cb, path, type, delay); blurWorker->Queue(); diff --git a/natives/explode.cc b/natives/explode.cc index 2b40e32..6ff19c5 100644 --- a/natives/explode.cc +++ b/natives/explode.cc @@ -49,7 +49,7 @@ Napi::Value Explode(const Napi::CallbackInfo &info) string path = obj.Get("path").As().Utf8Value(); int amount = obj.Get("amount").As().Int32Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; ExplodeWorker* explodeWorker = new ExplodeWorker(cb, path, amount, type, delay); explodeWorker->Queue(); diff --git a/natives/flag.cc b/natives/flag.cc index 025196d..d34337c 100644 --- a/natives/flag.cc +++ b/natives/flag.cc @@ -55,7 +55,7 @@ Napi::Value Flag(const Napi::CallbackInfo &info) string path = obj.Get("path").As().Utf8Value(); string overlay = obj.Get("overlay").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; FlagWorker* flagWorker = new FlagWorker(cb, path, overlay, type, delay); flagWorker->Queue(); diff --git a/natives/flip.cc b/natives/flip.cc index d977eca..71c1376 100644 --- a/natives/flip.cc +++ b/natives/flip.cc @@ -50,7 +50,7 @@ Napi::Value Flip(const Napi::CallbackInfo &info) string path = obj.Get("path").As().Utf8Value(); bool flop = obj.Has("flop") ? obj.Get("flop").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; FlipWorker* flipWorker = new FlipWorker(cb, path, flop, type, delay); flipWorker->Queue(); diff --git a/natives/freeze.cc b/natives/freeze.cc index 6f4a8dd..dc55fc2 100644 --- a/natives/freeze.cc +++ b/natives/freeze.cc @@ -42,7 +42,7 @@ Napi::Value Freeze(const Napi::CallbackInfo &info) string path = obj.Get("path").As().Utf8Value(); bool loop = obj.Has("loop") ? obj.Get("loop").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; FreezeWorker* blurWorker = new FreezeWorker(cb, path, loop, type, delay); blurWorker->Queue(); diff --git a/natives/gamexplain.cc b/natives/gamexplain.cc index fec8b6c..9ada8c3 100644 --- a/natives/gamexplain.cc +++ b/natives/gamexplain.cc @@ -53,7 +53,7 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; GamexplainWorker* blurWorker = new GamexplainWorker(cb, path, type, delay); blurWorker->Queue(); diff --git a/natives/globe.cc b/natives/globe.cc index a56b23e..a2de5c5 100644 --- a/natives/globe.cc +++ b/natives/globe.cc @@ -70,7 +70,7 @@ Napi::Value Globe(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; GlobeWorker* blurWorker = new GlobeWorker(cb, path, type, delay); blurWorker->Queue(); diff --git a/natives/invert.cc b/natives/invert.cc index ff17194..5db83f8 100644 --- a/natives/invert.cc +++ b/natives/invert.cc @@ -50,7 +50,7 @@ Napi::Value Invert(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; InvertWorker* invertWorker = new InvertWorker(cb, path, type, delay); invertWorker->Queue(); diff --git a/natives/leak.cc b/natives/leak.cc index 2851fb6..b566e54 100644 --- a/natives/leak.cc +++ b/natives/leak.cc @@ -52,7 +52,7 @@ Napi::Value Leak(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; LeakWorker* blurWorker = new LeakWorker(cb, path, type, delay); blurWorker->Queue(); diff --git a/natives/magik.cc b/natives/magik.cc index 23717b7..a6d4d61 100644 --- a/natives/magik.cc +++ b/natives/magik.cc @@ -50,7 +50,7 @@ Napi::Value Magik(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; MagikWorker* explodeWorker = new MagikWorker(cb, path, type, delay); diff --git a/natives/meme.cc b/natives/meme.cc index 523c5f4..1693b73 100644 --- a/natives/meme.cc +++ b/natives/meme.cc @@ -80,7 +80,7 @@ Napi::Value Meme(const Napi::CallbackInfo &info) string top = obj.Get("top").As().Utf8Value(); string bottom = obj.Get("bottom").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; MemeWorker* blurWorker = new MemeWorker(cb, path, top, bottom, type, delay); blurWorker->Queue(); diff --git a/natives/mirror.cc b/natives/mirror.cc index 4553848..be6b36e 100644 --- a/natives/mirror.cc +++ b/natives/mirror.cc @@ -49,11 +49,11 @@ class MirrorWorker : public Napi::AsyncWorker { appendImages(&final, mirrored.begin(), mirrored.end(), vertical); final.repage(); final.magick(type); + final.animationDelay(delay == 0 ? image.animationDelay() : delay); mid.push_back(final); } optimizeImageLayers(&result, mid.begin(), mid.end()); - if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); writeImages(result.begin(), result.end(), &blob); } @@ -78,7 +78,7 @@ Napi::Value Mirror(const Napi::CallbackInfo &info) bool vertical = obj.Has("vertical") ? obj.Get("vertical").As().Value() : false; bool first = obj.Has("first") ? obj.Get("first").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; MirrorWorker* mirrorWorker = new MirrorWorker(cb, path, vertical, first, type, delay); mirrorWorker->Queue(); diff --git a/natives/misc.cc b/natives/misc.cc index e93b4be..1df1ff7 100644 --- a/natives/misc.cc +++ b/natives/misc.cc @@ -48,7 +48,7 @@ Napi::Value Swirl(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; SwirlWorker* flopWorker = new SwirlWorker(cb, path, type, delay); flopWorker->Queue(); diff --git a/natives/motivate.cc b/natives/motivate.cc index 6ff42de..a8f30f6 100644 --- a/natives/motivate.cc +++ b/natives/motivate.cc @@ -56,11 +56,11 @@ class MotivateWorker : public Napi::AsyncWorker { appendImages(&final, to_append.begin(), to_append.end(), true); final.repage(); final.magick(type); + final.animationDelay(delay == 0 ? image.animationDelay() : delay); mid.push_back(final); } optimizeImageLayers(&result, mid.begin(), mid.end()); - if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); writeImages(result.begin(), result.end(), &blob); } @@ -84,7 +84,7 @@ Napi::Value Motivate(const Napi::CallbackInfo &info) string top = obj.Get("top").As().Utf8Value(); string bottom = obj.Get("bottom").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; MotivateWorker* blurWorker = new MotivateWorker(cb, path, top, bottom, type, delay); blurWorker->Queue(); diff --git a/natives/resize.cc b/natives/resize.cc index 96d153a..dbd7fef 100644 --- a/natives/resize.cc +++ b/natives/resize.cc @@ -58,7 +58,7 @@ Napi::Value Resize(const Napi::CallbackInfo &info) bool stretch = obj.Has("stretch") ? obj.Get("stretch").As().Value() : false; bool wide = obj.Has("wide") ? obj.Get("wide").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; ResizeWorker* explodeWorker = new ResizeWorker(cb, path, stretch, wide, type, delay); explodeWorker->Queue(); diff --git a/natives/reverse.cc b/natives/reverse.cc index 12e2cb2..9819083 100644 --- a/natives/reverse.cc +++ b/natives/reverse.cc @@ -52,7 +52,7 @@ Napi::Value Reverse(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); bool soos = obj.Has("soos") ? obj.Get("soos").As().Value() : false; - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; ReverseWorker* explodeWorker = new ReverseWorker(cb, path, soos, delay); explodeWorker->Queue(); diff --git a/natives/scott.cc b/natives/scott.cc index c124705..f102881 100644 --- a/natives/scott.cc +++ b/natives/scott.cc @@ -31,11 +31,11 @@ class ScottWorker : public Napi::AsyncWorker { image.extent(Geometry("864x481"), Magick::CenterGravity); watermark_new.composite(image, Geometry("-110+83"), Magick::OverCompositeOp); watermark_new.magick(type); + watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay); mid.push_back(watermark_new); } optimizeImageLayers(&result, mid.begin(), mid.end()); - if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); writeImages(result.begin(), result.end(), &blob); } @@ -57,7 +57,7 @@ Napi::Value Scott(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; ScottWorker* blurWorker = new ScottWorker(cb, path, type, delay); blurWorker->Queue(); diff --git a/natives/speed.cc b/natives/speed.cc index f40c726..1ef4028 100644 --- a/natives/speed.cc +++ b/natives/speed.cc @@ -14,10 +14,23 @@ class SpeedWorker : public Napi::AsyncWorker { void Execute() { list frames; readImages(&frames, in_path); - - int new_delay = slow ? delay * 2 : delay / 2; + + int old_delay = 0; + // if passed a delay, use that. otherwise use the average frame delay. + // GIFs can have a variable framerate, and the frameskipping logic here doesn't handle that. + // TODO: revisit? + if (delay == 0) { + for (Image &image : frames) { + old_delay += image.animationDelay(); + } + old_delay /= frames.size(); + } else { + old_delay = delay; + } + + int new_delay = slow ? old_delay * 2 : old_delay / 2; if (new_delay <= 1) { - new_delay = delay; + new_delay = old_delay; auto it = frames.begin(); while(it != frames.end() && ++it != frames.end()) it = frames.erase(it); } else { @@ -49,7 +62,7 @@ Napi::Value Speed(const Napi::CallbackInfo &info) string path = obj.Get("path").As().Utf8Value(); bool slow = obj.Has("slow") ? obj.Get("slow").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; SpeedWorker* explodeWorker = new SpeedWorker(cb, path, slow, type, delay); explodeWorker->Queue(); diff --git a/natives/spin.cc b/natives/spin.cc index 54fc7f5..29c4e64 100644 --- a/natives/spin.cc +++ b/natives/spin.cc @@ -64,7 +64,7 @@ Napi::Value Spin(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; SpinWorker* blurWorker = new SpinWorker(cb, path, type, delay); blurWorker->Queue(); diff --git a/natives/tile.cc b/natives/tile.cc index b8623ff..710a5ea 100644 --- a/natives/tile.cc +++ b/natives/tile.cc @@ -36,11 +36,11 @@ class TileWorker : public Napi::AsyncWorker { appendImages(&frame, montage.begin(), montage.end(), true); frame.repage(); frame.scale(Geometry("800x800>")); + frame.animationDelay(delay == 0 ? image.animationDelay() : delay); mid.push_back(frame); } optimizeImageLayers(&result, mid.begin(), mid.end()); - if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); writeImages(result.begin(), result.end(), &blob); } @@ -62,7 +62,7 @@ Napi::Value Tile(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; TileWorker* flopWorker = new TileWorker(cb, path, type, delay); flopWorker->Queue(); diff --git a/natives/trump.cc b/natives/trump.cc index 2721a05..b21d0f2 100644 --- a/natives/trump.cc +++ b/natives/trump.cc @@ -31,11 +31,11 @@ class TrumpWorker : public Napi::AsyncWorker { image.extent(Geometry("800x450"), Magick::CenterGravity); watermark_new.composite(image, Geometry("-25+134"), Magick::DstOverCompositeOp); watermark_new.magick(type); + watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay); mid.push_back(watermark_new); } optimizeImageLayers(&result, mid.begin(), mid.end()); - if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); writeImages(result.begin(), result.end(), &blob); } @@ -57,7 +57,7 @@ Napi::Value Trump(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; TrumpWorker* blurWorker = new TrumpWorker(cb, path, type, delay); blurWorker->Queue(); diff --git a/natives/wall.cc b/natives/wall.cc index ec795bf..37449a5 100644 --- a/natives/wall.cc +++ b/natives/wall.cc @@ -55,7 +55,7 @@ Napi::Value Wall(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; WallWorker* flopWorker = new WallWorker(cb, path, type, delay); flopWorker->Queue(); diff --git a/natives/watermark.cc b/natives/watermark.cc index 2db7358..88e7526 100644 --- a/natives/watermark.cc +++ b/natives/watermark.cc @@ -47,11 +47,11 @@ class WatermarkWorker : public Napi::AsyncWorker { final = image; } image.magick(type); + final.animationDelay(delay == 0 ? image.animationDelay() : delay); mid.push_back(final); } optimizeImageLayers(&result, mid.begin(), mid.end()); - if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); writeImages(result.begin(), result.end(), &blob); } @@ -79,7 +79,7 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) bool append = obj.Has("append") ? obj.Get("append").As().Value() : false; bool mc = obj.Has("mc") ? obj.Get("mc").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; WatermarkWorker* watermarkWorker = new WatermarkWorker(cb, path, water, gravity, type, delay, resize, append, mc); watermarkWorker->Queue(); diff --git a/natives/wdt.cc b/natives/wdt.cc index 6ef8d76..b30b6c9 100644 --- a/natives/wdt.cc +++ b/natives/wdt.cc @@ -26,11 +26,11 @@ class WdtWorker : public Napi::AsyncWorker { image.scale(Geometry("374x374>")); watermark_new.composite(image, Magick::CenterGravity, Magick::OverCompositeOp); watermark_new.magick(type); + watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay); mid.push_back(watermark_new); } optimizeImageLayers(&result, mid.begin(), mid.end()); - if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); writeImages(result.begin(), result.end(), &blob); } @@ -52,7 +52,7 @@ Napi::Value Wdt(const Napi::CallbackInfo &info) Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Get("delay").As().Int32Value(); + int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; WdtWorker* blurWorker = new WdtWorker(cb, path, type, delay); blurWorker->Queue(); diff --git a/utils/image-runner.js b/utils/image-runner.js index ff3b53a..f8aa040 100644 --- a/utils/image-runner.js +++ b/utils/image-runner.js @@ -1,6 +1,5 @@ const magick = require("../build/Release/image.node"); const { promisify } = require("util"); -const execPromise = promisify(require("child_process").exec); const { isMainThread, parentPort, workerData } = require("worker_threads"); exports.run = async object => { @@ -11,8 +10,6 @@ exports.run = async object => { buffer: Buffer.alloc(0), fileExtension: "nogif" }); - const delay = (await execPromise(`ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate ${object.path}`)).stdout.replace("\n", ""); - object.delay = (100 / delay.split("/")[0]) * delay.split("/")[1]; } // Convert from a MIME type (e.g. "image/png") to something ImageMagick understands (e.g. "png"). // Don't set `type` directly on the object we are passed as it will be read afterwards.