Perl로 짠 ts 파일 mp4 변환 소스

방송 프로그램을 녹화한 ts파일을 mp4로 인코딩하는 소스입니다.
잘 나오는 인코딩 프로그램을 쓰면 되겠지만 자동으로 변환한다는데 의미가 있는 겁니다. :D
취미로 하는 프로그래밍이라 이번엔 Perl로 짠 소스입니다.


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

use constant SOURCE_DIR => "i:\\RECORD\\TS_ready\\";
use constant TARGET_DIR => "g:\\RECORD\\Converted\\";
use constant MOVE_DIR => "i:\\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
use constant ENCODER => "c:\\AutoMen6_beta_4\\mencoder.exe";
use constant ENCODE_OPTION => "-mc 1 -sws 10 -of lavf -lavfopts format=MP4 -ofps 29.970 -vf-add crop=1920:1080 -vf-add scale=1440:810 -vf-add expand=1440:810:-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 10 -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 ";
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 $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;
    }
   }
my $cmd = sprintf("%s \"%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;
# rename
rename($tempname, $target);

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



다운로드 : auto_convert.pl

by NuRi | 2011/04/18 22:02 | 잡동사니 창고 | 트랙백 | 덧글(0)

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

:         :

:

비공개 덧글