##########################
# NES Encoder Script v1 #
##########################
#
# This script is governed by the provisions of the BSD license.
#
# Copyright (c) 2007, Andrew "ccfreak2k" Waters
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the script nor the names of its contributors may
# be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ABOUT:
# Written by ccfreak2k
# Based on settings provided in "Nes encoding.bat" by Phil
#
# PURPOSE:
# The purpose of this script is to help automate the encoding process. It does a basic check to see if the encoding process failed somewhere and alerts the user.
#
# REQUIRES:
# .\mencoder.exe
# A font file.
#
# INPUT:
# A logo video.
# The recorded video.
# A subtitles file.
#
# OUTPUT:
# The encoded video.
#
# CAVEATS:
# The script makes mencoder generate interim video files; used disk space from the initial input videos may TRIPLE, so make sure you have at least that much.
#
# TODO:
# Maybe add some logging support so the user can log the process.
# Maybe add an optional backup option to let the user backup the "video is encoding.avi" files (currently, if an error occurs, the file gets clobbered).
# Default values for variables are set here.
$global:logofile = "neslogo.avi"
$global:recordedfile = "recorded.avi"
$global:recordedaudiofile = "audio.wav"
$global:subfile = "subtitles.sub"
$global:subfont = "subfont.ttf"
$global:suboptions = "-subfont-text-scale 2 -sub-bg-alpha 15 -subpos 0"
$global:encodedfile = "encoded.avi"
$global:vcodec = "x264"
$global:voptions = "bitrate=125:pass=3:frameref=15:qp_min=10:qp_max=51:qcomp=1:qp_step=30:ratetol=100:me=umh:me_range=64:subq=6:partitions=all:mixed_refs:trellis=2"
$global:acodec = "mp3lame"
$global:aoptions = "preset=40:aq=0:mode=3"
######################################################################################################
# NOTHING PAST THIS POINT IS OF CONCERN TO THE USER. DON'T MODIFY UNLESS YOU KNOW WHAT YOU'RE DOING. #
######################################################################################################
$Host.UI.RawUI.WindowTitle = "NES Encoder Script"
write-host "*************************************************************************" -foregroundcolor green -backgroundcolor black
write-host "* NES Encoder Script v1 *" -foregroundcolor green -backgroundcolor black
write-host "* For use of conversion of NES recorded AVIs into end-user video files. *" -foregroundcolor green -backgroundcolor black
write-host "* (c) 2007, ccfreak2k *" -foregroundcolor green -backgroundcolor black
write-host "*************************************************************************" -foregroundcolor green -backgroundcolor black
# Import the settings file, if it exists.
if (test-path .\nesconfig.ps1)
{
write-host "---------------------------------------" -foregroundcolor green -backgroundcolor black
write-host "nesconfig.cfg found; using config file." -foregroundcolor green -backgroundcolor black
write-host "---------------------------------------" -foregroundcolor green -backgroundcolor black
& '.\nesconfig.ps1'
}
else
{
write-host "----------------------------------------" -foregroundcolor yellow -backgroundcolor black
write-host "nesconfig.cfg not found; using defaults." -foregroundcolor yellow -backgroundcolor black
write-host "----------------------------------------" -foregroundcolor yellow -backgroundcolor black
}
write-host " "
# Verify the files.
write-host "------------------" -foregroundcolor yellow -backgroundcolor black
write-host "Verifying files..." -foregroundcolor yellow -backgroundcolor black
$logofile,$recordedfile,$subfile,$subfont | foreach-object
{
write-host "$_..." -nonewline -foregroundcolor yellow -backgroundcolor black
if (test-path $_)
{
write-host "OK" -foregroundcolor green -backgroundcolor black
}
else
{
write-host "Failed!" -foregroundcolor red -backgroundcolor black
write-host "Check your files." -foregroundcolor red -backgroundcolor black
exit
}
}
# Verify other variables.
write-host "---------------------------------------------------------" -foregroundcolor yellow -backgroundcolor black
write-host "File verification succeeded. Verifying other variables..." -foregroundcolor yellow -backgroundcolor black
$suboptions,$vcodec,$voptions,$acodec,$aoptions | foreach-object
{
if ($_ -eq "")
{
write-host "Failed!" -foregroundcolor red -backgroundcolor black
write-host "Check your codec choices/options." -foregroundcolor red -blackground black
exit
}
else
{
write-host "OK" -foregroundcolor green -backgroundcolor black
}
}
write-host "Variable verification succeeded." -foregroundcolor green -backgroundcolor black
write-host "--------------------------------" -foregroundcolor green -backgroundcolor black
write-host " "
# The fluff is over. Serious business from now on.
# First, run the first command.
write-host "-------------------------------------------" -foregroundcolor green -backgroundcolor black
write-host "Step 1: Mix logo and recorded into one file" -foregroundcolor green -backgroundcolor black
write-host "-------------------------------------------" -foregroundcolor green -backgroundcolor black
$Host.UI.RawUI.WindowTitle = "NES Encoder Script - Step 1: Mix"
# This now uses x264 lossless (q=0) instead of ffv1; thanks adelikat
.\mencoder.exe -priority idle -oac pcm -mc 0 -ovc x264 -x264encopts qp=0:partitions=all:me=umh:me_range=64 -o mixed.avi $logofile $recordedfile
#if ($file.length -gt 0) #This originally checked file length.
if ($LASTEXITCODE -eq 0) #We check to see if the program exited successfully.
{
# The last command was successful. Keep going.
write-host "-------------------------------" -foregroundcolor green -backgroundcolor black
write-host "Step 2: Video encoding pass 1/3" -foregroundcolor green -backgroundcolor black
write-host "-------------------------------" -foregroundcolor green -backgroundcolor black
$Host.UI.RawUI.WindowTitle = "NES Encoder Script - Step 2: Video Pass 1"
.\mencoder.exe -priority idle -vf decimate=10:0:0:1 -aspect 4:3 -oac copy -mc 0 -ovc $vcodec -x264encopts pass=1 -o Video_is_encoding.avi mixed.avi -sub $subfile $suboptions -font $subfont
if ($LASTEXITCODE -eq 0)
{
# On to step three.
write-host "-------------------------------" -foregroundcolor green -backgroundcolor black
write-host "Step 3: Video encoding pass 2/3" -foregroundcolor green -backgroundcolor black
write-host "-------------------------------" -foregroundcolor green -backgroundcolor black
$Host.UI.RawUI.WindowTitle = "NES Encoder Script - Step 3: Video Pass 2"
.\mencoder.exe -aspect 4:3 -vf decimate=10:0:0:1 -priority idle -oac copy -mc 0 -ovc $vcodec -x264encopts $voptions -o Video_is_encoding.avi mixed.avi -sub $subfile $suboptions -font $subfont
if ($LASTEXITCODE -eq 0)
{
# Step 4.
write-host "-------------------------------" -foregroundcolor green -backgroundcolor black
write-host "Step 4: Video encoding pass 3/3" -foregroundcolor green -backgroundcolor black
write-host "-------------------------------" -foregroundcolor green -backgroundcolor black
$Host.UI.RawUI.WindowTitle = "NES Encoder Script - Step 4: Video Pass 3"
.\mencoder.exe -aspect 4:3 -vf decimate=10:0:0:1 -priority idle -oac copy -mc 0 -ovc $vcodec -x264encopts $voptions -o Video_is_encoding.avi mixed.avi -sub $subfile $suboptions -font $subfont
if ($LASTEXITCODE -eq 0)
{
# Last step!
write-host "--------------------" -foregroundcolor green -backgroundcolor black
write-host "Step 5: Encode audio" -foregroundcolor green -backgroundcolor black
write-host "--------------------" -foregroundcolor green -backgroundcolor black
$Host.UI.RawUI.WindowTitle = "NES Encoder Script - Step 5: Audio"
.\mencoder.exe -priority idle -oac $acodec -mc 0 -lameopts $aoptions -ovc copy -o $encodedfile Video_is_encoding.avi
# The train stops here. Thanks for riding.
if ($LASTEXITCODE -ne 0)
{
write-host "--------------------------------------------" -foregroundcolor red -background black
write-host "Error: Step 5 failed with an exit code of ' -nonewline -foregroundcolor red -background black
write-host $LASTEXITCODE -foregroundcolor red -background black
write-host "Check the output of mencoder for details. " -foregroundcolor red -background black
write-host "--------------------------------------------" -foregroundcolor red -background black
$Host.UI.RawUI.WindowTitle = "Windows PowerShell"
exit # Ja mata!
}
write-host "******************************" -foregroundcolor green -backgroundcolor black
write-host "* Encoding process complete! *" -foregroundcolor green -backgroundcolor black
write-host "******************************" -foregroundcolor green -backgroundcolor black
$Host.UI.RawUI.WindowTitle = "Windows PowerShell"
exit
}
else
{
write-host "--------------------------------------------" -foregroundcolor red -background black
write-host "Error: Step 4 failed with an exit code of " -nonewline -foregroundcolor red -background black
write-host $LASTEXITCODE -foregroundcolor red -background black
write-host "Check the output of mencoder for details. " -foregroundcolor red -background black
write-host "--------------------------------------------" -foregroundcolor red -background black
$Host.UI.RawUI.WindowTitle = "Windows PowerShell"
exit
}
}
else
{
write-host "--------------------------------------------" -foregroundcolor red -background black
write-host "Error: Step 3 failed with an exit code of " -nonewline -foregroundcolor red -background black
write-host $LASTEXITCODE -foregroundcolor red -background black
write-host "Check the output of mencoder for details. " -foregroundcolor red -background black
write-host "--------------------------------------------" -foregroundcolor red -background black
$Host.UI.RawUI.WindowTitle = "Windows PowerShell"
exit
}
}
else
{
write-host "--------------------------------------------" -foregroundcolor red -background black
write-host "Error: Step 2 failed with an exit code of " -nonewline -foregroundcolor red -background black
write-host $LASTEXITCODE -foregroundcolor red -background black
write-host "Check the output of mencoder for details. " -foregroundcolor red -background black
write-host "--------------------------------------------" -foregroundcolor red -background black
$Host.UI.RawUI.WindowTitle = "Windows PowerShell"
exit
}
}
else
{
write-host "--------------------------------------------" -foregroundcolor red -background black
write-host "Error: Step 1 failed with an exit code of " -nonewline -foregroundcolor red -background black
write-host $LASTEXITCODE -foregroundcolor red -background black
write-host "Check the output of mencoder for details. " -foregroundcolor red -background black
write-host "--------------------------------------------" -foregroundcolor red -background black
$Host.UI.RawUI.WindowTitle = "Windows PowerShell"
exit
}