Skip to content

Instantly share code, notes, and snippets.

@xntrik
Created December 2, 2012 11:06
Show Gist options
  • Save xntrik/4188235 to your computer and use it in GitHub Desktop.
Save xntrik/4188235 to your computer and use it in GitHub Desktop.
Batch job for Windows Powershell Movies/TV Shows via Handbrake into iTunes autoMAGICALLY
$tvsourcefolder = "F:\TV Shows\"
$moviesourcefolder = "F:\Movies\"
$tvproxfile = "C:\tvprox.txt"
$movieproxfile = "C:\movieprox.txt"
$targetfolder = "F:\iTunes\"
$handbrakecli = "C:\Program Files\Handbrake\HandBrakeCLI.exe"
$handbrakepreset = "AppleTV 2"
#Before you run me, I would recommend running the following commands
#To run these, start up powershell, and, either set the variables as above, or execute these long hang
#This command will populate the initial tv show file
#get-childitem $tvsourcefolder -include *.mkv,*.avi,*.m4v,*.mp4 -recurse | foreach ($_) { $_.fullname } | Out-File $tvproxfile
#and this command will do the same for movies..
#get-childitem $moviesourcefolder -include *.mkv,*.avi,*.m4v,*.mp4 -recurse | where {$_.length -ge 100MB} | foreach ($_) {$_.fullname} | Out-File $movieproxfile
#After these files are populated, when this script runs, it'll only ever look for differences.
#capiche?
"Starting TV Prox"
"Checking for existing TV Prox"
$prox = Get-WmiObject Win32_Process -Filter "Name like '%powershell%'" | select-Object CommandLine | where {$_ -match "tvprox"}
$proxc = $prox | measure
if ($proxc.Count -gt 1) {
"We already have a running TV Prox .. exiting"
exit
} else {
"No other TV Prox running .. continuing"
#This just gets all the files and dumps them into a folder
#get-childitem $tvsourcefolder -include *.mkv,*.avi,*.m4v,*.mp4 -recurse | foreach ($_) { $_.fullname } | Out-File $tvproxfile
#The same for movies
#get-childitem $moviesourcefolder -include *.mkv,*.avi,*.m4v,*.mp4 -recurse | where {$_.length -ge 100MB} | foreach ($_) {$_.fullname} | Out-File $movieproxfile
#Similar to above, but, dumping it into an array
$filelist = get-childitem $tvsourcefolder -include *.mkv,*.avi,*.m4v,*.mp4 -recurse | foreach ($_) { $_.fullname } | sort
#Doing the same for movie files
$filelistmovies = get-childitem $moviesourcefolder -include *.mkv,*.avi,*.m4v,*.mp4 -recurse | where {$_.length -ge 100MB} | foreach ($_) {$_.fullname} | sort
#Gather the processed files into a different array
$alreadyProcessed = Get-Content $tvproxfile | sort
#Doing the same for movie files
$alreadyProcessedMovies = Get-Content $movieproxfile | sort
#Comparing the file system to the processed file
$cmp = Compare-Object -ReferenceObject $filelist -DifferenceObject $alreadyProcessed -PassThru
if ($cmp) {
"There are differences in TV shows.. processing files now"
foreach ($file in $cmp) {
"Processing input file: $file"
$targetfile = [io.path]::GetFileNameWithoutExtension($file)
"Saving to: $targetfolder$targetfile.m4v"
"Kicking off Handbrake...."
& $handbrakecli -i $file -o "$targetfolder$targetfile.m4v" --preset=$handbrakepreset
"Handbrake complete .. "
"Going to add to iTunes now .. lets hope iTunes is running :D"
$iTunes = New-Object -comObject iTunes.Application
$iTunesLibrary = $iTunes.LibraryPlaylist
$iTunesLibrary.AddFile("$targetfolder$targetfile.m4v")
"Added to iTunes .. file complete .. "
"Adding file to the processed file"
"$file" | Out-File -FilePath $tvproxfile -Append
}
"Done processing TV Shows.. "
#exit
} else {
"There are no differences in TV shows.."
#exit
}
#Comparing the file system to processed movie files
$cmpmovies = Compare-Object -ReferenceObject $filelistmovies -DifferenceObject $alreadyProcessedMovies -PassThru
if ($cmpmovies) {
"There are differences in Movies.. processing files now"
foreach ($file in $cmpmovies) {
"Processing input file: $file"
$targetfile = [io.path]::GetFileNameWithoutExtension($file)
"Saving to: $targetfolder$targetfile.m4v"
"Kicking off Handbrake...."
& $handbrakecli -i $file -o "$targetfolder$targetfile.m4v" --preset=$handbrakepreset
"Handbrake complete .. "
"Going to add to iTunes now .. lets hope iTunes is running :D"
$iTunes = New-Object -comObject iTunes.Application
$iTunesLibrary = $iTunes.LibraryPlaylist
$iTunesLibrary.AddFile("$targetfolder$targetfile.m4v")
"Added to iTunes .. file complete .. "
"Adding file to the processed file"
"$file" | Out-File -FilePath $movieproxfile -Append
}
"Done processing movies.."
} else {
"There are no differences in movies.."
}
}
So I run a peculiar setup, ATV2 with my media coming from iTunes on a desktop, Windows 7 PC I never really touch
anymore. I know I could JB the ATV, or I could replace the PC with a NAS or some shit .. anyway, I'm lazy.
...
But not so lazy not to figure out how to make a crappy powershell script, hook it up in task scheduler under hstart
(http://www.ntwind.com/software/hstart.html), run it every 5 minutes against my TV / Movie folders, looking for new
files, then automatically converting them (with Handbrake) into the right format, and then automatically
adding them to iTunes.
.. Different kinda lazy really.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment