utils
Commands

ffmpeg

A collection of useful FFmpeg commands for video and audio manipulation.

FFmpeg Cheatsheet

FFmpeg is a powerful command-line tool for handling video, audio, and other multimedia files and streams.


Create a High-Quality GIF from a Video

This command converts a video into a high-quality GIF. It uses a custom palette to improve the GIF's quality.

ffmpeg -i input.mp4 -vf "fps=10,scale=540:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif

Arguments Explained

  • -i input.mp4: Specifies the input video file.
  • -vf "...": Applies a filtergraph to the video.
    • fps=10: Sets the frame rate of the GIF to 10 frames per second.
    • scale=540:-1: Resizes the video to a width of 540 pixels, maintaining the aspect ratio.
    • flags=lanczos: Uses the Lanczos resampling algorithm for better quality.
    • split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse: This is a chain of filters that generates a custom color palette from the video and then uses it to create the GIF. This significantly improves the quality of the final GIF.

Quality Adjustments

  • fps: Higher values result in a smoother but larger GIF. Lower values create a smaller but choppier GIF. 10-15 is usually a good range.
  • scale: A smaller width will result in a smaller file size. -1 tells ffmpeg to automatically calculate the height while preserving the aspect ratio.

Other Useful Commands

Convert Video to Another Format

ffmpeg -i input.mp4 output.webm

Extract Audio from Video

ffmpeg -i input.mp4 -vn -acodec copy output.aac
  • -vn: Disables video recording.
  • -acodec copy: Copies the audio stream without re-encoding.

Cut/Trim a Video

ffmpeg -i input.mp4 -ss 00:01:00 -to 00:02:00 -c copy output.mp4
  • -ss hh:mm:ss: Specifies the start time.
  • -to hh:mm:ss: Specifies the end time.
  • -c copy: Copies the streams without re-encoding, which is fast but may not be frame-accurate.

For frame-accurate trimming, remove the -c copy flag, but be aware that this will re-encode the video.

Resize a Video

ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4
  • -vf scale=width:height: Resizes the video to the specified dimensions.