REDnet.nl
REDnet.nl
Creative Vision Interaction   [=] NederlandsSwitch to Nederlands Home
Articles
(0 comments) show comments Batch Encode AVI files with TMPGEnc using VBS (Su sep. 7th, '08 at 22:27 pm) This language is available.

Summary



A little .VBS help on automatically batch encoding a lot of .AVI files to .MPG Mpeg files using TMPGEnc is here.



Intro



TMPGEnc has become a notion in the media scene. I don't like TMPGEnc, but it's like the best quality encoder out there AFAIK. One of the many annoyances is that it won't allow for descent batch encoding the way most of us like it. Namely, you have to add everything manually to a Batch List, requiring at least 5 mouse clicks per file in a normal Windows installation.

I also don't like the old MPEG, but sometimes it's necessary, and you'll need a lot of files to be encoded the same way. There's no tools out there, and since I'm no programmer I cannot make a fancy tool. What I can do is create a blunt Visual Basic Script file that sort of does the autobatching.

All you have to do is set up a one video encode Batch List within TMPGEnc in a predefined folder and name, with all the settings you want. The script then walks through a folder with .AVI files and one by one copies them over that predefined file, runs the Batch Encode and renames and copies the result back.

During this, the script only encodes the files that aren't already encoded. Why? Because I sometimes have a few files I keep updating, and it's pretty convenient I can just run the script without isolating those files.

There are some vars you need to change in the script. I set the defaults to something that is convenient for me. Because I can. But at least you want to take a look at these: TMPGEncPath, TempDir, aviTrail, mpgTrail. You probably want to set the last two vars to nothing ("").
In the workflow below I assume using my defaults.



Workflow



  • Edit MpegBatcher.vbs and update the variables to something that works for you, making sure referenced folders exist and names are used in the following steps.
  • Take one of your to-be-batched video files and copy it to P:/Temp/TMPGEncBatcher.avi (Yep that's a file name, not a folder).
  • Fire up TMPGEnc (Free) and set the Video Source to P:/Temp/TMPGEncBatcher.avi. The Audio Source will automatically follow.
  • Load or apply your favorite MPEG settings.
  • Press Ctrl+Alt+M to move the current project to the batch list. Save the resulting MPEG to P:/Temp/TMPGEncBatcher.mpg.
  • Press Ctrl+B and double check that the just created render is the only one in the Batch List.
  • Close the Batch List and Close TMPGEnc.
  • Run MpegBatcher.vbs from the folder where your to be encoded .AVI files are residing (by (double)click or command line, whatever).


Nice, eh? Kind of blunt but it does the trick quite neatly imo. And of course scripts like this are highly tweakable so I encourage you to tweak it to the max of your desires.

Oh yeah the scipt.



The Script



You can just download the file below. But here's the code anyway:

MpegBatcher.vbs
'' RED MpegBatcher script
'' Convert all .AVI files without an .MPG counterpart to .MPG using TPMegEnc
''
'' By Sander Steenhuis
'' 2008-09-07
'' http://www.REDnet.nl/en/

' Usage:
'
' Copy this script to a folder full of .AVI's.
' Copy a random video file to P:\Temp\TMPGEncBatcher.avi (with same specs as the files you are going to convert)
' Open TMPGEnc, load P:\Temp\TMPGEncBatcher.avi, apply your favorite settings, press ctrl+alt+M (Add project to Batch List), use P:\Temp\TMPGEncBatcher.avi as name, exit TMPGEnc.
'
' Notes:
'
' Above filenames, drives and folders should correspondent to the ones used in this script.
' You can change them somewhere below, but use your brain.

Option Explicit
'On Error Resume Next
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim currentFolder, fileDone, fileSkip, fsoDo, fileMpeg



' Set this to your TMPGEnc path INCLUDING END SLASH.
Dim TMPGEncPath: TMPGEncPath = "D:\Software\Video\TMPGEnc Free\"
' Set this to your TMPGEnc executable.
Dim TMPGEnc: TMPGEnc = "TMPGEnc.exe"
' Set this to your temporary video folder. INCLUDE END SLASH.
Dim TempDir: TempDir = "P:\Temp\"
' Set this to your temporary video name. NO EXTENSION.
Dim TempFile: TempFile = "TMPGEncBatcher"
' Set this to trailing text in your .AVI name that's not in your .MPEG files.
' For example, my lagarith avifiles are named as "Shot 02.01.03 Morning.Laggy.avi" while the mpegs are called "Shot 02.01.03 Morning.mpg"
Dim aviTrail: aviTrail = ".Laggy"
' Similar for the mpegfile, if you want to do something crazy like appending a date.
' Note that this is included in the search for already mpegged files.
Dim mpgTrail: mpgTrail = ""






' Begin
Main

Sub Main
' set globals
Set currentFolder = fso.GetFolder(".")
fileDone = 0: fileSkip = 0

' Begin output
WScript.Echo "**************************"
WScript.Echo "* RED MpegBatcher script *"
WScript.Echo "**************************"
WScript.Echo ""
WScript.Echo "Sending unprocessed .AVI files to TMPGEnc from this directory: "
WScript.Echo currentFolder.Path
WScript.Echo "While looking for:"
WScript.Echo "'[Filenames]" & aviTrail & ".avi' files without"
WScript.Echo "'[Filenames]" & mpgTrail & ".mpg' counterparts."
WScript.Echo ""
If Not promptYesNo("Continue? (y/n): ") Then Exit Sub
WScript.Echo ""

' Start the process if there's a batch project
If fso.FileExists(TMPGEncPath & "CurrentBatch.tbe") Then
' Make template copy of CurrentBatch.tbe to reset finished state again and again
WScript.Echo "Backing up CurrentBatch.tbe ..."
Set fsoDo = fso.GetFile(TMPGEncPath & "CurrentBatch.tbe")
fsoDo.Copy(TMPGEncPath & "CurrentBatch.tbe.bak")

' Parse folder
parseFolder currentFolder

' Ergo Conclusio
WScript.Echo fileDone & " files encoded; " & fileSkip & " files were already encoded."
WScript.Echo "Press ENTER to exit."
Else
WScript.Echo "You did not save your project as a Batch List in TMPGEnc!"
End If

' Pause so the promt doesn't close immediately
Do While Not WScript.StdIn.AtEndOfLine
Dim Input: Input = WScript.StdIn.Read(1)
Loop
WScript.Echo "Done."

End Sub



Sub parseFolder (ByVal Folder)
' Prepare vars
Dim Files: Set Files = Folder.Files
Dim File

' Loop through dir
For Each File In Files
Dim fileBase: fileBase = fso.getBaseName(File.Name)
Dim fileExt: fileExt = fso.getExtensionName(File.Name)
If UCase(fileExt) = "AVI" Then
fileMpeg = Replace(fileBase, aviTrail, mpgTrail) & ".mpg"
If Not fso.FileExists(currentFolder.Path & "\" & fileMpeg) Then
WScript.Echo File.Name & " found without " & fileMpeg
doTMPGEnc File
Else
fileSkip = fileSkip + 1
End If
End If
Next
End Sub

Sub doTMPGEnc (ByVal File)
' Dim vars
Dim tmpAvi: tmpAvi = TempDir & TempFile & ".avi"
Dim tmpMpeg: tmpMpeg = TempDir & TempFile & ".mpg"

' Check if temp files exists, delete..
If fso.FileExists(tmpAvi) Then
WScript.Echo "Deleting " & tmpAvi & " ..."
fso.DeleteFile(tmpAvi)
End If
If fso.FileExists(tmpMpeg) Then
WScript.Echo "Deleting " & tmpMpeg & " ..."
fso.DeleteFile(tmpMpeg)
End If

' Copy target file to temp file
Set fsoDo = fso.GetFile(File)
WScript.Echo "Copying " & File.Name & " to " & TempDir & "..."
fsoDo.copy(TempDir)
WScript.Echo "Renaming " & File.Name & " to " & TempFile & ".avi" & "..."
Set fsoDo = fso.GetFile(TempDir & File.Name)
''fsoDo.Rename(TempDir & TempFile & ".avi") '' Not working?
fsoDo.Move(TempDir & TempFile & ".avi")

' Reset Batch List template
resetBatchList

' Run TMPGEnc in batch + autoclose mode
WScript.Echo "Starting " & TMPGEnc & "..."
run chr(34) & TMPGEncPath & TMPGEnc & chr(34) & " /batch /close"
fileDone = fileDone + 1

' Reset Batch List template again, (You'd understand why if you was debugging this script)
resetBatchList

' Successful? Delete .avi
If fso.FileExists(tmpAvi) Then
WScript.Echo "Deleting " & tmpAvi & " ..."
fso.DeleteFile(tmpAvi)
End If

' and move .mpg
WScript.Echo "Moving '" & tmpMpeg & "' to " & currentFolder.Path
Set fsoDo = fso.GetFile(tmpMpeg)
fsoDo.Move(currentFolder.Path & "\" & fileMpeg)
End Sub

Function resetBatchList()
WScript.Echo "Resetting CurrentBatch.tbe ..."
If fso.FileExists(TMPGEncPath & "CurrentBatch.tbe") Then
fso.DeleteFile(TMPGEncPath & "CurrentBatch.tbe")
End If
Set fsoDo = fso.GetFile(TMPGEncPath & "CurrentBatch.tbe.bak")
fsoDo.Copy(TMPGEncPath & "CurrentBatch.tbe")
End Function

Function promptYesNo(ByVal strMsg)
Do
WScript.Echo strMsg
Dim key: key = WScript.StdIn.ReadLine()
Select Case LCase(Trim(key))
Case "n","no" promptYesNo = False: Exit Function
Case "y","yes" promptYesNo = True: Exit Function
End Select
WScript.StdOut.WriteLine "Just enter 'y' or 'n'..."
Loop
End Function

Sub run(ByVal executable)
Dim shell: Set shell = CreateObject("WScript.Shell")
shell.Run executable, 1, true
Set shell = Nothing
End Sub
You can just download the .zip here:
MpegBatcher.zip


Known Problems



Okay, not so nice. There's this annoying status report after TMPGEnc is done. You're gonna have to click OKAY. So I guess you can batch encode in the background while browsing the web or doing some Photoshop, but you're gonna have to swap back once in a while to see if you have to click OKAY yet.

You need to run this script in Command Prompt mode, NOT in Windows mode. If messages appear in Windows-style popups, you're doing it wrong. Make sure you use Command Prompt mode. You either type in the command prompt:
cscript filename.vbs
Or right-click the .vbs file, choosing "Open with Command Prompt".



Anything else? More problems or a thank you? Please leave a comment. If there's errors, I'm probably not gonna do anything about it because you're supposed to fix it yourself and give me the fix, but I appreciate any comments.
Home
Creative
Vision
Interaction
 
~Home~


Recent reactions
[13/11] Firefox versus Opera
[3/11] CSS en DIVs - gedoe
[22/12] Korn coverband
[14/5] Lost en de buggy leader
[10/1] J-Five - Modern Times
[7/2] Schijt aan deze buurt
[22/3]
[1/4] Wie heeft mijn tas meegenomen?
~Creative~


Parts
Red photoshed
Classics
RED @ RED
Interdimensionals
RED's Place 3
The Radon clan
RED's Place 2
~Vision~


Articles
[8/1] Windows 7, SSH daemons and Cygwin
[31/12] I'ma stick to Ubuntu, sorry!
[15/4] Rid your consoles- XBMC and Arcade Browser are here!
[5/2] Import those frickin' contacts on your Nokia N Series
[30/11] Onhandigheden en ambtenarij van de IB-Groep
Columns
[9/1] C1000
[3/4] Albert Heijn Klantenservice is een idiocratie
[11/3] What Really Boils My Blood II
[16/2] Je beste vriend de politie
[4/2] Kanker NS
~Interaction~


Guestbook
Post message
Contact