How to merge animations/ add frames to existing animation?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Shantanu

How to merge animations/ add frames to existing animation?

Post by Shantanu »

Hi,

I am exploring the possibility of creating GIF animations and showing them as videos in Qt- a thing I did not know could be done as I am new to ImageMagick.

The problem now is that the number of frames I can combine into an ImageList is limited due to the large size of the frames and I can only create animations of around 100 frames in one go. Thus I now want a way to merge/concatenate separate animations of 100 frames each that I will combine to form one large video. I looked on IM forums and google but could not find any methods to do so except for some scripts through the commandline (which I couldnt completely understand), but nothing that uses the API Magick++.

My question is whether this thing can be done through the API in the first place, and if yes-how?
I am currently using the following to create the animation:

writeImages(ImageList.begin(),ImageList.end(),"Try_Animation.gif");

I will be very gratefull for any suggestions or directions towards resources which might help me in adding frames to an existing animation or combining complete animations.

Thanks!

-Shantanu
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to merge animations/ add frames to existing animatio

Post by anthony »

see IM Examples animations. Basics, Optimization and Modification (See link below)

The only way to add animations to a existing GIF animation is to read it in and add the frame into the sequence.
Optimizaions may also need to be handled. This is dealt with in 'Animation Modifications' but you should understand the other two sections first
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Shantanu

Re: How to merge animations/ add frames to existing animatio

Post by Shantanu »

Hi Anthony,

Agian- thanks for the quick response but I guess u forgot to paste the link because its not showing in ur reply :)
But I did some research on my own and found a link for "Magick++ STL support" as below:

http://www.imagemagick.org/Magick++/STL.html

There are a few options there that I think might be helpful. Below are a couple of approaches which i think might help my cause- tell me if its feasible and if I am on the correct track:

Approach 1:

Image appended;
appended.read(xyz.gif) //existing animation
appendImages( &appended, imageList.begin(), imageList.end() ); // "imageList" will now contain the list of images to added to the first animation
appended.write( "appended_animation.gif" );

Approach 2:

Read the animation file and split it into its constituent frames using a command similar to "adjoin" which is used from the commandline (but will be a function in the API in my case) and store the resulting images in a imagelist.Then add new images to this list one by one. The resulting final imagelist will be written to disk in gif format to get the resultant combined animation.
This could be accomplished by the following lines of code which i think will be the crux of this approach:

readImages(Container * sequence, const std::string ImageUrl) // "sequence" will contain the frames split from existing animation and "seqence" will most probably be an imageList again
writeImages(sequence.begin(),sequence.end(),"NewAnimation.gif");

Please let me know if any of these methods are practically implementable and if the logic is correct.
Also am looking forward to your link.

-Shantanu
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to merge animations/ add frames to existing animatio

Post by anthony »

You may also need to 'Coalesce' the animation to remove any optimizations, before adding the new image and re-optimizing.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Shantanu

Re: How to merge animations/ add frames to existing animatio

Post by Shantanu »

Hi,

First of all - which of the two approaches do you think will work? The first approach seems a little weird to me since it is improbable that IM will let me add images to a gif animation directly - ie without splitting it and rebuilding it.

And the problem with the second approach is again the memory limitation- because if I split the frames of the animation in the main memory they will consume the entire memory themselves and there will be no room to add new frames(have already tried it- the program runs out of memory and crashes) which was the problem to begin with and because of which I cant build bigger animations in spite of all the frames being present from the very begining.

Like always- any help is precious!

-Shantanu
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to merge animations/ add frames to existing animatio

Post by anthony »

Because of the size of the image you may need to find some method of GIF animation batch processing. however even just displaying GIF animation generally requires it to be read completely into memory. If you can't display a GIF animation because of its memory requirements, then it probably is not going to be much good.

With the size and the scale you are dealing with perhaps a proper video format may be better. These are designed to be pipelined with only the current frame needing to be in mamory during processing or display.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Shantanu

Re: How to merge animations/ add frames to existing animatio

Post by Shantanu »

Have already considered building MPEG videos(and even built a few) but there is a fundamental problem with it regarding the requirements of my application.

The application I am trying to build is a playback viewer which fits into a product used for digital restoration of damaged video content(generally old movies). The process involves splitting the video into frames and manipulating the individual frame to eliminate the defects. For this to be done the set of frames in the software need to be viewed as a video to see the occurrence of the defects in fluid sequences; but to correct the detected defect a mapping from the video to the particular unique defective frame needs to be done in order to correct the defective frame. With GIF this mapping is possible as the unique frame number can be identified whenever the animation is paused but which I believe cannot be done with MPEG or other such formats as the fundamental information is lost.Thus the whole purpose is defeated as the reverse mapping is impossible which is necessary to correct defects- which is the basic purpose of the product.Please tell me if I am wrong about this assumption.

