Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rename video files with a ISO date prefix
#!/usr/bin/perl -w
# Rename video files with a ISO date prefix, e.g.
# rename from P12345.MOV to 2018-01-30.P12345.MOV
# Tested with MTS, 3GP and MOV files.
#
# (c) 2018 Yrjö Kari-Koskinen <ykk@peruna.fi>
# Licensed with the MIT License
use Image::ExifTool qw(:Public);
my @dateTags = ('CreateDate', 'MediaCreateDate', 'DateTimeOriginal');
if (@ARGV == 0) {
die("Usage: $0 files...\n");
}
foreach (@ARGV) {
my $filename = $_;
my $date = getCreateDate($filename);
if ($date =~ /([0-9]{4}):([0-9]{2}):([0-9]{2}) /) {
my $newname = "$1-$2-$3.$filename";
if (-e $newname) {
print("$newname exists. Skpping file.\n");
} else {
print("Renaming $filename to $newname\n");
rename($filename, $newname) or die("Failed to rename $filename to $newname");
}
}
}
sub getCreateDate {
my ($filename) = @_;
$info = ImageInfo($filename);
foreach(@dateTags) {
if ($info->{$_}) {
return $info->{$_};
}
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment