How to create an animated GIF from MP4 video via command line?











up vote
83
down vote

favorite
46












I want make an animated .gif from an .mp4 video. I would prefer to do it from the command line, so please only list command line tools.










share|improve this question




























    up vote
    83
    down vote

    favorite
    46












    I want make an animated .gif from an .mp4 video. I would prefer to do it from the command line, so please only list command line tools.










    share|improve this question


























      up vote
      83
      down vote

      favorite
      46









      up vote
      83
      down vote

      favorite
      46






      46





      I want make an animated .gif from an .mp4 video. I would prefer to do it from the command line, so please only list command line tools.










      share|improve this question















      I want make an animated .gif from an .mp4 video. I would prefer to do it from the command line, so please only list command line tools.







      command-line mp4 gif






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 22 at 17:23









      Ciro Santilli 新疆改造中心 六四事件 法轮功

      8,95444146




      8,95444146










      asked Jul 15 '15 at 8:01









      Maythux

      49.9k32163214




      49.9k32163214






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          100
          down vote













          Two steps:





          • Extract Images from Video



            Create a directory called frames in the same directory with your .mp4 file.
            Use command:



            ffmpeg -i video.mp4  -r 5 'frames/frame-%03d.jpg'





            -r 5 stands for FPS value
            for better quality choose bigger number
            adjust the value with the -delay in 2nd step
            to keep the same animation speed

            %03d gives sequential filename number in decimal form


            source




          • Convert Images to gif



            cd frames
            convert -delay 20 -loop 0 *.jpg myimage.gif





            -delay 20 means the time between each frame is 0.2 seconds
            which match 5 fps above.
            When choosing this value
            1 = 100 fps
            2 = 50 fps
            4 = 25 fps
            5 = 20 fps
            10 = 10 fps
            20 = 5 fps
            25 = 4 fps
            50 = 2 fps
            100 = 1 fps
            in general 100/delay = fps

            -loop 0 means repeat forever


            Docs: convert gif options




          You will end up with an rather big file, have a look at the image magick guide to optimize gif on options you can add to the second step command to obtain a smaller file.






          share|improve this answer



















          • 1




            great answer- some additional info: ffmpeg can be installed using directions here
            – chepyle
            Mar 29 '16 at 2:02








          • 2




            Keep in mind that the frames folder must exist for the first command to work.
            – totymedli
            Jun 7 '17 at 15:09






          • 1




            For those that want a quick way to optimize that generally works and are too lazy to read the link, just add -layers Optimize to the last convert command, before *.jpg. Check the output though, it might be affected. For me it reduced the gif size from 5 MB to 700 KB without any perceivable loss in quality :)
            – cpury
            Jul 10 '17 at 8:54












          • Thanks that was an interesting process, a 5.6MB mp4 ended up a 236MB gif, not sure I'll be putting that one up on my website ;) Possibly a gif needs to be limited to seconds rather than a minute.
            – cardamom
            Sep 12 '17 at 9:37






          • 1




            Instead of using the JPG file format, very lossy with the default settings, I'd recommend using PNG for the initial file export. ffmpeg -i video.mp4 -r 5 'frames/frame-%03d.png'.
            – Pierre F
            Jun 7 at 11:15




















          up vote
          75
          down vote













          ffmpeg 3.4.4 can do it directly on Ubuntu 18.04



          You likely want to use something like:



          sudo apt-get install ffmpeg
          wget -O opengl-rotating-triangle.mp4 https://github.com/cirosantilli/media/blob/master/opengl-rotating-triangle.mp4?raw=true
          ulimit -Sv 1000000
          ffmpeg
          -i opengl-rotating-triangle.mp4
          -r 15
          -vf scale=512:-1
          -ss 00:00:03 -to 00:00:06
          opengl-rotating-triangle.gif




          Test data generation procedure described on this post.



          A more direct:



          sudo apt-get install ffmpeg
          ffmpeg -i in.mp4 out.gif


          also works, but the output GIF would be way larger than the input video, because video formats compress intelligently across frames.



          Argument breakdown:





          • ulimit -Sv 1000000: set a maximum 1Gb memory size for the program.



            Mostly me ensuring that the command is not using unlimited memory like certain previous attempts.



            500Mb makes ffmpeg fail to load shared libraries... time to upgrade your RAM?




          • -ss 00:00:03 -to 00:00:06: start and end time to cut the video from.



            No, GIFs are not the best way to pirate distribute videos online.



            See also: https://stackoverflow.com/questions/18444194/cutting-the-videos-based-on-start-and-end-time-using-ffmpeg




          • -vf scale=512:-1: make the output 512 pixels in height, and adjust width to maintain the aspect ratio.



            This is common use case for images for the web, which tend to have much smaller resolution than video.



            If you remove this option, the output GIF has the same height as the input video.



            The original video height can be found for example with ffprobe: https://superuser.com/questions/595177/how-to-retrieve-video-file-information-from-command-line-under-linux/1035178#1035178 and is 1024 x 1024 in our case.




          • -r 15: sampling FPS.



            For example, the original video was 30 FPS, so -r 15 means that ffmpeg will pick one frame in every 2 (= 30 / 15).



            The perceived output FPS is adjusted to match the input however, so you won't notice a speedup, only greater granularity.



            The input FPS can be found with ffprobe, and the total number of input frames can be found with mediainfo as explained at: https://superuser.com/questions/84631/how-do-i-get-the-number-of-frames-in-a-video-on-the-linux-command-line/1044894#1044894



            I recommend this option because video formats usually have a higher framerate due to the larger resolution. With smaller GIFs, the lower framerate is less noticeable, and so we can skip some frames and make smaller GIFs.




          Before pre 18.04: ffmpeg + convert one-liner without intermediate files



          ffmpeg could not handle GIF previously. The best I had was something along:



          sudo apt-get install ffmpeg imagemagick
          ffmpeg -i input.mp4 -r 10 -f image2pipe -vcodec ppm - |
          convert -delay 5 -loop 0 - output.gif


          Explanation of some of the arguments:




          • -loop 0: add the Netscape Gif extension Loop count field to the output. 0 means infinite loop as described at: http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension eog, firefox and chromium all loop infinitely by default even without it, so I'm not sure how necessary it is anymore.


          • -delay 5: time waited before showing the next frame, in hundreths of second, as described at: https://en.wikipedia.org/wiki/GIF#Animated_GIF byte 324. So 100 means 1 FPS, 5 means 1 / 0.5 == 20FPS.



          Even if you reduce the height and framerate, the output GIF may still be larger than the video, since "real" non-GIF video formats compress across frames, while GIF only compresses individual frames.



          A direct:



          convert input.mp4 rpi2-bare-metal-blink.gif


          worked, but almost killed my computer because of memory overflow, and produced an ouptput 100x larger for my 2s 1Mb input file. Maybe one day ImageMagick will catch up.



          See also: https://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality



          Tested on Ubuntu 17.10.






          share|improve this answer



















          • 1




            Downvoters please explain ;-)
            – Ciro Santilli 新疆改造中心 六四事件 法轮功
            Oct 21 '16 at 17:49






          • 1




            Your delay does not match your -r value (the resulting gif is 2x speed). Also you added a scale argument for no apparent reason (it makes the gif really small).
            – asmeurer
            Oct 21 '16 at 17:51






          • 2




            I agree. Small size gifs are better for web. Thanks for scale=320:-1
            – zombic
            Oct 26 '16 at 8:26






          • 2




            For those who are wondering: removing -r 10 will bring the GIF back to normal speed.
            – Mitch
            Feb 13 '17 at 8:05






          • 2




            I got a gif 20% bigger than the mp4 :O
            – Adam Goldman
            May 11 '17 at 2:39


















          up vote
          8
          down vote













          gifify is an all-in-one node-based utility that simplifies the conversion. It depends on nodejs, npm, ffmpeg, and imagemagick which are all available in the repos.



          Once you have npm installed you can install gifify globally with:



              npm install -g gifify


          A video can be converted to a .GIF with:



              gifify video.mp4 -o video.gif


          You can also optionally set a start and end position in the video and add a text caption:



              gifify clip.mp4 -o clip.gif --from 01:48:23.200 --to 01:48:25.300 --text 'we are the knights who say nip!'


          ❗️ It can take several minutes for the conversion to complete even with smaller videos.




          NOTE: ffmpeg and imagemagick might need to be compiled with some specific libraries (i.e. libass and fontconfig accordingly).







          share|improve this answer





















          • Doesn't handle approx > 40 sec clips: github.com/vvo/gifify/issues/99
            – oligofren
            Jan 30 at 16:03











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f648603%2fhow-to-create-an-animated-gif-from-mp4-video-via-command-line%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          100
          down vote













          Two steps:





          • Extract Images from Video



            Create a directory called frames in the same directory with your .mp4 file.
            Use command:



            ffmpeg -i video.mp4  -r 5 'frames/frame-%03d.jpg'





            -r 5 stands for FPS value
            for better quality choose bigger number
            adjust the value with the -delay in 2nd step
            to keep the same animation speed

            %03d gives sequential filename number in decimal form


            source




          • Convert Images to gif



            cd frames
            convert -delay 20 -loop 0 *.jpg myimage.gif





            -delay 20 means the time between each frame is 0.2 seconds
            which match 5 fps above.
            When choosing this value
            1 = 100 fps
            2 = 50 fps
            4 = 25 fps
            5 = 20 fps
            10 = 10 fps
            20 = 5 fps
            25 = 4 fps
            50 = 2 fps
            100 = 1 fps
            in general 100/delay = fps

            -loop 0 means repeat forever


            Docs: convert gif options




          You will end up with an rather big file, have a look at the image magick guide to optimize gif on options you can add to the second step command to obtain a smaller file.






          share|improve this answer



















          • 1




            great answer- some additional info: ffmpeg can be installed using directions here
            – chepyle
            Mar 29 '16 at 2:02








          • 2




            Keep in mind that the frames folder must exist for the first command to work.
            – totymedli
            Jun 7 '17 at 15:09






          • 1




            For those that want a quick way to optimize that generally works and are too lazy to read the link, just add -layers Optimize to the last convert command, before *.jpg. Check the output though, it might be affected. For me it reduced the gif size from 5 MB to 700 KB without any perceivable loss in quality :)
            – cpury
            Jul 10 '17 at 8:54












          • Thanks that was an interesting process, a 5.6MB mp4 ended up a 236MB gif, not sure I'll be putting that one up on my website ;) Possibly a gif needs to be limited to seconds rather than a minute.
            – cardamom
            Sep 12 '17 at 9:37






          • 1




            Instead of using the JPG file format, very lossy with the default settings, I'd recommend using PNG for the initial file export. ffmpeg -i video.mp4 -r 5 'frames/frame-%03d.png'.
            – Pierre F
            Jun 7 at 11:15

















          up vote
          100
          down vote













          Two steps:





          • Extract Images from Video



            Create a directory called frames in the same directory with your .mp4 file.
            Use command:



            ffmpeg -i video.mp4  -r 5 'frames/frame-%03d.jpg'





            -r 5 stands for FPS value
            for better quality choose bigger number
            adjust the value with the -delay in 2nd step
            to keep the same animation speed

            %03d gives sequential filename number in decimal form


            source




          • Convert Images to gif



            cd frames
            convert -delay 20 -loop 0 *.jpg myimage.gif





            -delay 20 means the time between each frame is 0.2 seconds
            which match 5 fps above.
            When choosing this value
            1 = 100 fps
            2 = 50 fps
            4 = 25 fps
            5 = 20 fps
            10 = 10 fps
            20 = 5 fps
            25 = 4 fps
            50 = 2 fps
            100 = 1 fps
            in general 100/delay = fps

            -loop 0 means repeat forever


            Docs: convert gif options




          You will end up with an rather big file, have a look at the image magick guide to optimize gif on options you can add to the second step command to obtain a smaller file.






          share|improve this answer



















          • 1




            great answer- some additional info: ffmpeg can be installed using directions here
            – chepyle
            Mar 29 '16 at 2:02








          • 2




            Keep in mind that the frames folder must exist for the first command to work.
            – totymedli
            Jun 7 '17 at 15:09






          • 1




            For those that want a quick way to optimize that generally works and are too lazy to read the link, just add -layers Optimize to the last convert command, before *.jpg. Check the output though, it might be affected. For me it reduced the gif size from 5 MB to 700 KB without any perceivable loss in quality :)
            – cpury
            Jul 10 '17 at 8:54












          • Thanks that was an interesting process, a 5.6MB mp4 ended up a 236MB gif, not sure I'll be putting that one up on my website ;) Possibly a gif needs to be limited to seconds rather than a minute.
            – cardamom
            Sep 12 '17 at 9:37






          • 1




            Instead of using the JPG file format, very lossy with the default settings, I'd recommend using PNG for the initial file export. ffmpeg -i video.mp4 -r 5 'frames/frame-%03d.png'.
            – Pierre F
            Jun 7 at 11:15















          up vote
          100
          down vote










          up vote
          100
          down vote









          Two steps:





          • Extract Images from Video



            Create a directory called frames in the same directory with your .mp4 file.
            Use command:



            ffmpeg -i video.mp4  -r 5 'frames/frame-%03d.jpg'





            -r 5 stands for FPS value
            for better quality choose bigger number
            adjust the value with the -delay in 2nd step
            to keep the same animation speed

            %03d gives sequential filename number in decimal form


            source




          • Convert Images to gif



            cd frames
            convert -delay 20 -loop 0 *.jpg myimage.gif





            -delay 20 means the time between each frame is 0.2 seconds
            which match 5 fps above.
            When choosing this value
            1 = 100 fps
            2 = 50 fps
            4 = 25 fps
            5 = 20 fps
            10 = 10 fps
            20 = 5 fps
            25 = 4 fps
            50 = 2 fps
            100 = 1 fps
            in general 100/delay = fps

            -loop 0 means repeat forever


            Docs: convert gif options




          You will end up with an rather big file, have a look at the image magick guide to optimize gif on options you can add to the second step command to obtain a smaller file.






          share|improve this answer














          Two steps:





          • Extract Images from Video



            Create a directory called frames in the same directory with your .mp4 file.
            Use command:



            ffmpeg -i video.mp4  -r 5 'frames/frame-%03d.jpg'





            -r 5 stands for FPS value
            for better quality choose bigger number
            adjust the value with the -delay in 2nd step
            to keep the same animation speed

            %03d gives sequential filename number in decimal form


            source




          • Convert Images to gif



            cd frames
            convert -delay 20 -loop 0 *.jpg myimage.gif





            -delay 20 means the time between each frame is 0.2 seconds
            which match 5 fps above.
            When choosing this value
            1 = 100 fps
            2 = 50 fps
            4 = 25 fps
            5 = 20 fps
            10 = 10 fps
            20 = 5 fps
            25 = 4 fps
            50 = 2 fps
            100 = 1 fps
            in general 100/delay = fps

            -loop 0 means repeat forever


            Docs: convert gif options




          You will end up with an rather big file, have a look at the image magick guide to optimize gif on options you can add to the second step command to obtain a smaller file.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 13 '17 at 12:25









          Community

          1




          1










          answered Jul 15 '15 at 8:01









          Maythux

          49.9k32163214




          49.9k32163214








          • 1




            great answer- some additional info: ffmpeg can be installed using directions here
            – chepyle
            Mar 29 '16 at 2:02








          • 2




            Keep in mind that the frames folder must exist for the first command to work.
            – totymedli
            Jun 7 '17 at 15:09






          • 1




            For those that want a quick way to optimize that generally works and are too lazy to read the link, just add -layers Optimize to the last convert command, before *.jpg. Check the output though, it might be affected. For me it reduced the gif size from 5 MB to 700 KB without any perceivable loss in quality :)
            – cpury
            Jul 10 '17 at 8:54












          • Thanks that was an interesting process, a 5.6MB mp4 ended up a 236MB gif, not sure I'll be putting that one up on my website ;) Possibly a gif needs to be limited to seconds rather than a minute.
            – cardamom
            Sep 12 '17 at 9:37






          • 1




            Instead of using the JPG file format, very lossy with the default settings, I'd recommend using PNG for the initial file export. ffmpeg -i video.mp4 -r 5 'frames/frame-%03d.png'.
            – Pierre F
            Jun 7 at 11:15
















          • 1




            great answer- some additional info: ffmpeg can be installed using directions here
            – chepyle
            Mar 29 '16 at 2:02








          • 2




            Keep in mind that the frames folder must exist for the first command to work.
            – totymedli
            Jun 7 '17 at 15:09






          • 1




            For those that want a quick way to optimize that generally works and are too lazy to read the link, just add -layers Optimize to the last convert command, before *.jpg. Check the output though, it might be affected. For me it reduced the gif size from 5 MB to 700 KB without any perceivable loss in quality :)
            – cpury
            Jul 10 '17 at 8:54












          • Thanks that was an interesting process, a 5.6MB mp4 ended up a 236MB gif, not sure I'll be putting that one up on my website ;) Possibly a gif needs to be limited to seconds rather than a minute.
            – cardamom
            Sep 12 '17 at 9:37






          • 1




            Instead of using the JPG file format, very lossy with the default settings, I'd recommend using PNG for the initial file export. ffmpeg -i video.mp4 -r 5 'frames/frame-%03d.png'.
            – Pierre F
            Jun 7 at 11:15










          1




          1




          great answer- some additional info: ffmpeg can be installed using directions here
          – chepyle
          Mar 29 '16 at 2:02






          great answer- some additional info: ffmpeg can be installed using directions here
          – chepyle
          Mar 29 '16 at 2:02






          2




          2




          Keep in mind that the frames folder must exist for the first command to work.
          – totymedli
          Jun 7 '17 at 15:09




          Keep in mind that the frames folder must exist for the first command to work.
          – totymedli
          Jun 7 '17 at 15:09




          1




          1




          For those that want a quick way to optimize that generally works and are too lazy to read the link, just add -layers Optimize to the last convert command, before *.jpg. Check the output though, it might be affected. For me it reduced the gif size from 5 MB to 700 KB without any perceivable loss in quality :)
          – cpury
          Jul 10 '17 at 8:54






          For those that want a quick way to optimize that generally works and are too lazy to read the link, just add -layers Optimize to the last convert command, before *.jpg. Check the output though, it might be affected. For me it reduced the gif size from 5 MB to 700 KB without any perceivable loss in quality :)
          – cpury
          Jul 10 '17 at 8:54














          Thanks that was an interesting process, a 5.6MB mp4 ended up a 236MB gif, not sure I'll be putting that one up on my website ;) Possibly a gif needs to be limited to seconds rather than a minute.
          – cardamom
          Sep 12 '17 at 9:37




          Thanks that was an interesting process, a 5.6MB mp4 ended up a 236MB gif, not sure I'll be putting that one up on my website ;) Possibly a gif needs to be limited to seconds rather than a minute.
          – cardamom
          Sep 12 '17 at 9:37




          1




          1




          Instead of using the JPG file format, very lossy with the default settings, I'd recommend using PNG for the initial file export. ffmpeg -i video.mp4 -r 5 'frames/frame-%03d.png'.
          – Pierre F
          Jun 7 at 11:15






          Instead of using the JPG file format, very lossy with the default settings, I'd recommend using PNG for the initial file export. ffmpeg -i video.mp4 -r 5 'frames/frame-%03d.png'.
          – Pierre F
          Jun 7 at 11:15














          up vote
          75
          down vote













          ffmpeg 3.4.4 can do it directly on Ubuntu 18.04



          You likely want to use something like:



          sudo apt-get install ffmpeg
          wget -O opengl-rotating-triangle.mp4 https://github.com/cirosantilli/media/blob/master/opengl-rotating-triangle.mp4?raw=true
          ulimit -Sv 1000000
          ffmpeg
          -i opengl-rotating-triangle.mp4
          -r 15
          -vf scale=512:-1
          -ss 00:00:03 -to 00:00:06
          opengl-rotating-triangle.gif




          Test data generation procedure described on this post.



          A more direct:



          sudo apt-get install ffmpeg
          ffmpeg -i in.mp4 out.gif


          also works, but the output GIF would be way larger than the input video, because video formats compress intelligently across frames.



          Argument breakdown:





          • ulimit -Sv 1000000: set a maximum 1Gb memory size for the program.



            Mostly me ensuring that the command is not using unlimited memory like certain previous attempts.



            500Mb makes ffmpeg fail to load shared libraries... time to upgrade your RAM?




          • -ss 00:00:03 -to 00:00:06: start and end time to cut the video from.



            No, GIFs are not the best way to pirate distribute videos online.



            See also: https://stackoverflow.com/questions/18444194/cutting-the-videos-based-on-start-and-end-time-using-ffmpeg




          • -vf scale=512:-1: make the output 512 pixels in height, and adjust width to maintain the aspect ratio.



            This is common use case for images for the web, which tend to have much smaller resolution than video.



            If you remove this option, the output GIF has the same height as the input video.



            The original video height can be found for example with ffprobe: https://superuser.com/questions/595177/how-to-retrieve-video-file-information-from-command-line-under-linux/1035178#1035178 and is 1024 x 1024 in our case.




          • -r 15: sampling FPS.



            For example, the original video was 30 FPS, so -r 15 means that ffmpeg will pick one frame in every 2 (= 30 / 15).



            The perceived output FPS is adjusted to match the input however, so you won't notice a speedup, only greater granularity.



            The input FPS can be found with ffprobe, and the total number of input frames can be found with mediainfo as explained at: https://superuser.com/questions/84631/how-do-i-get-the-number-of-frames-in-a-video-on-the-linux-command-line/1044894#1044894



            I recommend this option because video formats usually have a higher framerate due to the larger resolution. With smaller GIFs, the lower framerate is less noticeable, and so we can skip some frames and make smaller GIFs.




          Before pre 18.04: ffmpeg + convert one-liner without intermediate files



          ffmpeg could not handle GIF previously. The best I had was something along:



          sudo apt-get install ffmpeg imagemagick
          ffmpeg -i input.mp4 -r 10 -f image2pipe -vcodec ppm - |
          convert -delay 5 -loop 0 - output.gif


          Explanation of some of the arguments:




          • -loop 0: add the Netscape Gif extension Loop count field to the output. 0 means infinite loop as described at: http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension eog, firefox and chromium all loop infinitely by default even without it, so I'm not sure how necessary it is anymore.


          • -delay 5: time waited before showing the next frame, in hundreths of second, as described at: https://en.wikipedia.org/wiki/GIF#Animated_GIF byte 324. So 100 means 1 FPS, 5 means 1 / 0.5 == 20FPS.



          Even if you reduce the height and framerate, the output GIF may still be larger than the video, since "real" non-GIF video formats compress across frames, while GIF only compresses individual frames.



          A direct:



          convert input.mp4 rpi2-bare-metal-blink.gif


          worked, but almost killed my computer because of memory overflow, and produced an ouptput 100x larger for my 2s 1Mb input file. Maybe one day ImageMagick will catch up.



          See also: https://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality



          Tested on Ubuntu 17.10.






          share|improve this answer



















          • 1




            Downvoters please explain ;-)
            – Ciro Santilli 新疆改造中心 六四事件 法轮功
            Oct 21 '16 at 17:49






          • 1




            Your delay does not match your -r value (the resulting gif is 2x speed). Also you added a scale argument for no apparent reason (it makes the gif really small).
            – asmeurer
            Oct 21 '16 at 17:51






          • 2




            I agree. Small size gifs are better for web. Thanks for scale=320:-1
            – zombic
            Oct 26 '16 at 8:26






          • 2




            For those who are wondering: removing -r 10 will bring the GIF back to normal speed.
            – Mitch
            Feb 13 '17 at 8:05






          • 2




            I got a gif 20% bigger than the mp4 :O
            – Adam Goldman
            May 11 '17 at 2:39















          up vote
          75
          down vote













          ffmpeg 3.4.4 can do it directly on Ubuntu 18.04



          You likely want to use something like:



          sudo apt-get install ffmpeg
          wget -O opengl-rotating-triangle.mp4 https://github.com/cirosantilli/media/blob/master/opengl-rotating-triangle.mp4?raw=true
          ulimit -Sv 1000000
          ffmpeg
          -i opengl-rotating-triangle.mp4
          -r 15
          -vf scale=512:-1
          -ss 00:00:03 -to 00:00:06
          opengl-rotating-triangle.gif




          Test data generation procedure described on this post.



          A more direct:



          sudo apt-get install ffmpeg
          ffmpeg -i in.mp4 out.gif


          also works, but the output GIF would be way larger than the input video, because video formats compress intelligently across frames.



          Argument breakdown:





          • ulimit -Sv 1000000: set a maximum 1Gb memory size for the program.



            Mostly me ensuring that the command is not using unlimited memory like certain previous attempts.



            500Mb makes ffmpeg fail to load shared libraries... time to upgrade your RAM?




          • -ss 00:00:03 -to 00:00:06: start and end time to cut the video from.



            No, GIFs are not the best way to pirate distribute videos online.



            See also: https://stackoverflow.com/questions/18444194/cutting-the-videos-based-on-start-and-end-time-using-ffmpeg




          • -vf scale=512:-1: make the output 512 pixels in height, and adjust width to maintain the aspect ratio.



            This is common use case for images for the web, which tend to have much smaller resolution than video.



            If you remove this option, the output GIF has the same height as the input video.



            The original video height can be found for example with ffprobe: https://superuser.com/questions/595177/how-to-retrieve-video-file-information-from-command-line-under-linux/1035178#1035178 and is 1024 x 1024 in our case.




          • -r 15: sampling FPS.



            For example, the original video was 30 FPS, so -r 15 means that ffmpeg will pick one frame in every 2 (= 30 / 15).



            The perceived output FPS is adjusted to match the input however, so you won't notice a speedup, only greater granularity.



            The input FPS can be found with ffprobe, and the total number of input frames can be found with mediainfo as explained at: https://superuser.com/questions/84631/how-do-i-get-the-number-of-frames-in-a-video-on-the-linux-command-line/1044894#1044894



            I recommend this option because video formats usually have a higher framerate due to the larger resolution. With smaller GIFs, the lower framerate is less noticeable, and so we can skip some frames and make smaller GIFs.




          Before pre 18.04: ffmpeg + convert one-liner without intermediate files



          ffmpeg could not handle GIF previously. The best I had was something along:



          sudo apt-get install ffmpeg imagemagick
          ffmpeg -i input.mp4 -r 10 -f image2pipe -vcodec ppm - |
          convert -delay 5 -loop 0 - output.gif


          Explanation of some of the arguments:




          • -loop 0: add the Netscape Gif extension Loop count field to the output. 0 means infinite loop as described at: http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension eog, firefox and chromium all loop infinitely by default even without it, so I'm not sure how necessary it is anymore.


          • -delay 5: time waited before showing the next frame, in hundreths of second, as described at: https://en.wikipedia.org/wiki/GIF#Animated_GIF byte 324. So 100 means 1 FPS, 5 means 1 / 0.5 == 20FPS.



          Even if you reduce the height and framerate, the output GIF may still be larger than the video, since "real" non-GIF video formats compress across frames, while GIF only compresses individual frames.



          A direct:



          convert input.mp4 rpi2-bare-metal-blink.gif


          worked, but almost killed my computer because of memory overflow, and produced an ouptput 100x larger for my 2s 1Mb input file. Maybe one day ImageMagick will catch up.



          See also: https://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality



          Tested on Ubuntu 17.10.






          share|improve this answer



















          • 1




            Downvoters please explain ;-)
            – Ciro Santilli 新疆改造中心 六四事件 法轮功
            Oct 21 '16 at 17:49






          • 1




            Your delay does not match your -r value (the resulting gif is 2x speed). Also you added a scale argument for no apparent reason (it makes the gif really small).
            – asmeurer
            Oct 21 '16 at 17:51






          • 2




            I agree. Small size gifs are better for web. Thanks for scale=320:-1
            – zombic
            Oct 26 '16 at 8:26






          • 2




            For those who are wondering: removing -r 10 will bring the GIF back to normal speed.
            – Mitch
            Feb 13 '17 at 8:05






          • 2




            I got a gif 20% bigger than the mp4 :O
            – Adam Goldman
            May 11 '17 at 2:39













          up vote
          75
          down vote










          up vote
          75
          down vote









          ffmpeg 3.4.4 can do it directly on Ubuntu 18.04



          You likely want to use something like:



          sudo apt-get install ffmpeg
          wget -O opengl-rotating-triangle.mp4 https://github.com/cirosantilli/media/blob/master/opengl-rotating-triangle.mp4?raw=true
          ulimit -Sv 1000000
          ffmpeg
          -i opengl-rotating-triangle.mp4
          -r 15
          -vf scale=512:-1
          -ss 00:00:03 -to 00:00:06
          opengl-rotating-triangle.gif




          Test data generation procedure described on this post.



          A more direct:



          sudo apt-get install ffmpeg
          ffmpeg -i in.mp4 out.gif


          also works, but the output GIF would be way larger than the input video, because video formats compress intelligently across frames.



          Argument breakdown:





          • ulimit -Sv 1000000: set a maximum 1Gb memory size for the program.



            Mostly me ensuring that the command is not using unlimited memory like certain previous attempts.



            500Mb makes ffmpeg fail to load shared libraries... time to upgrade your RAM?




          • -ss 00:00:03 -to 00:00:06: start and end time to cut the video from.



            No, GIFs are not the best way to pirate distribute videos online.



            See also: https://stackoverflow.com/questions/18444194/cutting-the-videos-based-on-start-and-end-time-using-ffmpeg




          • -vf scale=512:-1: make the output 512 pixels in height, and adjust width to maintain the aspect ratio.



            This is common use case for images for the web, which tend to have much smaller resolution than video.



            If you remove this option, the output GIF has the same height as the input video.



            The original video height can be found for example with ffprobe: https://superuser.com/questions/595177/how-to-retrieve-video-file-information-from-command-line-under-linux/1035178#1035178 and is 1024 x 1024 in our case.




          • -r 15: sampling FPS.



            For example, the original video was 30 FPS, so -r 15 means that ffmpeg will pick one frame in every 2 (= 30 / 15).



            The perceived output FPS is adjusted to match the input however, so you won't notice a speedup, only greater granularity.



            The input FPS can be found with ffprobe, and the total number of input frames can be found with mediainfo as explained at: https://superuser.com/questions/84631/how-do-i-get-the-number-of-frames-in-a-video-on-the-linux-command-line/1044894#1044894



            I recommend this option because video formats usually have a higher framerate due to the larger resolution. With smaller GIFs, the lower framerate is less noticeable, and so we can skip some frames and make smaller GIFs.




          Before pre 18.04: ffmpeg + convert one-liner without intermediate files



          ffmpeg could not handle GIF previously. The best I had was something along:



          sudo apt-get install ffmpeg imagemagick
          ffmpeg -i input.mp4 -r 10 -f image2pipe -vcodec ppm - |
          convert -delay 5 -loop 0 - output.gif


          Explanation of some of the arguments:




          • -loop 0: add the Netscape Gif extension Loop count field to the output. 0 means infinite loop as described at: http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension eog, firefox and chromium all loop infinitely by default even without it, so I'm not sure how necessary it is anymore.


          • -delay 5: time waited before showing the next frame, in hundreths of second, as described at: https://en.wikipedia.org/wiki/GIF#Animated_GIF byte 324. So 100 means 1 FPS, 5 means 1 / 0.5 == 20FPS.



          Even if you reduce the height and framerate, the output GIF may still be larger than the video, since "real" non-GIF video formats compress across frames, while GIF only compresses individual frames.



          A direct:



          convert input.mp4 rpi2-bare-metal-blink.gif


          worked, but almost killed my computer because of memory overflow, and produced an ouptput 100x larger for my 2s 1Mb input file. Maybe one day ImageMagick will catch up.



          See also: https://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality



          Tested on Ubuntu 17.10.






          share|improve this answer














          ffmpeg 3.4.4 can do it directly on Ubuntu 18.04



          You likely want to use something like:



          sudo apt-get install ffmpeg
          wget -O opengl-rotating-triangle.mp4 https://github.com/cirosantilli/media/blob/master/opengl-rotating-triangle.mp4?raw=true
          ulimit -Sv 1000000
          ffmpeg
          -i opengl-rotating-triangle.mp4
          -r 15
          -vf scale=512:-1
          -ss 00:00:03 -to 00:00:06
          opengl-rotating-triangle.gif




          Test data generation procedure described on this post.



          A more direct:



          sudo apt-get install ffmpeg
          ffmpeg -i in.mp4 out.gif


          also works, but the output GIF would be way larger than the input video, because video formats compress intelligently across frames.



          Argument breakdown:





          • ulimit -Sv 1000000: set a maximum 1Gb memory size for the program.



            Mostly me ensuring that the command is not using unlimited memory like certain previous attempts.



            500Mb makes ffmpeg fail to load shared libraries... time to upgrade your RAM?




          • -ss 00:00:03 -to 00:00:06: start and end time to cut the video from.



            No, GIFs are not the best way to pirate distribute videos online.



            See also: https://stackoverflow.com/questions/18444194/cutting-the-videos-based-on-start-and-end-time-using-ffmpeg




          • -vf scale=512:-1: make the output 512 pixels in height, and adjust width to maintain the aspect ratio.



            This is common use case for images for the web, which tend to have much smaller resolution than video.



            If you remove this option, the output GIF has the same height as the input video.



            The original video height can be found for example with ffprobe: https://superuser.com/questions/595177/how-to-retrieve-video-file-information-from-command-line-under-linux/1035178#1035178 and is 1024 x 1024 in our case.




          • -r 15: sampling FPS.



            For example, the original video was 30 FPS, so -r 15 means that ffmpeg will pick one frame in every 2 (= 30 / 15).



            The perceived output FPS is adjusted to match the input however, so you won't notice a speedup, only greater granularity.



            The input FPS can be found with ffprobe, and the total number of input frames can be found with mediainfo as explained at: https://superuser.com/questions/84631/how-do-i-get-the-number-of-frames-in-a-video-on-the-linux-command-line/1044894#1044894



            I recommend this option because video formats usually have a higher framerate due to the larger resolution. With smaller GIFs, the lower framerate is less noticeable, and so we can skip some frames and make smaller GIFs.




          Before pre 18.04: ffmpeg + convert one-liner without intermediate files



          ffmpeg could not handle GIF previously. The best I had was something along:



          sudo apt-get install ffmpeg imagemagick
          ffmpeg -i input.mp4 -r 10 -f image2pipe -vcodec ppm - |
          convert -delay 5 -loop 0 - output.gif


          Explanation of some of the arguments:




          • -loop 0: add the Netscape Gif extension Loop count field to the output. 0 means infinite loop as described at: http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension eog, firefox and chromium all loop infinitely by default even without it, so I'm not sure how necessary it is anymore.


          • -delay 5: time waited before showing the next frame, in hundreths of second, as described at: https://en.wikipedia.org/wiki/GIF#Animated_GIF byte 324. So 100 means 1 FPS, 5 means 1 / 0.5 == 20FPS.



          Even if you reduce the height and framerate, the output GIF may still be larger than the video, since "real" non-GIF video formats compress across frames, while GIF only compresses individual frames.



          A direct:



          convert input.mp4 rpi2-bare-metal-blink.gif


          worked, but almost killed my computer because of memory overflow, and produced an ouptput 100x larger for my 2s 1Mb input file. Maybe one day ImageMagick will catch up.



          See also: https://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality



          Tested on Ubuntu 17.10.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 at 23:28

























          answered Oct 15 '16 at 18:56









          Ciro Santilli 新疆改造中心 六四事件 法轮功

          8,95444146




          8,95444146








          • 1




            Downvoters please explain ;-)
            – Ciro Santilli 新疆改造中心 六四事件 法轮功
            Oct 21 '16 at 17:49






          • 1




            Your delay does not match your -r value (the resulting gif is 2x speed). Also you added a scale argument for no apparent reason (it makes the gif really small).
            – asmeurer
            Oct 21 '16 at 17:51






          • 2




            I agree. Small size gifs are better for web. Thanks for scale=320:-1
            – zombic
            Oct 26 '16 at 8:26






          • 2




            For those who are wondering: removing -r 10 will bring the GIF back to normal speed.
            – Mitch
            Feb 13 '17 at 8:05






          • 2




            I got a gif 20% bigger than the mp4 :O
            – Adam Goldman
            May 11 '17 at 2:39














          • 1




            Downvoters please explain ;-)
            – Ciro Santilli 新疆改造中心 六四事件 法轮功
            Oct 21 '16 at 17:49






          • 1




            Your delay does not match your -r value (the resulting gif is 2x speed). Also you added a scale argument for no apparent reason (it makes the gif really small).
            – asmeurer
            Oct 21 '16 at 17:51






          • 2




            I agree. Small size gifs are better for web. Thanks for scale=320:-1
            – zombic
            Oct 26 '16 at 8:26






          • 2




            For those who are wondering: removing -r 10 will bring the GIF back to normal speed.
            – Mitch
            Feb 13 '17 at 8:05






          • 2




            I got a gif 20% bigger than the mp4 :O
            – Adam Goldman
            May 11 '17 at 2:39








          1




          1




          Downvoters please explain ;-)
          – Ciro Santilli 新疆改造中心 六四事件 法轮功
          Oct 21 '16 at 17:49




          Downvoters please explain ;-)
          – Ciro Santilli 新疆改造中心 六四事件 法轮功
          Oct 21 '16 at 17:49




          1




          1




          Your delay does not match your -r value (the resulting gif is 2x speed). Also you added a scale argument for no apparent reason (it makes the gif really small).
          – asmeurer
          Oct 21 '16 at 17:51




          Your delay does not match your -r value (the resulting gif is 2x speed). Also you added a scale argument for no apparent reason (it makes the gif really small).
          – asmeurer
          Oct 21 '16 at 17:51




          2




          2




          I agree. Small size gifs are better for web. Thanks for scale=320:-1
          – zombic
          Oct 26 '16 at 8:26




          I agree. Small size gifs are better for web. Thanks for scale=320:-1
          – zombic
          Oct 26 '16 at 8:26




          2




          2




          For those who are wondering: removing -r 10 will bring the GIF back to normal speed.
          – Mitch
          Feb 13 '17 at 8:05




          For those who are wondering: removing -r 10 will bring the GIF back to normal speed.
          – Mitch
          Feb 13 '17 at 8:05




          2




          2




          I got a gif 20% bigger than the mp4 :O
          – Adam Goldman
          May 11 '17 at 2:39




          I got a gif 20% bigger than the mp4 :O
          – Adam Goldman
          May 11 '17 at 2:39










          up vote
          8
          down vote













          gifify is an all-in-one node-based utility that simplifies the conversion. It depends on nodejs, npm, ffmpeg, and imagemagick which are all available in the repos.



          Once you have npm installed you can install gifify globally with:



              npm install -g gifify


          A video can be converted to a .GIF with:



              gifify video.mp4 -o video.gif


          You can also optionally set a start and end position in the video and add a text caption:



              gifify clip.mp4 -o clip.gif --from 01:48:23.200 --to 01:48:25.300 --text 'we are the knights who say nip!'


          ❗️ It can take several minutes for the conversion to complete even with smaller videos.




          NOTE: ffmpeg and imagemagick might need to be compiled with some specific libraries (i.e. libass and fontconfig accordingly).







          share|improve this answer





















          • Doesn't handle approx > 40 sec clips: github.com/vvo/gifify/issues/99
            – oligofren
            Jan 30 at 16:03















          up vote
          8
          down vote













          gifify is an all-in-one node-based utility that simplifies the conversion. It depends on nodejs, npm, ffmpeg, and imagemagick which are all available in the repos.



          Once you have npm installed you can install gifify globally with:



              npm install -g gifify


          A video can be converted to a .GIF with:



              gifify video.mp4 -o video.gif


          You can also optionally set a start and end position in the video and add a text caption:



              gifify clip.mp4 -o clip.gif --from 01:48:23.200 --to 01:48:25.300 --text 'we are the knights who say nip!'


          ❗️ It can take several minutes for the conversion to complete even with smaller videos.




          NOTE: ffmpeg and imagemagick might need to be compiled with some specific libraries (i.e. libass and fontconfig accordingly).







          share|improve this answer





















          • Doesn't handle approx > 40 sec clips: github.com/vvo/gifify/issues/99
            – oligofren
            Jan 30 at 16:03













          up vote
          8
          down vote










          up vote
          8
          down vote









          gifify is an all-in-one node-based utility that simplifies the conversion. It depends on nodejs, npm, ffmpeg, and imagemagick which are all available in the repos.



          Once you have npm installed you can install gifify globally with:



              npm install -g gifify


          A video can be converted to a .GIF with:



              gifify video.mp4 -o video.gif


          You can also optionally set a start and end position in the video and add a text caption:



              gifify clip.mp4 -o clip.gif --from 01:48:23.200 --to 01:48:25.300 --text 'we are the knights who say nip!'


          ❗️ It can take several minutes for the conversion to complete even with smaller videos.




          NOTE: ffmpeg and imagemagick might need to be compiled with some specific libraries (i.e. libass and fontconfig accordingly).







          share|improve this answer












          gifify is an all-in-one node-based utility that simplifies the conversion. It depends on nodejs, npm, ffmpeg, and imagemagick which are all available in the repos.



          Once you have npm installed you can install gifify globally with:



              npm install -g gifify


          A video can be converted to a .GIF with:



              gifify video.mp4 -o video.gif


          You can also optionally set a start and end position in the video and add a text caption:



              gifify clip.mp4 -o clip.gif --from 01:48:23.200 --to 01:48:25.300 --text 'we are the knights who say nip!'


          ❗️ It can take several minutes for the conversion to complete even with smaller videos.




          NOTE: ffmpeg and imagemagick might need to be compiled with some specific libraries (i.e. libass and fontconfig accordingly).








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 3 '17 at 19:44









          ccpizza

          809812




          809812












          • Doesn't handle approx > 40 sec clips: github.com/vvo/gifify/issues/99
            – oligofren
            Jan 30 at 16:03


















          • Doesn't handle approx > 40 sec clips: github.com/vvo/gifify/issues/99
            – oligofren
            Jan 30 at 16:03
















          Doesn't handle approx > 40 sec clips: github.com/vvo/gifify/issues/99
          – oligofren
          Jan 30 at 16:03




          Doesn't handle approx > 40 sec clips: github.com/vvo/gifify/issues/99
          – oligofren
          Jan 30 at 16:03


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f648603%2fhow-to-create-an-animated-gif-from-mp4-video-via-command-line%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mouse cursor on multiple screens with different PPI

          Agildo Ribeiro

          Sometime when accessing a menu: “Ubuntu 16.04 has experienced an internal error”