-
Notifications
You must be signed in to change notification settings - Fork 34
/
webp2png.pl
103 lines (76 loc) · 2.04 KB
/
webp2png.pl
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
95
96
97
98
99
100
101
102
103
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 10 April 2021
# https://github.com/trizen
# Convert WEBP images to PNG, using the `dwebp` tool from "libwebp".
# The original WEBP files are deleted.
use 5.036;
use File::Find qw(find);
use Getopt::Long qw(GetOptions);
my $dwebp_cmd = "dwebp"; # `dwebp` command
my $use_exiftool = 0; # true to use `exiftool` instead of `File::MimeInfo::Magic`
`$dwebp_cmd -h`
or die "Error: `$dwebp_cmd` tool from 'libwebp' is not installed!\n";
sub webp2png ($file) {
my $orig_file = $file;
my $png_file = $file;
if ($png_file =~ s/\.webp\z/.png/i) {
## ok
}
else {
$png_file .= '.png';
}
if (-e $png_file) {
warn "[!] File <<$png_file>> already exists...\n";
next;
}
system($dwebp_cmd, $orig_file, '-o', $png_file);
if ($? == 0 and (-e $png_file) and ($png_file ne $orig_file)) {
unlink($orig_file);
}
else {
return;
}
return 1;
}
sub determine_mime_type ($file) {
if ($file =~ /\.webp\z/i) {
return "image/webp";
}
if ($use_exiftool) {
my $res = `exiftool \Q$file\E`;
$? == 0 or return;
defined($res) or return;
if ($res =~ m{^MIME\s+Type\s*:\s*(\S+)}mi) {
return $1;
}
return;
}
require File::MimeInfo::Magic;
File::MimeInfo::Magic::magic($file);
}
my %types = (
'image/webp' => {
call => \&webp2png,
}
);
GetOptions('exiftool!' => \$use_exiftool,)
or die "Error in command-line arguments!";
@ARGV or die <<"USAGE";
usage: perl $0 [options] [dirs | files]
options:
--exiftool : use `exiftool` to determine the MIME type (default: $use_exiftool)
USAGE
find(
{
no_chdir => 1,
wanted => sub {
(-f $_) || return;
my $type = determine_mime_type($_) // return;
if (exists $types{$type}) {
$types{$type}{call}->($_);
}
}
} => @ARGV
);
say ":: Done!";