preloader
post-thumb

Last Update: February 24, 2025 6 min read


BYauthor-thumberic

|Loading...

FFmpeg Commands on Ubuntu: Convert, Trim, Compress, Merge and Record Video

Almost every video job on Linux comes down to ffmpeg. This page is a cheat sheet of the commands you'll actually reach for, each one run against a test clip with the FFmpeg 6.1.1 package that ships with Ubuntu 24.04. Where a popular one-liner is wrong or has a trap, the note under it says so.

Quick reference

Task
Command
Convert to an MP4 that plays everywhere
ffmpeg -i in.mkv -c:v libx264 -crf 23 -pix_fmt yuv420p -c:a aac -movflags +faststart out.mp4
Change container only (no quality loss)
ffmpeg -i in.mkv -c copy out.mp4
Extract audio as MP3
ffmpeg -i in.mp4 -vn -c:a libmp3lame -q:a 2 out.mp3
Resize to 720p, keep aspect ratio
ffmpeg -i in.mp4 -vf scale=-2:720 -c:a copy out.mp4
Compress with H.265
ffmpeg -i in.mp4 -c:v libx265 -crf 28 -tag:v hvc1 -c:a copy out.mp4
Trim fast (cuts on keyframes)
ffmpeg -ss 00:00:10 -to 00:00:50 -i in.mp4 -c copy out.mp4
Trim frame-accurate
ffmpeg -ss 10 -i in.mp4 -t 40 -c:v libx264 -crf 18 -c:a aac out.mp4
Loop 3 times in total
ffmpeg -stream_loop 2 -i in.mp4 -c copy out.mp4
Merge clips with the same format
ffmpeg -f concat -safe 0 -i list.txt -c copy out.mp4
Grab one frame
ffmpeg -ss 5 -i in.mp4 -frames:v 1 still.png
Remove the audio track
ffmpeg -i in.mp4 -c copy -an out.mp4
Inspect a file
ffprobe -hide_banner in.mp4

Two rules explain most FFmpeg surprises:

  1. Option position matters. An option applies to the file that comes after it. Put it before -i and it's an input option; put it after and it applies to the output.
  2. -c copy never re-encodes. It's fast and lossless, but it can't change the codec, size or quality, and it can only cut where a keyframe is.

Install the tools

On Ubuntu or Debian:

bash
sudo apt update
sudo apt install ffmpeg mpv

ffmpeg also installs ffprobe. Ubuntu's build includes the encoders used on this page (libx264, libx265, libvpx-vp9, libopus, libmp3lame and the native aac). You can check with:

bash
ffmpeg -hide_banner -encoders | grep -E "264|265|vp9|opus|mp3lame"

On Fedora, the ffmpeg-free package in the default repos leaves out patent-encumbered codecs such as H.264 and H.265. For the full build, enable RPM Fusion and swap it in:

bash
sudo dnf install https://mirrors.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm
sudo dnf swap ffmpeg-free ffmpeg --allowerasing

RHEL, Rocky and AlmaLinux need EPEL plus RPM Fusion for EL first. An earlier version of this post suggested sudo dnf install ffmpeg vlc mencoder mpv on Fedora, CentOS and RHEL. That doesn't work on a stock install, and MEncoder isn't something you need any more.

Convert formats

For a file that plays in browsers, phones and TVs, use H.264 video and AAC audio in MP4:

bash
ffmpeg -i input.mkv -c:v libx264 -crf 23 -preset medium -pix_fmt yuv420p \
  -c:a aac -b:a 128k -movflags +faststart output.mp4
  • -crf 23 is the default quality. Lower is better and bigger; 18 looks close to lossless.
  • -preset trades speed for size. slow gives a smaller file at the same quality; veryfast finishes sooner.
  • -pix_fmt yuv420p avoids files that won't play on some devices when the source is 4:2:2 or 4:4:4.
  • -movflags +faststart moves the index to the front so the video can start playing before it has fully downloaded.

If the codecs are already fine and you only want a different container (say MKV to MP4), don't re-encode:

bash
ffmpeg -i input.mkv -c copy output.mp4

This takes seconds. It fails if the target container doesn't support one of the streams, for example some subtitle formats in MP4. Add -sn to drop subtitles in that case.

For WebM (VP9 + Opus), use constant quality rather than a fixed bitrate:

bash
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 31 -b:v 0 -row-mt 1 \
  -c:a libopus -b:a 128k output.webm