Also I would like some information about mng animations- I looked on google but did not find anything which was of much help.
First of all- are mng animations of better quality than gif? Because the color pallete for gif is limited to 256 I believe but which is not the case with mng according to what I read. Can anyone tell me the size of the color pallete for mng and if this format is more suitable to display photographic content than GIF? The reason I ask this is because I am experiencing severe quality loss with gif animations.

Shantanu
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to merge animations/ add frames to existing animatio

Post by snibgo »

As Anthony says, a proper video format would make more sense. Identifying defects when the movie is 256 pallete would be difficult.

Some software can display frame numbers. For my purposes, I use ImageMagic to put a frame number or timecode on each frame before assembling the movie. That way, I can use any viewing software.

I've never used mng, so can't advise there. I just use avi or mov containers (file formats), and generally avchd or mpeg4 codecs (the actual compression methods). There is generally a trade-off in file size versus required computer power.
snibgo's IM pages: im.snibgo.com
Shantanu

Re: How to merge animations/ add frames to existing animatio

Post by Shantanu »

Hi,

@Snibgo- Thaks for the reply.I will definitely try doing it your way(by adding frame numbers), but there was a problem I faced when I converted DPX or any other format frames into MPEG videos in ImgeMagick- the quality becomes extremely poor with abnormal colors and the final video is not even close to the original frames. Same is the case with GIF and MNG animations(GIF is because of the limited color pallete-I know but I am not sure about MNG as it is supposed to be derived from PNG format which has a pretty big pallete according to what I have read ).

Is this supposed to happen? If not, are there any settings and options that I need to specify when creating videos and animations in IM? Please let me know if I should be posting this question in a separate thread.

-Shantanu
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to merge animations/ add frames to existing animatio

Post by snibgo »

ImageMagick uses ffmpeg for video operations. I call ffmpeg directly (from within a C++ program) so I can control the framerate, bitrate and quality. When doing this manually, I can see what quality is used: 1 or 2 is good, 20 is horrible. I often use ffmpeg option "-sameq" for assembling movies, which generally gives q=1 or 2.

Doing this, I get movies where a freeze-frame looks exactly like the source frame.
snibgo's IM pages: im.snibgo.com
Shantanu

Re: How to merge animations/ add frames to existing animatio

Post by Shantanu »

Hi,

I mentioned before that I am new to ImageMagick and image processing in general and as such cannot understand the terminology, API Magick++ or the commandline syntax properly yet in the context of what it is that each option in a command exactly does when executed on the shell. Could you please tell me how you accomplish the task through an actual code snippet?

Thanks in advance!

Shantanu
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to merge animations/ add frames to existing animatio

Post by snibgo »

Sample ffmpeg command to extract all the frames from a movie into GOPR00012_000001.JPG etc, also resizing to 960x540:

Code: Select all

ffmpeg -i GOPR0001.MP4 -s 960x540 -sameq GOPR00012_%06d.JPG
Sample command to assemble frames named out_00001.JPG, out_000002.JPG etc into a movie at 29.97 fps:

Code: Select all

ffmpeg -y -r 2997/100 -i out_%06d.JPG -r 2997/100 -sameq out.mov
The command "ffmpeg --help" lists available options.
snibgo's IM pages: im.snibgo.com
Shantanu

Re: How to merge animations/ add frames to existing animatio

Post by Shantanu »

Thanks!

This does work to create videos but I was wondering how can u use it through an API- I think it must have one to be used with C++ projects. Or do you use it from the command line and not through your own code - ie literally as u have posted the commands?
If u do use an API please tell me how you generate a video using ffmpeg through code as I looked for it but could not find anything that made sense to me.

Right now I am trying to find ways to add text and numbers to an image through IM and I think using "Annotate" and "Labels" is the way to go along with setting the correct "Gravity". Do let me know if I am on the correct track with these parameters.

Like always- your help is precious. Thanks!

Shantanu
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to merge animations/ add frames to existing animatio

Post by snibgo »

My C++ (Borland compiler) software calls spawnlp to run ffmpeg, or writes the command to a script file then calls spawnlp to run that. I guess I could call an ffmpeg API directly, but I don't know how, and the extra time required for spawn is trivial. (Likewise SOX for processing sound.)

There are many ways to add text to an image. For frame numbers, I use these IM options (for frame number 456):

Code: Select all

-gravity SouthWest -pointsize 20 -fill black -stroke white -strokewidth 1 -annotate 0 "000456"
I call IM by writing script files then spawnlp. I estimate that calling via API might improve performance by 10%. My software is very volatile, because I keep adding new features (fade, pan/zoom, transition, grading, frame positioning, frame or clip special effects, ...) and the maintenance cost of APIs isn't worth it for me.

I should say that open source GUIs are available for video editing, eg Cinelerra and Blender. These don't have the flexibility I want.
snibgo's IM pages: im.snibgo.com
Post Reply