HomeSoftwareScriptsNotesToolboxGallery
Scripts
2D Rotation (JS)3D Rotation (JS)Ping Flood (CMD)Self Signed CertPHP Database InterfaceAutoWrite (JS)Digitize Text (JS)GrafJS (JS)Image Animation (JS)ASCII Loading Indicator (JS)Div Layer Movement (JS)Div Layer Resizing (JS)Text Scrolling (JS)Statistics Graph (PHP)Turn off AAM/APM (BAT)RW Protect (BAT)SRT Renamer (PHP)String Encryption (PHP)Windows WiFi Hotspot (BAT)YouTube DL Script (BAT)

SRT Renamer



(1 of 2) Click to see images.

A script I use for renaming bulks of SRT files instead of doing them one-by-one. Put all 3 files in the same directory as the media files and srt files. The script should figure out and also propose before doing any changes, how to rename the SRT files. As long as they are in addic7ed (00x00) or subscene (S00E00) format, and episodes have S00E00 in their name.

Download the script HERE, PHP parser is included. MSDOS Batch based, if you have Linux you can just run the php files without parameters first to preview changes, and with "takeaction" as parameter afterwards. Listing of files included below.

SRT-Renamer.bat
@ECHO OFF
MODE CON:COLS=100 LINES=100
TITLE SRT-Renamer
COLOR 17

ECHO.
ECHO SRT Renamer
ECHO ===========

php SRT-Renamer-proposer.php
PAUSE
php SRT-Renamer-Submitter.php
PAUSE


SRT-Renamer.php
<?php
$srt_files_renamed = 0;
$srt_registry = [];
$vid_registry = [];
$SeasonIdentifier = '';
$EpisodeIdentifier = '';
$VideoFileTypes = ['mp4','mkv','avi'];

try {
if (!$curdir = @scandir('./', 0))
	throw new Exception('Could not scan the directory.');

foreach ($curdir as $f) {
	
	// Search for SRT files.
	if (stripos($f, '.srt') !== false) {
		$srt_registry[] = $f;
		continue;
	}
	
	// Search for video files.
	foreach ($VideoFileTypes as $vidtype) {
		if (stripos($f, $vidtype) !== false) {
			$vid_registry[] = $f;
			break;
		}
	}
}

// Iterate SRT files and rename them if corresponding video file is found.
foreach ($srt_registry as $srt_f) {
	
	// Search for S00E00 - Subscene++ format.
	if (preg_match('/S([0-9]{2}).*?E([0-9]{2})/', $srt_f, $m) == 1) {
		$SeasonIdentifier = $m[1];
		$EpisodeIdentifier = $m[2];
		
	// Search for 00x00 - addic7ed format.
	} else if (preg_match('/([0-9]{2})x([0-9]{2})/', $srt_f, $m) == 1) {
		$SeasonIdentifier = $m[1];
		$EpisodeIdentifier = $m[2];
	}
	
	if ($SeasonIdentifier == '' || $EpisodeIdentifier == '') {
		echo 'Passing on file '. $srt_f ." since season or episode could not be detected.\r\n\r\n";
		continue;
	}
	
	foreach ($vid_registry as $vid_f) {
		// Video files are expected to have S00E00 in their name.
		// Use Bulk Rename Utility or something to fix them first if not!
		if (stripos($vid_f, 'S'. $SeasonIdentifier .'E'. $EpisodeIdentifier) !== false) {
			if (isset($argv[1]) && $argv[1] == 'takeaction') {
				rename($srt_f, substr($vid_f, 0, -4) .'.en.srt');
			}
			echo $srt_f ." >\r\n". substr($vid_f, 0, -4) .'.en.srt'."\r\n\r\n";
			++$srt_files_renamed;
			break;
		}
	}
	
	$SeasonIdentifier = '';
	$EpisodeIdentifier = '';
}

if (isset($argv[1]) && $argv[1] == 'takeaction') {
	echo $srt_files_renamed .' SRT files renamed.'."\r\n\r\n";
} else {
	echo $srt_files_renamed .' '.
	'SRT files will be renamed.'."\r\n".
	'Continue only if the above looks OK!'.
	"\r\n\r\n";
}

} catch (Exception $e) {
	echo 'Error occurred: '. $e->getMessage() ."\r\n\r\n";
}
?>