Wednesday, May 22, 2024
HomeWindowsBatch file programmingBatch File Converting of Video Files: SageTV Example

Batch File Converting of Video Files: SageTV Example

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

Jimmy S
Jimmy Shttp://blogs.tech-recipes.com/jimmyselix
Jimmy Selix is an early adopter that loves to be one of the first on the block to have the latest and greatest in technology and gadgets. Another love of his is being able to share his knowledge to others seeking it. Feel free to drop any comments or questions that you may have.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!