Skip to content

Instantly share code, notes, and snippets.

@ykarikos
Created January 28, 2018 11:47
Show Gist options
  • Save ykarikos/7582b80833f8857b4f212521197b9393 to your computer and use it in GitHub Desktop.
Save ykarikos/7582b80833f8857b4f212521197b9393 to your computer and use it in GitHub Desktop.
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 <[email protected]>
# 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