乐闻世界logo
搜索文章和话题

How to use FFmpeg to get video information? How to analyze video quality?

2月18日 11:03

FFmpeg provides powerful tools to retrieve and analyze detailed information about video files, which is very important for video processing and quality assessment.

Using ffprobe to Get Video Information

Basic Information Viewing

bash
# View basic file information ffprobe input.mp4 # Show more detailed information ffprobe -v quiet -print_format json -show_format -show_streams input.mp4 # Show only video stream information ffprobe -v quiet -select_streams v -show_entries stream input.mp4 # Show only audio stream information ffprobe -v quiet -select_streams a -show_entries stream input.mp4

Formatted Output

bash
# JSON format output ffprobe -v quiet -print_format json -show_format -show_streams input.mp4 # XML format output ffprobe -v quiet -print_format xml -show_format -show_streams input.mp4 # CSV format output ffprobe -v quiet -print_format csv -show_entries stream=codec_name,width,height input.mp4 # Flat text format ffprobe -v quiet -print_format flat -show_format -show_streams input.mp4

Getting Specific Information

Video Resolution

bash
# Get video resolution ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of default=noprint_wrappers=1:nokey=1 input.mp4 # Get video width ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 input.mp4 # Get video height ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=noprint_wrappers=1:nokey=1 input.mp4

Video Frame Rate

bash
# Get video frame rate ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 input.mp4 # Get average frame rate ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 input.mp4

Video Duration

bash
# Get video duration (seconds) ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4 # Get video duration (HH:MM:SS) ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1 input.mp4 | awk '{print int($1/3600)":"int(($1%3600)/60)":"int($1%60)}' # Get video duration (frame count) ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=noprint_wrappers=1:nokey=1 input.mp4

Video Bitrate

bash
# Get video bitrate ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 input.mp4 # Get overall bitrate ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 input.mp4

Video Codec

bash
# Get video codec ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 input.mp4 # Get video codec description ffprobe -v error -select_streams v:0 -show_entries stream=codec_long_name -of default=noprint_wrappers=1:nokey=1 input.mp4

Audio Information

bash
# Get audio sample rate ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 input.mp4 # Get audio channel count ffprobe -v error -select_streams a:0 -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 input.mp4 # Get audio codec ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 input.mp4

Video Quality Analysis

PSNR (Peak Signal-to-Noise Ratio)

bash
# Calculate PSNR of two videos ffmpeg -i original.mp4 -i compressed.mp4 -lavfi psnr -f null - # Show detailed PSNR information ffmpeg -i original.mp4 -i compressed.mp4 -lavfi "psnr=stats_file=psnr.log" -f null -

SSIM (Structural Similarity)

bash
# Calculate SSIM of two videos ffmpeg -i original.mp4 -i compressed.mp4 -lavfi ssim -f null - # Show detailed SSIM information ffmpeg -i original.mp4 -i compressed.mp4 -lavfi "ssim=stats_file=ssim.log" -f null -

VMAF (Video Multimethod Assessment Fusion)

bash
# Calculate VMAF score (requires libvmaf) ffmpeg -i original.mp4 -i compressed.mp4 -lavfi "libvmaf=model_path=vmaf_v0.6.1.pkl" -f null - # Generate VMAF report ffmpeg -i original.mp4 -i compressed.mp4 -lavfi "libvmaf=model_path=vmaf_v0.6.1.pkl:log_path=vmaf.log" -f null -

Video Frame Analysis

Extract Frame Information

bash
# Show information for each frame ffprobe -v quiet -select_streams v:0 -show_frames input.mp4 # Show frame types (I/P/B frames) ffprobe -v quiet -select_streams v:0 -show_entries frame=pict_type -of default=noprint_wrappers=1:nokey=1 input.mp4 # Count frame types ffprobe -v quiet -select_streams v:0 -show_entries frame=pict_type -of csv=p=0 input.mp4 | sort | uniq -c

GOP Analysis

bash
# Show GOP structure ffprobe -v quiet -select_streams v:0 -show_entries frame=pict_type,pts_time input.mp4 # Calculate GOP size ffprobe -v quiet -select_streams v:0 -show_entries frame=pict_type -of csv=p=0 input.mp4 | grep -c "^I"

Audio Analysis

Audio Waveform Analysis

bash
# Generate audio waveform ffmpeg -i input.mp3 -lavfi "showwavespic=s=640x320" waveform.png # Generate audio spectrogram ffmpeg -i input.mp3 -lavfi showspectrumpic=s=640x320 spectrum.png # Generate audio video ffmpeg -i input.mp3 -lavfi "showwaves=s=640x320" output.mp4

Audio Volume Analysis

bash
# Analyze audio volume ffmpeg -i input.mp3 -af "volumedetect" -f null - # Normalize audio ffmpeg -i input.mp3 -af "loudnorm" output.mp3

File Information Analysis

File Size

bash
# Get file size ls -lh input.mp4 # Get file size using ffprobe ffprobe -v error -show_entries format=size -of default=noprint_wrappers=1:nokey=1 input.mp4

Container Format

bash
# Get container format ffprobe -v error -show_entries format=format_name -of default=noprint_wrappers=1:nokey=1 input.mp4 # Get container format description ffprobe -v error -show_entries format=format_long_name -of default=noprint_wrappers=1:nokey=1 input.mp4

Useful Scripts

Complete Video Information Script

bash
#!/bin/bash file=$1 echo "=== Video Information ===" echo "Resolution: $(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of default=noprint_wrappers=1:nokey=1 "$file")" echo "Frame Rate: $(ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 "$file")" echo "Duration: $(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file") seconds" echo "Video Codec: $(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$file")" echo "Bitrate: $(ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 "$file") bps" echo "" echo "=== Audio Information ===" echo "Audio Codec: $(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$file")" echo "Sample Rate: $(ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 "$file") Hz" echo "Channels: $(ffprobe -v error -select_streams a:0 -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 "$file")"

Batch Analysis Script

bash
#!/bin/bash for file in *.mp4; do echo "Analyzing: $file" duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file") resolution=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of default=noprint_wrappers=1:nokey=1 "$file") echo "Duration: ${duration}s, Resolution: $resolution" echo "" done

Best Practices

  1. Use appropriate output format: Choose JSON, XML, or flat text based on needs
  2. Filter unnecessary information: Use -select_streams and -show_entries to reduce output
  3. Quiet mode: Use -v quiet or -v error to reduce noise output
  4. Batch processing: Combine with scripts to analyze multiple files
  5. Quality assessment: Use PSNR, SSIM, VMAF and other tools to assess video quality

Retrieving and analyzing video information is an important step in video processing, helping you understand video characteristics and choose appropriate processing parameters.

标签:FFmpeg