http://qs321.pair.com?node_id=11147686

Bod has asked for the wisdom of the Perl Monks concerning the following question:

I'm wanting to start with an MP4 and extract a short section to end up with a GIF. Currently we use ezgif and do this manually. But I want a solution that is much more automated either running on our webserver or locally.

Thinking that someone must have wanted to do this before, I expected to find something suitable on CPAN but cannot find anything.

So, the plan is to use Video::FrameGrab to extract a few consecutive frames then use GD to stitch these into a GIF

Is this a good approach or can you suggest a better methodology?

Replies are listed 'Best First'.
Re: MP4 to GIF
by marto (Cardinal) on Oct 25, 2022 at 17:18 UTC

    ffmpeg is my go to for any media -> any other media, I've yet to find any stream (audio/video) I can't manipulate. There will either be something on cpan or you could write a thin wrapper around the command. I'd be surprised if that site you linked to doesn't use ffmpeg/libavcodec on their backend.

      Thanks marto

      A very quick look over the documentation didn't show we anything about GIFs. Have I not come across it yet or do I still have to extract frames and then stitch them together?

        'ffmpeg video to gif' in your search engine of choice will no doubt find what you're looking for.

Re: MP4 to GIF
by marto (Cardinal) on Oct 26, 2022 at 07:58 UTC

    Depending on your use case WebM is also worth considering as an alternative to gif. Again, ffmpeg anything -> anything has you covered.

      Depending on your use case WebM is also worth considering

      Like, for example:

      • Much better compression (it's astonoshing what 21 years of additional research&development can achieve)
      • More than 256 colors (again, 1989 tech vs. 2010 tech)
      • Support for actual media player controls
      • Audio-Support
      • Webpages can load and display a small thumbnail, instead of having to load the whole damn thing (very much appreciated when i'm somewhere with a small bandwidth).

      On a sidenote: If i were to do some sort of "show not-really-funny videos snippets all day long one after the other" platform(*), i'd most likely choose .m3u8 + .ts. Easy enough to dynamically create to m3u8 text files and (god forbid) even add a 5 second advertisement every few minutes into the stream.

      (*) Let's call it "Flip/Flop" or something ;-)

      PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
      Depending on your use case WebM is also worth considering...

      My use case is to extract a short section of a video and display it in an email. I've not researched it but I suspect WebM will be less supported in email clients than GIF. For emails, especially for Outlook for desktop, we have to write very ancient-looking HTML code.

        Before you do this: have the intended recipients of the email explicitly asked for this embedded video? I would hate for you to undertake a lot of work to accomplish this and then for the recipients just to go "Oh, some chopper has sent me an email with an embedded video. That's getting binned right this second." and delete it without reading it. Because that's what I would surely do. :-)


        🦛

Re: MP4 to GIF
by tangent (Parson) on Oct 25, 2022 at 23:15 UTC
    As marto has said, use ffmpeg. Here is a little wrapper script that will extract 10 frames ('-frames:v', $num_frames) from the input video "filename.mp4", starting at 4 seconds ('-ss', $start_time) and save them as PNGs named "filename_1.png" to "filename_10.png"
    my $input_file = '/path/to/filename.mp4'; my $output_files = '/path/to/filename_%d.png'; my $start_time = 4; my $num_frames = 10; my @args = ( '/path/to/ffmpeg', '-loglevel', 'quiet', '-ss', $start_time, '-i', $input_file, '-frames:v', $num_frames, $output_files ); my $response = system( @args ); if ($response == 0) { print "system command success"; } else { print "system command failed"; }