//AD  <- Needed to identify//
//--automatically built--
//--Project: convert_to_g1.js

	// --------------------------------------------------------------------------
	// Conversion configuration
	// --------------------------------------------------------------------------
	var config =
	{
		// Width of target screen
		screen_width: 480,
		// Height of target screen
		screen_height: 320,
		// Subtitles font
		sub_font_file: "c:\\Windows\\Fonts\\arialbd.ttf",
		// Subtitles font size
		sub_font_height: 16, // Optimized for viewing from a 40 cm distance
		// Subtitles charset
		sub_charset: "CP1250",
		// Result video variable bitrate
		video_bitrate: 300,
		// Result audio bitrate
		audio_bitrate: 96,
		// Mono or stereo
		audio_mono: false,
		// Fullscreen maximizes the video, whole screen area will be used for the movie, but
		// video will be cropped on the left and right side
		// If you want to see whole video, with black borders on top and bottom, set false
		full_screen: true,
		// In case of fullscreen, you can minimize the crop effect by specifying a number between 0.0 and 1.0,
		// 1.0 would mean that movie will cover whole screen
		// 0.8 would mean that movie will cover 80% (256px) of the screen with 10% (32px) black border on the top
		//     and 10% (32px) on the bottom. This value can be useful for 16:9 movies to lower the crop effect on sides.
		height_coverage: 1.0,
	}
	
	
	// --------------------------------------------------------------------------
	// End of conversion configuration
	// --------------------------------------------------------------------------

	// Get the video from the user
	var file_name_video = fileReadSelect();
	if( file_name_video )
	{
		var file_name_video_base = file_name_video.match( /((.*)[\.])[^\.]+$/ )[1];
		// Calculate subtitles file name
		var file_name_subtitles = file_name_video_base + "srt";

		// Initialize
		var app = new Avidemux();		
		// Load the video
		app.load( file_name_video );
		app.rebuildIndex();
		
		// displayInfo( "FPS: " + app.video.getFps1000() );

		var dim_video = {};
		dim_video.ratio	= app.video.getWidth() / app.video.getHeight();
		
		if( config.full_screen )
		{
			dim_video.height = Math.round( config.screen_height * config.height_coverage );
			dim_video.width = Math.floor( dim_video.ratio * dim_video.height / 2 ) * 2;
			// Calculate the crop value
			dim_video.crop = Math.max( 0, ( dim_video.width - config.screen_width ) / 2 );
		}
		else
		{
			dim_video.width = config.screen_width;
			dim_video.height = Math.round( dim_video.width / dim_video.ratio / 2 ) * 2;
			dim_video.crop = 0;
		}
		
		/*
		displayInfo
		(
			"Original video: " + app.video.getWidth() + " x " + app.video.getHeight() +
			"\n" +
			"Result video: " + dim_video.width + " x " + dim_video.height + ' cropped_width = ' + ( dim_video.width - (2*dim_video.crop) )
		);
		*/

		// Video Filters
		app.video.clearFilters();
		app.video.addFilter("mpresize","w="+dim_video.width,"h="+dim_video.height,"algo=0");
		app.video.addFilter("crop","left="+dim_video.crop,"right="+dim_video.crop,"top=0","bottom=0");
		if( file_name_subtitles )
		{
			var sub_baseline = dim_video.height - ( 3 * config.sub_font_height );
			app.video.addFilter("subtitle","_fontsize="+config.sub_font_height,"_subname="+file_name_subtitles,"_fontname="+config.sub_font_file,"_charset="+config.sub_charset,"_baseLine="+sub_baseline,"_Y_percent=178","_U_percent=43","_V_percent=-127","_selfAdjustable=1","_delay=0","_useBackgroundColor=0","_bg_Y_percent=0","_bg_U_percent=0","_bg_V_percent=0","_blend=1");
		}

		// Video codec
		app.video.codec("FFMpeg4","2PASSBITRATE="+config.video_bitrate,"160 05 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 1f 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 fe ff ff ff 01 00 00 00 fb ff ff ff cd cc 4c 3d 01 00 00 00 0a d7 23 3c 01 00 00 00 00 00 00 3f 00 00 00 3f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 40 1f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ");

		// Audio codec
		app.audio.reset();
		app.audio.codec("aac",config.audio_bitrate,4,"80 00 00 00 ");
		app.audio.normalizeMode=1;
		app.audio.normalizeValue=0;
		app.audio.delay=0;
		app.audio.mixer(config.audio_mono ? "MONO" : "STEREO");
		app.setContainer("MP4");

		app.save( file_name_video_base + "mp4" );
		setSuccess(1);

		//End of script
	}