Eliminate slow speed factor in ffmpeg and image-magic commands - ffmpeg

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

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.
Keywords: animation, wiper effect, animated GIF

Related

How to add a background to a transparent PNG file?

I'm trying to add a PNG file as background to an PNG image with transparent regions using ImageMagick like so:
convert input.png -texture bg.png output.png
I'm not so far to find out how to fill my background to 100% width / height, because the above example does not work. I end up with the same file that is transparent and I don't know why.
How can I apply a background that uses full width/height across the whole file? (Preferable using the cover method, meaning to keep aspect ratio of the background and cutting overlaps)
If you want to tile out the background then on Unix, try
convert input.png \( +clone -tile bg.png -draw "color 0,0 reset" \) +swap -compose over -composite output.png
On Windows:
convert input.png ( +clone -tile bg.png -draw "color 0,0 reset" ) +swap -compose over -composite output.png
If you just want to resize the background, then on Unix try
dims=$( convert input.png -format "%wx%h" info: )
convert input.png \( bg.png -resize $dims +repage \) +swap -compose over -composite output.png
I do not know Windows that well to tell you how to do the variable
On ImageMagick 7, in Unix, you can do it easier as:
magick input.png -set option:dims "%wx%h" \( bg.png -resize "%[dims]" \) +swap -compose over -composite output.png
Or in Windows:
magick input.png -set option:dims "%wx%h" ( bg.png -resize "%[dims]" ) +swap -compose over -composite output.png

Reduce size of an image when it is fusioned with other using ImageMagick from terminal in Linux

I am having these two pictures, labeled 1.png and 2.png, respectively:
When I run this code from ImageMagick in my Linux terminal:
composite -blend 75 -gravity West 2.png 1.png prueba.png
I get this result:
However, I would like the logo (2.png) to be half or one-third size reduced and placed in the bottom left corner. How can I do it from the terminal?
You can do that in ImageMagick by using the convert syntax. It is more flexible than the composite syntax.
convert 1.png \( 2.png -resize 50% \) -gravity southwest -define compose:args=75 -compose blend -composite 1_2.png
If you want to move it further left, you can simply chop off some of the left side before compositing using ImageMagick.
convert 1.png \( 2.png -resize 50% -gravity west -chop 20x0 \) -gravity southwest -define compose:args=75 -compose blend -composite 1_2_b.png
Alternately, you can simply trim the excess empty space around the 2.png before blending.
convert 1.png \( 2.png -resize 50% -trim +repage \) -gravity southwest -define compose:args=75 -compose blend -composite 1_2_c.png

Create two sizes of an image using one ImageMagick command

I am using ImageMagick to generate small size JPEG versions of large TIFF images. For each TIFF image I must generate two smaller JPEG versions.
I am currently using two convert commands:
convert.exe 4096-by-3072px-120mb.tif -resize "1024x>" -strip -interlace Plane 1024px-wide-for-web.jpg
convert.exe 4096-by-3072px-120mb.tif -resize "1600x>" -strip -interlace Plane 1600px-wide-for-web.jpg
Converting TIFF to JPEG one-by-one is taking too much time. This approach is inefficient as each image is loaded across the network and processed twice. It will get worse as I plan to create more sizes for each TIFF (think 10,000 TIFFs x 5 sizes).
So, is it possible to generate two or more output files of different sizes using a single ImageMagick command?
Yes, it is possible, using the -write option:
convert 4096-by-3072px-120mb.tif -resize "1600x>" -strip -interlace Plane \
-write 1600px-wide-for-web.jpg -resize "1024x>" 1024px-wide-for-web.jpg
which rescales the input image to 1600 pixels wide, writes it out, then rescales the result to 1024 pixels wide and writes that. It's important to write the images in descending order of size, to avoid loss of quality due to scaling to a small size and then back up to a larger one.
If you prefer to rescale both images from the input image, use the +clone
option:
convert 4096-by-3072px-120mb.tif -strip -interlace Plane \
\( +clone -resize "1024x>" -write 1024px-wide-for-web.jpg +delete \) \
-resize "1600x>" 1600px-wide-for-web.jpg
In this case the order of writing images does not matter.
Here is an alternate command that uses memory program register:
magick.exe 4096-by-3072px-120mb.tif -write mpr:main +delete ^
mpr:main -resize "1024x>" -quality 80 -interlace Plane -strip -write 1024px-wide-for-web.jpg +delete ^
mpr:main -resize "1280x>" -quality 80 -interlace Plane -strip -write 1280px-wide-for-web.jpg +delete ^
mpr:main -resize "1600x>" -quality 80 -interlace Plane -strip -write 1600px-wide-for-web.jpg +delete ^
mpr:main -resize "2048x>" -quality 80 -interlace Plane -strip 2048px-wide-for-web.jpg
When tested:
the files generated with this command were identical to those created by separate convert commands
this command was twice as fast compared to separate commands
Note: ^ is the line continuation character on Windows.

How do I efficiently concatenate ImageMagick and convert commands to produce a no of images

I have a big big size image of dimensions 4000*4000. I want to carry out various operations on the image :
convert a.jpg -crop 6x6# +repage +adjoin tile_6_%d.jpg
convert a.jpg -crop 9x9# +repage +adjoin tile_9_%d.jpg
convert a.jpg -crop 3x3# +repage +adjoin tile_3_%d.jpg
convert a.jpg -resize 120x120 thumbnail.jpg
Thus creating a batch of 36+81+9+1 = 127
I am trying to do something like
convert a.jpg \
\( +clone -resize 66% -crop 6x6# +repage +adjoin -write tile_6x6_%d.jpg +delete \) \
\( +clone -resize 33% -crop 3x3# +repage +adjoin -write tile_3x3_%d.jpg +delete \) \
\( +clone -crop 9x9# +repage +adjoin -write tile_9x9_%d.jpg +delete \) \
-density 150 -quality 50 -resize 120x120 thumbnail.jpg
But this doesn't work the desired way and produces some 250 files. what is wrong here ? What is the best way to concatenate all these commands ?
+delete deletes only the last image in the image sequence. You want -delete 0--1, which means delete images 0 through -1, where the negatively indexed -1 refers to the last image in the sequence.
To elaborate just a bit, each of those -crop commands creates several images, so you want to make sure that you delete all of them from the stack after writing them to disk. When working with a complex convert command line like this, a good way to see what is going on with your image stack(s) at any point is to insert a -write info:.

ImageMagick: Is it possible to remove "Crop marks"?

I'm using ImageMagick to create a a thumb of a PDF.
/opt/local/bin/convert \
"$inputFile[0]" \
-geometry 157x200 \
-colorspace RGB \
"$ThumbFile"
But lastly we got PDF with "crop marks" (for bleeding).
Is there an option that I can use to don't take those?
How can I do this?
Example:
I would like to have only the content of the second image.
I ended by indicating to image magick to grab only the trim box:
/opt/local/bin/convert \
-geometry 157x200 \
-colorspace RGB \
-define pdf:use-trimbox=true \
"$inputFile[0]" \
"$ThumbFile"
The very simple to use -shave N parameter will remove N pixels depth from each edge of the image.
For example:
convert \
original.gif \
-shave 30 \
result.gif
This will shave of the 30 outmost rows/columns of pixels from each edge of the original image.
This also works for PDF input pages (and PNG output thumbnails, for example):
convert \
some.pdf[0] \
-scale 25% \
-shave 30 \
result.png

Resources