2

The basic idea of these commands is to create a compare, (A compare has defined a jpeg from past and one from the present, combine such as they will slide on each other and show before after images.)

e.g. https://media.evercam.io/v1/cameras/1lowe-scnoe/compares/lower-jreyh.gif

All the commands are written below, doing these operations

  • Resize before after image.
  • create a Gif using both images.
  • Add a log to Gif.
  • Create an MP4 file of from GIF.
  • Create a thumbnail from mp4 file.

the logo is:

enter image description here

we are making animation and mp4 files using FFmpeg and ImageMagick commands such as

ffmpeg -i before_image.jpg -s 1280x720 before_image_resize.jpg

ffmpeg -i after_image.jpg -s 1280x720 after_image_resize.jpg

The above commands are first to resize both images which are going to be used in animation.

This command is being used for creating a gif.

convert after_image_resize.jpg before_image_resize.jpg -write mpr:stack -delete 0--1 mpr:stack'[1]' \\( mpr:stack'[0]' -set delay 25 -crop 15x0 -reverse \\) mpr:stack'[0]' \\( mpr:stack'[1]' -set delay 27 -crop 15x0 \\) -set delay 2 -loop 0 temp.gif

This command to add a logo to the animation.

convert temp.gif -gravity SouthEast -geometry +15+15 null: evercam-logo.png -layers Composite compa-efxfphu.gif

Then to create an mp4 file as

ffmpeg -f gif -i compa-efxfphu.gif -pix_fmt yuv420p -c:v h264_nvenc -movflags +faststart -filter:v crop='floor(in_w/2)*2:floor(in_h/2)*2' compa-efxfphu.mp4

then to create a thumbnail from this mp4.

ffmpeg -i compa-efxfphu.mp4 -vframes 1 -vf scale=640:-1 -y thumb-compa-efxfphu.jpg

Is there any possibility to reduce any of these steps? This all takes a lot of time, I am merely interested in both convert commands, can we make them into one command?

Or do you see any chance to reduce these all 4 in one? any input will be so thankful.

Junaid Farooq
  • 2,484
  • 5
  • 26
  • 63
  • Please try to simplify your code, prefer `logo.png` in place of `/home/junaid/evercam/evercam-server/_build/dev/lib/evercam_media/priv/static/images/evercam-logo.png` Also, please share the logo. Also, please say, in simple English at the start what the overall outcome of your process is supposed to be and define what each step is supposed to do. Not everyone will have the time to work out all the long filenames and every single step... thank you. If you want people to help you, make it easy for them! :-) – Mark Setchell Oct 23 '18 at 09:10
  • Okay thank you, I will do that now – Junaid Farooq Oct 23 '18 at 09:29

1 Answers1

1

Updated Answer

Here is my best shot at this, it runs in about 50% of the time of the original answer...

convert -depth 8 -gravity southeast -define jpeg:size=1280x720   \
   logo.png   -write MPR:logo +delete                            \
   \( before.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:before \)   \
   \( after.jpg  -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:after  \)   \
   +append -quantize transparent -colors 250 -unique-colors +repage -write MPR:commonmap +delete \
   MPR:after  -map MPR:commonmap +repage -write MPR:after  +delete \
   MPR:before -map MPR:commonmap +repage -write MPR:before         \
              \( MPR:after -set delay 25 -crop 15x0 -reverse \)    \
   MPR:after  \( MPR:before -set delay 27 -crop 15x0         \)    \
   -set delay 2 -loop 0 -write anim.gif                            \
   -delete 1--1 -resize 640x thumb.jpg

Here's my thinking...

  • set the depth to 8 as we are using JPEG, set gravity and initialise libjpeg's "shrink-on-load" feature,
  • load the logo and save to MPR as we will be using it twice,
  • start some "aside processing" inside (...) to load the before image, resize it, paste on the logo and save to MPR for later use,
  • repeat previous step for the after image,
  • append the before and after images together and calculate a combined colormap suitable for both and save it,
  • map both before and after images to new, combined colormap,
  • delete all junk,
  • animate one way,
  • animate the other,
  • make animation endless, and save animation to GIF,
  • delete all except first frame, resize that and save as thumbnail.

Original Answer

Still a work in progress, but you can avoid the first two invocations of ffmpeg and do the resizing within ImageMagick, and also paste the logos on top in one go like this:

convert -gravity southeast logo.png  -write MPR:logo \
  \( before.jpg -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:before \) \
  \( after.jpg  -resize '1280x720!' MPR:logo -geometry +15+15 -composite -write MPR:after  \) \
  -delete 0--1                                           \
  MPR:before  \( MPR:after  -set delay 25 -crop 15x0 -reverse     \) \
  MPR:after   \( MPR:before -set delay 27 -crop 15x0   \) \
  -set delay 2 -loop 0 temp.gif

You can probably also create the thumbnail with that command too, if you change the last line from:

-set delay 2 -loop 0 temp.gif

to

-set delay 2 -loop 0 -write temp.gif \
-delete 1--1 -resize 640x thumbnail.jpg

You can also speed things up and reduce memory usage by using libjpeg's "shrink-on-load" feature. Basically, you tell it before reading the disk how big a file you need and it only reads a subset of the file thereby reducing I/O time and memory pressure:

convert -gravity southeast logo.png  -write MPR:logo    \
  \( -define jpeg:size=1280x720 before.jpg -resize ... \
  \( -define jpeg:size=1280x720 after.jpg  -resize ... 

That reduces the time by around 25-30% on my machine.


Here's my thinking...

  • set the gravity first as it is a setting and remains set till changed,
  • load the logo and save to an MPR because we will need to paste it twice and reading from disk twice is slow,
  • start some "aside processing" inside (...) to load the before image, resize it, paste on the logo and save to MPR for later use,
  • repeat previous step for the after image,
  • delete all junk,
  • animate one way,
  • animate the other,
  • make animation endless, and save animation to GIF,
  • delete all except first frame, resize that and save as thumbnail.

enter image description here


Keywords: animation, wiper effect, animated GIF

Community
  • 1
  • 1
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • what do you mean by still work in progress? sorry if something is still missing then I can add it too? – Junaid Farooq Oct 23 '18 at 10:45
  • It just means I might think of more improvements and add them later. – Mark Setchell Oct 23 '18 at 10:46
  • Okay it will be a huge favor and I will be very thankful for this, If you will add more improvments. – Junaid Farooq Oct 23 '18 at 10:50
  • Okay thank, you my only question right now, is, will it decrease slow speed factor or you can even going to add more for fast speed and keeping everything same as above. – Junaid Farooq Oct 23 '18 at 12:17
  • So here are few results. after.jpg = 503.6KB, after.jpg=84.7KB, logo.png=13.7kb It took **23 second** to create gif(2.3MB) and also thumbail(51.3) – Junaid Farooq Oct 23 '18 at 12:53
  • Can you please reconsider it? it should not take more 5 seconds, what do you think about that? – Junaid Farooq Oct 23 '18 at 13:02
  • It is very hard to say! It takes 1.8s to do the whole lot on my Mac. What are the dimensions in pixels of your JPEG images? What computer are you running on - is it a Raspberry Pi? How much RAM do you have? What CPU do you have? What Operating System are you using? – Mark Setchell Oct 23 '18 at 13:05
  • You can check those images,, there are 2, before and after. And they are 500Kb each. https://drive.google.com/drive/folders/15Rsue6e88js_Y4_sKsuImfbyzcMW93gK?usp=sharing this will give little bit overview of this. – Junaid Farooq Oct 23 '18 at 14:27
  • I added a couple of things to improve the speed - see *"shrink-on-load"*. – Mark Setchell Oct 23 '18 at 15:55
  • Actually, we have Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz GenuineIntel, MemTotal: 65832972 kB, MemFree: 47573452 kB, MemAvailable: 59485832 kB, also compiled ffmpeg with nvidia, the changes as you suggested only lower 10 seconds. what should we do? – Junaid Farooq Oct 24 '18 at 09:31
  • `Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114` and `Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff wmf x xml zli` – Junaid Farooq Oct 24 '18 at 09:34
  • I have spent some more time on this and updated my answer - I don't think I can improve it much further. – Mark Setchell Oct 24 '18 at 11:10
  • Okay how much time it took now on your machine? with full mp4 part? – Junaid Farooq Oct 24 '18 at 11:15
  • The **ImageMagick** part takes around 1.1s and the `ffmpeg` part takes 0.8s. You can use `convert -debug all ...` to see how long the various pieces of processing take. – Mark Setchell Oct 24 '18 at 11:28
  • Another thing you could try is putting the input and output images on a RAMdisk https://askubuntu.com/a/152871 – Mark Setchell Oct 24 '18 at 11:40
  • can you help? https://stackoverflow.com/questions/53004096/imagemagick-wiper-effect-gif-with-less-frames – Junaid Farooq Oct 26 '18 at 10:13
  • Clever approach, Mark, using -crop and relying upon the frame disposal! I had not seen that way of creating a wipe effect without having to use a composite mask to blend the two frames and sliding the mask using -roll. – fmw42 Oct 26 '18 at 22:02
  • @fmw42 this will change your answer now? or you thinking what you answered is the best? – Junaid Farooq Oct 29 '18 at 06:47
  • `@Junaid Farooq`. I do not understand. What will change my answer? My comment was to Mark about his method of doing a wipe effect with ImageMagick. I did not say it was necessarily the best to use Imageamgick. FFMPEG is very fast. I was only commenting that I had not seen that approach before with ImageMagick. Do I misunderstand your comment? – fmw42 Oct 29 '18 at 16:49
  • Actually yes, @fmw42 I misunderstood, but if you think FFMPEG is very fast then I think we should use it not ImageMagick? – Junaid Farooq Oct 30 '18 at 06:18
  • `@Junaid Farooq`. I suggest you try both and see which is faster. I have little experience with ffmpeg, but when I used it, it was very fast. But I am not an expert with it. – fmw42 Oct 30 '18 at 15:52
  • @MarkSetchell can you take a look? https://stackoverflow.com/questions/59176151/add-watermark-at-bottom-right-1cm-squared-ffmpeg – Junaid Farooq Dec 04 '19 at 13:48