@@ -9,11 +9,11 @@ using FFMPEG
99export movie_from_images
1010
1111"""
12- movie_from_images(; dir=pwd(), file=nothing, outfile=nothing, framerate=10, copy_to_current_dir=true, type=:mp4_default, collect=true)
12+ movie_from_images(; dir=pwd(), file=nothing, outfile=nothing, framerate=10, copy_to_current_dir=true, type=:mp4_default, gif=false, collect=true)
1313
1414The typical way to create animations with `Paraview` is to use the `Save Animation` option to save a series of `*.png` images.
1515
16- This function combines these images to an `*.mp4` movie.
16+ This function combines these images to an `*.mp4` movie (default) or a `*.gif` animation .
1717
1818Optional options
1919===
@@ -25,9 +25,11 @@ Optional options
2525- `type`: type of movie that is created; possible options are:
2626 - `:mp4_default`: Default option that saves a well-compressed `mp4` movie that works well for us on ipad and embedded in a powerpoint presentation.
2727 - `:mov_hires`: Higher-resolution quicktime movie (larger filesize & not compatible with windows)
28+ - `:gif`: Animated GIF (same as passing `gif=true`).
29+ - `gif`: if `true`, creates a `*.gif` animation in addition to the `mp4` movie. If `type=:gif`, only a gif is created.
2830- `collect`: suppresses output of `FFMPEG` if `true` (default).
2931"""
30- function movie_from_images (; dir = pwd (), file = nothing , outfile = nothing , framerate = 10 , copy_to_current_dir = true , type = :mp4_default )
32+ function movie_from_images (; dir = pwd (), file = nothing , outfile = nothing , framerate = 10 , copy_to_current_dir = true , type = :mp4_default , gif = false )
3133 curdir = pwd ()
3234 cd (dir)
3335 files = split .(readdir (), " ." )
@@ -55,22 +57,48 @@ function movie_from_images(; dir = pwd(), file = nothing, outfile = nothing, fra
5557 elseif type == :mov_hires
5658 outfile_ext = outfile * " .mov"
5759 cmd = ` -y -f image2 -framerate $framerate -i $file .%0$(le) d.$fileext -c:v prores_ks -profile:v 1 $outfile_ext `
60+ elseif type == :gif
61+ gif = true # ensure gif branch is taken below
62+ outfile_ext = nothing
63+ cmd = nothing
5864 else
5965 error (" unknown movie type $type " )
6066 end
6167
62- # run
63- FFMPEG. exe (cmd, collect = true )
68+ result = nothing
6469
65- result = joinpath (pwd (), outfile_ext)
66- if copy_to_current_dir
67- # copy result
68- result = joinpath (curdir, outfile_ext)
69- cp (outfile_ext, result, force = true )
70+ if type != :gif
71+ # run ffmpeg for mp4/mov
72+ FFMPEG. exe (cmd, collect = true )
73+
74+ result = joinpath (pwd (), outfile_ext)
75+ if copy_to_current_dir
76+ result = joinpath (curdir, outfile_ext)
77+ cp (outfile_ext, result, force = true )
78+ end
79+ println (" created movie: $result " )
80+ end
81+
82+ if gif
83+ # create palette for better gif quality, then encode gif
84+ outfile_gif = outfile * " .gif"
85+ palette = outfile * " _palette.png"
86+ cmd_palette = ` -y -framerate $framerate -f image2 -i $file .%0$(le) d.$fileext -vf palettegen $palette `
87+ cmd_gif = ` -y -framerate $framerate -f image2 -i $file .%0$(le) d.$fileext -i $palette -lavfi paletteuse $outfile_gif `
88+ FFMPEG. exe (cmd_palette, collect = true )
89+ FFMPEG. exe (cmd_gif, collect = true )
90+ rm (palette, force = true )
91+
92+ result_gif = joinpath (pwd (), outfile_gif)
93+ if copy_to_current_dir
94+ result_gif = joinpath (curdir, outfile_gif)
95+ cp (outfile_gif, result_gif, force = true )
96+ end
97+ println (" created gif: $result_gif " )
98+ isnothing (result) && (result = result_gif)
7099 end
71- println (" created movie: $result " )
72100
73101 cd (curdir) # return to original directory
74102
75- return outfile_ext
103+ return isnothing (outfile_ext) ? outfile * " .gif " : outfile_ext
76104end
0 commit comments