$ carton install
$ curl -L https://github.com/yoheimuta/mobile-videoplayer.js/blob/b5b7473bc2afcae487203dd64080272c182eaaa6/src/assets/tamagotchi_4u.mp4?raw=true > target.mp4
$ carton exec -- perl ./convert.pl --file tamagotchi_4u.mp4
use strict; | |
use warnings; | |
use File::Basename qw/basename/; | |
use POSIX qw(strftime); | |
use Getopt::Long qw/:config posix_default no_ignore_case bundling auto_help/; | |
use FFmpeg::Command; | |
use Image::Magick; | |
GetOptions( | |
\my %opt, qw/ | |
file|f=s | |
/ | |
) or die "args error"; | |
my $input_file = $opt{file}; | |
my $output_base = sub { | |
(my $base = basename $input_file) =~ s/\.[^.]+$//; | |
my $today = strftime "%Y%m%d%H%M%S", localtime; | |
return "${today}_${base}"; | |
}->(); | |
convert_mp4_to_seqpngs($input_file, $output_base); | |
convert_seqpngs_to_gif($output_base); | |
unlink glob "$output_base/*"; | |
rmdir $output_base; | |
sub convert_mp4_to_seqpngs { | |
my ($input_file, $output_base) = @_; | |
mkdir $output_base, 0755 or die "cannot make directory: $!"; | |
my $output_file = "$output_base/%04d.png"; | |
my $ffmpeg = FFmpeg::Command->new('/usr/local/bin/ffmpeg'); | |
$ffmpeg->input_options({ | |
file => $input_file, | |
}); | |
$ffmpeg->timeout(300); | |
$ffmpeg->output_file($output_file); | |
$ffmpeg->options( | |
'-an', | |
'-r' => '12', | |
'-s' => '320x180', | |
); | |
my $result = $ffmpeg->exec(); | |
die $ffmpeg->errstr unless $result; | |
} | |
sub convert_seqpngs_to_gif { | |
my $output_base = shift; | |
my $gif_input_file = "$output_base/*.png"; | |
my $gif_output_file = "$output_base.gif"; | |
my $image = Image::Magick->new; | |
my $read_res = $image->Read($gif_input_file); | |
die $read_res if $read_res; | |
my $write_res = $image->Write($gif_output_file); | |
die $write_res if $write_res; | |
} |
requires 'FFmpeg::Command'; | |
requires 'Image::Magick'; |