Automating Media File Management with Linux and Command-Line Tools
Introduction
Efficiently organizing and processing media files can save significant time and effort. This blog post outlines highly technical solutions for renaming, sorting, and processing media files using Linux command-line tools. Each command is explained in detail, highlighting its purpose, caveats, and possible improvements. SEO-optimized for media management enthusiasts.
Renaming GoPro Clips Based on Creation Dates
for f in *.MP4; do mv -n $f $(date -r $f +%Y%m%d_%H%M%S).mp4; done
Explanation:
for f in *.MP4
: Loops through all MP4 files in the current directory.mv -n $f $(date -r $f +%Y%m%d_%H%M%S).mp4
: Renames each file to its creation date (format: YYYYMMDD_HHMMSS)-r $f
: Extracts the modification time of the file.+%Y%m%d_%H%M%S
: Formats the timestamp.-n
: Prevents overwriting existing files.
Caveats:
- Relies on file modification times, which might differ from actual creation times. If precise creation dates are required, use EXIF metadata instead. Cameras easily loose track of time due to lack of a RTC
Improvement:
-
Use
exiftool
for more reliable metadata extraction:for f in *.MP4; do mv -n "$f" $(exiftool -d '%Y%m%d_%H%M%S.mp4' -CreateDate "$f"); done
- use
find
&xargs
instead of afor
loop
- use
Renaming Pictures Using EXIF Data
find . -type f ! -name '*.tmp' -print0 | xargs -0 -P 12 -n 100 exiftool -r '-FileName<\$CreateDate' -d '%Y-%m-%d %H.%M.%S%%-c.%%le'
Explanation:
find . -type f ! -name '*.tmp'
: Finds all non-temporary files recursively.-print0 | xargs -0
: Handles filenames with special characters.-P 12
: Runs 12 parallel processes for efficiency.-n 100
: Processes up to 100 files per command.exiftool -r '-FileName<\$CreateDate'
: Renames files based on their EXIFCreateDate
metadata.-d '%Y-%m-%d %H.%M.%S%%-c.%%le'
: Formats the filename to include date, time, and counter for duplicates.
Caveats:
- EXIF metadata must exist; otherwise, files won’t be renamed.
Improvement:
- Add error handling for files without EXIF metadata:
find . -type f ! -name '*.tmp' -print0 | xargs -0 -P 12 -n 100 exiftool -r '-FileName<\$CreateDate' -d '%Y-%m-%d %H.%M.%S%%-c.%%le' || echo "Some files lack EXIF data."
Creating a Timelapse Video from JPEG Snapshots
ffmpeg -r 12 -pattern_type glob -y -i '*.jpg' -vcodec mjpeg_qsv -crf 0 output.mp4
Explanation:
-r 12
: Sets the frame rate to 12 frames per second.-pattern_type glob -i '*.jpg'
: Matches all JPEG files.-vcodec mjpeg_qsv
: Uses Intel Quick Sync for MJPEG encoding.-crf 0
: Ensures lossless compression.
Caveats:
- Requires Intel hardware with Quick Sync support.
Improvement:
- Add resolution adjustment for consistency:
ffmpeg -r 12 -pattern_type glob -y -i '*.jpg' -vf scale=1920:1080 -vcodec mjpeg_qsv -crf 0 output.mp4
Timelapse from Video with Motion Blur
ffmpeg -i input.mkv -filter:v tblend=average,framestep=2,setpts=0.1*PTS -r 96 -b:v 30M -crf 10 -vcodec h264_qsv -an -y output.mkv
Explanation:
tblend=average
: Applies motion blur by blending frames.framestep=2
: Skips every other frame.setpts=0.1*PTS
: Speeds up playback to 10% of the original duration.-r 96
: Sets the output frame rate to 96 FPS.-b:v 30M
: Sets a high bitrate for quality.-crf 10
: Balances quality and compression.-vcodec h264_qsv
: Uses Intel Quick Sync for H.264 encoding.
Caveats:
- Requires substantial processing power.
Improvement:
- Automate bitrate calculation based on input resolution:
ffmpeg -i input.mkv -filter:v tblend=average,framestep=2,setpts=0.1*PTS -r 96 -b:v $(expr $(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 input.mkv) \* 100)k -crf 10 -vcodec h264_qsv -an -y output.mkv
Merging Videos Without Transcoding
ffmpeg -safe 0 -f concat -i <(find . -type f -name '*MP4' -printf file '$PWD/%p'\n | sort) -c copy output.mkv
Explanation:
find . -type f -name '*MP4'
: Finds all MP4 files.-printf file '$PWD/%p'\n
: Formats paths for FFmpeg.-safe 0
: Allows unsafe file paths.-f concat -i
: Concatenates video files.-c copy
: Merges files without re-encoding.
Caveats:
- Files must have identical codecs, resolution, and framerate.
Improvement:
- Validate file compatibility before merging:
find . -type f -name '*MP4' | xargs -I {} ffprobe -v error -show_entries stream=codec_name,height,width -of csv=p=0 {} | sort | uniq -c
Sorting Pictures by Camera Model
exiftool -d '.' '-directory<${model;}/$datetimeoriginal' *.jpg
Explanation:
-d '.'
: Uses a dot separator for directories.'-directory<${model;}/$datetimeoriginal'
: Organizes pictures into folders by camera model and original date.
Caveats:
- Assumes consistent EXIF metadata.
Improvement:
- Add fallback for files missing camera model metadata:
exiftool -d '.' '-directory<${model;}/$datetimeoriginal' -if '$model' *.jpg || mv *.jpg Unknown_Model/
Sorting Pictures by Year and Month
find . -type f ! -name '*.tmp' -print0 | xargs -0 -P 12 -n 100 exiftool -d '%Y/%m' '-directory<\$CreateDate'
Explanation:
- Organizes files into directories structured as
Year/Month
. - Multi-threaded for faster processing (
-P 12
).
Caveats:
- Requires valid EXIF
CreateDate
metadata.
Improvement:
- Create missing directories dynamically to prevent errors:
find . -type f ! -name '*.tmp' -print0 | xargs -0 -P 12 -n 100 exiftool -d '%Y/%m' '-directory<\$CreateDate' || mkdir -p Unknown_Date/
Injecting Dates into WhatsApp Media Files
exiftool -if 'not $CreateDate' -if '$filename =~ /^(?>VID|IMG)-\d{8}-WA\d{4,}\./' -r -overwrite_original_in_place -progress '-AllDates<${filename;s/WA.*//} 12:00:00' .
Explanation:
-if 'not $CreateDate'
: Ensures only files without aCreateDate
are processed.-if '$filename =~ /^(?>VID|IMG)-\d{8}-WA\d{4,}\./'
: Targets WhatsApp media files named in the formatVID-YYYYMMDD-WAXXXX
orIMG-YYYYMMDD-WAXXXX
.-r
: Recursively processes all files in the specified folder.-overwrite_original_in_place
: Updates files directly without creating backup copies.-progress
: Displays progress for better monitoring.'-AllDates<${filename;s/WA.*//} 12:00:00'
: Extracts the date from the filename and sets it as theAllDates
metadata, appending a fixed time (12:00:00
) for consistency.
Background and Use Case:
WhatsApp-received media often lacks proper EXIF metadata for the creation date, which can lead to incorrect grouping when imported into platforms like Immich. This command extracts the date embedded in the filename (representing the download date) and injects it into the file's metadata, ensuring accurate sorting on a timeline.
Caveats:
- Always back up your files before modifying metadata to prevent accidental loss or corruption.
- Ensure
exiftool
is installed and updated on your system. - Stop Immich containers before running the script to avoid file conflicts. Restart the containers afterward and rerun the \"Extract Metadata\" job for proper sorting.
Conclusion
These commands provide powerful tools for managing media files efficiently. With optimizations and error handling, they can handle large datasets with reliability and speed.
Keywords
- Media File Management
- Linux Media Automation
- ffmpeg Timelapse
- exiftool Picture Organization
Tags
- Linux
- ffmpeg
- exiftool
- Media Management