Advanced search

Medias (91)

Other articles (19)

  • Keeping control of your media in your hands

    13 April 2011, by

    The vocabulary used on this site and around MediaSPIP in general, aims to avoid reference to Web 2.0 and the companies that profit from media-sharing.
    While using MediaSPIP, you are invited to avoid using words like "Brand", "Cloud" and "Market".
    MediaSPIP is designed to facilitate the sharing of creative media online, while allowing authors to retain complete control of their work.
    MediaSPIP aims to be accessible to as many people as possible and development is based on expanding the (...)

  • Creating farms of unique websites

    13 April 2011, by

    MediaSPIP platforms can be installed as a farm, with a single "core" hosted on a dedicated server and used by multiple websites.
    This allows (among other things): implementation costs to be shared between several different projects / individuals rapid deployment of multiple unique sites creation of groups of like-minded sites, making it possible to browse media in a more controlled and selective environment than the major "open" (...)

  • Submit bugs and patches

    13 April 2011

    Unfortunately a software is never perfect.
    If you think you have found a bug, report it using our ticket system. Please to help us to fix it by providing the following information: the browser you are using, including the exact version as precise an explanation as possible of the problem if possible, the steps taken resulting in the problem a link to the site / page in question
    If you think you have solved the bug, fill in a ticket and attach to it a corrective patch.
    You may also (...)

