Skip to content

Commit e2df95d

Browse files
huge refactor (#20)
* huge refactor fix #17
1 parent 3599f09 commit e2df95d

3 files changed

Lines changed: 109 additions & 87 deletions

File tree

encode.avs

Lines changed: 96 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ Import(ScriptDir() + "programs/functions.avsi")
77

88
# "RGB24" for FFV1, "AUTO" for the rest
99
pixelType = "RGB24"
10-
1110
AviSource("movie.avi", pixel_type=pixelType)
1211

1312
# Dolphin (with FFMPEG). Use FFV1 patch for dumping:
1413
# https://github.com/vadosnaprimer/dolphin/releases
1514
# AviSource("movie.avi", pixel_type="RGB24")
1615
# AudioDub(last, wavsource("dspdump.wav"))
1716

18-
threads = 8 # manual, how many threads avisynth should use.
1917
trimFrame = 654321 # manual, discards logo lengh automatically
20-
handHeld = false # auto, but set it for preview
18+
ARCorrection = false # aspect ratio correction. auto, but set it for preview
2119
hd = false # auto, but set it for preview
2220
halfFPS = false # for games that "lag" every other frame
23-
prescaleFactor = 2 # set back to 1 if source is already 480p or 480i
21+
threads = 8 # manual, how many threads avisynth should use.
22+
23+
# if the script guessed wrong, force the right factor by setting to non-zero
24+
# for Dolphin footage, force 1
25+
prescaleOverride = 0
2426

2527
# Resizer (for hd upscaling and multisegment import)
2628
resizer = hd ? "Point" : "Lanczos"
@@ -37,24 +39,24 @@ rerecords = "0"
3739
# Subtitles timing and placement
3840
subFF = 2689 # first subtitles frame, set manually!
3941
subAlign = 8 # subtitles horizontal alignment (7/8/9)
40-
subEntry = 3 # sets the number of sub entries (2/3/4)
4142
subYpc = 1 # subtitles vertical position in percents of video height
4243
subXpc = 1 # subtitles horizontal position in percents of video width (can be negative)
43-
subSizepc = 5 # subtitles font size in percents of smaller video side
44-
subLengthMul = 5 # entry length in seconds (fractions work)
44+
subEntry = 4 # sets the number of sub entries (2/3/4)
4545
subFF2delay = 0 # extra delay in frames between subtitle entries 1 and 2
4646
subFF3delay = 0 # extra delay in frames between subtitle entries 2 and 3
4747
subFF4delay = 0 # extra delay in frames between subtitle entries 3 and 4
48+
subLengthMul = 5 # entry length in seconds (fractions work)
49+
subSizepc = 5 # subtitles font size in percents of smaller video side
4850

4951
# Multisegment import (upscales hires segments straight to HD when needed)
5052
# requires normally importing a sample whose attributes it will use for all segments
5153
ms = false # enable multisegment import
52-
ms_base = "movie_" # common string for all segment names
53-
ms_start = 0 # number of the first segment
54-
ms_end = 15 # number of the last segment
55-
ms_format = "%1.0f" # string format: http://avisynth.nl/index.php/Internal_functions#String
56-
ms_audio = false # use separate audio file for video
57-
ms_audiotrack = "PSXjin.wav" # path to audio file
54+
msBase = "movie_" # common string for all segment names
55+
msStart = 0 # number of the first segment
56+
msEnd = 15 # number of the last segment
57+
msFormat = "%1.0f" # string format: http://avisynth.nl/index.php/Internal_functions#String
58+
msAudio = false # use separate audio file for video
59+
msAudiotrack = "PSXjin.wav" # path to audio file
5860

5961
# Aspect ratio correction
6062
wAspect = 4
@@ -123,51 +125,66 @@ avDesyncFixer ? AssumeSampleRate(Round(num / denom)).ResampleAudio(48000) : 0
123125
# avDesyncFixer ? AssumeFPS(last.FrameCount / last.AudioLengthF * last.AudioRate) : 0
124126

125127
# Pick logo file
126-
file = hd \
127-
? "hdlogo.png" \
128-
: "logo.png"
129-
# file = !hd ? "logo34.png" : "hdlogo34.png"
128+
file = hd ? "hdlogo.png" : "logo.png"
129+
# file = hd ? "hdlogo34.png" : "logo34.png"
130+
131+
# Prescale lowres by 4, mid res by 2, and the rest ignore
132+
magicNumerator = 768
130133

131-
prescaleFactor > 1 && !hd ? \
132-
PointResize(last.width * prescaleFactor, last.height * prescaleFactor) : 0
134+
if (prescaleOverride > 0) {
135+
prescaleFactor = prescaleOverride
136+
} else {
137+
prescaleFactor = int(magicNumerator / last.height)
138+
}
139+
140+
# 3 is banned due to chroma subsampling, use 2 instead
141+
prescaleFactor = prescaleFactor.ForceModulo(2, false)
142+
143+
if (prescaleFactor > 1 && !hd) {
144+
PointResize(last.width * prescaleFactor, last.height * prescaleFactor)
145+
}
133146

134147
# Aspect ratio correction for SD encodes
135-
height = last.height
136-
width = handHeld \
137-
? last.width \
138-
: height * wAspect / hAspect
139-
width = width % 4 == 1 ? width + 3 \
140-
: width % 4 == 2 ? width + 2 \
141-
: width % 4 == 3 ? width + 1 : width
142-
height = height % 4 == 1 ? height + 3 \
143-
: height % 4 == 2 ? height + 2 \
144-
: height % 4 == 3 ? height + 1 : height
148+
# if dimensions are not multiples of 4, codecs freak out, so we enforce mod 4
149+
mod = 4
150+
hdHeight= 2160
151+
height = last.height.ForceModulo(mod, true)
152+
width = (ARCorrection \
153+
? height * wAspect / hAspect \
154+
: last.width) \
155+
.ForceModulo(mod, true)
156+
145157
# Remember SD frame size for subYpos HD tweaks
146158
wARC = width
147159
hARC = height
160+
148161
# Actually go HD if we need
149-
height = hd ? 2160 : height
150-
width = handHeld \
151-
? height * last.width / last.height \
152-
: height * wAspect / hAspect
153-
width = width % 4 == 1 ? width + 3 : \
154-
width % 4 == 2 ? width + 2 : \
155-
width % 4 == 3 ? width + 1 : width
156-
hStretch = height / last.height
162+
if (hd) {
163+
height = hdHeight
164+
}
165+
166+
width = (ARCorrection \
167+
? height * wAspect / hAspect \
168+
: height * last.width / last.height) \
169+
.ForceModulo(mod, true)
170+
hStretch= height / last.height
157171

158172
# Rescaling
159173
# hd: resize to 4K, then just subsample with lanczos in the end
160174
# handheld: do nothing
161175
# 480p: do ARC with lanczos
162-
resized = hd \
163-
? eval(resizer+"Resize(width, height)") \
164-
: handHeld \
165-
? last \
166-
: Lanczos4Resize(width, height)
176+
resized = hd || ARCorrection \
177+
? Eval((hd ? resizer : "Lanczos4") + "Resize(width, height)") \
178+
: last
167179

168180
# If ms enabled, we use parameters of "resized" to apply to all segments
169-
resized = ms ? resized.AppendSegment(ms_base, ms_start, ms_end, ms_format, resizer, hd, pixelType).ConvertToRGB32() : resized
170-
resized = ms_audio ? AudioDub(resized, WavSource(ms_audiotrack)) : resized
181+
if (ms) {
182+
resized = resized.AppendSegment(msBase, msStart, msEnd, msFormat, resizer, hd, pixelType).ConvertToRGB32()
183+
184+
if (msAudio) {
185+
resized = AudioDub(resized, WavSource(msAudiotrack))
186+
}
187+
}
171188

172189
# Logo
173190
logoVideo = ImageSource(file=file, start=0, end=int((resized.FrameRate * 2) - 1), fps=resized.FrameRate) \
@@ -177,14 +194,16 @@ logo = AudioDub(logoVideo, logoAudio).Lanczos4Resize(resized.width, resized.heig
177194
last = logo ++ resized
178195

179196
# Subtitles variables
180-
subXpos = float(last.width) / 100 * subXpc
181-
subXpos = subAlign == 7 || subAlign == 4 || subAlign == 1 \
182-
? subXpos \
183-
: subAlign == 8 || subAlign == 5 || subAlign == 2 \
184-
? last.width / 2 + subXpos \
185-
: last.width - subXpos
197+
subXpos = float(last.width) / 100 * subXpc
198+
199+
if (subAlign == 9 || subAlign == 6 || subAlign == 3) {
200+
subXpos = last.width - subXpos
201+
} else if (subAlign == 8 || subAlign == 5 || subAlign == 2) {
202+
subXpos = last.width / 2 + subXpos
203+
}
204+
186205
subYpos = float(last.height) / 100 * subYpc
187-
smallerSide = last.height < last.width ? last.height : last.width
206+
smallerSide = min(last.height, last.width)
188207
subSize = float(smallerSide) / 100 * subSizepc
189208
subHaloSize = floor(subSize / 7)
190209
subLength = int(last.FrameRate * subLengthMul)
@@ -194,41 +213,33 @@ subFF4 = subFF3 + subLength + 1 + subFF4delay
194213
subColor = $00FFFFFF
195214
subRadius = hd ? 0 : subHaloSize
196215

216+
if (subEntry == 2) {
217+
subString1 = subString1 + "\n" + SubString2
218+
SubString2 = subString3 + "\n" + subString4
219+
} else if (subEntry == 3) {
220+
subString3 = subString3 + "\n" + subString4
221+
}
222+
197223
# Subtitles functions
198-
subEntry == 2 ? ng_bighalo(subString1 + "\n" + SubString2, \
199-
x=subXpos, y=subYpos, align=subAlign, first_frame=subFF, \
200-
last_frame=subFF+subLength, size=subSize, \
201-
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) : 0
202-
203-
subEntry == 2 ? ng_bighalo(subString3 + "\n" + subString4, \
204-
x=subXpos, y=subYpos, align=subAlign, first_frame=subff2, \
205-
last_frame=subff2+subLength, size=subSize, \
206-
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) : 0
207-
208-
subEntry == 3 || subEntry == 4 ? ng_bighalo(subString1, \
209-
x=subXpos, y=subYpos, align=subAlign, first_frame=subFF, \
210-
last_frame=subFF+subLength, size=subSize, \
211-
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) : 0
212-
213-
subEntry == 3 || subEntry == 4 ? ng_bighalo(subString2, \
214-
x=subXpos, y=subYpos, align=subAlign, first_frame=subff2, \
215-
last_frame=subff2+subLength, size=subSize, \
216-
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) : 0
217-
218-
subEntry == 3 ? ng_bighalo(subString3 + "\n" + subString4, \
219-
x=subXpos, y=subYpos, align=subAlign, first_frame=subFF3, \
220-
last_frame=subFF3+subLength, size=subSize, \
221-
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) : 0
222-
223-
subEntry == 4 ? ng_bighalo(subString3, \
224-
x=subXpos, y=subYpos, align=subAlign, first_frame=subFF3, \
225-
last_frame=subFF3+subLength, size=subSize, \
226-
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) : 0
227-
228-
subEntry == 4 ? ng_bighalo(subString4, \
229-
x=subXpos, y=subYpos, align=subAlign, first_frame=subFF4, \
230-
last_frame=subFF4+subLength, size=subSize, \
231-
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius) : 0
224+
ng_bighalo(subString1, x=subXpos, y=subYpos, align=subAlign, \
225+
first_frame=subFF, last_frame=subFF+subLength, size=subSize, \
226+
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius)
227+
228+
ng_bighalo(SubString2, x=subXpos, y=subYpos, align=subAlign, \
229+
first_frame=subff2, last_frame=subff2+subLength, size=subSize, \
230+
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius)
231+
232+
if (subEntry == 3 || subEntry == 4) {
233+
ng_bighalo(subString3, x=subXpos, y=subYpos, align=subAlign, \
234+
first_frame=subFF3, last_frame=subFF3+subLength, size=subSize, \
235+
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius)
236+
}
237+
238+
if (subEntry == 4) {
239+
ng_bighalo(subString4, x=subXpos, y=subYpos, align=subAlign, \
240+
first_frame=subFF4, last_frame=subFF4+subLength, size=subSize, \
241+
text_color=subColor, halo_color=$00000000, lsp=2, halo_radius=subRadius)
242+
}
232243

233244
Trim(0, trimFrame)
234245

global.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ for /f "tokens=2 skip=2 delims== " %%G in ('find "wAspect = " "%~dp0encode.avs"'
8787
for /f "tokens=2 skip=2 delims== " %%G in ('find "hAspect = " "%~dp0encode.avs"') do (set current_hAspect=%%G)
8888
".\programs\replacetext" "encode.avs" "hAspect = %current_hAspect%" "hAspect = %ar_h%"
8989

90-
".\programs\replacetext" "encode.avs" "handHeld = true" "handHeld = false"
90+
".\programs\replacetext" "encode.avs" "ARCorrection = false" "ARCorrection = true"
9191
".\programs\ffprobe" -hide_banner -v error -select_streams v -of default -show_entries stream=width,height,r_frame_rate encode.avs > ".\temp\info.txt"
9292

9393
for /f "tokens=2 delims==" %%G in ('FINDSTR "width" "%~dp0temp\info.txt"') do (set width=%%G)
@@ -101,7 +101,7 @@ goto ENCODE_OPTIONS
101101

102102
: handHeld_SAR
103103
set VAR=1:1
104-
".\programs\replacetext" "encode.avs" "handHeld = false" "handHeld = true"
104+
".\programs\replacetext" "encode.avs" "ARCorrection = true" "ARCorrection = false"
105105
".\programs\ffprobe" -hide_banner -v error -select_streams v -of default -show_entries stream=r_frame_rate encode.avs > ".\temp\info.txt"
106106
goto ENCODE_OPTIONS
107107

programs/functions.avsi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ function AppendSegment(
4040
return result
4141
}
4242

43+
# rounds an integer up or down to the nearest multiple of mod
44+
function ForceModulo(
45+
\ int number,
46+
\ int mod,
47+
\ bool up
48+
\){
49+
return (up \
50+
? (int(number + mod - 1) / mod) * mod \
51+
: int(number / mod) * mod)
52+
}
53+
4354
function Remove(
4455
\ clip c,
4556
\ int start,

0 commit comments

Comments
 (0)