From 133052b58c45da62119c56c85138dd9b2abe0486 Mon Sep 17 00:00:00 2001 From: Andy Nowacki Date: Thu, 19 Oct 2017 11:23:23 +0100 Subject: [PATCH] Animations: improve movie compatibility when using ffmpeg Add the -pix_fmt yuv420p option when using ffmpeg to save animations with the mov or mp4 commands. This improves compatibility of the movie files since many players do not support the ffmpeg defaults. To do this, add a third positional argument to `buildanimation` to specify if we want an animated GIF file. The default is `true`. See the following for more information: - https://trac.ffmpeg.org/wiki/Encode/H.264 ('Compatibility') - https://apple.stackexchange.com/questions/166553/why-wont-video-from-ffmpeg-show-in-quicktime-imovie-or-quick-preview --- src/animation.jl | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/animation.jl b/src/animation.jl index 0f539b01..8d3c0341 100644 --- a/src/animation.jl +++ b/src/animation.jl @@ -61,18 +61,23 @@ end file_extension(fn) = Base.Filesystem.splitext(fn)[2][2:end] gif(anim::Animation, fn = giffn(); kw...) = buildanimation(anim.dir, fn; kw...) -mov(anim::Animation, fn = movfn(); kw...) = buildanimation(anim.dir, fn; kw...) -mp4(anim::Animation, fn = mp4fn(); kw...) = buildanimation(anim.dir, fn; kw...) +mov(anim::Animation, fn = movfn(); kw...) = buildanimation(anim.dir, fn, false; kw...) +mp4(anim::Animation, fn = mp4fn(); kw...) = buildanimation(anim.dir, fn, false; kw...) -function buildanimation(animdir::AbstractString, fn::AbstractString; +function buildanimation(animdir::AbstractString, fn::AbstractString, + is_animated_gif::Bool=true; fps::Integer = 20, loop::Integer = 0) fn = abspath(fn) - # generate a colorpalette first so ffmpeg does not have to guess it - run(`ffmpeg -v 0 -i $(animdir)/%06d.png -vf palettegen -y palette.png`) - # then apply the palette to get better results - run(`ffmpeg -v 0 -framerate $fps -loop $loop -i $(animdir)/%06d.png -i palette.png -lavfi paletteuse -y $fn`) + if is_animated_gif + # generate a colorpalette first so ffmpeg does not have to guess it + run(`ffmpeg -v 0 -i $(animdir)/%06d.png -vf palettegen -y palette.png`) + # then apply the palette to get better results + run(`ffmpeg -v 0 -framerate $fps -loop $loop -i $(animdir)/%06d.png -i palette.png -lavfi paletteuse -y $fn`) + else + run(`ffmpeg -v 0 -framerate $fps -loop $loop -i $(animdir)/%06d.png -pix_fmt yuv420p -y $fn`) + end info("Saved animation to ", fn) AnimatedGif(fn)