-b:v 0 is required for -crf to mean constant quality in VP9. VP9 is slow to encode; -row-mt 1 lets it use more CPU cores.

Extract audio

bash
# MP3, VBR at roughly 190 kbps (q 0 is the highest quality, 9 the lowest)
ffmpeg -i input.mp4 -vn -c:a libmp3lame -q:a 2 output.mp3

# Uncompressed WAV, e.g. for transcription or editing
ffmpeg -i input.mp4 -vn -c:a pcm_s16le output.wav

# Keep the original AAC track as-is (no quality loss)
ffmpeg -i input.mp4 -vn -c:a copy output.m4a

-vn drops the video. Use ffprobe input.mp4 first to see what audio codec you have. If it's AAC, the copy version is instant and lossless.

Resize

bash
ffmpeg -i input.mp4 -vf scale=-2:720 -c:v libx264 -crf 23 -c:a copy output.mp4

scale=-2:720 sets the height to 720 and works out the width from the aspect ratio, rounded to an even number (H.264 needs even dimensions). An earlier version used scale=1280:720, which stretches anything that isn't already 16:9.

Compress

bash
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -preset medium -tag:v hvc1 -c:a copy output.mp4

H.265 (HEVC) at CRF 28 looks roughly like H.264 at CRF 23 in a smaller file. Things to know:

  • -tag:v hvc1 makes the MP4 play in QuickTime and on Apple devices. Without it many of them refuse the file.
  • -c:a copy keeps the audio untouched, so only the video is re-encoded.
  • Some older TVs, browsers and editors can't play HEVC. If the file has to work everywhere, use H.264 with a higher CRF (26 to 28) instead.
  • Raise the CRF for a smaller file, or scale down at the same time (-vf scale=-2:720). Dropping resolution usually saves more than pushing CRF high.

Trim

There are two ways to cut, and they behave differently.

Fast, no re-encode:

bash
ffmpeg -ss 00:00:10 -to 00:00:50 -i input.mp4 -c copy output.mp4

Because nothing is re-encoded, FFmpeg can only start the cut at a keyframe. The clip may start a little early, or show a frozen or grey frame for a moment at the start in some players. Fine for rough cuts.

Frame-accurate, re-encodes:

bash
ffmpeg -ss 10 -i input.mp4 -t 40 -c:v libx264 -crf 18 -c:a aac output.mp4

Watch the timing options when -ss comes before -i. Output timestamps then start at zero, so an output -to 50 placed after -i means "stop 50 seconds into the output", not "at 0:50 in the source". Use -t (a duration), or put both -ss and -to before -i as in the fast example.

Subtitles

Burn them into the picture (always visible, needs a re-encode):

bash
ffmpeg -i input.mp4 -vf subtitles=subs.srt -c:a copy output.mp4

Or add them as a track the viewer can switch on and off, with no re-encode:

bash
ffmpeg -i input.mp4 -i subs.srt -map 0 -map 1 -c copy -c:s mov_text output.mp4

MP4 only accepts the mov_text subtitle format. For MKV you can keep SRT as is (-c:s srt).

Loop a video

bash
ffmpeg -stream_loop 2 -i input.mp4 -c copy output.mp4

-stream_loop is an input option, so it must come before -i. The number is how many extra times the input repeats: 2 gives three plays in total, and -1 loops forever (only useful for streaming).

An earlier version of this post put -stream_loop after -i. FFmpeg 6 rejects that with "Option stream_loop ... cannot be applied to output url ... Move this option before the file it belongs to."

Merge clips

If every clip has the same codecs, resolution and frame rate (for example, several recordings from the same camera or app), the concat demuxer joins them without re-encoding:

bash
for f in clip1.mp4 clip2.mp4 clip3.mp4; do printf "file '%s'\n" "$f"; done > list.txt
ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp4

-safe 0 is needed when a path is absolute or contains characters such as spaces; without it you get Unsafe file name. A file name containing a single quote needs escaping inside list.txt.

If the clips differ in size, frame rate or codec, -c copy produces a file that stutters or won't play. Re-encode them to a common format first, or use the concat filter, which re-encodes as it joins.

Extract frames

bash
# One frame per second
ffmpeg -i input.mp4 -vf fps=1 frame_%04d.png

# A single frame at 5 seconds (fast: seeks before decoding)
ffmpeg -ss 00:00:05 -i input.mp4 -frames:v 1 still.png

