handbrake판 동영상 인코딩 소스


#!/usr/bin/perl
use strict;
use File::Basename;
use File::Copy;

use constant SOURCE_DIR => "f:\\RECORD\\TS_ready\\";
use constant TARGET_DIR => "g:\\RECORD\\Converted\\";
use constant MOVE_DIR => "f:\\RECORD\\OLD\\";
use constant LOG_FILE => TARGET_DIR . "_convert.log";
# for test
#use constant ENCODER => "c:\\AutoMen6_beta_6\\applications\\mencoder.exe";
#use constant ENCODER => 'c:\mencoder\mencoder.exe';
# usally
# handbrake - START
use constant ENCODER => 'c:\Handbrake\HandBrakeCLI.exe';
# -b 3600 / 1650
# -q 23.4 / -q 23
use constant ENCODE_OPTION => '-f mp4 -4  -w 1280 -l 720 -e x264 --x264opts subq=1:global_header:threads=0:trellis=0:deblock:nocabac:level_idc=30 -b 3600 -r 29.97 -a 1 -E faac -6 stereo -R 48 -B 192 ';
use constant ENCODE_OPTION_SMALL => '-f mp4 -4  -w 960 -l 540 -e x264 --x264opts subq=1:global_header:threads=0:trellis=0:deblock:nocabac:level_idc=30 -b 1650 -r 29.97 -a 1 -E faac -6 stereo -R 48 -B 192 ';
# handbrake - END

# MENCODER - START
#use constant ENCODER => "c:\\AutoMen6_beta_4\\mencoder.exe";
#use constant ENCODE_OPTION => "-mc 1 -sws 2 -of lavf -lavfopts format=MP4 -ofps 29.970 -vf-add crop=1920:1080 -vf-add scale=1280:720 -vf-add expand=1280:720:-1:-1:1  -vf-add hqdn3d -vf-add harddup -ovc x264 -x264encopts subq=1:global_header:threads=2:trellis=0:deblock:nocabac:bitrate=3600:level_idc=30 -srate 48000 -oac faac -faacopts mpeg=4:object=2:raw:br=192 ";
#use constant ENCODE_OPTION_SMALL => "-mc 1 -sws 2 -of lavf -lavfopts format=MP4 -ofps 29.970 -vf-add crop=1920:1080 -vf-add scale=960:540 -vf-add expand=960:540:-1:-1:1  -vf-add hqdn3d -vf-add harddup -ovc x264 -x264encopts subq=1:global_header:threads=2:trellis=0:deblock:nocabac:bitrate=1650:level_idc=30 -srate 48000 -oac faac -faacopts mpeg=4:object=2:raw:br=192 ";
# MENCODER - END

use constant SLEEP_TIME => "60";
use constant STOP_TIME_START => 21;
use constant STOP_TIME_END => 8;

my @SMALL_MOVIE_LIST = ('뉴스 같은 방송 제목', '을 여기에 넣으면 됩니다');

my $flag = 1;
while($flag == 1) {
my @WORKING_LIST;
print "=== TS2MP4 Automatic Converting ===\n";
# check file list
print " -- check\n";
opendir (my $DIR, SOURCE_DIR) || die "Error in opening directory " . SOURCE_DIR . "\n";
while( (my $filename = readdir($DIR)) ){
   if ($filename =~ /\.ts$/) {
    # add to working array
    push(@WORKING_LIST, $filename);
    print " -- found! $filename\n";
   }
}
closedir($DIR);

# converting
my $count = 0;
open(my $log, ">> ". LOG_FILE) || die "Error in opening log file " . LOG_FILE . "\n";
foreach my $file (@WORKING_LIST) {
my $now = localtime time;
# time check
print " -- stop check ";
my $timeCheckFlag = 0;
while($timeCheckFlag == 1) {
$now = localtime time;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
if ($hour <= STOP_TIME_START && $hour >= STOP_TIME_END) {
$timeCheckFlag = 0;
}
else {
$| = 1;
print ".";
sleep(60*30);
}
}
print "\n";
$count++;
my $msg = sprintf(" -- [%02d/%02d] Now %s convert Start [%s]\n", $count, scalar(@WORKING_LIST), $file, $now);
print "\n\n" . $msg;
print $log $msg;
my $fullpath = SOURCE_DIR . $file;
my $target = TARGET_DIR . $file . ".mp4";
my $errorname = TARGET_DIR . $file . "_convert_ERROR.mp4";
my $tempname = TARGET_DIR . $file . "_convert.mp4";
   # check small movie files?
   my $option = ENCODE_OPTION;
   foreach my $name_pattern (@SMALL_MOVIE_LIST) {
    if ($file =~ m/$name_pattern/) {
    $option = ENCODE_OPTION_SMALL;
    #last;
    }
   }
   # MENCODER
   #my $cmd = sprintf("%s \"%s\" %s -o \"%s\"", ENCODER, $fullpath, $option, $tempname);
   # handbrake
my $cmd = sprintf("%s -i \"%s\" %s -o \"%s\"", ENCODER, $fullpath, $option, $tempname);

   # run
   #print "$cmd\n";
#print $log "$cmd\n";
my $result = system($cmd);
$now = localtime time;
my $msg = sprintf(" -- [%02d/%02d] Now %s convert End [%s]\n", $count, scalar(@WORKING_LIST), $file, $now);
print "\n\n" . $msg;
print $log $msg;
if ($result == 0) {
 # rename
 rename($tempname, $target);
}
else {
 # rename
 rename($tempname, $errorname);
}

# move file
my $movefile = MOVE_DIR . $file;
move($fullpath, $movefile);
}
print " -- Cycle end.\n";
#print $log " -- Cycle end.\n";
close($log);
sleep(SLEEP_TIME);
}




펄로 만든 코드입니다. 이번에는 handbrake판입니다. ^^

auto_convert.pl

by NuRi | 2011/07/09 21:12 | 잡동사니 창고 | 트랙백 | 덧글(0)

트랙백 주소 : http://nuridol.egloos.com/tb/3688668
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]

:         :

:

비공개 덧글