# PSNR Analysis Commands

This file contains `ffmpeg` commands for performing PSNR analysis on specific frames or times, and for visually verifying the selected frames.

```bash
source_video="IMG_3885-5s.mov" # Original source video (trimmed)
in1="output_trimmed-frames.mp4" # Source video with burned-in frames
in2="output_trimmed-frames-crf20.mp4" # CRF 20 compressed video with burned-in frames
in3="output_trimmed-frames-crf22.mp4" # CRF 22 compressed video with burned-in frames
```

## Get Timestamp of a Specific Frame

Use this command to get the presentation timestamp (PTS) of a specific frame (e.g., frame 100, which is index 99) from the source video (`$source_video`). Replace `99` with the desired frame index (frame number - 1).

```bash
ffprobe -v error -select_streams v:0 -show_entries frame=pts_time -of csv=p=0 "$source_video" | sed -n '100p'
```

## Extract All Frames from Reference Video

Use this command to extract all frames from the reference video (`$in2`) as individual PNG images. This will help you visually identify the frame that temporally aligns with the 100th frame of the original source video.

```bash
ffmpeg -y \
  -i "$in2" \
  output_reference_frames_%04d.png
```

# PSNR analysis on a specific frame and frame image export

This script performs PSNR analysis between the source video with burned-in frames (`$in1`) and the CRF 20 compressed video with burned-in frames (`$in2`) for a specific frame (frame 100). It also exports the specified frame from both videos for visual comparison.

```bash
#!/usr/bin/env bash

in1="output_trimmed-frames.mp4"
in2="output_trimmed-frames-crf20.mp4"


base1="${in1%.mp4}"
base2="${in2%.mp4}"

logfile="${base1}_vs_${base2}_psnr_frame100.log"

ffmpeg -y \
  -i "$in1" \
  -i "$in2" \
  -filter_complex "
    [0:v]select='eq(n,100)',setpts=PTS-STARTPTS,split=2[src_psnr][src_img];
    [1:v]select='eq(n,100)',setpts=PTS-STARTPTS,split=2[ref_psnr][ref_img];
    [src_psnr][ref_psnr]psnr=stats_file=${logfile}
  " \
  -map "[src_img]" -frames:v 1 -update 1 "${base1}_frame100.png" \
  -map "[ref_img]" -frames:v 1 -update 1 "${base2}_frame100.png" \
  -f null -
```
