Rename video files to filename with recorded date using mediainfo

Written by - 6 comments

Published on - last updated on January 11th 2023 - Listed in Personal Linux Multimedia


In my family we use videos - of course - to document our kids growing up. Compared to our parents in the 80's there isn't just one video camera available, nowadays we have many cams everywhere we look. Especially the cameras on mobile phones are handy to shoot some films. 

The problem with several video sources is however, that each video camera has its own file naming (let alone the video and audio encoding). I found it especially annoying when I wanted to sort the videos according to their recording date. Android in particular has the problem that you cannot use the "modified date" of the video file to use as recorded date. Example: When you move all your photos and videos from the internal to the external SD card, the modified timestamp changes and therefore they all have the same date.

Luckily video files have meta data containing a lot of information about the used encoding and the recorded date! This information can be retrieved using"mediainfo:

# mediainfo MOV_1259.mp4
General
Complete name                            : MOV_1259.mp4
Format                                   : MPEG-4
Format profile                           : Base Media / Version 2
Codec ID                                 : mp42
File size                                : 53.7 MiB
Duration                                 : 25s 349ms
Overall bit rate                         : 17.8 Mbps
Encoded date                             : UTC 2015-11-21 10:04:28
Tagged date                              : UTC 2015-11-21 10:04:28

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4.0
Format settings, CABAC                   : Yes
Format settings, ReFrames                : 1 frame
Format settings, GOP                     : M=1, N=18
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 25s 349ms
Bit rate                                 : 17.5 Mbps
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Variable
Frame rate                               : 29.970 fps
Minimum frame rate                       : 29.811 fps
Maximum frame rate                       : 30.161 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.281
Stream size                              : 52.8 MiB (98%)
Title                                    : VideoHandle
Language                                 : English
Encoded date                             : UTC 2015-11-21 10:04:28
Tagged date                              : UTC 2015-11-21 10:04:28

Audio
ID                                       : 2
Format                                   : AAC
Format/Info                              : Advanced Audio Codec
Format profile                           : LC
Codec ID                                 : 40
Duration                                 : 25s 335ms
Duration_FirstFrame                      : 13ms
Bit rate mode                            : Constant
Bit rate                                 : 156 Kbps
Nominal bit rate                         : 96.0 Kbps
Channel(s)                               : 2 channels
Channel positions                        : Front: L R
Sampling rate                            : 48.0 KHz
Compression mode                         : Lossy
Stream size                              : 483 KiB (1%)
Title                                    : SoundHandle
Language                                 : English
Encoded date                             : UTC 2015-11-21 10:04:28
Tagged date                              : UTC 2015-11-21 10:04:28
mdhd_Duration                            : 25335

Told you there's a lot of meta information.

What I needed in this case was the line "Encoded date" - the day and time the video was encoded/recorded. Using this information, I am able to rename all the video files.

Simulation first:

# ls | grep "^MOV" | while read line; do targetname=$(mediainfo $line | grep "Encoded date" | sort -u | awk '{print $5"-"$6}'); echo "Old name: $line, new name: ${targetname}.mp4"; done
Old name: MOV_0323.mp4, new name: 2015-03-08-17:24:27.mp4
Old name: MOV_0324.mp4, new name: 2015-03-13-19:12:33.mp4
Old name: MOV_0325.mp4, new name: 2015-03-13-19:18:40.mp4
Old name: MOV_0329.mp4, new name: 2015-03-18-18:41:55.mp4
Old name: MOV_0355.mp4, new name: 2015-03-21-10:05:55.mp4
Old name: MOV_0369.mp4, new name: 2015-03-22-08:38:06.mp4
Old name: MOV_0370.mp4, new name: 2015-03-22-08:38:44.mp4
Old name: MOV_0371.mp4, new name: 2015-03-22-08:39:36.mp4
Old name: MOV_0372.mp4, new name: 2015-03-22-14:05:30.mp4
Old name: MOV_0374.mp4, new name: 2015-03-24-18:31:21.mp4
Old name: MOV_0375.mp4, new name: 2015-03-24-18:31:52.mp4
Old name: MOV_0392.mp4, new name: 2015-03-28-10:54:17.mp4
[...]

And final renaming:

# ls | grep "^MOV" | while read line; do targetname=$(mediainfo $line | grep "Encoded date" | sort -u | awk '{print $5"-"$6}'); echo "Old name: $line, new name: ${targetname}.mp4"; mv $line ${targetname}.mp4; done
Old name: MOV_0323.mp4, new name: 2015-03-08-17:24:27.mp4
Old name: MOV_0324.mp4, new name: 2015-03-13-19:12:33.mp4
Old name: MOV_0325.mp4, new name: 2015-03-13-19:18:40.mp4
Old name: MOV_0329.mp4, new name: 2015-03-18-18:41:55.mp4
Old name: MOV_0355.mp4, new name: 2015-03-21-10:05:55.mp4
Old name: MOV_0369.mp4, new name: 2015-03-22-08:38:06.mp4
Old name: MOV_0370.mp4, new name: 2015-03-22-08:38:44.mp4
Old name: MOV_0371.mp4, new name: 2015-03-22-08:39:36.mp4
Old name: MOV_0372.mp4, new name: 2015-03-22-14:05:30.mp4
Old name: MOV_0374.mp4, new name: 2015-03-24-18:31:21.mp4
Old name: MOV_0375.mp4, new name: 2015-03-24-18:31:52.mp4
Old name: MOV_0392.mp4, new name: 2015-03-28-10:54:17.mp4
[...]

Yes, looks good! With this approach I am now able to unify the file names of all different video sources and can save them according to their recorded (real) date.

