Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save glenkusuma/b01672bb4cbc3527ab653e24df80b9dd to your computer and use it in GitHub Desktop.
Save glenkusuma/b01672bb4cbc3527ab653e24df80b9dd to your computer and use it in GitHub Desktop.
How to Make Gif from Youtube Video or any Websites using Linux & FOSS

How to Make Gif from Youtube Video or any Websites using Linux & FOSS

GIF or JIF?

“GIFs are like that ex you can't stand but keep going back to—they're quirky, outdated, sometimes glitchy — but somehow still steals every scene with its endless, looped charm — nothing else quite captures the moment like they do”

– Anonymous, from a Reddit thread on internet culture


This guide We going explore a workflow of cliping a youtube video then, video segment downloading, video editing with Kdenlive, conversion to GIF with FFmpeg, further optimization using Gifsicle, and finally uploading for public access.

This is how i maximize quality of a gif while reducing file size effectively :D

1. Download a YouTube Video Segment Using yt-dlp

yt-dlp

  • Use yt-dlp to download only a specific portion of a YouTube video.

  • Example command:

    yt-dlp --download-sections "*00:03:43-00:03:49" "https://www.youtube.com/watch?v=VIDEO_ID"
  • This command extracts the clip between the timestamps 00:03:43 and 00:03:49.

    Click here to reveal Explanation
    • yt-dlp: A popular fork of youtube-dl, used to download videos from YouTube and other sites.
    • --download-sections "*00:03:43-00:03:49":
      • The --download-sections option tells yt-dlp to download only a specific part of the video.
      • The asterisk (*) indicates that this section applies to all formats.
      • 00:03:43-00:03:49 defines the start and end timestamps (from 3 minutes, 43 seconds to 3 minutes, 49 seconds).
    • "https://www.youtube.com/watch?v=VIDEO_ID": URL of the YouTube video from which the segment will be downloaded.

2. Edit the Downloaded Video in Kdenlive

Kdenlive Logo

Kdenlive acronym for KDE Non-Linear Video Editor) is a free and open-source video editing software based on the MLT Framework, KDE and Qt.

Note

In this guide we going to use Kdenlive.

You can use Kdenlive or any editor you prefer that suite your needs :D

  • Cropping and Zooming:

  • Export Settings:

    • Export the edited video using an MP4 preset.

    • Example preset parameters:

      ab=160k acodec=aac channels=2 crf=23 f=mp4 g=15 movflags=+faststart preset=veryslow real_time=-1 threads=7 vcodec=libx264
      
    • These settings give a high-quality output with a balance between compression and quality for MP4 files.

    Click here to reveal Explanation of each parameter
    • ab=160k: Sets the audio bitrate to 160 kilobits per second.
    • acodec=aac: Specifies the audio codec as AAC (Advanced Audio Coding).
    • channels=2: Configures the audio to be stereo (2 channels).
    • crf=23: Constant Rate Factor for video quality; 23 is the default in x264 encoding, balancing quality and file size.
    • f=mp4: Sets the output file format to MP4.
    • g=15: Defines the group of pictures (GOP) size; 15 frames between keyframes.
    • movflags=+faststart: Enables the faststart flag, which moves the MOOV atom to the beginning of the file for faster web playback.
    • preset=veryslow: Uses the “veryslow” preset in libx264 for maximum compression efficiency (at the cost of encoding time).
    • real_time=-1: Disables real-time encoding constraints.
    • threads=7: Uses 7 threads for encoding (adjust according to your CPU cores).
    • vcodec=libx264: Specifies the video codec as libx264 (H.264 encoding).

3. Convert the MP4 to an Optimized GIF Using FFmpeg

FFmpeg logo

