Batch File Converting of Video Files: SageTV example

Contributor Icon Contributed by seamonkey420 Date Icon April 8, 2006  
Tag Icon Tagged: Batch file programming

Well, i used to run SageTV as my PVR frontend but have later decided to switch to MCE2005 due to its cleanness and integration into XP. Anywho, in that time i found a For statement batch file i used to create my own script to autoencode video. After playing with For statements so long, 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 recipes is another of my sagetv/htpc based ones but also can be used for much more! This recipe will explain how to use a For statement in a way you never though 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’ve included my own date parsing code too.. its handy to have and hard to find on the web..

Date Code:
usuage: 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 begining of script.

    @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%

____________________

here’s a quick example of a for statement that will be used in this recipe.

@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've already encoded.
@REM 4/5/2005: Added PSP Video 9 integration!! Requires PSPvideo9 be setup for autoconverting
@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 w/embedded windows media player in them, and also move files and cleanup files.

i’ve 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’ve used the for statement to check for the existence of files, however one could setup it up to be ran manually or called by windows scheduler..

hopefully that helped you enough to get on your way.. peace
seamonkey420

____
NOTE, the readme i include w/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.. its 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’m a fan of the batch file still.. so here’s 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, ie C:\Program Files\Windows Media Components\Encoder)
    -psp.prx or whatever you call your windows media encoder profile (put in windows media encoder folder, ie C:\Program Files\Windows Media Components\Encoder)
    -all the .txt files (ie embedS.txt, embedE.txt, indexS.txt, etc..) (put in windows media encoder folder, ie C:\Program Files\Windows Media Components\Encoder)
    -put the Encode.cmd into your cayars\external folder (ie C:\Program Files\Frey Technologies\SageTV\STVs\OriginalV2\cayars\External )

    _________

    files that need to modified!
    -wme.bat
    you will need to change the idir and the odir parameters.
    the idir is the location sagetv saves your tv recordings
    the odir is the locations you want to save your encoded files to (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 what they refer to:

    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 too, 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 webapges 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 you 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 you want to add the encoding option.
    -Choose Add New Menu Item > External Program
    -Then, for external program, use: cmd.exe
    -Then for arguments/parameters use: /C “C:\Program Files\Frey Technologies\SageTV\STVs\OriginalV2\cayars\External\Encode.cmd”
    (you can copy this line, then use the Paste From Clipboard option)
    -Then enter the menu title/text to show: ie, Encode and Publish
    -Then once your 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 teh background.!!

    ________

    here’s 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 is created with the current date mmddyyyy (ie 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 (ie FamilyGuyFunnyDay-3.html) in the folder that was created (ie 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 (ie 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. and finally once this is done, the batch will go back and clean up all of the files assocatied with the newly encoded wmv (ie , ccno, txt, log, etc) if it successfully encoded. And also move the mpg to the psp folder (for psp video to auto-convert later).
    However, if encoding of the mpg failed, the original mpg is left alone.
    11. then it will do some quick orphaned file cleanup…

Previous recipe | Next recipe |
 

 
close Reblog this comment
blog comments powered by Disqus