Video convert script using ffmpeg

Discuss and share scripts and script files...
Post Reply
highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Video convert script using ffmpeg

Post by highend »

I was asked if I could share this script so here it is.

Nothing special in it, I just need it for converting various video files to play them on my sat receiver (which accepts not a broad range of formats but mainly mpg compliant dvd files.

Be careful, a lot of folder constructions are suited to _MY_ needs and my way of organizing things in the file system.

Code: Select all

// 26.06.2011, use ffmpeg to convert video files to dvd-compliant .mpg
// setting('BackgroundFileOps', 0);

"Convert video files to...|<xypath>\.Icons\VideoConvert.ico|1"
	;

"... 4:3 DVD-MPEG"
	sub "_Initialize";

	global $FileExtNew, $fopts, $gopts;

	$FileExtNew = "mpg";
	$gopts = "-i";
	$fopts = "-threads 4 -bufsize 102400k -target pal-dvd -pass 1 -b 2000k -aspect 4:3 -loglevel quiet";

	sub "_WriteBatchFile";

	status "Video convert finished successfully";

"... 16:9 DVD-MPEG"
	sub "_Initialize";

	global $FileExtNew, $fopts, $gopts;

	$FileExtNew = "mpg";
	$gopts = "-i";
	$fopts = "-threads 4 -bufsize 102400k -target pal-dvd -pass 1 -b 2000k -aspect 16:9 -loglevel quiet";

	sub "_WriteBatchFile";

	status "Video convert finished successfully";

"... 16:9 DVD-MPEG (select streams)"
	sub "_Initialize";

	global $FileExtNew, $fopts, $gopts;
	global $StreamOpts;

	$FileExtNew = "mpg";
	$gopts = "-i";
	// -map 0:x (0:0 = video stream!)
	$StreamOpts = input("Select streams", "The video stream is normally at #0:0, audio streams begin with #0:1. If you only want to get a preview, add -t <xy> seconds at the end.", "-map 0:0 -map 0:");
	$StreamOpts = $StreamOpts . chr(32);
	$fopts = $StreamOpts . "-threads 4 -bufsize 102400k -target pal-dvd -pass 1 -b 2000k -aspect 16:9 -loglevel quiet";

	sub "_WriteBatchFile";

	status "Video convert finished successfully";

-

"Analyze only|<xypath>\.Icons\VideoConvert_Analyze.ico"
	sub "_Initialize";

	global $FileExtNew, $fopts, $gopts;

	$FileExtNew = "";
	$gopts = "-i";
	$fopts = "";

	sub "_WriteBatchFile";

	status "Analyze finished successfully";

-

"Join *.vobs|<xypath>\.Icons\VideoConvert_Join.ico"
	$Copy_Cmd = "COPY";
	$CopyOpts = "/Y /B";
	$Move_Cmd = "MOVE";
	$MoveOpts = "/Y";
	$TempFile = "%temp%\~mergevob.bat";
	$InputPath = "<curpath>";
	$OutputPath = "E:\Temp";
	$FileExtNew = "";
	$gopts = "-i";
	$fopts = "";
	$SelectedItems = quote(get("SelectedItemsPathNames", """ + """));

	// Only to do the sanity checks
	$SelectedItemsInclPaths = get("SelectedItemsPathNames", "|");

	// Check selection for files and folders
	foreach($Token, $SelectedItemsInclPaths, "|") {
		// If at least one folder is selected, abort script
		// Check if result of exists($Token) is 2 (Token would be a folder)
		end (exists($Token) == 2), "You've selected a folder but only file(s) are allowed, aborted!";
	}

	// If nothing is selected, abort script
	end (getinfo("CountSelected") < 1), "No file(s) selected, aborted!";

	// If only one file is selected, abort script
	end (getinfo("CountSelected") == 1), "Only one file selected - you need at least two *.vobs, aborted!";

	// Take <curpath>, step two folders up and take that
	// as the new folder to copy the .vob back
	$ReturnPath = resolvepath("..\..\..", "<curpath>", 0);
	// Cut off the base path to get the main movie folder
	$ReturnFileName = regexreplace("$ReturnPath", "^.+\\");

	writefile($TempFile, "@Echo off".<crlf>, o, ta);
	writefile($TempFile, "setlocal enabledelayedexpansion".<crlf>, a, ta);
	writefile($TempFile, <crlf>, a, ta);
	$ReturnCode = exists("$OutputPath");
	if($ReturnCode == 2) {
		writefile($TempFile, $Copy_Cmd $CopyOpts $SelectedItems "$OutputPath\$ReturnFileName.vob".<crlf>, a, ta);
		writefile($TempFile, $Move_Cmd $MoveOpts "$OutputPath\$ReturnFileName.vob" "$ReturnPath\$ReturnFileName.vob".<crlf>, a, ta);
	}
	else {
		writefile($TempFile, $Copy_Cmd $CopyOpts $SelectedItems "$ReturnPath\$ReturnFileName.vob".<crlf>, a, ta);
	}
	writefile($TempFile, <crlf>, a, ta);

	open "$TempFile";

	msg "Output will be created at:<br>$ReturnPath\$ReturnFileName.vob";

	status "Joining *.vobs finished successfully";


/*
Functions
*/

"_Initialize"
	global $FFmpeg_Cmd, $TempFile, $InputPath, $OutputPath, $SelectedItems;

	$FFmpeg_Cmd = quote("<xydrive>\Tools\FFmpeg\ffmpeg.exe");
	$TempFile = "%temp%\~ffmpeg.bat";
	$InputPath = "<curpath>";
	$OutputPath = "<curpath>";
	$SelectedItems = report("{Basename}?{Ext}|",1);
	$SelectedItems = substr($SelectedItems, 0, -1);

"_WriteBatchFile"
	global $FFmpeg_Cmd, $TempFile, $InputPath, $OutputPath, $SelectedItems;
	global $FileExtNew, $fopts, $gopts;
	global $StreamOpts;

	// Only to do the sanity checks
	$SelectedItemsInclPaths = get("SelectedItemsPathNames", "|");
	
	// Check if a single | multiple files, a folder or a drive is selected
	// False = folder | True = file | Drive = drive
	$CheckFileOrFolder = report("{Dir False|True|Drive}",1);

	// If nothing is selected, abort script
	// 0 (selected) < 1 = TRUE
	end (getinfo("CountSelected") < 1), "No file(s) selected, aborted!";

	// Check selection for files and folders
	foreach($Token, $SelectedItemsInclPaths, "|") {
		// If at least one folder is selected, abort script
		// Check if result of exists($Token) is 2 (Token would be a folder)
		end (exists($Token) == 2), "You've selected a folder but only file(s) are allowed, aborted!";
	}

	$WavPlayer = quote("<xypath>\.Tools\.Wav_Player\sWavPlayer.exe");
	$SoundFinished = quote("<xypath>\.Tools\.Wav_Player\Sync_Complete.wav");

	writefile($TempFile, "@Echo off".<crlf>, o, ta);
	writefile($TempFile, "setlocal enabledelayedexpansion".<crlf>, a, ta);
	writefile($TempFile, <crlf>, a, ta);

	foreach($Token, $SelectedItems, "|") {
		$FileBase = gettoken($Token,1,"?");
		$FileExtOld = gettoken($Token,2,"?");
		$FileNameOld = "$FileBase.$FileExtOld";
		$FileNameNew = "$FileBase.$FileExtNew";
		$InputFile = "$InputPath\$FileNameOld";
		$OutputFile = "$OutputPath\$FileNameNew";

		if($InputFile == $OutputFile) {
			$AvoidExtConflict = "_new";
			$OutputFile = "$OutputPath\$FileBase$AvoidExtConflict.$FileExtNew";
		}
		$ReturnCode = exists("$OutputFile");
		if($ReturnCode == 1) {
			delete 1, 0, "$OutputFile";
		}
		if($FileExtNew == "") {

			// If analyzing, only allow one file
			end (getinfo("CountSelected") > 1), "Don't select more than one file for analyzing, aborted!";

			writefile($TempFile, CALL $FFmpeg_Cmd $gopts "$InputFile".<crlf>, a, ta);
			writefile($TempFile, PAUSE.<crlf>, a, ta);
		}
		else {
			writefile($TempFile, CALL $FFmpeg_Cmd $gopts "$InputFile" $fopts "$OutputFile".<crlf>, a, ta);
		}
	}

	writefile($TempFile, <crlf>, a, ta);
	writefile($TempFile, $WavPlayer $SoundFinished.<crlf>, a, ta);
	writefile($TempFile, <crlf>, a, ta);

	open "$TempFile";

-
"Cancel"
-
"Edit this &script : edit"
	self $ScriptFile, file;
	openwith "<xydrive>\Tools\EditPlus\editplus.exe", ,$ScriptFile;

Regards,
highend
One of my scripts helped you out? Please donate via Paypal

Post Reply