PS script to re-encode livetv recordings

I’ve wanted my recordings to be shrunk for a while, and finally broke down and wrote something. I realize there’s some other methods to do this already discussed but I wanted the freedom of controlling exactly how it works.

I’ll start out by saying this is version one, a day old and has been killing it for 24 hours now for my livetv recordings, turning my OptiPlex i5 into a basement heater. My 2TB drive has begun getting free space again and I no longer am trying to be a miser with what episodes I keep after they are recorded and watched. So far it seems the files are 10-20% original size when done, and you’ll need to download the free handbrakecli program that this script uses. Handbrake is amazing, and the command line version is called handbrakecli. I made some decisions that work well so far, choosing very fast 720p30 processes the shows at about 2x speed (60fps) with my i5-3470 cpu. The script deletes the original when its done doing each one, if you don’t trust it I suggest commenting out the remove-item line until you trust it.

I give no warranties to this, you’ll need to modify it for your needs if you choose to use it. It can be setup as a scheduled task, or just ran when you want. It would be nice to know if it works for you, leave a comment if it does you good, and if you have any suggested changes.

#get all raw mpeg2 recordings recursively
$shows = get-childitem "d:\plexdata\tvdvr" -rec -include *.ts

foreach ($show in $shows) {

	#ignore files actively being recorded in grab directory
	if ($show.fullname -like "*.grab*") {continue}

	#set variables of sourcefile and outfile of particular recording
	#escape \ character required as period is interpreted as regex otherwise
	$sourcefile = $show.fullname
	$outfile = $sourcefile -replace "\.ts",".mp4"

	#dont create outfile if it was already created
	#if you ctrl-c a previous run leaving partial files, delete them yourself first
	if (test-path $outfile) {continue}

	#put together handbrakecli command for this particular recording
	$cmd = '"c:\program files\handbrake\handbrakecli.exe" -i "' + $sourcefile + '" -o "' + $outfile + '" --preset "Very Fast 720p30" -s "1,2,3,4,5,6"'

	#run handbrake to make outfile
	$cmd | cmd

	#cleanup once done
	#check if the outfile was created and if so if its at least 7% original size, delete sourcefile
	#unsure if handbrake can have problems this is safety mechanism rather than just assume it will always work perfectly.  
	#If the outfile is at least 7% of original size its probably a good output file
	if (test-path $outfile) {
		$sourcefilesize = (get-item $sourcefile).length
		$outfilesize = (get-item $outfile).length
		if (($outfilesize / $sourcefilesize) -gt .07) {
			remove-item $sourcefile
			}
		}
	}

Changed the size check to be .07 for the ratio of the outfile/sourcefile after it processed all my tv shows, some of them were less than 10% of the original size!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.