forked from vadosnaprimer/TASEncodingPackage
/
encode.avs
399 lines (342 loc) · 15.9 KB
/
encode.avs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# The downloadable/HD encoding script
# http://tasvideos.org/EncodingGuide/PublicationManual.html
SetFilterMTMode("DEFAULT_MT_MODE", MT_MULTI_INSTANCE)
AddAutoloadDir("MAINSCRIPTDIR/programs/plugins")
Import(ScriptDir() + "programs/functions.avsi")
# "RGB24" for FFV1, "AUTO" for the rest
pixelType = "RGB24"
AviSource("movie.avi", pixel_type=pixelType)
# Dolphin (with FFMPEG). Use FFV1 patch for dumping:
# https://github.com/vadosnaprimer/dolphin/releases
# AviSource("movie.avi", pixel_type="RGB24")
# AudioDub(last, wavsource("dspdump.wav"))
threads = 4 # manual, how many threads avisynth should use.
trimFrame = 654321 # manual, discards logo lengh automatically
handHeld = false # auto, but set it for preview
hd = false # auto, but set it for preview
halfFPS = false # for games that "lag" every other frame
stackNDS = false # stack one of NDS screens near both of them for HD
vb = false # for Virtual Boy encodes
# Resizer (for hd upscaling and multisegment import)
resizer = hd ? "Point" : "Lanczos"
# force Lanczos for 3D footage with increased internal resolution
# resizer = "Lanczos"
# Subtitles contents
game = "Game Name"
branch = "'branch'" # lowercase for usual words, capital for in-game capital words. when blank, leave "''"
author = "author"
time = "00:00.0"
rerecords = "0"
# Subtitles timing and placement
subFF = 600 # first subtitles frame, set manually!
subAlign = 8 # subtitles horizontal alignment (7/8/9)
subEntry = 2 # sets the number of sub entries (2/3/4)
subYpos = 0 # subtitles vertical position
subXoffs = 0 # subtitles horizontal offset (can be negative)
subLengthMul = 5 # entry length in seconds (fractions work)
subFF2delay = 0 # extra delay in frames between subtitle entries 1 and 2
subFF2delay2 = 0 # extra delay in frames between subtitle entries 2 and 3
subFF2delay3 = 0 # extra delay in frames between subtitle entries 3 and 4
subFont = "helv" # see "fonts" folder for available types
bighaloCutoff = 384 # width and height above this will use bighalo instead of freesub
# Multisegment import (upscales hires segments straight to HD when needed)
# requires normally importing a sample whose attributes it will use for all segments
ms = false # enable multisegment import
ms_base = "movie_" # common string for all segment names
ms_start = 0 # number of the first segment
ms_end = 15 # number of the last segment
ms_format = "%1.0f" # string format: http://avisynth.nl/index.php/Internal_functions#String
# Extra HQ (upscaled downloadable encodes)
hq = false # auto
hq_scale = 2 # manual for now
hq_hiResSrc = false # we downscale hires 3D source, and upscale lowres pixelated source
hq_baseWidth = 320 # aspect-corrected 1x width (only for hires source)
hq_baseHeight = 240 # aspect-corrected 1x height (only for hires source)
# Aspect ratio correction
wAspect = 4
hAspect = 3
# Subtitles text
subString1 = game + "\n" \
+ (branch == "''" ? "" : branch + "\n") \
+ "Played by " + author
subString2 = "Playing time: " + time \
+ "\nRerecord count: " + rerecords
subString3 = "This is a tool-assisted recording." \
subString4 = "For details, visit http://TASVideos.org/"
# Fix progressing (not floating) audio/video desync
avDesyncFixer = false
################
# LEGACY STUFF #
################
# Famtasia
# ConvertAudioTo16bit()
# Mupen64
# ResampleAudio(last, 44100)
# Crop(0,0,-0,-20) # when old Glide64 is used
# PSXjin
# audio = WavSource("movie_000.wav")
# AudioDub(audio, last)
# Old Dolphin (remove /* and */ to uncomment)
# get values at http://tasvideos.org/forum/viewtopic.php?p=373299#373299
/*
M = 486000000
F = 60
k = 1
N = 525
b = 0
D = (k * N + b) * floor(floor(M / F) / N) # don't edit this formula
video = last.TimecodeFPS("timecodes.txt", fpsnum=M, fpsden=D)
audio = \
WavSource("dspdump0.wav") + \
WavSource("dspdump1.wav").ResampleAudio(32000) + \
WavSource("dspdump.wav")
*/
######################
# AUTOMATED SETTINGS #
######################
ConvertToRGB32()
# Add 1 scanline if height is uneven (ntsc sms, pal c64)
last.height % 2 > 0 ? StackVertical(last, Crop(0, Height-1, 0, 0)) : 0
pass = 0
i444 = false
originalWidth = last.width
originalHeight = last.height
hdvb = hd && vb
# Make too dark scenes brighter
# AutoLevels(filterRadius=1000, sceneChgThresh=200, gamma=1.1)
# Fix progressing (not floating) audio/video desync
num = last.AudioLengthF * last.FrameRateNumerator
denom = last.FrameCount * last.FrameRateDenominator
avDesyncFixer ? AssumeSampleRate(Round(num / denom)).ResampleAudio(48000) : 0
# Alternative method that tweaks framerate instead (not recommended)
# avDesyncFixer ? AssumeFPS(last.FrameCount / last.AudioLengthF * last.AudioRate) : 0
# VirtualBoy (bizhawk only!)
# http://tasvideos.org/forum/viewtopic.php?p=435440#435440
left = vb ? Crop( 0, 0, 384, 224).Levels(0, 1, 255, 0, 157) : 0
right = vb ? Crop(384, 0, 384, 224).Levels(0, 1, 255, 0, 157) : 0
vb ? hd ? 0 : Anaglyph(left, right, "grey").AudioDub(left) : 0
# Pick logo file
file = hd || hq \
? vb \
? "logoGrey.png" \
: stackNDS \
? "logohdnds.png" \
: last.height == 384 \
? "logonds.png" \
: "hdlogo.png" \
: last.height == 384 \
? "logonds.png" \
: "logo.png"
# file = !hd ? "logo34.png" : "hdlogo34.png"
# Stack 3 NDS screens
upperScreen = last.Crop(0, 0, -0, last.height/2)
lowerScreen = last.Crop(0, last.height/2, -0, -0)
halfScreen = upperScreen # set which NDS screen to put as main
stackNDS && hd ? StackHorizontal(halfScreen.PointResize(last.width*2, last.height), last) : 0
# Aspect ratio correction for SD encodes
height = handHeld \
? last.height \
: last.width > last.height * wAspect / hAspect \
? last.width * hAspect / wAspect \
: last.height
width = handHeld \
? last.width \
: height * wAspect / hAspect
width = width % 4 == 1 ? width + 3 \
: width % 4 == 2 ? width + 2 \
: width % 4 == 3 ? width + 1 : width
height = height % 4 == 1 ? height + 3 \
: height % 4 == 2 ? height + 2 \
: height % 4 == 3 ? height + 1 : height
# Remember SD frame size for subYpos HD tweaks
wARC = width
hARC = height
# Actually go HD if we need
height = hd ? 2160 : height
width = handHeld \
? height * last.width / last.height \
: height * wAspect / hAspect
width = width % 4 == 1 ? width + 3 : \
width % 4 == 2 ? width + 2 : \
width % 4 == 3 ? width + 1 : width
# Rescaling
# extra HQ:
# hires source: downscale straight to aspect-corrected res with lanczos
# lowres source: upscalse by our factor with point, then do ARC with lanczos for 512kb
# hd:
# resize to 4K, then just subsample with lanczos in the end
# for hires 3D footage, upscaled with lanczos
# handheld and primary (dedupped): do nothing
# 512kb: do ARC with lanczos
resized = hq && !hd \
? hq_hiResSrc \
? Lanczos4Resize(hq_baseWidth * hq_scale, hq_baseHeight * hq_scale) \
: handHeld || pass != 0 \
? PointResize(last.width * hq_scale, last.height * hq_scale) \
: PointResize(last.width * hq_scale, last.height * hq_scale) \
.Lanczos4Resize(width * hq_scale, height * hq_scale) \
: hd \
? eval(resizer+"Resize(width, height)") \
: handHeld || pass != 0 \
? last \
: Lanczos4Resize(width, height)
# If ms enabled, we use parameters of "resized" to apply to all segments
resized = ms ? resized.AppendSegment(ms_base, ms_start, ms_end, ms_format, resizer, pixelType).ConvertToRGB32() : resized
# Logo
logoVideo = ImageSource(file=file, start=0, end=int((resized.FrameRate * 2) - 1), fps=resized.FrameRate) \
.ConvertToRGB32().AssumeFPS(resized)
logoAudio = BlankClip(logoVideo, audio_rate=resized.AudioRate, channels=resized.AudioChannels)
logo = AudioDub(logoVideo, logoAudio).Lanczos4Resize(resized.width, resized.height)
last = hdvb ? logo.PointResize(resized.width / 2, resized.height) : logo ++ resized
# Virtual Boy
left = hdvb ? last ++ left.PointResize(last.width, last.height) : 0
right = hdvb ? last ++ right.PointResize(last.width, last.height) : 0
# Additional subtitle magic
# figure out how much our font will be changed after x264 ARC and for HD
wFactor = hd || hq ? float(last.width) / wARC : 1
hFactor = hd || hq ? float(last.height) / hARC : 1
wStretch = (handHeld || pass == 0) && !hdvb ? 1 \
: (float(last.width) / last.height) / (float(wAspect) / hAspect)
smallerSide = last.height < last.width ? last.height : last.width
biggerSide = last.height > last.width ? last.height : last.width
# use bighalo if we're relatively hires
# or if font stretch on some side is too heavy (ARCed primary)
useBighalo = biggerSide > bighaloCutoff \
|| ((wStretch < 0.8 || wStretch > 1.3) \
&& !handHeld && pass != 0 \
&& smallerSide >= 224)
# Subtitles variables
subXoffs = int(subXoffs * wStretch)
subRatio = float(last.height) / 21
#subRatio = float(smallerSide) / 21
subXpos = subAlign == 7 || subAlign == 4 || subAlign == 1 \
? (2 + subXoffs) * wFactor \
: subAlign == 8 || subAlign == 5 || subAlign == 2 \
? last.width / 2 + subXoffs * wFactor \
: last.width - (2 + subXoffs) * wFactor
subXpos = hdvb ? subXpos / 2 : subXpos
subYpos = int(float(subYpos) / (wStretch > 1 ? wStretch : 1) * hFactor)
subYpos = hdvb ? subYpos * hFactor : subYpos
subSize = useBighalo ? subRatio \
: subRatio < 10 ? 8 \
: subRatio < 12 ? 10 \
: subRatio < 14 ? 12 \
: subRatio < 18 ? 14 \
: subRatio < 24 ? 18 : 24
subWidth = useBighalo && !hd ? float(subSize) / 2.323 * wStretch : 0
subHaloSize = useBighalo ? floor(subSize / 7) : floor(subSize / 4.5)
subHaloSize = subHaloSize < 3 ? 3 : subHaloSize
subFontName = subFont + (smallerSide < 224 ? "R" : "B")
subFontFile = "./fonts/" + subFontName + string(subSize) + ".bdf"
subLength = int(last.FrameRate * subLengthMul)
subFF2 = SubFF + subLength + 1 + subFF2delay
subFF3 = SubFF2 + subLength + 1 + subFF2delay2
subFF4 = SubFF3 + subLength + 1 + subFF2delay3
subColor = vb ? $009D9D9D : $00FFFFFF
subRadius = hd ? 0 : subHaloSize
# Subtitles functions
subEntry == 2 ? useBighalo ? vb ? 0 : ng_bighalo(subString1 + "\n" + SubString2, \
x=subXpos, y=subYpos, align=subAlign, first_frame=subFF, \
last_frame=subFF+subLength, size=subSize, font_width=subWidth, \
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) \
: FreeSub(subString1 + "\n" + SubString2, x=subXpos, y=subYpos+subHaloSize, \
font=subFontFile, halo_width=subHaloSize, halo_height=subHaloSize, \
align=subAlign, first_frame=subFF, last_frame=subFF+subLength, \
text_color=subColor, halo_color=$00000000, lsp=2):0
subEntry == 2 ? useBighalo ? vb ? 0 : ng_bighalo(subString3 + "\n" + subString4, \
x=subXpos, y=subYpos, align=subAlign, first_frame=subff2, \
last_frame=subff2+subLength, size=subSize, font_width=subWidth, \
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) \
: FreeSub(subString3 + "\n" + subString4, \
x=subXpos, y=subYpos+subHaloSize, \
font=subFontFile, halo_width=subHaloSize, halo_height=subHaloSize, \
align=subAlign, first_frame=subff2, last_frame=subff2+SubLength, \
text_color=subColor, halo_color=$00000000, lsp=2) : 0
subEntry == 3 || subEntry == 4 ? useBighalo ? vb ? 0 : ng_bighalo(subString1, \
x=subXpos, y=subYpos, align=subAlign, first_frame=subFF, \
last_frame=subFF+subLength, size=subSize, font_width=subWidth, \
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) \
: FreeSub(subString1, x=subXpos, y=subYpos+subHaloSize, \
font=subFontFile, halo_width=subHaloSize, halo_height=subHaloSize, \
align=subAlign, first_frame=subFF, last_frame=subFF+subLength, \
text_color=subColor, halo_color=$00000000, lsp=2):0
subEntry == 3 || subEntry == 4 ? useBighalo ? vb ? 0 : ng_bighalo(subString2, \
x=subXpos, y=subYpos, align=subAlign, first_frame=subff2, \
last_frame=subff2+subLength, size=subSize, font_width=subWidth, \
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) \
: FreeSub(subString2, \
x=subXpos, y=subYpos+subHaloSize, \
font=subFontFile, halo_width=subHaloSize, halo_height=subHaloSize, \
align=subAlign, first_frame=subff2, last_frame=subff2+SubLength, \
text_color=subColor, halo_color=$00000000, lsp=2):0
subEntry == 3 ? useBighalo ? vb ? 0 : ng_bighalo(subString3 + "\n" + subString4, \
x=subXpos, y=subYpos, align=subAlign, first_frame=subFF3, \
last_frame=subFF3+subLength, size=subSize, font_width=subWidth, \
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) \
: FreeSub(subString3 + "\n" + subString4, \
x=subXpos, y=subYpos+subHaloSize, \
font=subFontFile, halo_width=subHaloSize, halo_height=subHaloSize, \
align=subAlign, first_frame=subFF3, last_frame=subFF3+SubLength, \
text_color=subColor, halo_color=$00000000, lsp=2):0
subEntry == 4 ? useBighalo ? vb ? 0 : ng_bighalo(subString3, \
x=subXpos, y=subYpos, align=subAlign, first_frame=subFF3, \
last_frame=subFF3+subLength, size=subSize, font_width=subWidth, \
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) \
: FreeSub(subString3, \
x=subXpos, y=subYpos+subHaloSize, \
font=subFontFile, halo_width=subHaloSize, halo_height=subHaloSize, \
align=subAlign, first_frame=subFF3, last_frame=subFF3+SubLength, \
text_color=subColor, halo_color=$00000000, lsp=2):0
subEntry == 4 ? useBighalo ? vb ? 0 : ng_bighalo(subString4, \
x=subXpos, y=subYpos, align=subAlign, first_frame=subFF4, \
last_frame=subFF4+subLength, size=subSize, font_width=subWidth, \
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) \
: FreeSub(subString4, \
x=subXpos, y=subYpos+subHaloSize, \
font=subFontFile, halo_width=subHaloSize, halo_height=subHaloSize, \
align=subAlign, first_frame=subFF4, last_frame=subFF4+SubLength, \
text_color=subColor, halo_color=$00000000, lsp=2):0
# Virtual Boy hacks
left = hdvb ? left \
.ng_bighalo(subString1, x=subXpos, y=subYpos, \
align=subAlign, first_frame=subFF, last_frame=subFF+subLength, \
size=subSize, text_color=subColor, halo_color=$00000000, lsp=2) \
.ng_bighalo(subString2, x=subXpos, y=subYpos, \
align=subAlign, first_frame=subFF2, last_frame=subFF2+subLength, \
size=subSize, text_color=subColor, halo_color=$00000000, lsp=2) \
.ng_bighalo(subString3 + "\n" + subString4, x=subXpos, y=subYpos, \
align=subAlign, first_frame=subFF3, last_frame=(subFF3+SubLength), \
size=subSize, text_color=subColor, halo_color=$00000000, lsp=2) : 0
right = hdvb ? right \
.ng_bighalo(subString1, x=subXpos, y=subYpos, \
align=subAlign, first_frame=subFF, last_frame=subFF+subLength, \
size=subSize, text_color=subColor, halo_color=$00000000, lsp=2) \
.ng_bighalo(subString2, x=subXpos, y=subYpos, \
align=subAlign, first_frame=subFF2, last_frame=subFF2+subLength, \
size=subSize, text_color=subColor, halo_color=$00000000, lsp=2) \
.ng_bighalo(subString3 + "\n" + subString4, x=subXpos, y=subYpos, \
align=subAlign, first_frame=subFF3, last_frame=subFF3+SubLength, \
size=subSize, text_color=subColor, halo_color=$00000000, lsp=2) : 0
hdvb ? yt3d(left, right).AudioDub(left) : 0
Trim(0, trimFrame)
# pass == 1 ? assrender("subtitles.ass") : 0
# Youtube can't hanble fps above 60
hd && last.FrameRate > 60 ? ChangeFPS(60) : 0
halfFPS ? SelectOdd : 0
# YV24 conversion doesn't reduce chroma, so "point" is fine
ConvertToYV24(chromaresample="point", matrix=(hd \
? "Rec709" \
: (pass == 0 \
? "Rec601" \
: "PC.601")))
# Remove duplicate frames for primary
pass == 1 ? ExactDedup(firstpass=true, dupinfo="./temp/dup.txt",times="./temp/times.txt") : 0
pass == 2 ? ExactDedup(firstpass=false,dupinfo="./temp/dup.txt" ) : 0
# YV12 reduces chroma, so we use "lanczos" even for HD
# http://tasvideos.org/forum/viewtopic.php?t=19426
i444 ? last : ConvertToYV12(chromaresample="lanczos", matrix=(hd \
? "Rec709" \
: (pass == 0 \
? "Rec601" \
: "PC.601")))
pass != 1 ? Prefetch(threads) : last