-frames:v is the current name for the older -vframes. Use .jpg with -q:v 2 instead of PNG if you're extracting a lot of frames and disk space matters.

Make a GIF

bash
ffmpeg -i input.mp4 -t 3 \
  -vf "fps=12,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" \
  output.gif

Generating a palette from the clip gives far better colours than FFmpeg's default GIF palette. Keep GIFs short and small; a few seconds at 480 px wide is already several megabytes.

Record the screen

The common x11grab recipe only works in an X11 session:

bash
echo $XDG_SESSION_TYPE   # x11 or wayland

ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i $DISPLAY \
  -c:v libx264 -preset ultrafast -crf 23 output.mp4

Ubuntu uses Wayland by default on most hardware, and there x11grab records a black screen or only X11 (XWayland) windows. On Wayland, use:

  • The built-in GNOME recorder: press Print Screen and switch to the video icon. It doesn't record audio.
  • OBS Studio, which captures the screen through PipeWire and can record microphone and desktop audio.
  • Or log out and pick "Ubuntu on Xorg" at the login screen if you want the FFmpeg command above.

To add the microphone in an X11 session, add an audio input (this works on both PulseAudio and PipeWire, which Ubuntu now uses):

bash
ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i $DISPLAY \
  -f pulse -i default \
  -c:v libx264 -preset ultrafast -crf 23 -c:a aac output.mp4

default is your default input, usually the microphone. To record what's playing through the speakers instead, list sources with pactl list short sources and use the one ending in .monitor.

Play video from the terminal

bash
mpv input.mp4            # f toggles fullscreen, space pauses, q quits
mpv --fs input.mp4       # start fullscreen
vlc --fullscreen input.mp4

mpv is light and starts quickly; it can also play online video URLs if yt-dlp is installed. MPlayer still exists, but mpv is its actively developed fork and the better choice now.

Download video with yt-dlp

yt-dlp replaced youtube-dl, which stopped keeping up with site changes. Avoid the apt package: on Ubuntu 24.04 it's version 2024.04.09, and video sites change often enough that old versions break. Install the current release instead:

bash
# Either with pipx...
sudo apt install pipx
pipx install yt-dlp        # update later with: pipx upgrade yt-dlp

# ...or the standalone binary
mkdir -p ~/.local/bin
curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o ~/.local/bin/yt-dlp
chmod a+rx ~/.local/bin/yt-dlp   # update later with: yt-dlp -U

Then:

bash
yt-dlp "https://www.youtube.com/watch?v=VIDEO_ID"
yt-dlp -x --audio-format mp3 "https://www.youtube.com/watch?v=VIDEO_ID"

-x needs ffmpeg installed to convert the audio. If YouTube downloads fail on a current release, check the yt-dlp README: newer versions may need a JavaScript runtime such as Deno for YouTube. Only download content you have the rights to, and check the site's terms.

Getting help

bash
ffprobe -hide_banner input.mp4         # streams, codecs, duration
ffmpeg -h encoder=libx264              # options for one encoder
ffmpeg -h filter=scale                 # options for one filter
man ffmpeg

If you're cleaning up clips from AI video tools, these commands cover most of it. See how Kling, Runway, Pika, Luma, Sora and Hailuo compare on the same prompts.

Comments (0)

Leave a Comment
Your email won't be published. We'll only use it to notify you of replies to your comment.
Loading comments...
Previous Article
post-thumb

Feb 25, 2025

Marker vs MinerU vs Docling vs MarkItDown vs LlamaParse: Which PDF-to-Markdown Tool?

A practical comparison of five tools for turning PDFs and Office files into Markdown or JSON for LLMs and RAG: what each one actually runs on, what it handles well, where it falls down, licences, and the commands to try it.

Next Article
post-thumb

Feb 20, 2025

Kling vs Runway vs Pika vs Luma vs Sora vs Hailuo: Same Three Prompts Tested

I gave the same three text prompts (a neon concept car, a hotel ad and a zombie at a railway crossing) to six AI video generators in February 2025. Here are all 17 clips, which tool won each round, where each one ignored the prompt, and what the plans cost at the time.

agico

We transform visions into reality. We specializes in crafting digital experiences that captivate, engage, and innovate. With a fusion of creativity and expertise, we bring your ideas to life, one pixel at a time. Let's build the future together.

Copyright ©  2026  TYO Lab · v0.0.39