combining / piping / chaining multiline ffmpeg commands together

Greetings I can convert a video file to a high quality animated gif file by executing the following two lines of commands below one right after the other.

ffmpeg -i test.mkv -vf palettegen palette.png
ffmpeg -i test.mkv -i palette.png -lavfi paletteuse animated.gif

Is there away to chain the two lines of commands together so I just have to execute one line?

The reason I'm doing this is that i would like to use winff so I need to chain the two lines together into one ffmpeg executable line operation. I can convert any video file to a animated gif with winff by editing the xml file to look like the lines below.

<GifAnimation>
    <label>Gif Animation Simple</label>
    <params>-f gif</params>
    <extension>gif</extension>
    <category>Gif Animation</category>
  </GifAnimation>

But what alteration do I need to do to the <params> line to chain the two lines below into one ffmpeg executable line operation.

ffmpeg -i test.mkv -vf palettegen palette.png
ffmpeg -i test.mkv -i palette.png -lavfi paletteuse animated.gif

It looks like it needs to:
1) generate the color palette from the video
2) save the color palette to a file
3) read the color palette back in
4) apply it to the gif animation file.

UPDATE Question thanks to evilsoup's help

I tried the answers evilsoup's gave me see command below:

ffmpeg -i test.mkv -filter_complex 'palettegen[PAL],[0:v][PAL]paletteuse' output.gif

and

ffmpeg -i test.mkv -filter_complex 'palettegen,[v]paletteuse' output.gif

It creates an animated gif file but it only has an image on the first frame all the other frames are blank / it almost looks like all the frames but the 1st one has alpha channels. see attached gif image.

An image is only on the 1st frame all other frames have some type alpha channel.  It should be an image of an inclemently rotating object

I do get errors when I run the command see below, which could be the reason any idea how to fix this?

[Parsed_paletteuse_1 @ 0xd526c0] [framesync @ 0xec5108] Buffer queue overflow, dropping.
    Last message repeated 82 times
[Parsed_paletteuse_1 @ 0xd526c0] [framesync @ 0xec5108] Buffer queue overflow, dropping.
    Last message repeated 36 times

See full list below:

rt@sys76:/tmp$ ffmpeg -i test.mkv -filter_complex 'palettegen[PAL],[0:v][PAL]paletteuse' output.gif
ffmpeg version 2.8.6-1ubuntu2 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 5.3.1 (Ubuntu 5.3.1-11ubuntu1) 20160311
  configuration: --prefix=/usr --extra-version=1ubuntu2 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
  WARNING: library configuration mismatch
  avcodec     configuration: --prefix=/usr --extra-version=1ubuntu2 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv --enable-version3 --disable-doc --disable-programs --disable-avdevice --disable-avfilter --disable-avformat --disable-avresample --disable-postproc --disable-swscale --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libvo_aacenc --enable-libvo_amrwbenc
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, matroska,webm, from 'test.mkv':
  Metadata:
    ENCODER         : Lavf56.40.101
  Duration: 00:00:07.40, start: 0.000000, bitrate: 125 kb/s
    Stream #0:0: Video: h264 (High), yuv420p, 560x480, SAR 1:1 DAR 7:6, 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
    Metadata:
      ENCODER         : Lavc56.60.100 libx264
      DURATION        : 00:00:07.400000000
File 'output.gif' already exists. Overwrite ? [y/N] y
Output #0, gif, to 'output.gif':
  Metadata:
    encoder         : Lavf56.40.101
    Stream #0:0: Video: gif, pal8, 560x480 [SAR 1:1 DAR 7:6], q=2-31, 200 kb/s, 25 fps, 100 tbn, 25 tbc (default)
    Metadata:
      encoder         : Lavc56.60.100 gif
Stream mapping:
  Stream #0:0 (h264) -> palettegen
  Stream #0:0 (h264) -> paletteuse:default
  paletteuse -> Stream #0:0 (gif)
Press [q] to stop, [?] for help
[Parsed_paletteuse_1 @ 0xd526c0] [framesync @ 0xec5108] Buffer queue overflow, dropping.
    Last message repeated 82 times
[Parsed_paletteuse_1 @ 0xd526c0] [framesync @ 0xec5108] Buffer queue overflow, dropping.
    Last message repeated 36 times
[Parsed_palettegen_0 @ 0xd52220] 255(+1) colors generated out of 201259 colors; ratio=0.001267
frame=   65 fps= 47 q=-0.0 Lsize=     130kB time=00:00:07.40 bitrate= 143.5kbits/s    
video:128kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.005128%
rt@sys76:/tmp$

Solution 1:

You need to use a filterchain (see the FFmpeg wiki and online documentation).

ffmpeg -i input.mkv -filter_complex 'palettegen[PAL],[0:v][PAL]paletteuse' output.gif

Basically, the palettegen filter takes a single video input (so as long as you don't have more than one video stream amongst your inputs you don't need to specify where the input is) and gives out one video (well, image) output; meanwhile, paletteuse takes two video inputs (well, a video and an image).

Normally in a filterchain the first output of each filter becomes the first input of the next in the chain. However, the paletteuse filter expects the video to be its first input, with the 'palette' as its second input -- this is part of why you can't just use palettegen,paletteuse. The [0:v] uses FFmpeg's stream selection syntax (see here for some examples of its use) to tell paletteuse to take video from the first input file (FFmpeg starts counting from 0 for most things) as its first input.

You could technically write the above like this:

ffmpeg -i input.mkv -filter_complex 'palettegen,[v]paletteuse' output.gif

...and it would work just as well (assuming only one video stream amongst inputs). But I think that labelling the palettegen output and explicitly telling it where to go makes things a bit clearer to follow.

I've never used WinFF, but it looks like you just need to put all the FFmpeg options between the tags? So, this should work:

<GifAnimation>
    <label>Gif Animation Simple</label>
    <params>-filter_complex 'palettegen[PAL],[0:v][PAL]paletteuse'</params>
    <extension>gif</extension>
    <category>Gif Animation</category>
  </GifAnimation>

Solution 2:

I just wanted to add the code that worked in case someone else needs it

This is the final line that worked all the thanks go to evilsoup and Mulvya.

ffmpeg -i test.mkv -filter_complex 'palettegen[PAL];[0:v]fifo[v0];[v0][PAL]paletteuse' test.gif

Also if you want to get winff to work with animated gifs just add these lines to the winff presets.xml file

<GifAnimationSimple>
    <label>Gif Animation Simple</label>
    <params>-f gif</params>
    <extension>gif</extension>
    <category>Gif Animation</category>
  </GifAnimationSimple>

  <GifAnimationbitrate>
    <label>Gif Animation bitrate Quality</label>
    <params>-f -b 2048k gif</params>
    <extension>gif</extension>
    <category>Gif Animation</category>
  </GifAnimationbitrate>

  <GifAnimationHigh>
    <label>Gif Animation high Quality paletteuse</label>
    <params>-filter_complex 'palettegen[PAL];[0:v]fifo[v0];[v0][PAL]paletteuse' </params>
    <extension>gif</extension>
    <category>Gif Animation</category>
  </GifAnimationHigh>