Additional examples

Here are some other examples using different name formatting.

YYYY-MM-DD-Originalname.mp4

Original filename: VID_20211231_223131.mp4

New filename: 2021-12-31-VID_20211231_223131.mp4

Simulation:

$ ls | grep "^VID" | while read line; do targetname=$(mediainfo $line | grep "Encoded date" | sort -u | awk '{print $5}' | awk -F'-' '{print $1"-"$2"-"$3}'); echo "Old name: $line, new name: ${targetname}-${line}"; echo mv $line ${targetname}-${line}; done
Old name: VID_20211220_133228.mp4, new name: 2021-12-20-VID_20211220_133228.mp4
mv VID_20211220_133228.mp4 2021-12-20-VID_20211220_133228.mp4
Old name: VID_20211220_134100.mp4, new name: 2021-12-20-VID_20211220_134100.mp4
mv VID_20211220_134100.mp4 2021-12-20-VID_20211220_134100.mp4
Old name: VID_20211231_223131.mp4, new name: 2021-12-31-VID_20211231_223131.mp4
mv VID_20211231_223131.mp4 2021-12-31-VID_20211231_223131.mp4

Final renaming:

$ ls | grep "^VID" | while read line; do targetname=$(mediainfo $line | grep "Encoded date" | sort -u | awk '{print $5}' | awk -F'-' '{print $1"-"$2"-"$3}'); echo "Old name: $line, new name: ${targetname}-${line}"; mv $line ${targetname}-${line}; done
Old name: VID_20211220_133228.mp4, new name: 2021-12-20-VID_20211220_133228.mp4
Old name: VID_20211220_134100.mp4, new name: 2021-12-20-VID_20211220_134100.mp4
Old name: VID_20211231_223131.mp4, new name: 2021-12-31-VID_20211231_223131.mp4

MM-DD-YYYY-Originalname.mp4

Original filename: VID_20200112_131054.mp4

New filename: 01-12-2020-VID_20200112_131054.mp4

Simulation:

$ ls | grep "^VID" | while read line; do targetname=$(mediainfo $line | grep "Encoded date" | sort -u | awk '{print $5}' | awk -F'-' '{print $2"-"$3"-"$1}'); echo "Old name: $line, new name: ${targetname}-${line}"; echo mv $line ${targetname}-${line}; done
Old name: VID_20200112_131054.mp4, new name: 01-12-2020-VID_20200112_131054.mp4
mv VID_20200112_131054.mp4 01-12-2020-VID_20200112_131054.mp4
Old name: VID_20200112_131734.mp4, new name: 01-12-2020-VID_20200112_131734.mp4
mv VID_20200112_131734.mp4 01-12-2020-VID_20200112_131734.mp4
Old name: VID_20200112_132806.mp4, new name: 01-12-2020-VID_20200112_132806.mp4

Final renaming:

$ ls | grep "^VID" | while read line; do targetname=$(mediainfo $line | grep "Encoded date" | sort -u | awk '{print $5}' | awk -F'-' '{print $2"-"$3"-"$1}'); echo "Old name: $line, new name: ${targetname}-${line}"; mv $line ${targetname}-${line}; done
Old name: VID_20200112_131054.mp4, new name: 01-12-2020-VID_20200112_131054.mp4
Old name: VID_20200112_131734.mp4, new name: 01-12-2020-VID_20200112_131734.mp4
Old name: VID_20200112_132806.mp4, new name: 01-12-2020-VID_20200112_132806.mp4



Add a comment

Show form to leave a comment

Comments (newest first)

ck from Switzerland wrote on Jul 14th, 2020:

Jim, that is why it is important to run the simulation first ;-). In this case you should adapt the renaming to include the original file name. Example:

ls | grep "^MOV" | while read line; do targetname=$(mediainfo $line | grep "Encoded date" | sort -u | awk '{print $5"-"$6}'); echo "Old name: $line, new name: ${targetname}.mp4"; mv $line ${targetname}-${line}.mp4; done


Jim from wrote on Jul 14th, 2020:

This is dangerous, if any of the files share the same details it will overwrite them without asking, deleting them.

I used this on a folder of 20 gopro videos and it deleted most of them leaving just 4.


BIll from wrote on Nov 21st, 2018:

THANKS! This worked great. My original files weren't named MOV*.* files, so In my case I wanted to rename all the *.MOV videos in my active directory, I used this:
ls | while read line; do targetname=$(mediainfo $line | grep "Encoded date" | sort -u | awk '{print $5"-"$6}'); echo "Old name: $line, new name: ${targetname}.MOV"; mv $line ${targetname}.MOV; done


ck from Switzerland wrote on Nov 3rd, 2018:

In case you want another date format, in this case MM-DD-YYYY, use this:

ls | grep "^MOV" | while read line; do targetname=$(mediainfo $line | grep "Encoded date" | sort -u | awk '{print $5}' | awk -F'-' '{print $2"-"$3"-"$1}'); echo "Old name: $line, new name: ${targetname}.mp4"; mv $line ${targetname}.mp4; done


ck from Switzerland wrote on Jul 3rd, 2018:

These are commands in Linux. It won't help you much on a Windows computer (maybe they'll work in Mac OS X, too).


Ed from Moscow wrote on Jul 3rd, 2018:

How you did it? What is "# ls | grep "^MOV" | while read line; do targetname=$(mediainfo $line | grep "Encoded date" | sort -u | awk '{print $5"-"$6}'); echo "Old name: $line, new name: ${targetname}.mp4"; mv $line ${targetname}.mp4; done" ???


RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   OpenSearch   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder