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
This commit is contained in:
Andy Nowacki 2017-10-19 11:23:23 +01:00
parent 226bf7e705
commit 133052b58c

View File

@ -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)