I used to run SageTV as my PVR front-end but have later decided to switch to MCE2005 due to its cleanness and integration into XP. Anyway, in that time, I found a For statement batch file I used to create my own script to auto-encode video. After playing with For statements for quite some time, I realized that it could be used for so much more!
Requirements:
-Windows XP (any flavor) or 98 (I believe the statements will read fine.)
-Basic understanding of .bat files
-Full batch file contents for converting in sagetv (http://forums.sagetv.com/forums/showthread.php?t=10709)
Overview:
This tech-recipe is another of my sagetv/htpc based ones, but it also can be used for much more! This tutorial will explain how to use a For statement in a way you never thought you could. For statements can allow one to send data to apps via command line (if they support this), move data, create webportals, and more!
Also, I have included my own date parsing code. It is handy to have and hard to find on the web..
Date Code:
Usage: use in any batch file you need to use the current date in a MMDDYYYY format; can be called in script by %date; place in beginning of script
- @REM parses month, day, and year into mm, dd, yyyy formats to create folders
FOR /F “TOKENS=1* DELIMS= ” %%A IN (‘DATE/T’) DO SET CDATE=%%B
FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (‘DATE/T’) DO SET mm=%%B
FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (‘echo %CDATE%’) DO SET dd=%%B
FOR /F “TOKENS=2,3 DELIMS=/ ” %%A IN (‘echo %CDATE%’) DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%
____________________
Here is a simple example of a For statement that will be used in this tutorial.
@echo on
@REM Seamonkey420's SageTV Batch File - MPG to WMV
@REM created on: 3/14/05
@REM RECOMMENDED TO TEST IN A TEMP DIR FIRST
@REM Setup as is; it will encode mpg to divx, then
@REM clean up the log, txt, ccno and VPrj files
@REM Then moves the avi file to a new folder (odir)
@REM Then finally deletes the original mpg
@REM 3/15/05: Added creation of subfolders with date encoded
@REM and also allows date variable to be globally available
@REM and added in cleanup of orphan files
@REM 3/19/05: Updated date parsing/folder to be more accurate
@REM also added in new functionality to create an embeded webpage
@REM for each movie file created, is created in the same folder as
@REM avi is encoded to!
@REM 3/27/05: Updated batch now to create a log file of any .wmv or the filetype your
@REM encoding your movies to prior to encoding, then checks to see if log file exists, if so,
@REM skips the file so you do not have to waste time re-encoding movies you have already encoded.
@REM 4/5/2005: Added PSP Video 9 integration; Requires PSPvideo9 be set up for auto-converting
@REM Must have Videora installed or this folder created(C:\Program Files\Videora)
@REM moves original mpgs to new psp folder defined in pdir variable.
@REM Be sure to put this batch file in the same folder as your Virtualdub.exe/windows media encoder folder
@REM ALSO Be sure to create a .vcf settings file in Virtualdub or .prx profile in windows media encoder
@REM This file then needs to be saved in the same folder as virtualdub.exe/wme and this batch file
@REM And the embedE.txt, embedS.txt, ndexE, indexS, TVindexE, TVindexS text files
@REM need to be created in this same directory/folder
@REM PSPvideo9 can then autoconvert files later in day! Added for my PSP!! moves the movie to a new folder for psp video to look for!
@REM Be sure to setup PSP Video9 with videora (note, you only need the folder for it create and have settings in psp video 9
@REM look for this location: C:\Program Files\Videora , just make that folder if you don't want to install videora
@REM parses month, day, and year into mm , dd, yyyy formats to create folders off of!
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%
@REM SETS SCRIPT VARIABLES, ie folder paths for files to be encoded
@REM idir = the directory that sageTV saves mpg recordings to
@REM odir = folder where wmv files will be saved and webpages
@REM oFile = File extension to be outputted, .wmv by default
@REM ldir = Log file directory
@REM trigger = file to check for to see if programs are done recording/viewed
@REM pdir = Folder where original mpg gets moved if encoded correctly! for use w/
@REM PSP Video 9 and autoconvert feature
set idir=T:\Recorded Shows
set odir=T:\Encoded Shows
set oFile=wmv
set profile=psp.prx
set ldir=T:\Recorded Shows\Log
set trigger=VPrj
set pdir=T:\Recorded Shows\PSP
@REM This line checks for a .VPrj file in the idir location.
@REM If it finds the file, it will first create a folder with todays date using mmddyyyy format
@REM into the odir folder and it will then call virtualdub/cscript (windows media encoder) w/the parameters
@REM and the .vcf/.prx profile and encode the mpg into avi/wmv, (a more streamable format than divx)
for /f "tokens=1 delims=." %%i in ('dir /s/b "%idir%\*-?.%trigger%"') do if EXIST "%%i.mpg" (md "%odir%\%date%") 2>&1
@REM Newly added, creates log files for existing movies in the logdir folder
@REM Then checks for log file, if log file exists, it will not encode the mpg, but if it does, it will.
for /f "tokens=4 delims=\." %%i in ('dir /s/b "%odir%\*-?.%oFile%"') do if NOT EXIST "%ldir%\*%%i.log" (echo exists > "%ldir%\%%i.log")
for /f "tokens=1 delims=." %%i in ('dir /b "%idir%\*-?.%trigger%"') do if NOT EXIST "%ldir%\%%i.log" (cscript.exe wmcmd.vbs -input "%idir%\%%i.mpg" -description "%%i" -author "seamonkey420" -output "%idir%\%%i.%oFile%" -loadprofile %profile%)
@REM this section cleans up the old files and moves the avi to the odir\date folder
for /f "tokens=1 delims=." %%i in ('dir /b "%idir%\*-?.%oFile%"') do if EXIST "%idir%\%%i.%oFile%" (move "%idir%\%%i.%oFile%" "%odir%\%date%\%%i.%oFile%")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.%oFile%"') do if EXIST "%odir%\%date%\%%i.%oFile%" (del "%idir%\%%i.log")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.%oFile%"') do if EXIST "%odir%\%date%\%%i.%oFile%" (del "%idir%\%%i.txt")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.%oFile%"') do if EXIST "%odir%\%date%\%%i.%oFile%" (del "%idir%\%%i.%trigger%")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.%oFile%"') do if EXIST "%odir%\%date%\%%i.%oFile%" (del "%idir%\%%i.ccno")
@REM This line move the original mpg movie if the avi/wmv is created and moved successfully to the psp directory for PSP Video 9 to encode!
@REM I would @REM this line when testing! NOTE, do your deleting in sagetv, batch shouldn't re-encode already encoded files!
@REM NEED TO FIX MOVING PART!!
DEL /Q "%pdir%\*.*"
for /f "tokens=1 delims=." %%i in ('dir /b "%idir%\*-?.%trigger%"') do if NOT EXIST "%ldir%\%%i.log" (COPY "%idir%\%%i.mpg" "%pdir%\%%i.mpg")
@REM Remove the @REM from the line below to delete the movie if its been encoded. Defaulted to not delete.
@REM for /f "tokens=1 delims=." %%i in ('dir /b "%idir%\*-?.%trigger%"') do if NOT EXIST "%ldir%\%%i.log" (DEL /Q "%idir%\%%i.mpg")
@REM These lines create a web page for each movie encoded, embeds into page, play movie from page!
@REM NOTE! this again requires the embedE.txt and the embedS.txt to be located in the same folder
@REM as windows media encoder, this batch file, and the .prx profile (usually, C:\Program Files\Windows Media Components\Encoder )
@REM Also creates an index.html with links to all html embedded pages in the %date% folder (ie 03192005\index.html)
@REM And finally a shows_index.html in the root of the odir (all pages are linked together, so easy to use for your webserver!)
@REM Files are linked! Test before using, like always!
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.%oFile%"') do if EXIST "%odir%\%date%\%%i.%oFile%" (type embedS.txt > "%odir%\%date%\%%i.html")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.%oFile%"') do if EXIST "%odir%\%date%\%%i.%oFile%" (echo ^ >> "%odir%\%date%\%%i.html")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.%oFile%"') do if EXIST "%odir%\%date%\%%i.%oFile%" (echo ^ >> "%odir%\%date%\%%i.html")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.%oFile%"') do if EXIST "%odir%\%date%\%%i.%oFile%" (type embedE.txt >> "%odir%\%date%\%%i.html")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.html"') do if EXIST "%odir%\%date%\%%i.html" (type TVindexS.txt > "%odir%\%date%\index.html")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.html"') do if EXIST "%odir%\%date%\%%i.html" (echo ^%%i^^
>> "%odir%\%date%\index.html")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.html"') do if EXIST "%odir%\%date%\%%i.html" (type TVindexE.txt >> "%odir%\%date%\index.html")
@REM Creates an shows_index.html file, points to the individual tv show indexes created for each date
for /f "tokens=1 delims=" %%i in ('dir /b "%odir%\*"') do if EXIST "%odir%\%%i\index.html" (del "%odir%\shows_index.html")
for /f "tokens=1 delims=" %%i in ('dir /b/Ad "%odir%\*"') do if EXIST "%odir%\%%i\index.html" (type indexS.txt > "%odir%\shows_index.html")
for /f "tokens=1 delims=" %%i in ('dir /b/Ad "%odir%\*"') do if EXIST "%odir%\%%i\index.html" (echo ^
^^%%i - Show Listings^^^
>> "%odir%\shows_index.html")
for /f "tokens=1 delims=." %%i in ('dir /b "%odir%\%date%\*-?.html"') do if EXIST "%odir%\%date%\index.html" (echo ^-%%i^^
>> "%odir%\shows_index.html")
for /f "tokens=1 delims=" %%i in ('dir /b/Ad "%odir%\*"') do if EXIST "%odir%\%%i\index.html" (type indexE.txt >> "%odir%\shows_index.html")
@REM Cleans up any orphaned files, looks to see if there the mpg exists, if not, deletes the
@REM orphaned files
for /f "tokens=1 delims=." %%i in ('dir /b "%idir%\*-?.txt"') do if NOT EXIST "%idir%\%%i.mpg" (del "%idir%\%%i.log")
for /f "tokens=1 delims=." %%i in ('dir /b "%idir%\*-?.txt"') do if NOT EXIST "%idir%\%%i.mpg" (del "%idir%\%%i.ccno")
for /f "tokens=1 delims=." %%i in ('dir /b "%idir%\*-?.txt"') do if NOT EXIST "%idir%\%%i.mpg" (del "%idir%\%%i.VPrj")
for /f "tokens=1 delims=." %%i in ('dir /b "%idir%\*-?.txt"') do if NOT EXIST "%idir%\%%i.mpg" (del "%idir%\%%i.txt")
@REM This will then call windows media encoder script to then broadcast all the newly encoded files!
@REM Commented out for now; delete the @REM to use!
@REM for /f "tokens=1 delims=." %%i in ('dir /b "%bdir%\*-?.%filetype%"') do if EXIST "%bdir%\%%i.%filetype%" (cscript.exe wmcmd.vbs -input "%bdir%\%%i.%filetype%" -author seamonkey420 -description %%i -broadcast %port% -loadprofile tvshows.prx)
This is a crazy-looking batch file, yet its not as complex as it may seem. This file will basically allow one to check a dir for a file, if it exists. It will then run a series of commands on the files: encode to wmv, create a webportal structure with embedded Windows Media Player in them, and also move files and cleanup files.
I have explained each line with a remark. This code heavily uses variables, so that way, you can make changes to it very easily without it breaking. You can change the file type your are using as your source file or the file extension your encoding to and of course folder locations.
I have used the for statement to check for the existence of files. However one could set it up to be run manually or called by windows scheduler.
____
NOTE: the readme I include with the version at sage.tv’s forums boards.
get all of the files for batch file here:
http://forums.sagetv.com/forums/showthread.php?t=10709
- Overview: Well, it is amazing what a little time, thinking, discussing, and testing can accomplish. I know that currently ajuhawk has created an application to do most of this and more, but I am a fan of the batch file still, so here is the latest of my batch files.
Notes: This DOES NOT handle compression in SageTV. Movies/shows may be encoded more than once if compression is used.
Installation:
Required Programs:
-SageTV
-Cayars Ultimate STV installed
-Windows Media Encoder 9 (click to go to download page)
-PSP Video 9 (http://pspvideo9.com)
-Videora (http://videora.com )
_________
Where to Put Files
-wme.bat put in Windows Media Encoder folder (i.e., C:\Program Files\Windows Media Components\Encoder)
-psp.prx or whatever you call your Windows Media Encoder profile; put in Windows Media Encoder folder (i.e., C:\Program Files\Windows Media Components\Encoder)
-All the .txt files (i.e., embedS.txt, embedE.txt, indexS.txt, etc.) Put this in Windows Media Encoder folder (i.e., C:\Program Files\Windows Media Components\Encoder).
-Put the Encode.cmd into your cayars\external folder (i.e., C:\Program Files\Frey Technologies\SageTV\STVs\OriginalV2\cayars\External )
_________
Files That Need to Be Modified
-wme.bat
You will need to change the idir and the odir parameters.
The idir is the location where sagetv saves your TV recordings.
The odir contains the locations where you want to save your encoded files. (Note: This batch will create a new folder in this directory with the date stamp mmddyyyy format)
Here are the parameters that can be changed/need (with examples) and to what they refer:
set idir=T:\Recorded Shows (where sagetv saves the mpgs of tv shows)
set odir=T:\Encoded Shows (where you want to create your encoded tv show webportal)
set oFile=wmv (file type you want to encode to)
set profile=psp.prx (windows media player profile to use)
set ldir=T:\Recorded Shows\Log (location of log folder, be sure to create it before running)
set trigger=VPrj (main trigger file )
set pdir=T:\Recorded Shows\PSP (location that PSP Video 9 looks for movies to auto-convert)
Also, if you want to have the batch search for a different file type to trigger the encoding, you can do so here. By default, I have it set to look for a .VPrj file.
-.txt files
Modify these files to your liking. I would suggest to run the script/batch first and see how things look. These files are basically templates. I have my batch use to create the webpages and embed the movie into a webpage.
-Encode.cmd (put and located in C:\Program Files\Frey Technologies\SageTV\STVs\OriginalV2\cayars\External)
Edit this batch so that it points to where the wme.bat is saved (C:\Program Files\Windows Media Components\Encoder\wme.bat).
-Then once you have all of this done, in Cayars Ultimate STV.
you can add a menu item to point to this file.
-Right-click in the menu/submenu where you want to add the encoding option.
-Choose Add New Menu Item > External Program.
-Then, for the external program, use cmd.exe.
-For arguments/parameters, use /C “C:\Program Files\Frey Technologies\SageTV\STVs\OriginalV2\cayars\External\Encode.cmd”
(You can copy this line, and then use the Paste From Clipboard option.)
-Then enter the menu title/text to show the following: i.e., Encode and Publish
-Then once you are done, right-click again and now Save Menu Definition File.
Hopefully, you should now be able to just click the option, and the encoding will start automatically in the background.
________
Here is exactly what the batch does:
1. In sagetv, click the new menu item we added that points to the batch/encode script.
2. This calls our wme.bat script. This script does it all!
3. First, it creates a new folder and is created with the current date mmddyyyy (i.e., 03192005)
4. It then will check for a .VPrj file in the idir.
5. If it finds one, it will then check the log file folder for a log. If not,
6. it will then go and encode all the mpg movies that have a .VPrj file using Windows Media Encoder and the profile we saved in the psp.prx.
7. Once it finishes encoding the .VPrj, it will go back and then create a webpage file .html with the show name/episode (i.e., FamilyGuyFunnyDay-3.html) in the folder that was created (i.e. 03192005/FamilyGuyFunnyDay-3.html).
8. After the embedded webpage is created, it will go back and then create a main index.html file for that folder (i.e., 03192005\index.html).
9. After the date index is created, another index is then created with links to all of the index.html files that might exist.
10. Finally, once this is done, the batch will go back and clean up all of the files assocated with the newly encoded wmv (i.e., ccno, txt, log, etc.) if it successfully encoded. Also, move the mpg to the psp folder (for psp video to auto-convert later).
However, if the encoding of the mpg failed, the original mpg is left alone.
11. Then it will do some quick orphaned file cleanup.