Difference between revisions of "Anri-chan/Source/sasami.pl"

From SDA Knowledge Base

Jump to: navigation, search
(getting closer ... mplayer's crop and expand are the very definition of hard to use)
(gba/gb in; who knew retard bob was built into mencoder? grenola!)
Line 8: Line 8:
  
 
#system("export DYLD_LIBRARY_PATH=/Volumes/l/anrix");
 
#system("export DYLD_LIBRARY_PATH=/Volumes/l/anrix");
 +
 +
#to move to anri config file later
 +
$gba_crop_left = 43;
 +
$gba_crop_top = 39;
 +
$gba_crop_right = 44;
 +
$gba_crop_bottom = 41;
 +
 +
$gb_crop_left = 80;
 +
$gb_crop_top = 48;
 +
$gb_crop_right = 80;
 +
$gb_crop_bottom = 48;
  
 
sub sasami {
 
sub sasami {
open(AVS, "${basename}.avs");
+
open(AVS, "${basename}.avs"); #must be unix text when running sasami under unix ...
 
@avs = <AVS>;
 
@avs = <AVS>;
 
close(AVS);
 
close(AVS);
Line 24: Line 35:
 
$d = 4 if m/^global d1 = false/;
 
$d = 4 if m/^global d1 = false/;
 
$f = $1 if m/^changefps\(f1\/([0-9])\)/;
 
$f = $1 if m/^changefps\(f1\/([0-9])\)/;
 +
$deflicker = 1 if m/^nate_retard_bob_2/;
 
$vhs = 1 if m/^nate_vhs_head_change_erase/;
 
$vhs = 1 if m/^nate_vhs_head_change_erase/;
 
$onepixel = 1 if m/^nate_1_pixel_bob_fix/;
 
$onepixel = 1 if m/^nate_1_pixel_bob_fix/;
$nes = 1 if m/^nate_nes/;
+
$nes = 1 if m/^nate_nes$/;
 
if (m/trim\(([0-9]+),([0-9]+)\)/) {
 
if (m/trim\(([0-9]+),([0-9]+)\)/) {
trimone=$1;
+
$trimone=$1;
trimtwo=$2;
+
$trimtwo=$2;
 
}
 
}
 +
$gba = 1 if m/^nate_gba$/;
 +
$gb = 1 if m/^nate_gb$/;
 +
$resize = 1 if m/^lanczos4resize\(d1,last\.height\)/;
 
}
 
}
 
 
Line 47: Line 62:
 
$maxaudiobitrate = $_ if s/ID_AUDIO_BITRATE=([0-9]+)/$1/;
 
$maxaudiobitrate = $_ if s/ID_AUDIO_BITRATE=([0-9]+)/$1/;
 
}
 
}
 +
# print "$width\n";
 +
# print "$height\n";
 +
# print "$framerate\n";
 +
# print "$maxaudiobitrate\n";
 
$pal = ($height eq "576");
 
$pal = ($height eq "576");
 
 
Line 55: Line 74:
 
# $framerate = 29.97;
 
# $framerate = 29.97;
 
# $framerate = 25 if $pal;
 
# $framerate = 25 if $pal;
$mencoderargs .= " -ss ".($trimone * $framerate);
+
$mencoderargs .= " -ss ".($trimone * $framerate) if $trimone;
$mencoderargs .= " -endpos ".($trimtwo * $framerate);
+
$mencoderargs .= " -endpos ".($trimtwo * $framerate) if $trimtwo;
 
 
 
#one pixel bob
 
#one pixel bob
Line 68: Line 87:
 
$mencoderargs .= " -vf-add crop=".($width-8).":${height}:8:0,expand=${width}:${height}:8:0" if $nes;
 
$mencoderargs .= " -vf-add crop=".($width-8).":${height}:8:0,expand=${width}:${height}:8:0" if $nes;
 
#-af channels=6:4:0:0:0:1:0:2:0:3 media.avi
 
#-af channels=6:4:0:0:0:1:0:2:0:3 media.avi
        #Would change the number of channels to 6 and set up 4 routes that copy channel 0 to channels 0 to 3.  Channel 4 and 5 will contain silence.
+
#Would change the number of channels to 6 and set up 4 routes that copy channel 0 to channels 0 to 3.  Channel 4 and 5 will contain silence.
 
 
#note space at start of string
 
 
$mencoderargs .= " -vf-add yadif=3 -vf-add mcdeint=1:${fieldorder}" if $d == 1;
 
$mencoderargs .= " -vf-add yadif=3 -vf-add mcdeint=1:${fieldorder}" if $d == 1;
$mencoderargs .= " -vf-add tfields=0" if $d == 4;
+
$tfields = 0;
$mencoderargs .= " -vf-add crop=${width}:".($height-12).":0:0,expand=0:-12:0:0" if $vhs;
+
$tfields = 4 if $deflicker;
 +
$mencoderargs .= " -vf-add tfields=${tfields}" if $d == 4;
 
 
# function nate_retard_bob_2(last) {
+
#i just eyeballed this - someone will need to sync the avisynth and mencoder sharpen filters
# converttorgb32
+
$mencoderargs .= " -vf-add unsharp=l3x3:0.6:c3x3:0.6" if $deflicker;
# field1=SelectEven.PointResize(last.width,last.height*4).crop(0,1,0,0).addborders(0,0,0,1).bilinearresize(d1,last.height)
+
# field2=SelectOdd.PointResize(last.width,last.height*4).crop(0,0,0,-1).addborders(0,1,0,0).bilinearresize(d1,last.height)
+
# interleave(field1,field2)
+
# sharpen(0.31)
+
# converttoyv12
+
# }
+
  
# function nate_gba(last) {
+
$mencoderargs .= " -vf-add crop=${width}:".($height-12).":0:0,expand=0:-12:0:0" if $vhs;
# converttorgb
+
# lanczos4resize(320,240)
+
if ($gba) {
# crop(43,39,-44,-41)
+
$mencoderargs .= " -vf-add scale=320:240";
# lanczos4resize(240,160)
+
$mencoderargs .= " -vf-add crop=".(320-$gba_crop_right-$gba_crop_left).":".(240-$gba_crop_bottom-$gba_crop_top).":${gba_crop_left}:${gba_crop_top}";
# converttoyv12
+
$mencoderargs .= " -vf-add scale=240:160";
# }
+
$newwidth = 240;
#
+
$newheight = 160;
# function nate_gb(last) {
+
} elsif ($gb) {
# lanczos4resize(320,240)
+
$mencoderargs .= " -vf-add scale=320:240";
# Crop(80,48,-80,-48) #thanks Quijo
+
$mencoderargs .= " -vf-add crop=".(320-$gb_crop_right-$gb_crop_left).":".(240-$gb_crop_bottom-$gb_crop_top).":${gb_crop_left}:${gb_crop_top}";
# }
+
$newwidth = (320-$gb_crop_right-$gb_crop_left);
 +
$newheight = (240-$gb_crop_bottom-$gb_crop_top);
 +
}
  
 
$newframerate = ($pal ? (50/$f) : (59.94/$f));
 
$newframerate = ($pal ? (50/$f) : (59.94/$f));
Line 102: Line 117:
 
$mencoderargs .= " -vf-add framestep=${f}";
 
$mencoderargs .= " -vf-add framestep=${f}";
 
 
$newwidth  = (($d == 1) ? ($pal ? 704 : 640) : ($pal ? 352 : 320));
+
$newwidth  = (($d == 1) ? ($pal ? 704 : 640) : ($pal ? 352 : 320)) unless $newwidth;
$newheight = (($d == 1) ? ($pal ? 576 : 480) : ($pal ? 288 : 240));
+
$newheight = (($d == 1) ? ($pal ? 576 : 480) : ($pal ? 288 : 240)) unless $newheight;
$mencoderargs .= " -vf-add scale=${newwidth}:${newheight}";
+
$mencoderargs .= " -vf-add scale=${newwidth}:${newheight}" if $resize;
 
 
 
$mencoderargs .= " -vf-add format=i420 -ovc raw -of rawvideo -nosound";
 
$mencoderargs .= " -vf-add format=i420 -ovc raw -of rawvideo -nosound";
Line 112: Line 127:
 
}
 
}
  
$basename = 'pso';
+
$basename = 'pokemon';
 
$avs = "${basename}.pipe";
 
$avs = "${basename}.pipe";
 
