How to Get Video Resolution with ffprobe: From Basic Commands to Batch Processing
This guide is brought to you by filethings.net — whether you're adapting videos for different devices, organizing media libraries, or transcoding footage, obtaining accurate video resolution is a fundamental first step. ffprobe from the FFmpeg toolkit acts like a precision measuring instrument, quickly parsing media metadata to extract width and height information without decoding the entire video, streamlining your video processing workflow.
Unlike Getting Video Metadata with ffprobe, this article focuses specifically on advanced resolution extraction techniques, including basic commands, parameter breakdowns, batch processing, and special case resolution calculations.
Resolution Extraction Principles 🔍
ffprobe extracts stream information by parsing media container formats (MP4, MKV, AVI, etc.) without decoding the entire video, making it highly efficient. Video files typically contain multiple streams: video, audio, subtitles, and more.
To obtain resolution, ffprobe follows this workflow:
- Identify all stream types in the file
- Locate the video stream (usually the first video stream, marked as
v:0
) - Extract
width
andheight
parameters from the video stream metadata - Format output according to user specifications
This non-decoding approach not only delivers speed but also handles corrupted or partially downloaded video files.
Quick Start: Basic Commands ⚡
The simplest command to get resolution for a single video file:
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input.mp4
Parameter Explanation
-v error
: Only output error messages for cleaner results-select_streams v:0
: Select the first video stream (v
indicates video stream type,:0
indicates index)-show_entries stream=width,height
: Display only width and height information-of csv=s=x:p=0
: Set output format to CSV, separate width and height with "x", and omit header line
Output Example
1920x1080
This command returns the video resolution directly in widthxheight
format, ideal for scripting purposes.
Advanced Applications: Resolution Extraction Techniques 🚀
1. JSON Output (for Programmatic Processing)
For scenarios requiring programmatic result handling, JSON format provides structured output that's easier to parse:
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json input.mp4
Output:
{
"programs": [],
"streams": [
{
"width": 1920,
"height": 1080
}
]
}
2. Getting Aspect Ratio Information
Beyond basic resolution, you may need to know the video's aspect ratio:
ffprobe -v error -select_streams v:0 -show_entries stream=width,height,sample_aspect_ratio -of json input.mp4
Output example:
{
"programs": [],
"streams": [
{
"width": 1920,
"height": 1080,
"sample_aspect_ratio": "1:1"
}
]
}
3. Working with Files Containing Multiple Video Streams
Certain container formats (like MKV) may contain multiple video streams. You can specify the stream index to get resolution for a specific stream:
# Get resolution of the second video stream
ffprobe -v error -select_streams v:1 -show_entries stream=width,height -of csv=s=x:p=0 input.mkv
4. Batch Resolution Extraction for Multiple Files
When working with numerous video files, batch operations save significant time:
# Process all MP4 files in current directory
echo "File,Resolution" > video_resolutions.csv
for file in *.mp4; do
resolution=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$file")
echo "$file,$resolution"
echo "$file,$resolution" >> video_resolutions.csv
done
5. Batch Processing on Windows
Windows users can achieve similar functionality using PowerShell:
Get-ChildItem -Filter *.mp4 | ForEach-Object {
$resolution = ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $_.FullName
Write-Output "$($_.Name): $resolution"
}
Frequently Asked Questions ❓
Q: No output or errors when executing commands?
A: Possible causes and solutions:
- File doesn't exist or incorrect path: Verify the file path, use quotes around paths containing spaces
- No video stream: Use
ffprobe -show_streams input.mp4
to confirm video stream existence - FFmpeg not properly installed: Run
ffprobe -version
to check installation status - Permissions issue: Ensure read permissions for the file
Q: How to get actual displayed resolution after rotation?
A: Videos shot on mobile devices or cameras may contain rotation metadata (typically 90°, 180°, or 270°). In these cases, combine the rotate
parameter to determine actual displayed resolution:
ffprobe -v error -select_streams v:0 -show_entries stream=width,height,rotate -of csv=s=x:p=0 input.mp4
If output includes rotation angle (e.g., 1920x1080x90
), the actual displayed resolution would be 1080x1920
(width and height swapped).
Q: How to distinguish between stored resolution and displayed resolution?
A: Stored resolution refers to the actual encoded pixel dimensions (width×height) in the video file, while displayed resolution may differ due to aspect ratio, rotation, or scaling. Use this command to get both:
ffprobe -v error -select_streams v:0 -show_entries stream=width,height,display_aspect_ratio,rotate -of json input.mp4
Q: How to batch process different video formats?
A: Extend the bash script to handle multiple video formats using wildcards:
# Process all video files in current directory
for file in *.{mp4,mkv,avi,mov}; do
if [ -f "$file" ]; then
resolution=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$file")
echo "$file: $resolution"
fi
done
Q: How to get other video information (frame rate, codec format)?
A: Simply extend the -show_entries
parameter:
# Get resolution, frame rate, and codec format
ffprobe -v error -select_streams v:0 -show_entries stream=width,height,r_frame_rate,codec_name -of csv=s=x:p=0 input.mp4
Quick Reference 📋
Here's a summary of the core commands introduced in this article for quick lookup:
Function | Command |
---|---|
Basic resolution | ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input.mp4 |
JSON output | ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json input.mp4 |
Aspect ratio information | ffprobe -v error -select_streams v:0 -show_entries stream=width,height,sample_aspect_ratio -of json input.mp4 |
Specific stream resolution | ffprobe -v error -select_streams v:1 -show_entries stream=width,height -of csv=s=x:p=0 input.mkv |
Check rotation angle | ffprobe -v error -select_streams v:0 -show_entries stream=width,height,rotate -of csv=s=x:p=0 input.mp4 |
Stored vs displayed resolution | ffprobe -v error -select_streams v:0 -show_entries stream=width,height,display_aspect_ratio,rotate -of json input.mp4 |
Extended video information | ffprobe -v error -select_streams v:0 -show_entries stream=width,height,r_frame_rate,codec_name -of csv=s=x:p=0 input.mp4 |