FFmpeg is a free and open-source software project consisting of a suite of libraries and programs for handling video, audio, and other multimedia files and streams.

  • Step 1: Generate a Color Palette

    ffmpeg -y -i ArchIsoBooting.mp4 -vf "fps=4,scale=720:-1:flags=lanczos,palettegen" palette.png
    • This command:
      • Sets the frame rate to 4 fps.
      • Scales the video to a width of 720 pixels (height adjusted to maintain aspect ratio) using the Lanczos algorithm.
      • Generates an optimal color palette saved as palette.png.
    Click here to reveal Explanation of each parameter
    • ffmpeg: A versatile multimedia framework used to convert video formats.
    • -y: Automatically overwrite the output file if it exists.
    • -i ArchIsoBooting.mp4: Specifies the input video file.
    • -vf "fps=4,scale=720:-1:flags=lanczos,palettegen": Applies a series of video filters:
    • fps=4: Sets the output frame rate to 4 frames per second (reducing the number of frames to lower the GIF size).
    • scale=720:-1:flags=lanczos: Scales the video so that the width is 720 pixels; the height is automatically calculated to maintain the aspect ratio (-1). The lanczos filter is used for high-quality scaling.
    • palettegen: Generates an optimal color palette for the video, which is crucial since GIFs support a maximum of 256 colors.
    • palette.png: Output file where the generated palette is saved.

Important

If you want to change the FPS or Resolution, you need to change the parameter on both steps of generating color pallet and creating the gif, to make sure an optimize gif is made!

  • Step 2: Create the GIF Using the Palette

    ffmpeg -i ArchIsoBooting.mp4 -i palette.png -filter_complex "fps=4,scale=720:-1:flags=lanczos[x];[x][1]paletteuse" output.gif
    • This command:
      • Reads the MP4 file and the generated palette.
      • Sets the desired frame rate (5 fps) and scales the video similarly.
      • Applies the palette to produce the GIF.
    Click here to reveal Explanation of each parameter
    • -i ArchIsoBooting.mp4: Input video file.
    • -i palette.png: Input palette file generated from the previous command.
    • -filter_complex "fps=4,scale=720:-1:flags=lanczos[x];[x][1]paletteuse": Applies a complex filter chain:
    • fps=5: Sets the frame rate for the GIF output to 5 frames per second (this may differ from the palette generation step for a smoother GIF).
    • scale=720:-1:flags=lanczos: Scales the video to the same width (720 pixels) while preserving the aspect ratio, using the Lanczos algorithm.
    • [x]: Labels the output stream from the scaling/filtering step as [x].
    • [x][1]paletteuse: Combines the labeled video stream [x] with the palette (input index [1]) to apply the optimized colors to the GIF.
    • output.gif: Final GIF file generated from the conversion.

4. Optimize and Compress the GIF Using Gifsicle

Gifsicle Logo

Gifsicle is a powerful, simple command-line tool for creating, editing, and optimizing animated GIFs

  • Run the following command to further reduce file size:

    gifsicle -O3 --lossy=80 output.gif -o output_optimized.gif
    • -O3: Applies the highest level of optimization.
    • --lossy=80: Enables lossy compression (with a value of 80) to aggressively reduce file size.
    • output.gif to output_optimized.gif: Saves the further compressed GIF.
    Click here to reveal Explanation of each parameter
    • gifsicle: A command-line tool specifically designed for manipulating and optimizing GIF files.
    • -O3: Specifies the highest level of optimization, which reorders and compresses the data as much as possible.
    • --lossy=80: Enables lossy compression at a level of 90. This setting discards some image details to further reduce file size. A higher value typically means more compression (and potential quality loss), so the value can be adjusted based on the desired balance.
    • output.gif: The input GIF file (generated from the FFmpeg conversion).
    • -o output_optimized.gif: Specifies the output file name for the compressed GIF.

5. Upload the Final File to a Public Repository

  • Once optimized, upload your final GIF (or MP4 if needed) to a public repository.
  • This allows you to access the file via a raw URL feature for sharing or embedding.

Tip

This is an optional steps,

For instance, i made a public repository for this purpose glenkusuma/gif.

arch-iso-booting.gif

This gist was sincerely made with Arch <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment