************************************************************************
* Some ffmpeg-oneliners that work with PyTROLL/Satpy or xrit2pic (avi) *
************************************************************************

ffmpeg can be used in my PyTROLL/Satpy Starter Kit to produce animations.
We only use free video codecs: libtheora making movies.ogv is deprecated
now. To produce movies.webm we use libvpx (for codec vp8) and libvpx-vp9
(for codec vp9). Only codec vp9 has a -lossless switch. All these videos
should directly play in modern browsers and can easily be used in HTML5.
Browsers FireFox and Edge have a fullscreen mode just like Media Player.
Different from Media Player you can watch more than 1 video concurrently.


http://www.ffmpeg.org
https://trac.ffmpeg.org/wiki
https://www.webmproject.org
http://wiki.webmproject.org
https://www.ionos.com/digitalguide/websites/website-creation/webm/
https://www.ionos.com/digitalguide/websites/web-design/webp-format/


ffmpeg has an overwhelming number of command line switches both global,
input, output and/or codec specific. A number of these have a couple of
aliases that makes it even more complicated to value all the different
ffmpeg oneliners we find on the Internet. We only use very few of them:

-b:v                          bitrate video (also depends on image size)
-b:a                          bitrate audio (no audio channels used yet)
-vcodec, -codec:v, -c:v       will be followed by the video codec used
-acodec, -codec:a, -c:a       will be followed by the audio codec used
-r                            we use it to specify the output framerate
-i                            that's where the input comes from (images)
-crf                          *.webm quality, numbers 0..63, 0 highest
-pix_fmt                      pixel format, encoder has a default format
-lossless                     only works with libvpx-vp9 (vcodec vp9)
-y                            output file is overwritten if it exists

Sometimes you find oneliners starting with -f image2 but ffmpeg will
choose this demuxer automatically if we specify a sequence of images
-f image2                     force the use of the image2 demuxer!
-framerate (default 25)       is a parameter of the image2 demuxer
-start_number (default 0)     is a parameter of the image2 demuxer
Documentaition says that -r and -framerate is not exactly the same:

<cite man>
-r[:stream_specifier] fps (input/output,per-stream)
   Set frame rate (Hz value, fraction or abbreviation).

   As an input option, ignore any timestamps stored in the file and
   instead generate timestamps assuming constant frame rate fps.  This
   is not the same as the -framerate option used for some input
   formats like image2 or v4l2 (it used to be the same in older
   versions of FFmpeg).  If in doubt use -framerate instead of the
   input option -r.

   As an output option, duplicate or drop input frames to achieve
   constant output frame rate fps.
</cite man>

BOTTOM LINE: When assembling from images, both  -r  or  -framerate
can be used. When converting from an avi-file only  -r  does work!


We use these oneliners that produce similar video quality output:
-----------------------------------------------------------------

ffmpeg -y -r 6 -i dust-%04d.suf -c:v libvpx -pix_fmt yuv420p -crf 20 -b:v 2M dust-suf2vp8.webm
ffmpeg -y -r 6 -i dust-%04d.suf -c:v libvpx-vp9 -pix_fmt yuv420p -crf 20 -b:v 2M dust-suf2vp9.webm
ffmpeg -y -r 6 -i dust-%04d.suf -c:v libvpx-vp9 -pix_fmt yuv420p -lossless 1 dust-suf2vp9-lossless.webm

ffmpeg -y -r 6 -i dust-%04d.suf -c:v vp8 -pix_fmt yuv420p -crf 20 -b:v 2M dust-suf2vp8.webm
ffmpeg -y -r 6 -i dust-%04d.suf -c:v vp9 -pix_fmt yuv420p -crf 20 -b:v 2M dust-suf2vp9.webm
ffmpeg -y -r 6 -i dust-%04d.suf -c:v vp9 -pix_fmt yuv420p -lossless 1 dust-suf2vp9-lossless.webm

For image type 'suf' I have tested (see below) 'jpg', 'png' and 'webp'.

ffmpeg webm generation defaults to vp9. That's why we have to specify
either libvpx or libvpx-vp9 to get (much faster) vp8 or vp9 encoding.
As aliases we can use the short form vp8 (libvpx) or vp9 (libvpx-vp9).

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ All these oneliners can be used with a video.avi generated by xrit2pic. +
+ Just input like  .. -i msg3_rss.avi .. and output to .. msg3_rss.webm ! +
+ They can also be used to translate animated gif images to webm videos.  +
+ Just input like  .. -i animated.gif .. and output to .. animated.webm ! +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

On my running Basic Service only EUMETCast receiver Terra I assembled 1 day
RSS movies using a reduced resolution of 1024x1024 pixels with my Satpy area
'Isle Of Man'. Including the caption at left input image size is 1201x1024.
Terra is a Fujitsu/Siemens Esprimo P7935 (Intel Core2 Duo 3.16GHz, 8GB RAM).

+--------------------+-------------------+----------------+-------------+
| Conversion of 288  |  Devuan Beowulf   | typical *.suf  | 1 day movie |
| Isle Of Man (RSS)  |     GNU/Linux     |   frame size   | *.webm size |
| frames to *.webm   |  time ffmpeg ...  |      [KB]      |     [MB]    |
+--------------------+-------------------+----------------+-------------+
| jpg --> webm (vp8) |  real   1m3.058s  |                |             |
|                    |  user  1m46.042s  |       629      |     8.6     |
| -crf 20  -b:v 2M   |  sys    0m0.651s  |                |             |
+--------------------+-------------------+----------------+-------------+
| jpg --> webm (vp9) |  real  4m14.262s  |                |             |
|                    |  user   7m0.044s  |       629      |     9.3     |
| -crf 20  -b:v 2M   |  sys    0m2.366s  |                |             |
+--------------------+-------------------+----------------+-------------+
| jpg --> webm (vp9) |  real  2m56.933s  |                |             |
|                    |  user  4m38.456s  |       629      |     165     |
| -lossless 1        |  sys    0m1.259s  |                |             |
+--------------------+-------------------+----------------+-------------+
| png --> webm (vp8) |  real  1m12.113s  |                |             |
|                    |  user  1m49.530s  |       925      |     8.6     |
| -crf 20  -b:v 2M   |  sys    0m0.890s  |                |             |
+--------------------+-------------------+----------------+-------------+
| png --> webm (vp9) |  real  4m11.018s  |                |             |
|                    |  user  6m50.311s  |       925      |     8.8     |
| -crf 20  -b:v 2M   |  sys    0m2.015s  |                |             |
+--------------------+-------------------+----------------+-------------+
| png --> webm (vp9) |  real  2m55.150s  |                |             |
|                    |  user  4m37.926s  |       925      |     140     |
| -lossless 1        |  sys    0m1.479s  |                |             |
+--------------------+-------------------+----------------+-------------+
| webp -> webm (vp8) |  real   1m3.056s  |                |             |
|                    |  user  1m50.793s  |       255      |     9.4     |
| -crf 20  -b:v 2M   |  sys    0m0.599s  |                |             |
+--------------------+-------------------+----------------+-------------+
| webp -> webm (vp9) |  real  4m18.908s  |                |             |
|                    |  user  6m54.500s  |       255      |     9.5     |
| -crf 20  -b:v 2M   |  sys    0m2.343s  |                |             |
+--------------------+-------------------+----------------+-------------+
| webp -> webm (vp9) |  real  2m52.148s  |                |             |
|                    |  user  4m30.660s  |       255      |     137     |
| -lossless 1        |  sys    0m1.567s  |                |             |
+--------------------+-------------------+----------------+-------------+
NOTE: frames *.png uncompressed, *.jpg and *.webp use IM default settings.
All frames are 1201 x 1024 pixels, size adapted to Meteosat-10 resolution.
Muxing an animated *.webp with 288 frames takes 1s and has a size of 60MB.


CONCLUSION: On my GNU/Linux EUMETCast receivers that produce on the fly webm
movies with ffmpeg I will use codec vp8 (libvpx). As input files I will go
on with *.jpg at the moment. For MTG and Metop 2nd Gen *.webp is promissing.




Belp, October 9th 2021                                        Ernst Lobsiger


---------------------------------------------------------------------------

ANNEX 1:

I have used these two versions of ffmpeg and had an issue with version 1):

1)
ffmpeg version 4.1.6-1~deb10u1 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 8 (Debian 8.3.0-6)
...
This version is dynamically linked and distributed with Devuan 3.1 Beowulf.
It has a problem when encoding webm from webp images. The resulting movie
is generated and it works but while encoding there are a lot of messages:
"[webp @ 0x5564eb8f8840] Multiple ff_thread_finish_setup() calls"  ?{:-(.


2)
ffmpeg version 4.4-static https://johnvansickle.com/ffmpeg/  Copyright (c)
2000-2021 the FFmpeg developers built with gcc 8 (Debian 8.3.0-6)
...
This version is statically linked and took webp files as input flawlessly.
I dowloaded it as "ffmpeg-release-amd64-static.tar.xz" from www.ffmpeg.org.

---------------------------------------------------------------------------
---------------------------------------------------------------------------

ANNEX 2:

To lower system load all RSS Satpy area definitions should have a pixel size
that reflects the resolution of the Satellite instrument (Seviri 1-3km at SSP).
WebM videos can be maximized to full screen. In the case of my ASUS monitor
this is FullHD = W x H = 1920 x 1080 pixels. It is therefore of no use at all
to produce videos with a height in excess of 1080 pixels (the browser will
downsize it to H=1080 at best). On the other hand there might be big images
like in my test case Electro-L full_scan 3264 x 2784 or Meteosat-8 seviri_iodc
4352 x 3712 and Meteosat-11 westminster 2401 x 2048 that can be used as frames
(a reduced resolution area has not "yet" been defined for the above images!).

The following line will resize the video to H=1080 with W scaled accordingly:

nice ffmpeg -y -r 6  -i frame-%04d.jpg -filter:v "scale=w=-1:h=1080" -c:v libvpx -pix_fmt yuv420p -crf 20 -b:v 2M resized.webm

A 24 hours Electro-L   video had a reduction of encoding time 1'02" --> 17".
A 24 hours Meteosat-8  video had a reduction of encoding time 3'10" --> 44".
A 24 hours Meteosat-11 video had a reduction of encoding time 1'21" --> 38".

VLC Media Player has an interactive zoom function. But that's another story.
---------------------------------------------------------------------------
