Stop using ffprobe to read GIF frame delay (#57)

* Stop using ffprobe to read GIF delay

* Default image delay to 0
This commit is contained in:
adroitwhiz 2021-01-26 21:30:04 -05:00 committed by GitHub
parent 8483cff28f
commit 7167956a76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 54 additions and 44 deletions

View file

@ -51,7 +51,7 @@ Napi::Value Blur(const Napi::CallbackInfo &info)
string path = obj.Get("path").As<Napi::String>().Utf8Value();
bool sharp = obj.Get("sharp").As<Napi::Boolean>().Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
BlurWorker* blurWorker = new BlurWorker(cb, path, sharp, type, delay);
blurWorker->Queue();

View file

@ -49,7 +49,7 @@ Napi::Value Blurple(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
BlurpleWorker* blurpleWorker = new BlurpleWorker(cb, path, type, delay);
blurpleWorker->Queue();

View file

@ -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<Napi::String>().Utf8Value();
string caption = obj.Get("caption").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
CaptionWorker* captionWorker = new CaptionWorker(cb, caption, path, type, delay);
captionWorker->Queue();

View file

@ -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<Napi::String>().Utf8Value();
string caption = obj.Get("caption").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
CaptionTwoWorker* captionTwoWorker = new CaptionTwoWorker(cb, caption, path, type, delay);
captionTwoWorker->Queue();

View file

@ -48,7 +48,7 @@ Napi::Value Circle(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
CircleWorker* circleWorker = new CircleWorker(cb, path, type, delay);
circleWorker->Queue();

View file

@ -49,7 +49,7 @@ Napi::Value Crop(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
CropWorker* blurWorker = new CropWorker(cb, path, type, delay);
blurWorker->Queue();

View file

@ -49,7 +49,7 @@ Napi::Value Explode(const Napi::CallbackInfo &info)
string path = obj.Get("path").As<Napi::String>().Utf8Value();
int amount = obj.Get("amount").As<Napi::Number>().Int32Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
ExplodeWorker* explodeWorker = new ExplodeWorker(cb, path, amount, type, delay);
explodeWorker->Queue();

View file

@ -55,7 +55,7 @@ Napi::Value Flag(const Napi::CallbackInfo &info)
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string overlay = obj.Get("overlay").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
FlagWorker* flagWorker = new FlagWorker(cb, path, overlay, type, delay);
flagWorker->Queue();

View file

@ -50,7 +50,7 @@ Napi::Value Flip(const Napi::CallbackInfo &info)
string path = obj.Get("path").As<Napi::String>().Utf8Value();
bool flop = obj.Has("flop") ? obj.Get("flop").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
FlipWorker* flipWorker = new FlipWorker(cb, path, flop, type, delay);
flipWorker->Queue();

View file

@ -42,7 +42,7 @@ Napi::Value Freeze(const Napi::CallbackInfo &info)
string path = obj.Get("path").As<Napi::String>().Utf8Value();
bool loop = obj.Has("loop") ? obj.Get("loop").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
FreezeWorker* blurWorker = new FreezeWorker(cb, path, loop, type, delay);
blurWorker->Queue();

View file

@ -53,7 +53,7 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
GamexplainWorker* blurWorker = new GamexplainWorker(cb, path, type, delay);
blurWorker->Queue();

View file

@ -70,7 +70,7 @@ Napi::Value Globe(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
GlobeWorker* blurWorker = new GlobeWorker(cb, path, type, delay);
blurWorker->Queue();

View file

@ -50,7 +50,7 @@ Napi::Value Invert(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
InvertWorker* invertWorker = new InvertWorker(cb, path, type, delay);
invertWorker->Queue();

View file

@ -52,7 +52,7 @@ Napi::Value Leak(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
LeakWorker* blurWorker = new LeakWorker(cb, path, type, delay);
blurWorker->Queue();

View file

@ -50,7 +50,7 @@ Napi::Value Magik(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
MagikWorker* explodeWorker = new MagikWorker(cb, path, type, delay);

View file

@ -80,7 +80,7 @@ Napi::Value Meme(const Napi::CallbackInfo &info)
string top = obj.Get("top").As<Napi::String>().Utf8Value();
string bottom = obj.Get("bottom").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
MemeWorker* blurWorker = new MemeWorker(cb, path, top, bottom, type, delay);
blurWorker->Queue();

View file

@ -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<Napi::Boolean>().Value() : false;
bool first = obj.Has("first") ? obj.Get("first").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
MirrorWorker* mirrorWorker = new MirrorWorker(cb, path, vertical, first, type, delay);
mirrorWorker->Queue();

View file

@ -48,7 +48,7 @@ Napi::Value Swirl(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
SwirlWorker* flopWorker = new SwirlWorker(cb, path, type, delay);
flopWorker->Queue();

View file

@ -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<Napi::String>().Utf8Value();
string bottom = obj.Get("bottom").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
MotivateWorker* blurWorker = new MotivateWorker(cb, path, top, bottom, type, delay);
blurWorker->Queue();

View file

@ -58,7 +58,7 @@ Napi::Value Resize(const Napi::CallbackInfo &info)
bool stretch = obj.Has("stretch") ? obj.Get("stretch").As<Napi::Boolean>().Value() : false;
bool wide = obj.Has("wide") ? obj.Get("wide").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
ResizeWorker* explodeWorker = new ResizeWorker(cb, path, stretch, wide, type, delay);
explodeWorker->Queue();

View file

@ -52,7 +52,7 @@ Napi::Value Reverse(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
bool soos = obj.Has("soos") ? obj.Get("soos").As<Napi::Boolean>().Value() : false;
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
ReverseWorker* explodeWorker = new ReverseWorker(cb, path, soos, delay);
explodeWorker->Queue();

View file

@ -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<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
ScottWorker* blurWorker = new ScottWorker(cb, path, type, delay);
blurWorker->Queue();

View file

@ -14,10 +14,23 @@ class SpeedWorker : public Napi::AsyncWorker {
void Execute() {
list <Image> 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<Napi::String>().Utf8Value();
bool slow = obj.Has("slow") ? obj.Get("slow").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
SpeedWorker* explodeWorker = new SpeedWorker(cb, path, slow, type, delay);
explodeWorker->Queue();

View file

@ -64,7 +64,7 @@ Napi::Value Spin(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
SpinWorker* blurWorker = new SpinWorker(cb, path, type, delay);
blurWorker->Queue();

View file

@ -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<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
TileWorker* flopWorker = new TileWorker(cb, path, type, delay);
flopWorker->Queue();

View file

@ -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<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
TrumpWorker* blurWorker = new TrumpWorker(cb, path, type, delay);
blurWorker->Queue();

View file

@ -55,7 +55,7 @@ Napi::Value Wall(const Napi::CallbackInfo &info)
Napi::Function cb = info[1].As<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
WallWorker* flopWorker = new WallWorker(cb, path, type, delay);
flopWorker->Queue();

View file

@ -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<Napi::Boolean>().Value() : false;
bool mc = obj.Has("mc") ? obj.Get("mc").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
WatermarkWorker* watermarkWorker = new WatermarkWorker(cb, path, water, gravity, type, delay, resize, append, mc);
watermarkWorker->Queue();

View file

@ -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<Napi::Function>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Get("delay").As<Napi::Number>().Int32Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
WdtWorker* blurWorker = new WdtWorker(cb, path, type, delay);
blurWorker->Queue();

View file

@ -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.