Encoding Guide / Hybrid Encode Script
The following is an AVS script that can produce HD, downloadable, and 512kb (archive.org streamable) encodes all in one. It is to be maintained, documented, and supported by the community. If you have a worthwhile contribution to it, please post in
this thread explaining what it does and why you think it should be included. Upon review and a general acceptance of the improvement, it will be included here by a site editor.
This script has been rewritten and perhaps popularized by
Brandon.
Flygon was the original creator, who then gave it to Brandon because he liked his rewrite. Brandon, merely being a fan of good syntax, did not feel capable of maintaining a script he barely understood, so he first implicitly, then explicitly gave it to the community. Flygon has credited MisterEpic and Grunt for their "great contributions",
Aktan his audio / video desync fixing algorithm, JEEB and J_Darnley of Freenode's #avisynth IRC channel for assisting with the automatic detection of the branch name, and "you for using this script!"
Please make sure you know what you are doing encoding-wise before using this script. Many of the concepts in use and the command lines given below are derived from the main
Encoding Guide, so if you are unsure what anything is doing please look through the guide first. If you are still uncertain as to how something works, consult
this thread or post in the Encoder's Corner for more general questions.
Table of contents [
expand all] [
collapse all]
Script
# The downloadable / HD encoding script. For more information, see:
# http://tasvideos.org/EncodingGuide/HybridEncodeScript.html
# This is the TASBlend function. It reduces framerate to 1/2 but leave
# flickering effects partly visible. Do not touch it.
function TASBlend(clip c, float "ratio") {
# reduces framerate to 1/2 but leaves flicker effects partly visible
# blends frame pairs with alternating opacity
# The default for this is 2/3+1/3;1/3+2/3)
# optional "ratio" is the opacity of the first frame out of the four
ratio = default(ratio, 2.0 / 3)
opacity1 = round((1 - ratio) * 257)
opacity2 = round((ratio) * 257)
c
Interleave( \
Layer(SelectEvery(4, 0), SelectEvery(4, 1), level=opacity1), \
Layer(SelectEvery(4, 2), SelectEvery(4, 3), level=opacity2) \
)
}
function Replace(clip c, int i, int j, clip d) {
Assert(i >= 0, "Replace: parameter i is negative")
Assert(j >= 0, "Replace: parameter j is negative")
p1 = c.Trim(0, -i)
p2 = d.Trim(i, j)
p3 = c.Trim(j + 1, 0)
p1 = (i == 0) ? c.Trim(0, -1).DeleteFrame(0) : p1
p3 = (j == 0) ? c.Trim(0, -1).DeleteFrame(0) : p3
p1 + p2 + p3
return (c.HasAudio) ? last.AudioDub(c) : last
}
# The below filenames are the filenames of your logo, set one for handheld
# consoles and TV based consoles, be sure to keep the "" tags.
file = "logo.png"
# This sets the start time for the subtitles to start displaying, this is set
# as a frame number.
subff = 764
# This controls how long the subtitles stay on the screen.
sublength = 198
# This sets the start time for the second set of subtitles to start displaying,
# this is set as a frame number. Make sure there's at least a delay of 1 frame
# in between this set and the previous one.
subff2 = subff + sublength + 1
# This controls how long the second set of subtitles stay on the screen.
sublength2 = sublength
# Below is the size for the subtitles, standard encodes use a size of 10.
subsize = 10
# Adjust the below items to adjust the subtitles.
# This is so that the movie will have the correct subtitles.
# All the required information will be on the movies submission page.
# Be sure to keep the quotes everywhere, even when branch is blank ("").
game = "A Nightmare On Elm Street"
branch = ""
author = "goofydylan8"
time = "13:25.57"
rerecords = "16726"
# Below is the values for the left, center, or right positioning for the
# subtitles below in the script, modification shouldn't be attempted unless you
# know what you're doing.
positionleft = 7
positioncenter = 8
positionright = 9
# This sets the position of the subtitles in terms of direction, it can be
# left, right, or center.
# Set positionleft for left, set positioncenter for center, set positionright
# for right.
# Setting this to positioncenter is considered standard practice, so other
# positions shouldn't be used unless it looks far better.
position = positioncenter
# This sets the y position for the subtitles, basically, how high or low they
# are up on the screen.
# The higher the number, the lower the subtitles.
ypos = 180
# Set this to true for HD encode resolutions.
hd = false
# Set this to true if this is a handheld console to disable aspect correction.
handheld = false
# Set this to true to enable the TASBlend function, which is useful for YouTube
# streams since it cuts the framerate to 1/2 while leaving flickering effects
# partly visible. It increases the file size due to the frame blending.
tb = false
# Set this to true for 10 bit 444 encodes. You additionally have to:
# 1) Use 10-bit version of x264.
# 2) Use command line option '--output-csp i444'.
i444 = true
# Passes for downloadable/streaming encodes:
# 0: audio extraction / streaming (archive.org 512kb; AR correction);
# 1: DupMC (no AR correction);
# 2: DeDup (no AR correction).
pass = 2
# This setting turns the audio/video desync fixer on/off using true/false.
# The below should always be set to false, barring the unusual occasion that it
# actually fixes the audio.
desyncfixer = false
LoadPlugin("ExactDedup.dll")
sourcevideo = AVISource("movie.avi").Trim(0, 50690 - 121).ConvertToRGB32()
# This setting adjusts the framerate for HD encodes when uploading to YouTube.
# This is to save space due to YouTube having a maximum framerate of 30 anyway.
# It is suggested to be at either 24 fps or 30fps for NTSC games (Depending on
# if they use alternating frame flicker or not).
# PAL games should generally be set to 30 fps, it can be set to 25 fps if there
# is no flickering, but it's difficult to notice the strobing effect in 30fps
# anyway. If you have TASBlend enabled, you can ignore this.
# hdframerate = ( \
# sourcevideo.FrameRateNumerator / sourcevideo.FrameRateDenominator / 2 \
# )
hdframerate = 30
# THE BELOW CODE SHOULDN'T BE MODIFIED UNLESS YOU KNOW WHAT YOU ARE DOING!
# This automatically fixes length based audio/video desync if it exists inside
# the encode (eg. Many PCEngine games).
num = sourcevideo.AudioLengthF * sourcevideo.FrameRateNumerator
denom = sourcevideo.FrameCount * sourcevideo.FrameRateDenominator
fixedaudiorate = Round(num / denom)
(desyncfixer) ? sourcevideo.AssumeSampleRate(fixedaudiorate) : 0
# High Definition.
factor = (sourcevideo.height > 540) ? 1 : \
(sourcevideo.height > 270) ? 2 : \
(sourcevideo.height > 180) ? 3 : 4
factor = hd ? factor : 1
b = sourcevideo.PointResize( \
sourcevideo.width * factor, sourcevideo.height * factor \
)
width = (b.width > b.height * 4 / 3) ? b.width : b.height * 4 / 3
width = (width % 4 == 1) ? width + 3 : \
(width % 4 == 2) ? width + 2 : \
(width % 4 == 3) ? width + 1 : width
height = (b.width > b.height * 4 / 3) ? b.width * 3 / 4 : b.height
height = (height % 4 == 1) ? height + 3 : \
(height % 4 == 2) ? height + 2 : \
(height % 4 == 3) ? height + 1 : height
c = hd ? b.PointResize(width, height) : b.LanczosResize(width, height, taps=2)
c = handheld ? b : c
g = (pass == 0) ? c : sourcevideo
g = hd ? c.PointResize(c.width * 2, c.height * 2) : g
multi = g.height / 224
multi = (multi > (g.height / sourcevideo.height)) ? multi : ( \
g.height / sourcevideo.height \
)
# Logo.
d = ImageSource( \
file=file, start=0, end=int((sourcevideo.FrameRate * 2) - 1), \
fps=sourcevideo.FrameRate \
).ConvertToRGB32().AssumeFPS( \
sourcevideo.FrameRateNumerator, sourcevideo.FrameRateDenominator \
)
e = BlankClip( \
d, audio_rate=sourcevideo.AudioRate, channels=sourcevideo.AudioChannels \
)
f = AudioDub(d, e).LanczosResize(g.width, g.height, taps=2)
last = f + g
# This is the first set of subtitles.
# This shouldn't be modified unless you know what you are doing
Subtitle( \
game + "\n" + ( \
(branch == "") ? "" : branch + "\n" \
) + "Played by " + author + "\nPlaying time: " + \
time + "\nRerecord count: " + rerecords, y=(ypos * multi), \
align=position, first_frame=subff, last_frame=(subff + sublength), \
size=(subsize * multi), text_color=$00FFFFFF, halo_color=$00000000, lsp=2 \
)
# This is the second set of subtitles, do not modify unless necessary.
Subtitle( \
"This is a tool\nassisted recording.\nFor details, visit\n" + \
"http://TASVideos.org/", y=(ypos * multi), \
align=position, first_frame=subff2, last_frame=(subff2 + sublength2), \
size=(subsize * multi), text_color=$00FFFFFF, halo_color=$00000000, lsp=2 \
)
# This activates the frame dropper or the TASBlend function for HD encodes.
# This depends on if the game simulates transperancy with alternating frame
# flicker. The frame dropper saves filesize without image loss, because YouTube
# caps at 30fps. The TASBlend function, however, increases the filesize due to
# the frame blending, but optimizes the flicker effects for YouTube.
# blended = last.TASBlend()
# SelectEven()
hd ? (tb ? TASBlend() : ChangeFPS(hdframerate)) : 0
# Downloadable encodes need a resizing filter for the colourspace reduction.
# HD encodes need to be point resized to keep the colour information faithful.
ConvertToYV24(chromaresample="point", matrix=(hd ? "Rec709" : "PC.601"))
(pass == 1) ? ExactDedup( \
firstpass=true, dupinfo="dup.txt", times="times.txt" \
) : last
(pass == 2) ? ExactDedup(firstpass=false, dupinfo="dup.txt") : last
i444 ? last : ConvertToYV12( \
chromaresample=(hd ? "point" : "lanczos"), \
matrix=(hd ? "Rec709" : (pass == 0 ? "Rec601" : "PC.601")) \
)
Settings
Brandon used the following algorithm with this script (Referenced as encode.avs), to produce encodes:
For all types of encodes, first dump the movie, load the parts, change the subtitle text and positions, trim the file as necessary, change any necessary additional settings (Ex. handheld), and preview in VirtualDub with hd = false. Once the preview is acceptable, click File > Save WAV as, and name it "audio.wav". What is done from here on depends on the type of encode:
For primary downloadable:
- Once the audio file is extracted, double click ogg.bat
- Set hd = false, pass = 1, and double click first.bat
- Once first.bat finishes running, set pass = 2 and double click second.bat (Make sure i444 is set to false)
- Once all of the above are done processing, double click mux.bat. This will produce the final product, an encode.mkv file.
For 10-bit 444:
- Once the audio file is extracted, double click ogg.bat
- Set hd = false, pass = 1, and double click first.bat
- Once first.bat finishes running, set pass = 2, set i444 = true, and double click second_10bit444.bat
- Once all of the above are done processing, double click mux_10bit444.bat. This will produce the final product, an encode_10bit444.mkv file.
For 512kb:
- Once the audio file is extracted, double click mp4.bat
- Set hd = false, pass = 0 and double click 512kb.bat
- Once all of the above are done processing, double click mux_512kb.bat. This will produce the final product, an encode_512kb.mp4 file.
For HD:
- Once the audio file is extracted, double click youtubeogg.bat
- Set hd = true, pass = 0 and double click youtube.bat
- Once the above are done processing, double click mux_youtube.bat. This will produce the final product, hd.mkv.
Commands
ogg.bat
venc -q2 audio.wav audio.ogg
first.bat
x264 --sar 7:6 --keyint infinite --crf 0 --range pc --input-range pc -o NUL encode.avs --preset ultrafast
second.bat
x264 --threads 1 --sar 7:6 --crf 20 --keyint 600 --ref 16 --no-fast-pskip --bframes 16 --b-adapt 2 --direct auto --me umh --merange 64 --subme 10 --trellis 2 --partitions all --rc-lookahead 250 --no-dct-decimate --range pc --input-range pc --colormatrix smpte170m --tcfile-in times.txt -o video.mkv encode.avs
mux.bat
mkvmerge -o encode.mkv --timecodes -1:times.txt video.mkv audio.ogg
second_10bit444.bat
x264-10 --threads 1 --sar 7:6 --crf 20 --keyint 600 --ref 16 --no-fast-pskip --bframes 16 --b-adapt 2 --direct auto --me umh --merange 64 --subme 10 --trellis 2 --partitions all --rc-lookahead 250 --no-dct-decimate --input-range pc --range pc --tcfile-in times.txt -o video_10bit444.mkv --colormatrix smpte170m --output-csp i444 encode.avs
mux_10bit444.bat
mkvmerge -o encode_10bit444.mkv --timecodes -1:times.txt video_10bit444.mkv audio.ogg
mp4.bat
sox audio.wav audio_trimmed.wav trim 4672s
neroAacEnc -q 0.25 -if audio_trimmed.wav -of audio.mp4
512kb.bat
x264 --threads 1 --crf 20 --keyint 600 --ref 16 --no-fast-pskip --bframes 16 --b-adapt 2 --direct auto --me umh --merange 64 --subme 10 --trellis 2 --partitions all --rc-lookahead 250 --no-dct-decimate --range tv --input-range tv --colormatrix smpte170m -o video_512kb.mp4 encode.avs
mux_512kb.bat
MP4Box -add video_512kb.mp4 -add audio.mp4 -new encode_512kb.mp4
youtubeogg.bat
venc -q10 audio.wav audio_youtube.ogg
youtube.bat
x264.exe --qp 0 --keyint 600 --output video_youtube.mkv encode.avs
mux_youtube.bat
mkvmerge.exe -o encode_youtube.mkv --compression -1:none video_youtube.mkv audio_youtube.ogg
These files require several dependencies. All of them are included in
Brandon's setup.
EncodingGuide/HybridEncodeScript last edited by
natt on 2012-04-24 21:08:01
Page info and history | Latest diff | List referrers | View Source