Rename video files to filename with recorded date using mediainfo

Written by - 4 comments

Published on January 16th 2017 - 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 source 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.


Add a comment

Show form to leave a comment

Comments (newest first)

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" ???