for(my $N = 1; $N le 2 ; $N++) {
 
for(my $N = 1; $N le 2 ; $N++) {

Revision as of 00:00, 4 October 2008

#!/usr/bin/perl
#nathan jahnke <njahnke@gmail.com>

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

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

#to move to anri config file later
$gba_crop_left = 43;
$gba_crop_top = 39;
$gba_crop_right = 44;
$gba_crop_bottom = 41;

$gb_crop_left = 80;
$gb_crop_top = 48;
$gb_crop_right = 80;
$gb_crop_bottom = 48;

sub sasami {
	open(AVS, "${basename}.avs"); #must be unix text when running sasami under unix ...
	@avs = <AVS>;
	close(AVS);
	@sources = ();
	for (@avs) {
		chomp;
		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])\)/;
		$deflicker = 1 if m/^nate_retard_bob_2/;
		$vhs = 1 if m/^nate_vhs_head_change_erase/;
		$onepixel = 1 if m/^nate_1_pixel_bob_fix/;
		$nes = 1 if m/^nate_nes$/;
		if (m/trim\(([0-9]+),([0-9]+)\)/) {
			$trimone=$1;
			$trimtwo=$2;
		}
		$gba = 1 if m/^nate_gba$/;
		$gb = 1 if m/^nate_gb$/;
		$resize = 1 if m/^lanczos4resize\(d1,last\.height\)/;
	}
	
	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/;
	}
# 	print "$width\n";
# 	print "$height\n";
# 	print "$framerate\n";
# 	print "$maxaudiobitrate\n";
	$pal = ($height eq "576");
	
	#build mencoder command to feed fifo
	$mencoderargs = "./mencoder";
	
	#trimming
# 	$framerate = 29.97;
# 	$framerate = 25 if $pal;
	$mencoderargs .= " -ss ".($trimone * $framerate) if $trimone;
	$mencoderargs .= " -endpos ".($trimtwo * $framerate) if $trimtwo;
	
	#one pixel bob
	if ($onepixel) {
		$mencoderargs .= " -vf-add crop=${width}:".($height-1).":0:0,expand=${width}:${height}:0:1";
		$fieldorder = 0 if $fieldorder == 1;
		$fieldorder = 1 if $fieldorder == 0;
	}
	
	#nes
	$mencoderargs .= " -vf-add crop=".($width-8).":${height}:8:0,expand=${width}:${height}:8:0" if $nes;
	#-af channels=6:4:0:0:0:1:0:2:0:3 media.avi
	#Would change the number of channels to 6 and set up 4 routes that copy channel 0 to channels 0 to 3.  Channel 4 and 5 will contain silence.
	
	$mencoderargs .= " -vf-add yadif=3 -vf-add mcdeint=1:${fieldorder}" if $d == 1;
	$tfields = 0;
	$tfields = 4 if $deflicker;
	$mencoderargs .= " -vf-add tfields=${tfields}" if $d == 4;
	
	#i just eyeballed this - someone will need to sync the avisynth and mencoder sharpen filters
	$mencoderargs .= " -vf-add unsharp=l3x3:0.6:c3x3:0.6" if $deflicker;

	$mencoderargs .= " -vf-add crop=${width}:".($height-12).":0:0,expand=0:-12:0:0" if $vhs;
	
	if ($gba) {
		$mencoderargs .= " -vf-add scale=320:240";
		$mencoderargs .= " -vf-add crop=".(320-$gba_crop_right-$gba_crop_left).":".(240-$gba_crop_bottom-$gba_crop_top).":${gba_crop_left}:${gba_crop_top}";
		$mencoderargs .= " -vf-add scale=240:160";
		$newwidth = 240;
		$newheight = 160;
	} elsif ($gb) {
		$mencoderargs .= " -vf-add scale=320:240";
		$mencoderargs .= " -vf-add crop=".(320-$gb_crop_right-$gb_crop_left).":".(240-$gb_crop_bottom-$gb_crop_top).":${gb_crop_left}:${gb_crop_top}";
		$newwidth = (320-$gb_crop_right-$gb_crop_left);
		$newheight = (240-$gb_crop_bottom-$gb_crop_top);
	}

	$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)) unless $newwidth;
	$newheight = (($d == 1) ? ($pal ? 576 : 480) : ($pal ? 288 : 240)) unless $newheight;
	$mencoderargs .= " -vf-add scale=${newwidth}:${newheight}" if $resize;
	
	$mencoderargs .= " -vf-add format=i420 -ovc raw -of rawvideo -nosound";
	
	$mencoderargs .= " ".join(" ",@sources);
	$mencoderargs .= " -o ${avs}";
}

$basename = 'pokemon';
$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