As part of my #StillStanding project I need to handle a lot of video files on a daily basis. Today, I wanted to check the duration and pixel dimensions of a bunch of files in different folders. As always, I turned to FFmpeg, or more specifically FFprobe, for help. However, figuring out all the details of how to get out the right information is tricky. So I decided to ask ChatGPT for help.

It came up with a useful script for me on the first attempt, including information about how to install FFprobe and how to get the script working. The result wasn’t exactly what I was after, but after just a couple of iterations, it helped me generate two scripts that do what I want.

The first one prints information about duration and pixel dimensions in the terminal:

#!/bin/bash
for file in $(find ./ -type f -name "*.mp4" -o -name "*.avi" -o -name "*.mkv" -o -name "*.360")
do
  duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file")
  width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=s=x:p=0 "$file")
  height=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=s=x:p=0 "$file")
  echo "File: $file, Duration: $duration s, Width: $width px, Height: $height px"
done

And the second one saves the information into a CSV file for further inspection:

#!/bin/bash
echo "File,Duration (s),Width (px),Height (px)" > videodata.csv
for file in $(find ./ -type f -name "*.mp4" -o -name "*.avi" -o -name "*.mkv" -o -name "*.360")
do
  duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file")
  width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=s=x:p=0 "$file")
  height=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=s=x:p=0 "$file")
  echo "$file,$duration,$width,$height" >> videodata.csv
done

I would have figured it out myself, but now I saved half an hour of fiddling around that I could spend on writing this blog post and still have some time left over.