Results 1 to 10 of 10

Thread: HOW-TO: Convert a video on youtube to animated gif

  1. #1
    Join Date
    Aug 2006
    Beans
    4

    HOW-TO: Convert a video on youtube to animated gif

    Hi, I don't know if people still use animated gifs at all but I see them used on forums etc. I just wanted to make one and found that the other guides out there didn't get me quite what I wanted. I.e. it wasn't easy to select the exact frames I wanted with mplayer and the other guides seem to use all the frames which can be bulky and slow. I've found this worked well and just wanted to write it up in case I ever want to do it again.

    # Youtube to animated gif HOW-TO:

    1) Download youtube-dl (http://www.arrakis.es/~rggi3/youtube-dl/). Run the youtube-dl to rip the video from Youtube into flash video format:

    ./youtube-dl -o example.flv "http://www.youtube.com/watch?v=cdaAWFoWr2c"

    2) Convert the flash video format into an avi:

    ffmpeg -i example.flv example.avi


    3) Launch avidemux (sudo apt-get it first) and open the avi file you just created, select the range of frames you want to rip from the avi using the 'A' and 'B' buttons at the bottom, then go to File->Save->Save Selection As jpeg Images and save the images into a new directory.

    4) Now this step is optional, but I have found that including all the frames in the gif will make it too large and it will also look slow. I found a rather hacky way around this by writing a perl script to remove frames. Create a new file in a text editor called 'chop.pl' in the same directory of the images. Copy and paste the following into it, changing the variables for youself:

    #!/usr/local/bin/perl
    # start_frame should be the smallest number in the filenames of the jpegs e.g example0000.jpg
    $start_frame = 0000;
    # end_frame should be the largest number in the filenames of the jpegs e.g example1855.jpg
    $end_frame = 1855;
    # scaling factor is how many frames you want to remove, 5 for example will remove 4/5's of the frames
    $scaling_factor = 5;
    # name should be the string appearing in the filenames of the jpegs before the numbers
    $name = "example";

    # Compute number of digits needed to display number
    use POSIX;
    $width = floor(log10($end_frame))+1;

    # Remove frames
    for ($i = $start_frame; $i <= $end_frame; ++$i)
    {
    $str = sprintf("%04d", $i);

    $check = $i % $scaling_factor;
    if($check != 0) {
    system("rm $name$str.jpg");
    }
    }

    5) Execute the script:

    perl chop.pl

    6) Open up GIMP and open the first jpeg in the series. Go to File->Open As Layers and select the rest of the series. Now go to Filters->Animation->Optimize For Gif. And then save the thing as a gif and enabled animation and voila.

  2. #2
    Join Date
    Oct 2007
    Location
    /home
    Beans
    177
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: HOW-TO: Convert a video on youtube to animated gif

    Thank you!

  3. #3
    Join Date
    Feb 2008
    Beans
    18

    Re: HOW-TO: Convert a video on youtube to animated gif

    For those who think "Oh my gosh,
    I have to import all those images
    as layers one by one! Highlight the images
    then just click and drag them into the layers
    box. (Dialogs > Layers)

  4. #4
    Join Date
    May 2007
    Beans
    40

    Re: HOW-TO: Convert a video on youtube to animated gif

    I would suggest the Phatch Photo Batch Processor for reducing size, changing resolution, etc.

    ^_^ Took me *forever to figure this out, thanks for the post!

  5. #5
    Join Date
    Aug 2007
    Location
    Edinburgh, Scotland
    Beans
    101
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: HOW-TO: Convert a video on youtube to animated gif

    You the man. I've been having problems with the "extract video range" feature and this has solved my problem> Wish I'd seen this post 2 days ago.

  6. #6
    Join Date
    Jun 2007
    Beans
    602
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: HOW-TO: Convert a video on youtube to animated gif

    Thanks for the post, but I am having trouble using your perl script.
    I get the following error when I run it:
    Illegal octal digit '9' at chop.pl line 5, at end of line
    BEGIN not safe after errors--compilation aborted at chop.pl line 12
    Here is what the script looks like.
    Code:
    #!/usr/local/bin/perl
    # start_frame should be the smallest number in the filenames of the jpegs e.g example0000.jpg
    $start_frame = 0003;
    # end_frame should be the largest number in the filenames of the jpegs e.g example1855.jpg
    $end_frame = 0480;
    # scaling factor is how many frames you want to remove, 5 for example will remove 4/5's of the frames
    $scaling_factor = 5;
    # name should be the string appearing in the filenames of the jpegs before the numbers
    $name = "AG";
    
    # Compute number of digits needed to display number
    use POSIX;
    $width = floor(log10($end_frame))+1;
    
    # Remove frames
    for ($i = $start_frame; $i <= $end_frame; ++$i)
    {
    $str = sprintf("%04d", $i);
    
    $check = $i % $scaling_factor;
    if($check != 0) {
    system("rm $name$str.jpg");
    }
    }
    EDIT:
    Nevermind, I figured it out, I just had to remove the 0 from 0480, so now the line reads:
    Code:
    $end_frame = 480;
    And it worked
    Thanks !
    Last edited by BassKozz; September 10th, 2009 at 05:38 AM.
    Ubuntu 12.04 64-bit - IP35-Pro | Q6600@3.3ghz(TRUE [lapped]) | G.Skill 2x2GB | MSI NX8600GT | 150GB WD Raptor ||| NAS - Ubuntu 10.04 - Intel D865GBF | P4 3.0GHz | G.Skill 4x1GB | FX5200-AGP | 3x250GB mdadm Raid5

  7. #7
    Join Date
    Apr 2007
    Location
    Morrisville, NY
    Beans
    2,150

    Re: HOW-TO: Convert a video on youtube to animated gif

    Nice thread! Too bad you aren't a regular - 2 beans +1 for you.

  8. #8
    Join Date
    Oct 2009
    Beans
    11

    Re: HOW-TO: Convert a video on youtube to animated gif

    Thanks a lot!

  9. #9
    Join Date
    Nov 2009
    Location
    Two Rivers
    Beans
    30
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: HOW-TO: Convert a video on youtube to animated gif

    Great example thank you!
    even easier way :
    Code:
    sudo apt-get install youtube-dl ffmpeg avidemux mplayer
    
    youtube-dl -o example.flv "http://www.youtube.com/watch?v=cdaAWFoWr2c"
    
    ffmpeg -i example.flv example.avi
    
    avidemux --load example.avi --begin A --end B --save output.avi --quit
    
    mplayer output.avi -nosound -vo gif89a:fps=15:output=example.gif -vf scale=200:150
    Last edited by rocuan; October 25th, 2010 at 11:33 AM.

  10. #10
    Join Date
    Nov 2010
    Beans
    1

    Re: HOW-TO: Convert a video on youtube to animated gif

    Even easier:

    Code:
    sudo apt-get install youtube-dl mplayer
    
    youtube-dl -o example.flv "http://www.youtube.com/watch?v=BNR74UCidBI"
    
    mplayer example.flv -nosound -vo gif89a:fps=15:output=solid.gif -vf scale=240:180 -ss 3:26 -endpos 7
    Result: http://i.imgur.com/xJhoK.gif

    There are sites (like http://www.gifsoup.com/) that will automate this process for you but you often have register or supply an email... I like the command line way best.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •