Skip to content

Instantly share code, notes, and snippets.

@unstabler
Created June 23, 2015 23:52
Show Gist options
  • Save unstabler/8b9c6772c0bde7ae5176 to your computer and use it in GitHub Desktop.
Save unstabler/8b9c6772c0bde7ae5176 to your computer and use it in GitHub Desktop.
SD 카드에 있는 flac 파일을 AAC(m4a)로 인코드 해주는 스크립트
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use File::Find;
local $| = 1;
my $src_ext = 'flac';
my $dest_ext = 'm4a';
my $ffmpeg_options = "-strict -2 -acodec aac -b:a 320k -f mp4";
my $sd_id = '8845-8DFB';
my $sd_path_format = '/run/media/cheesekun/%s/Music';
my $sd_path = sprintf $sd_path_format, $sd_id;
my $sd_retry_count_max = 5;
my $sd_retry_count = 0;
# SD 카드 검색
# 마운트 해제 잘못 하고 나면 8845-8DFB1, 8845-8DFB2.. 같은 식으로 마운트 되더라.. ㅠㅜ
while (1) {
last if -d $sd_path;
$sd_retry_count++;
die if $sd_retry_count >= $sd_retry_count_max;
warn "Cannot access to SD Card ($sd_path). retrying..";
$sd_path = sprintf $sd_path_format, $sd_id . $sd_retry_count;
}
finddepth(\&wanted, $sd_path);
say "syncing..";
system 'sync';
sub chomp_ext ($) { shift; s/^(.+)\.(.+?)$/$1/g; return ($_, $2) }
sub wanted {
my ($dir, $name, $fullpath) = ($File::Find::dir, $_, $File::Find::name);
my ($name_without_ext, $ext) = chomp_ext($name);
return if -d $fullpath || $ext ne $src_ext;
encode($dir, $name_without_ext, $ext);
}
sub encode {
my ($dir, $name, $ext) = @_;
my ($src, $dest) = map { sprintf "%s/%s.%s", $dir, $name, $_ } ($ext, $dest_ext);
printf "%s ... ", $src;
if (ffmpeg($src, $dest, $ffmpeg_options)) {
say "ok";
unlink $src;
} else {
say "failed";
}
}
sub ffmpeg {
my ($src, $dest, $options) = @_;
my $val = system sprintf "ffmpeg -y -i '%s' %s '%s' 2> /dev/null > /dev/null", $src, $options, $dest;
return !$val; # NOT 연산 => $val
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment