Preserve File Dates with FFmpeg on Linux
When converting media files using FFmpeg on Linux, the operating system automatically assigns the current timestamp as the new file’s creation and modification date. To preserve the original timeline of your media library, you must extract the original timestamps before conversion and reapply them to the output file. This article provides a straightforward, actionable guide using standard Linux command-line tools and FFmpeg to keep your file metadata intact during conversion.
The Challenge with FFmpeg and File Timestamps
By default, FFmpeg focuses entirely on the data streams inside the container (video, audio, and subtitle tracks). When it writes a new output file, the Linux kernel treats it as a brand-new entity, stamping it with the current system time.
For users organizing historical archives, family videos, or sorted
photo/video directories, this behavior destroys the chronological order
of the files. Because Linux filesystems handle “creation time” (birth
time or btime) strictly, the most reliable way to maintain
your timeline is to copy the modification time
(mtime), which most media players and file
managers use for sorting.
Method 1: The Touch Command Shortcut (Recommended)
The easiest way to preserve the file date is to use the standard
Linux touch command with the -r (reference)
flag immediately after running your FFmpeg command. This reads the
timestamps of the original file and applies them directly to the new
file.
ffmpeg -i input.mp4 -vcodec libx264 -acodec aac output.mp4 && touch -r input.mp4 output.mp4In this command:
ffmpeg -i input.mp4 ... output.mp4performs the actual video conversion.&&ensures that the second command only runs if the FFmpeg conversion finishes successfully.touch -r input.mp4 output.mp4takes the timestamp frominput.mp4and forcesoutput.mp4to match it exactly.
Method 2: Automation with a Bash Script
If you have a large folder of videos to convert, applying the
touch command manually to every single file is inefficient.
You can automate the entire conversion and date-preservation process
using a simple Bash for loop.
for file in *.wmv; do
if [ -f "$file" ]; then
# Define the output name
output="${file%.wmv}.mp4"
# Convert the file
ffmpeg -i "$file" -c:v libx264 -c:a aac "$output"
# Copy the original timestamp to the new file
touch -r "$file" "$output"
fi
doneThis script loops through every .wmv file in your
current directory, converts it to an .mp4 format, and
immediately restores the original file date to the newly created file
before moving on to the next item.
A Note on Internal Metadata vs. Filesystem Dates
It is important to distinguish between filesystem
dates (when the file was written to the hard drive) and
internal metadata tags (like the
creation_time embedded inside MP4 or MKV headers).
If you want FFmpeg to copy the internal global metadata tags from the
source container to the destination container, add the
-map_metadata flag to your FFmpeg command:
ffmpeg -i input.mp4 -map_metadata 0 -c:v libx264 -c:a aac output.mp4 && touch -r input.mp4 output.mp4Using -map_metadata 0 tells FFmpeg to copy all metadata
from the first input file (file 0) into the output file. Combining this
internal mapping flag with the external touch -r command
ensures that both your Linux filesystem sorting and your media player’s
internal details remain perfectly accurate.