Anri-chan/Source/sasami.pl

From SDA Knowledge Base

< Anri-chan‎ | Source
Revision as of 18:21, 19 September 2008 by Njahnke (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#!/usr/bin/perl
#nathan jahnke <njahnke@gmail.com>

use warnings;
use strict 'subs';
package sasami;

#system("export DYLD_LIBRARY_PATH=/Volumes/l/anrix");

sub sasami {
	@sources = ();
	open(AVS, "${basename}.avs");
	for (<AVS>) {
		push(@sources,"${1}.VOB") if m/^.*MPEG2source\("(.+)\.d2v".*$/;
		push(@sources,$1) if m/^.*avisource\("([^"]+)".*$/;
		push(@sources,$1) if m/^.*directshowsource\("([^"]+)".*$/;
		$fieldorder = 1 if m/^AssumeTFF\(\)/;
		$fieldorder = 0 if m/^AssumeBFF\(\)/;
		$d = 1 if m/^global d1 = true/;
		$d = 4 if m/^global d1 = false/;
		$f = $1 if m/^changefps\(f1\/([0-9])\)/;
		$vhs = 1 if m/^nate_vhs_head_change_erase/;
	}
	close(AVS);
	
	die 'sasami: no sources detected in .avs!' if @sources == 0;
	#get vital info about first source
	$mplayeridentify = <<SABRAC;
mplayer -identify -vo null -slave ${sources[0]} 2>&1 <<ENDOFSCRIPT
quit
ENDOFSCRIPT
SABRAC
	for (qx($mplayeridentify)) {
		chomp;
		$width = $_ if s/ID_VIDEO_WIDTH=([0-9]+)/$1/;
		$height = $_ if s/ID_VIDEO_HEIGHT=([0-9]+)/$1/;
		$framerate = $_ if s/ID_VIDEO_FPS=([0-9.]+)/$1/;
		$maxaudiobitrate = $_ if s/ID_AUDIO_BITRATE=([0-9]+)/$1/;
	}
	$pal = ($height eq "576");
	
	#build mencoder command to feed fifo
	$mencoderargs = "./mencoder";
	
	#note space at start of string
	$mencoderargs .= " -vf-add yadif=3 -vf-add mcdeint=1:${fieldorder}" if $d == 1;
	$mencoderargs .= " -vf-add tfields=0" if $d == 4;
	$mencoderargs .= " -vf-add crop=${width}:".($height-12).":0:0,expand=0:-12:0:0" if $vhs;
	
	$newframerate = ($pal ? (50/$f) : (59.94/$f));
	$x264newframerate = "--fps ${newframerate}";
	$mp4boxnewframerate = "-fps ${newframerate}";
	$mencoderargs .= " -vf-add framestep=${f}";
	
	$newwidth  = (($d == 1) ? ($pal ? 704 : 640) : ($pal ? 352 : 320));
	$newheight = (($d == 1) ? ($pal ? 576 : 480) : ($pal ? 288 : 240));
	$mencoderargs .= " -vf-add scale=${newwidth}:${newheight}";
	
	$mencoderargs .= " -vf-add format=i420 -ovc raw -of rawvideo -nosound";
	
	$mencoderargs .= " ".join(" ",@sources);
	$mencoderargs .= " -o ${avs}";
}

$basename = 'test';
$avs = "${basename}.pipe";
for(my $N = 1; $N le 2 ; $N++) {
	system("rm ${avs}");
	system("mkfifo ${avs}");
	# this is a hack to deal with the fact that sometimes x264 takes a while to finish encoding and write out the statsfile
	if ($N eq 2) {
		while (!(-e "${basename}.stats")) {
			print("Waiting for stats file ...\n");
			sleep(1);
		}
	}
	&sasami;# if $os eq 'unix';
	print("$mencoderargs\n");
	$x264command = "./x264 --pass $N --stats ${basename}.stats --bitrate 5000 --qpmin 19 ${x264newframerate} -o ${basename}.264 ${avs} ${newwidth}x${newheight} &";
	print("$x264command\n");
	system($x264command);
	system($mencoderargs);# if $os eq 'unix';
	#rm -f "$avs"
}
$mp4boxcommand = "./mp4box ${mp4boxnewframerate} -new -add ${basename}.264 ${basename}.mp4";
print ("$mp4boxcommand\n");
system($mp4boxcommand);

exit 0;
Personal tools