Convert, Trim and Inspect Media Safely with ffmpeg
You will finish with a small ffmpeg workflow for inspecting a media file, converting it, cutting a short section, selecting streams and applying a resize filter. The examples match ffmpeg 8.0.1 installed here from the ffmpeg package.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about fifteen minutes. You need a shell, an input media file and enough free space for a second copy. The commands only read your input and write new files, but an output path can still overwrite existing media if you ask it to.
1. Check the installed command
Start by confirming which executable will run and which version it reports:
$ command -v ffmpeg
/home/linuxbrew/.linuxbrew/bin/ffmpeg
$ ffmpeg -version | head -1
ffmpeg version 8.0.1 Copyright (c) 2000-2025 the FFmpeg developers
This is an ordinary read-only check. Package versions and available encoders vary between machines, so do not assume that an example using a named encoder is portable just because the command itself is.
Checkpoint
Set a shell variable to the real input file. Quote it, especially if its path contains spaces:
INPUT='/path/to/input.mkv'
test -f "$INPUT" && printf 'input found: %s\n' "$INPUT"
2. Inspect streams before choosing an output
Ask ffmpeg to read the input and stop before writing anything. The first few lines identify the container, streams, codecs, dimensions and duration:
$ ffmpeg -hide_banner -i "$INPUT"
At least one output file must be specified
The final error is expected here. ffmpeg has inspected the input but no output URL was supplied. Read the lines above it. A file may contain several video, audio, subtitle, attachment or data streams. Input indices start at zero, and so do stream indices.
Do not infer that stream 0 is audio or that every input has one video stream. When you need a precise choice, use -map rather than relying on automatic selection.
3. Make a first conversion without overwriting
Use the output extension to let ffmpeg choose the output format, and use -n as a safety boundary:
$ ffmpeg -hide_banner -n -i "$INPUT" output.mp4
With no codec options, ffmpeg decodes and re-encodes the selected streams using defaults suitable for the output format. The exact codecs and quality depend on the build and format. If output.mp4 already exists, -n refuses to overwrite it and exits immediately.
Check the result by inspecting it again:
$ ffmpeg -hide_banner -i output.mp4
At least one output file must be specified
See the reported output streams above the final message. If you need to replace a file deliberately, -y overwrites without asking. Treat that as a destructive action: verify the path first, and prefer a new filename until the result is confirmed. There is no ffmpeg undo command; recover by restoring the original from your backup or recreating the output.
4. Copy streams when no re-encoding is needed
For a container change that is compatible with the existing streams, stream copy avoids decoding and encoding:
$ ffmpeg -hide_banner -n -i "$INPUT" -map 0 -c copy copied.mkv
-map 0 manually includes every stream from the first input. -c copy copies packets instead of re-encoding them. This is fast and preserves the encoded data, but the destination container still has to support those streams. It is not a way to convert a codec into another codec.
Checkpoint
Rerun the inspection command on copied.mkv. The stream list should match the input, subject to what the Matroska container accepts.
5. Trim a clip and resize it
To create a one-minute clip starting at 30 seconds, put the input seek before -i and the output duration before the output name:
$ ffmpeg -hide_banner -n -ss 00:00:30 -i "$INPUT" -t 00:01:00 \
-vf 'scale=1280:-2' -c:v libx264 -c:a aac clip.mp4
Here -ss seeks in the input, -t limits the output duration, and -vf applies a simple video filter. The height -2 preserves the aspect ratio while making the computed value encoder-friendly. The video and audio are re-encoded because filtering requires decoded frames, and the selected encoders must exist in your build.
Check the duration and streams with the same no-output inspection command:
$ ffmpeg -hide_banner -i clip.mp4
At least one output file must be specified
Input placement matters. An input -ss seeks before decoding; an output -ss decodes and discards data until the requested position. Seeking may land near a keyframe, especially with stream copy. For an accurate re-encoded clip, test the start and end rather than trusting the filename.
6. Select one stream explicitly
To extract the first video stream as one PNG frame, map it and disable audio:
$ ffmpeg -hide_banner -n -i "$INPUT" \
-map 0:v:0 -an -frames:v 1 frame.png
The selector means input 0, video stream 0. -an disables audio output, and -frames:v 1 stops after one video frame. Replace 0:v:0 only after checking the stream list. A subtitle or attached-picture stream is not interchangeable with the main video.
For scripts, prefer explicit maps when the input is not fully under your control. Automatic selection chooses streams based on what the output format accepts, which can change the result when a file gains an extra audio language or subtitle track.
7. Keep the common failure modes visible
- Output exists: use a new path or inspect it before choosing between
-nand the destructive-y. - Unknown encoder: run
ffmpeg -encodersand choose a codec listed by this build. - Wrong stream: inspect first, then add
-mapand stream specifiers such as-c:vor-c:a. - Unexpected timing: check whether
-ss,-tand-toare input or output options. The manpage says-ttakes priority if both-tand-toare supplied. - Missing file or permission error: check the path and directory permissions. Do not reach for elevated privileges unless the media is genuinely in a protected location;
sudo ffmpegdoes not repair a bad path or codec choice.
Done means
- You confirmed the installed ffmpeg version.
- You inspected the input before selecting streams.
- You produced a conversion, a stream copy or a filtered clip under a new filename.
- You used
-nfor examples that must not overwrite existing files. - You checked the output again and know how to recover from an unwanted result.