2024-09-24 Video Production
Automating Video Metadata Extraction with Bash: A Step-by-Step Guide
By O. Wolfson
Managing a large collection of video files can quickly become cumbersome, especially when you need to gather technical details like codecs, resolutions, frame rates, and audio properties. Luckily, with a simple Bash script and FFmpeg’s ffprobe
tool, you can automate this process and store the metadata in a JSON file. In this post, I’ll walk you through a custom script I developed for just this purpose.
The Problem
Whether you’re archiving videos, prepping them for editing, or simply need an inventory, manually extracting metadata for each file can be a tedious task. You might have dozens (or hundreds!) of videos to check. Doing this manually is inefficient, and this is where automation shines.
The Solution
The script I’ve created gathers key information about each video file in a specified directory and outputs it into a well-formatted JSON file. It even calculates the total runtime of all videos and includes it at the top of the output.
Here’s the breakdown of the process.
What the Script Does:
- Scans a directory for
.mp4
and.mov
files. - Uses
ffprobe
(from FFmpeg) to gather:- Video codec
- Resolution (width x height)
- Frame rate
- Audio codec, channels, and sample rate
- Duration (in seconds)
- Calculates the total duration of all videos and adds it to the output.
- Generates a JSON file with all this data, which can be used for further processing or documentation.
The Script
Here’s the full script:
How the Script Works
-
Setting up the environment: The script first checks whether the specified directory exists. If it doesn't, the script exits early to avoid errors.
-
Looping through files: The script loops over each
.mp4
and.mov
file in the directory and uses FFmpeg'sffprobe
tool to extract metadata about each video and its audio stream (if present). This data is parsed and stored in variables for later use. -
Calculating total duration: The script calculates the total duration of all videos in the directory and formats it into hours, minutes, and seconds.
-
Generating the JSON output: The extracted metadata is structured into a JSON format and saved to a file named after the folder containing the videos.
Output Example
Here’s an example of what the JSON output looks like:
Why Use This Script?
- Automation: No need to manually check each file’s metadata. The script handles that automatically.
- Easy Integration: The JSON output can be used in further automation workflows, such as cataloging videos, creating reports, or feeding into another system for processing.
- Customizable: You can easily modify the script to gather more (or less) information, depending on your needs.
Final Thoughts
This script makes it easy to manage and document large collections of video files. By using ffprobe
from FFmpeg, you can extract detailed metadata quickly and accurately, making your workflow much more efficient. Whether you’re a video editor, content creator, or just someone who works with a lot of media files, automating these tasks can save you a lot of time.