#!/usr/bin/perl

#########################################
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
##########################################
# 
#   mov2gp v 0.2
#   	by Ivaylo Mitov CMPTuOMP3 at gmail dot com
#
# Script for resizing movies for the GP2X game console or other handheld device.
#
# The script resizes and reencodes all movie files given to it.
# 
# Make sure you have mencoder installed (usually comes with mplayer).
# 
# First copy the script to /usr/bin for example and then
# $ chmod 755 /usr/bin/mov2gp
#
# Then you can use it like this:
#
# $ mov2gp /data/movies/*.avi
# 
# or
# 
# $ mov2gp *
#
# You will get new files in the folder of the movies in the format file_name.gp2x.avi.
# 
# The encoders used are: 
#	for the video:
#	-xvid
# 	-bitrate = 300kbps, 
# 	-fps 	= 25, 
# 	-size 	= 320x240 
# 	
# 	for the audio:
# 	-mp3lame
# 	-bitrate = 128kbps
#
# The script will process all files that appear to be movies 
# (defined in the @ext array) and skip all other files. 
# (You can freely change that array to define what is a movie and what is not).
#
##################################################

use strict;
use Term::ANSIColor qw(:constants);

unless (@ARGV) {
	print RED, "Give me movies to encode... pleaaase....\n", RESET;
	exit;
}

my @ext = qw(.avi .mpg .mpeg .flv .mov .rm .wmv .asf .mkv);

foreach (@ARGV) {
	my $allow = 0;
	my $in_file = $_;
	$in_file =~ s/(\..{2,4}$)//;
	foreach (@ext) { $allow = 1 if $_ eq lc($1); }
	my $out_file = $in_file . '.gp2x.avi';
	$in_file = $in_file . $1;
	if ($allow == 1) {
		print GREEN, BOLD, "="x50, "\nProcessing file $in_file...\n", "="x50, "\n", RESET;
		system("/usr/bin/mencoder \"$in_file\" -o \"$out_file\" -ovc xvid -xvidencopts bitrate=300 -vop scale=320:240 -ofps 25 -oac mp3lame -lameopts abr:br=128");
		print GREEN, BOLD, "="x50, "\nDone file $in_file.\n", "="x50, "\n", RESET;
	} else {
		print RED, BOLD, "="x50, "\n Skipping file $in_file, not a movie file.\n", "="x50, "\n", RESET;
	}
}