On other websites (5690)

  • Evolution #4417: Augmenter la longueur du mot de passe demandé pour créer un nouvel auteur

    19 December 2019, by jean marie grall

    vu sur Seenthis à l’instant :

    It’s Time to Kill Your Eight-Character Password
    It’s time to throw away any passwords of eight characters or less and replace them with much longer passwords — let’s say at least 12 characters.
    https://www.tomsguide.com/us/8-character-password-dead,news-29429.html

    Dans mon idée, c’est surtout pour mixer lettres/chiffres et éviter d’avoir chocolat ou basket comme mot de passe :)
    Pour al longueur, c’est à discuter effectivement. Je ne sais pas ce qui se fait chez les autres...

  • ffmpeg conversion: Twitter rejects video with "Not valid video"

    12 February 2020, by DeadlyBacon

    I have an app that uploads via twitter api chunked upload, and it finally works with photos.

    However, I am trying to get it to work with videos.

    Uploading didnt work out-of-the-box even though the video format is mp4. The twitter guidelines for uploads are these

    With that in mind, I have this ffmpeg command so far:

    ffmpeg -i in.mp4  -vf \"scale=1280:720\" -b:v 5000K -minrate 5000K -maxrate 5000K -b:a 128K -r 30 -f mp4 -vcodec libx264 -profile:v high -pix_fmt yuv420p -strict -2  -ac 2 -acodec aac out.mp4

    I still get "Not valid video", and I don’t know why.

    Here’s my mediainfo output for out.mp4:

    General
    Count                                    : 328
    Count of stream of this kind             : 1
    Kind of stream                           : General
    Kind of stream                           : General
    Stream identifier                        : 0
    Count of video streams                   : 1
    Count of audio streams                   : 1
    Video_Format_List                        : AVC
    Video_Format_WithHint_List               : AVC
    Codecs Video                             : AVC
    Video_Language_List                      : English
    Audio_Format_List                        : AAC
    Audio_Format_WithHint_List               : AAC
    Audio codecs                             : AAC LC
    Audio_Language_List                      : English
    Complete name                            : video-5e4405cd4348a5e4405cd434d2.mp4
    File name                                : video-5e4405cd4348a5e4405cd434d2
    File extension                           : mp4
    Format                                   : MPEG-4
    Format                                   : MPEG-4
    Format/Extensions usually used           : mov mp4 m4v m4a m4b m4p 3ga 3gpa 3gpp 3gp 3gpp2 3g2 k3g jpm jpx mqv ismv isma ismt f4a f4b f4v
    Commercial name                          : MPEG-4
    Format profile                           : Base Media
    Internet media type                      : video/mp4
    Codec ID                                 : isom
    Codec ID                                 : isom (isom/iso2/avc1/mp41)
    Codec ID/Url                             : http://www.apple.com/quicktime/download/standalone.html
    CodecID_Compatible                       : isom/iso2/avc1/mp41
    Codec                                    : MPEG-4
    Codec                                    : MPEG-4
    Codec/Extensions usually used            : mov mp4 m4v m4a m4b m4p 3ga 3gpa 3gpp 3gp 3gpp2 3g2 k3g jpm jpx mqv ismv isma ismt f4a f4b f4v
    File size                                : 52664272
    File size                                : 50.2 MiB
    File size                                : 50 MiB
    File size                                : 50 MiB
    File size                                : 50.2 MiB
    File size                                : 50.22 MiB
    Duration                                 : 79744
    Duration                                 : 1 min 19 s
    Duration                                 : 1 min 19 s 744 ms
    Duration                                 : 1 min 19 s
    Duration                                 : 00:01:19.744
    Duration                                 : 00:01:19:20
    Duration                                 : 00:01:19.744 (00:01:19:20)
    Overall bit rate                         : 5283334
    Overall bit rate                         : 5 283 kb/s
    Frame rate                               : 30.000
    Frame rate                               : 30.000 FPS
    Frame count                              : 2390
    Stream size                              : 88780
    Stream size                              : 86.7 KiB (0%)
    Stream size                              : 87 KiB
    Stream size                              : 87 KiB
    Stream size                              : 86.7 KiB
    Stream size                              : 86.70 KiB
    Stream size                              : 86.7 KiB (0%)
    Proportion of this stream                : 0.00169
    HeaderSize                               : 40
    DataSize                                 : 52575500
    FooterSize                               : 88732
    IsStreamable                             : No
    File last modification date              : UTC 2020-02-12 14:05:37
    File last modification date (local)      : 2020-02-12 15:05:37
    Writing application                      : Lavf57.83.100
    Writing application                      : Lavf57.83.100

    Video
    Count                                    : 342
    Count of stream of this kind             : 1
    Kind of stream                           : Video
    Kind of stream                           : Video
    Stream identifier                        : 0
    StreamOrder                              : 0
    ID                                       : 1
    ID                                       : 1
    Format                                   : AVC
    Format/Info                              : Advanced Video Codec
    Format/Url                               : http://developers.videolan.org/x264.html
    Commercial name                          : AVC
    Format profile                           : High@L3.1
    Format settings                          : CABAC / 4 Ref Frames
    Format settings, CABAC                   : Yes
    Format settings, CABAC                   : Yes
    Format settings, ReFrames                : 4
    Format settings, ReFrames                : 4 frames
    Internet media type                      : video/H264
    Codec ID                                 : avc1
    Codec ID/Info                            : Advanced Video Coding
    Codec                                    : AVC
    Codec                                    : AVC
    Codec/Family                             : AVC
    Codec/Info                               : Advanced Video Codec
    Codec/Url                                : http://developers.videolan.org/x264.html
    Codec/CC                                 : avc1
    Codec profile                            : High@L3.1
    Codec settings                           : CABAC / 4 Ref Frames
    Codec settings, CABAC                    : Yes
    Codec_Settings_RefFrames                 : 4
    Duration                                 : 79667
    Duration                                 : 1 min 19 s
    Duration                                 : 1 min 19 s 667 ms
    Duration                                 : 1 min 19 s
    Duration                                 : 00:01:19.667
    Duration                                 : 00:01:19:20
    Duration                                 : 00:01:19.667 (00:01:19:20)
    Bit rate                                 : 5000000
    Bit rate                                 : 5 000 kb/s
    Width                                    : 1280
    Width                                    : 1 280 pixels
    Height                                   : 720
    Height                                   : 720 pixels
    Sampled_Width                            : 1280
    Sampled_Height                           : 720
    Pixel aspect ratio                       : 1.000
    Display aspect ratio                     : 1.778
    Display aspect ratio                     : 16:9
    Rotation                                 : 0.000
    Frame rate mode                          : CFR
    Frame rate mode                          : Constant
    FrameRate_Mode_Original                  : VFR
    Frame rate                               : 30.000
    Frame rate                               : 30.000 FPS
    Frame count                              : 2390
    Resolution                               : 8
    Resolution                               : 8 bits
    Colorimetry                              : 4:2:0
    Color space                              : YUV
    Chroma subsampling                       : 4:2:0
    Chroma subsampling                       : 4:2:0
    Bit depth                                : 8
    Bit depth                                : 8 bits
    Scan type                                : Progressive
    Scan type                                : Progressive
    Interlacement                            : PPF
    Interlacement                            : Progressive
    Bits/(Pixel*Frame)                       : 0.181
    Stream size                              : 51297022
    Stream size                              : 48.9 MiB (97%)
    Stream size                              : 49 MiB
    Stream size                              : 49 MiB
    Stream size                              : 48.9 MiB
    Stream size                              : 48.92 MiB
    Stream size                              : 48.9 MiB (97%)
    Proportion of this stream                : 0.97404
    Writing library                          : x264 - core 152 r2854 e9a5903
    Writing library                          : x264 core 152 r2854 e9a5903
    Encoded_Library_Name                     : x264
    Encoded_Library_Version                  : core 152 r2854 e9a5903
    Encoding settings                        : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=12 / lookahead_threads=2 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=abr / mbtree=1 / bitrate=5000 / ratetol=1.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
    Language                                 : en
    Language                                 : English
    Language                                 : English
    Language                                 : en
    Language                                 : eng
    Language                                 : en

    Audio
    Count                                    : 275
    Count of stream of this kind             : 1
    Kind of stream                           : Audio
    Kind of stream                           : Audio
    Stream identifier                        : 0
    StreamOrder                              : 1
    ID                                       : 2
    ID                                       : 2
    Format                                   : AAC
    Format/Info                              : Advanced Audio Codec
    Commercial name                          : AAC
    Format profile                           : LC
    Format settings, SBR                     : No (Explicit)
    Format settings, SBR                     : No (Explicit)
    Codec ID                                 : mp4a-40-2
    Codec                                    : AAC LC
    Codec                                    : AAC LC
    Codec/Family                             : AAC
    Codec/CC                                 : 40
    Duration                                 : 79744
    Duration                                 : 1 min 19 s
    Duration                                 : 1 min 19 s 744 ms
    Duration                                 : 1 min 19 s
    Duration                                 : 00:01:19.744
    Duration                                 : 00:01:19:25
    Duration                                 : 00:01:19.744 (00:01:19:25)
    Bit rate mode                            : CBR
    Bit rate mode                            : Constant
    Bit rate                                 : 128257
    Bit rate                                 : 128 kb/s
    Channel(s)                               : 2
    Channel(s)                               : 2 channels
    Channel positions                        : Front: L R
    Channel positions                        : 2/0/0
    ChannelLayout                            : L R
    Samples per frame                        : 1024
    Sampling rate                            : 48000
    Sampling rate                            : 48.0 kHz
    Samples count                            : 3827712
    Frame rate                               : 46.875
    Frame rate                               : 46.875 FPS (1024 SPF)
    Frame count                              : 3738
    Compression mode                         : Lossy
    Compression mode                         : Lossy
    Stream size                              : 1278470
    Stream size                              : 1.22 MiB (2%)
    Stream size                              : 1 MiB
    Stream size                              : 1.2 MiB
    Stream size                              : 1.22 MiB
    Stream size                              : 1.219 MiB
    Stream size                              : 1.22 MiB (2%)
    Proportion of this stream                : 0.02428
    Language                                 : en
    Language                                 : English
    Language                                 : English
    Language                                 : en
    Language                                 : eng
    Language                                 : en
    Default                                  : Yes
    Default                                  : Yes
    Alternate group                          : 1
    Alternate group                          : 1

    Edit: Guess Ill add my PHP code too (This is chopped in a sensible manner because the actual file is very large):

    // Set up Codebird
    \Codebird\Codebird::setConsumerKey($consumer_key, $consumer_secret); // static, see README

    $cb = \Codebird\Codebird::getInstance();

    $cb->setToken($token, $token_secret);
    $cb->setTimeout(60 * 1000); // 60 second request timeout

     $video = new Video($path);

       // Convert to the parameter required by twitter.
       $converted = $video->convert();

       $path = realpath('videos/' . $converted);

       $file = fopen($path, 'rb');

       $size = fstat($file)['size'];
       $mime_type = mime_content_type($path);

       $media = $cb->media_upload([
         'command' => 'INIT',
         'media_type' => $mime_type,
       'media_category' => 'tweet_video',
         'total_bytes' => $size,
       ]);

    $success = $media->httpstatus >= 200 && $media->httpstatus < 300; # 2xx

     if (!$success) {
       throw new TwitterException("Failed to INIT upload for $path...");
     }


     // APPEND chunks to upload.
     $mediaId = $media->media_id_string;
     $segmentId = 0;

     while (!feof($file)) {
       echo "chunk #$segmentId....";

       $chunk = fread($file, 512 * 1024); // caps out at 512 MB

       echo "chunk size: ". strlen($chunk);

       $media = $cb->media_upload([
         'command' => 'APPEND',
         'media_id' => $mediaId,
         'segment_index' => $segmentId,
         'media' => $chunk,
       ]);

       $success = $media->httpstatus >= 200 && $media->httpstatus < 300; # 2xx

       if (!$success) {
         throw new TwitterException("Failed to APPEND to upload for $path, chunk $segmentId...");
       }

       $segmentId++;
     }
     // Close file and FINALIZE upload.
     fclose($file);

    echo "FINALIZING id $mediaId...";
     $media = $cb->media_upload([
         'command' => 'FINALIZE',
         'media_id' => $mediaId,
     ]);

     $success = $media->httpstatus >= 200 && $media->httpstatus < 300; # 2xx

     if (!$success) {
       var_dump($media);
       throw new TwitterException("Failed to FINALIZE upload for $path...");
     }

     return $mediaId;

    Video is my ffmpeg class, which I’ll paste below, and $path is a URL leading to a perfectly valid mp4 video.

    Video.php:

    <?php

    class VideoConversionException extends \Exception {}

    class Video {
     public $name;
     public $converted;


     public function __construct($name) {
       self::clear();

       $this->name = $name;
     }


     public function convert() {
       $tmpVideo = 'video-' . uniqid() . uniqid() . '.mp4';
       $videoPath = 'videos/' . $tmpVideo;

       //$ffmpeg = "ffmpeg -i https://tvcanarias.acfipress.com/BC_190907_gc_teror.mp4  -vf "scale=1280:720" -b:v 5000K -b:a 128K -r 30 -f mp4 -vcodec libx264 -acodec aac output_video.mp4";
       $ffmpeg = "ffmpeg -i {$this->name}  -vf \"scale=1280:720\" -b:v 5000K -minrate 5000K -maxrate 5000K -b:a 128K -r 30 -f mp4 -vcodec libx264 -profile:v high -pix_fmt yuv420p -strict -2  -ac 2 -acodec aac $videoPath";
       //$ffmpeg = "ffmpeg -i {$this->name} -pix_fmt yuv420p -vcodec libx264 -vf scale=640:-1 -acodec aac -vb 1024k -minrate 1024k -maxrate 1024k -bufsize 1024k -ar 44100  -ac 2  -strict experimental -r 30 $videoPath";
       $output = [];
       exec($ffmpeg, $output, $status);

       if ($status != 0) {
         //die("Couldnt run ffmpeg. (Error code: #$status)");
         throw new VideoConversionException("Couldn't run ffmpeg. (Error code: #$status)");
       }

       $this->converted = $tmpVideo;
       return $tmpVideo;
     }

     public function shredConverted() {
       // delete video.
       @unlink("videos/{$this->$converted}");
     }

     public static function clear() {
       // We can't really shred videos right away as they might be still uploading.
       // Therefore, every time this library is used, we will just delete videos older
       // than, say, an hour.

       $files = scandir('videos');

       $curtime = time();

       foreach ($files as $file) {
         if ($file == '.gitignore' || $file == '.' || $file == '..') {
           continue;
         }

         $mtime = filemtime("videos/$file");

         $diff = $curtime - $mtime;
         $overAnHour = $diff > (60 * 60);

         if ($overAnHour) {
           @unlink("videos/$file");
         }
       }
     }
    }
  • Trying to cancel execution and delete file using ffmpeg C API

    6 March 2020, by Vuwox

    The code below is a class that handle the conversion of multiples images, through add_frame() method, into a GIF with encode(). It also use a filter to generate and apply the palette. The usage is like this:

    Code call example

    std::unique_ptr gif_obj = nullptr;
    try
    {
       gif_obj = std::make_unique({1000,1000}, 12, "C:/out.gif",
                 "format=pix_fmts=rgb24,split [a][b];[a]palettegen[p];[b][p]paletteuse");

       // Example: a simple vector of images (usually process internally)
       for(auto img : image_vector)
            gif_obj->add_frame(img);

       // Once all frame were added, encode the final GIF with the filter applied.
       gif_obj->encode();
    }
    catch(const std::exception& e)
    {
       // An error occured! We must close FFMPEG properly and delete the created file.
       gif_obj->cancel();
    }

    I have the following issue. If the code for any reason throw an exception, I call ffmpeg->cancel() and it supposes to delete the GIF file on disk. But this is never working, I assume there is a lock on the file or something like that. So here are my question:

    What is the proper way to close/free ffmpeg object in order to remove the file afterward ?


    Full class code below

    Header

    // C++ Standard includes    
    #include <memory>
    #include <string>
    #include <vector>


    // 3rd Party incldues
    #ifdef __cplusplus
    extern "C" {
    #include "libavformat/avformat.h"
    #include "libavfilter/avfilter.h"
    #include "libavutil/opt.h"
    #include "libavfilter/buffersrc.h"
    #include "libavfilter/buffersink.h"
    #include "libswscale/swscale.h"
    #include "libavutil/imgutils.h"
    }
    #endif

    #define FFMPEG_MSG_LEN 2000

    namespace px
    {
       namespace GIF
       {
           class FFMPEG
           {
           public:
               FFMPEG(const px::Point2D<int>&amp; dim,
                      const int framerate,
                      const std::string&amp; filename,
                      const std::string&amp; filter_cmd);

               ~FFMPEG();

               void add_frame(pxImage * const img);
               void encode();
               void cancel();

           private:

               void init_filters();            // Init everything that needed to filter the input frame.
               void init_muxer();              // The muxer that creates the output file.
               void muxing_one_frame(AVFrame* frame);
               void release();

               int _ret = 0;                   // status code from FFMPEG.
               char _err_msg[FFMPEG_MSG_LEN];  // Error message buffer.


               int m_width = 0;                // The width that all futur images must have to be accepted.
               int m_height = 0;               // The height that all futur images must have to be accepted.

               int m_framerate = 0;            // GIF Framerate.
               std::string m_filename = "";    // The GIF filename (on cache?)
               std::string m_filter_desc = ""; // The FFMPEG filter to apply over the frames.

               bool as_frame = false;

               AVFrame* picture_rgb24 = nullptr;           // Temporary frame that will hold the pxImage in an RGB24 format (NOTE: TOP-LEFT origin)

               AVFormatContext* ofmt_ctx = nullptr;        // ouput format context associated to the
               AVCodecContext* o_codec_ctx = nullptr;      // output codec for the GIF

               AVFilterGraph* filter_graph = nullptr;      // filter graph associate with the string we want to execute
               AVFilterContext* buffersrc_ctx = nullptr;   // The buffer that will store all the frames in one place for the palette generation.
               AVFilterContext* buffersink_ctx = nullptr;  // The buffer that will store the result afterward (once the palette are used).

               int64_t m_pts_increment = 0;
           };
       };
    };
    </int></vector></string></memory>

    ctor

    px::GIF::FFMPEG::FFMPEG(const px::Point2D<int>&amp; dim,
                           const int framerate,
                           const std::string&amp; filename,
                           const std::string&amp; filter_cmd) :
       m_width(dim.x()),
       m_height(dim.y()),
       m_framerate(framerate),
       m_filename(filename),
       m_filter_desc(filter_cmd)
    {
    #if !_DEBUG
       av_log_set_level(AV_LOG_QUIET); // Set the FFMPEG log to quiet to avoid too much logs.
    #endif

       // Allocate the temporary buffer that hold the ffmpeg image (pxImage to AVFrame conversion).
       picture_rgb24 = av_frame_alloc();
       picture_rgb24->pts = 0;
       picture_rgb24->data[0] = NULL;
       picture_rgb24->linesize[0] = -1;
       picture_rgb24->format = AV_PIX_FMT_RGB24;
       picture_rgb24->height = m_height;
       picture_rgb24->width = m_width;

       if ((_ret = av_image_alloc(picture_rgb24->data, picture_rgb24->linesize, m_width, m_height, (AVPixelFormat)picture_rgb24->format, 24)) &lt; 0)
           throw px::GIF::Error("Failed to allocate the AVFrame for pxImage conversion with error: " +
                                std::string(av_make_error_string(_err_msg, FFMPEG_MSG_LEN, _ret)),
                                "GIF::FFMPEG CTOR");  

       //printf("allocated picture of size %d, linesize %d %d %d %d\n", _ret, picture_rgb24->linesize[0], picture_rgb24->linesize[1], picture_rgb24->linesize[2], picture_rgb24->linesize[3]);

       init_muxer();   // Prepare the GIF encoder (open it on disk).
       init_filters(); // Prepare the filter that will be applied over the frame.

       // Instead of hardcoder {1,100} which is the GIF tbn, we collect it from its stream.
       // This will avoid future problem if the codec change in ffmpeg.
       if (ofmt_ctx &amp;&amp; ofmt_ctx->nb_streams > 0)
           m_pts_increment = av_rescale_q(1, { 1, m_framerate }, ofmt_ctx->streams[0]->time_base);
       else
           m_pts_increment = av_rescale_q(1, { 1, m_framerate }, { 1, 100 });
    }
    </int>

    FFMPEG Initialization (Filter and muxer)

    void px::GIF::FFMPEG::init_filters()
    {
       const AVFilter* buffersrc = avfilter_get_by_name("buffer");
       const AVFilter* buffersink = avfilter_get_by_name("buffersink");

       AVRational time_base = { 1, m_framerate };
       AVRational aspect_pixel = { 1, 1 };

       AVFilterInOut* inputs = avfilter_inout_alloc();
       AVFilterInOut* outputs = avfilter_inout_alloc();

       filter_graph = avfilter_graph_alloc();

       try
       {
           if (!outputs || !inputs || !filter_graph)
               throw px::GIF::Error("Failed to 'init_filters' could not allocated the graph/filters.", "GIF::FFMPEG init_filters");

           char args[512];
           snprintf(args, sizeof(args),
                    "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
                    m_width, m_height,
                    picture_rgb24->format,
                    time_base.num, time_base.den,
                    aspect_pixel.num, aspect_pixel.den);

           if (avfilter_graph_create_filter(&amp;buffersrc_ctx, buffersrc, "in", args, nullptr, filter_graph) &lt; 0)
               throw px::GIF::Error("Failed to create the 'source buffer' in init_filer method.", "GIF::FFMPEG init_filters");


           if (avfilter_graph_create_filter(&amp;buffersink_ctx, buffersink, "out", nullptr, nullptr, filter_graph) &lt; 0)
               throw px::GIF::Error("Failed to create the 'sink buffer' in init_filer method.", "GIF::FFMPEG init_filters");

           // GIF has possible output of PAL8.
           enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE };

           if (av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN) &lt; 0)
               throw px::GIF::Error("Failed to set the output pixel format.", "GIF::FFMPEG init_filters");

           outputs->name = av_strdup("in");
           outputs->filter_ctx = buffersrc_ctx;
           outputs->pad_idx = 0;
           outputs->next = nullptr;

           inputs->name = av_strdup("out");
           inputs->filter_ctx = buffersink_ctx;
           inputs->pad_idx = 0;
           inputs->next = nullptr;

           // GIF has possible output of PAL8.
           if (avfilter_graph_parse_ptr(filter_graph, m_filter_desc.c_str(), &amp;inputs, &amp;outputs, nullptr) &lt; 0)
               throw px::GIF::Error("Failed to parse the filter graph (bad string!).", "GIF::FFMPEG init_filters");

           if (avfilter_graph_config(filter_graph, nullptr) &lt; 0)
               throw px::GIF::Error("Failed to configure the filter graph (bad string!).", "GIF::FFMPEG init_filters");

           avfilter_inout_free(&amp;inputs);
           avfilter_inout_free(&amp;outputs);
       }
       catch (const std::exception&amp; e)
       {
           // Catch exception to delete element.
           avfilter_inout_free(&amp;inputs);
           avfilter_inout_free(&amp;outputs);
           throw e; // re-throuw
       }
    }


    void px::GIF::FFMPEG::init_muxer()
    {
       AVOutputFormat* o_fmt = av_guess_format("gif", m_filename.c_str(), "video/gif");

       if ((_ret = avformat_alloc_output_context2(&amp;ofmt_ctx, o_fmt, "gif", m_filename.c_str())) &lt; 0)
           throw px::GIF::Error(std::string(av_make_error_string(_err_msg, FFMPEG_MSG_LEN, _ret)) + " allocate output format.", "GIF::FFMPEG init_muxer");

       AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_GIF);
       if (!codec) throw px::GIF::Error("Could to find the 'GIF' codec.", "GIF::FFMPEG init_muxer");

    #if 0
       const AVPixelFormat* p = codec->pix_fmts;
       while (p != NULL &amp;&amp; *p != AV_PIX_FMT_NONE) {
           printf("supported pix fmt: %s\n", av_get_pix_fmt_name(*p));
           ++p;
       }
    #endif

       AVStream* stream = avformat_new_stream(ofmt_ctx, codec);

       AVCodecParameters* codec_paramters = stream->codecpar;
       codec_paramters->codec_tag = 0;
       codec_paramters->codec_id = codec->id;
       codec_paramters->codec_type = AVMEDIA_TYPE_VIDEO;
       codec_paramters->width = m_width;
       codec_paramters->height = m_height;
       codec_paramters->format = AV_PIX_FMT_PAL8;

       o_codec_ctx = avcodec_alloc_context3(codec);
       avcodec_parameters_to_context(o_codec_ctx, codec_paramters);

       o_codec_ctx->time_base = { 1, m_framerate };

       if (ofmt_ctx->oformat->flags &amp; AVFMT_GLOBALHEADER)
           o_codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

       if ((_ret = avcodec_open2(o_codec_ctx, codec, NULL)) &lt; 0)
           throw px::GIF::Error(std::string(av_make_error_string(_err_msg, FFMPEG_MSG_LEN, _ret)) + " open output codec.", "GIF::FFMPEG init_muxer");

       if ((_ret = avio_open(&amp;ofmt_ctx->pb, m_filename.c_str(), AVIO_FLAG_WRITE)) &lt; 0)
           throw px::GIF::Error(std::string(av_make_error_string(_err_msg, FFMPEG_MSG_LEN, _ret)) + " avio open error.", "GIF::FFMPEG init_muxer");

       if ((_ret = avformat_write_header(ofmt_ctx, NULL)) &lt; 0)
           throw px::GIF::Error(std::string(av_make_error_string(_err_msg, FFMPEG_MSG_LEN, _ret)) + " write GIF header", "GIF::FFMPEG init_muxer");

    #if _DEBUG
       // This print the stream/output format.
       av_dump_format(ofmt_ctx, -1, m_filename.c_str(), 1);
    #endif
    }

    Add frame (usually in a loop)

    void px::GIF::FFMPEG::add_frame(pxImage * const img)
    {
       if (img->getImageType() != PXT_BYTE || img->getNChannels() != 4)
           throw px::GIF::Error("Failed to 'add_frame' since image is not PXT_BYTE and 4-channels.", "GIF::FFMPEG add_frame");

       if (img->getWidth() != m_width || img->getHeight() != m_height)
           throw px::GIF::Error("Failed to 'add_frame' since the size is not same to other inputs.", "GIF::FFMPEG add_frame");

       const int pitch = picture_rgb24->linesize[0];
       auto px_ptr = getImageAccessor(img);

       for (int y = 0; y &lt; m_height; y++)
       {
           const int px_row = img->getOrigin() == ORIGIN_BOT_LEFT ? m_height - y - 1 : y;
           for (int x = 0; x &lt; m_width; x++)
           {
               const int idx = y * pitch + 3 * x;
               picture_rgb24->data[0][idx] = px_ptr[px_row][x].ch[PX_RE];
               picture_rgb24->data[0][idx + 1] = px_ptr[px_row][x].ch[PX_GR];
               picture_rgb24->data[0][idx + 2] = px_ptr[px_row][x].ch[PX_BL];
           }
       }

       // palettegen need a whole stream, just add frame to buffer.
       if ((_ret = av_buffersrc_add_frame_flags(buffersrc_ctx, picture_rgb24, AV_BUFFERSRC_FLAG_KEEP_REF)) &lt; 0)
           throw px::GIF::Error("Failed to 'add_frame' to global buffer with error: " +
                                std::string(av_make_error_string(_err_msg, FFMPEG_MSG_LEN, _ret)),
                                "GIF::FFMPEG add_frame");

       // Increment the FPS of the picture for the next add-up to the buffer.      
       picture_rgb24->pts += m_pts_increment;

       as_frame = true;
    }    

    Encoder (final step)

    void px::GIF::FFMPEG::encode()
    {
       if (!as_frame)
           throw px::GIF::Error("Please 'add_frame' before running the Encoding().", "GIF::FFMPEG encode");

       // end of buffer
       if ((_ret = av_buffersrc_add_frame_flags(buffersrc_ctx, nullptr, AV_BUFFERSRC_FLAG_KEEP_REF)) &lt; 0)
           throw px::GIF::Error("error add frame to buffer source: " + std::string(av_make_error_string(_err_msg, FFMPEG_MSG_LEN, _ret)), "GIF::FFMPEG encode");

       do {
           AVFrame* filter_frame = av_frame_alloc();
           _ret = av_buffersink_get_frame(buffersink_ctx, filter_frame);
           if (_ret == AVERROR(EAGAIN) || _ret == AVERROR_EOF) {
               av_frame_unref(filter_frame);
               break;
           }

           // write the filter frame to output file
           muxing_one_frame(filter_frame);

           av_frame_unref(filter_frame);
       } while (_ret >= 0);

       av_write_trailer(ofmt_ctx);
    }

    void px::GIF::FFMPEG::muxing_one_frame(AVFrame* frame)
    {
       int ret = avcodec_send_frame(o_codec_ctx, frame);
       AVPacket *pkt = av_packet_alloc();
       av_init_packet(pkt);

       while (ret >= 0) {
           ret = avcodec_receive_packet(o_codec_ctx, pkt);
           if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
               break;
           }

           av_write_frame(ofmt_ctx, pkt);
       }
       av_packet_unref(pkt);
    }

    DTOR, Release and Cancel

    px::GIF::FFMPEG::~FFMPEG()
    {
       release();
    }


    void px::GIF::FFMPEG::release()
    {
       // Muxer stuffs
       if (ofmt_ctx != nullptr) avformat_free_context(ofmt_ctx);
       if (o_codec_ctx != nullptr) avcodec_close(o_codec_ctx);
       if (o_codec_ctx != nullptr) avcodec_free_context(&amp;o_codec_ctx);

       ofmt_ctx = nullptr;
       o_codec_ctx = nullptr;

       // Filter stuffs
       if (buffersrc_ctx != nullptr) avfilter_free(buffersrc_ctx);
       if (buffersink_ctx != nullptr) avfilter_free(buffersink_ctx);
       if (filter_graph != nullptr) avfilter_graph_free(&amp;filter_graph);

       buffersrc_ctx = nullptr;
       buffersink_ctx = nullptr;
       filter_graph = nullptr;

       // Conversion image.
       if (picture_rgb24 != nullptr) av_frame_free(&amp;picture_rgb24);
       picture_rgb24 = nullptr;
    }

    void px::GIF::FFMPEG::cancel()
    {
       // In-case of failure we must close ffmpeg and exit.
       av_write_trailer(ofmt_ctx);

       // Release and close all elements.
       release();

       // Delete the file on disk.
       if (remove(m_filename.c_str()) != 0)
           PX_LOG0(PX_LOGLEVEL_ERROR, "GIF::FFMPEG - On 'cancel' failed to remove the file.");
    }