Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

MP4 to GIF

by Bod (Priest)
on Oct 25, 2022 at 16:53 UTC ( #11147686=perlquestion: print w/replies, xml ) Need Help??

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"; }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11147686]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2023-06-05 17:35 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (25 votes). Check out past polls.