-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdv-rename
executable file
·94 lines (72 loc) · 1.73 KB
/
dv-rename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env perl
#
# martin, 2020-07-22
#
# dv-rename renames .dv files using the 'Recorded date' tag
# returned by MediaInfo. Make sure the 'mediainfo' binary
# is in your path.
#
# Usage:
# ./dv-rename *.dv # dry run
# ./dv-rename -f *.dv # serious run
use strict;
use warnings;
use 5;
use File::Basename qw/basename dirname/;
use File::Copy qw/move/;
use File::Spec::Functions qw/catfile/;
use IPC::Open2 qw/open2/;
sub sanitize_recdate {
my $date = shift;
chomp $date;
# Replace spaces with a dot
$date =~ s/\s+/./g;
# Replace double colons with dashes
$date =~ s/:/-/g;
# Remove '.000' if present
$date =~ s/\.000\b//g;
return $date;
}
sub rename_file {
my $fn = shift;
my $mode = shift;
my $dry = 0;
if (defined $mode and $mode eq 'dry-run') {
$dry = 1;
}
my $fndir = dirname $fn;
my $fnbase = basename $fn;
my $pid = open2(my $child_out, my $child_in,
mediainfo => '--Output=General;%Recorded_Date%',
$fn);
my $recdate = sanitize_recdate(readline $child_out);
waitpid($pid, 0);
my $rc = $? >> 8;
if ($rc != 0) {
die "Fatal: mediainfo exited with status $rc\n";
}
my $newbase = "$recdate.dv";
my $newpath = catfile($fndir, $newbase);
print "[Rename:] $fn => $newpath\n";
if (-e $newpath) {
die "Fatal: $newpath already exists!\n";
}
if (!$dry) {
move($fn, $newpath);
}
}
sub main {
if ($ARGV[0] ne '-f') {
# Dry run mode
print "[Mode:] Using dry run mode to preview changes!\n";
my @files = @ARGV;
rename_file($_, 'dry-run') for @files;
print "This has been a dry run, use -f to commit.\n";
} else {
# Serious mode
shift @ARGV;
my @files = @ARGV;
rename_file($_) for @files;
}
}
main;