Skip to content

Commit

Permalink
[EncodeClip] Add option for context in video clips (1.1.0) (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
petzku authored Apr 25, 2024
1 parent 2fcb61f commit 83c43be
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 61 deletions.
54 changes: 5 additions & 49 deletions DependencyControl.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@
"description": "Encode a hardsubbed clip encompassing the current selection",
"channels": {
"stable": {
"version": "1.0.0",
"released": "2023-11-20",
"version": "1.1.0",
"released": "2024-04-25",
"default": true,
"files": [
{
"name": ".lua",
"url": "@{fileBaseUrl}@{fileName}",
"sha1": "be8a4b972c4430e36d6750c8a1a2ce9be84f1f77"
"sha1": "b1382a5898cc467b14f94a26846222e51f6fba3d"
}
],
"requiredModules": [
Expand All @@ -168,55 +168,11 @@
}
},
"changelog": {
"0.3.0": ["Add to DependencyControl"],
"0.4.0": [
"Make DependencyControl optional",
"Fallback to video file location if missing subtitle file",
"Fix major video artifacting in non-mpv players"
],
"0.5.0": [
"Add GUI",
"Add macros for audioless clips and audio-only clips",
"Prefer non-ffmpeg AAC encoders, raise bitrate to 256k"
],
"0.5.3": [
"Add keyboard shortcuts in GUI",
"Add short README inside script"
],
"0.6.0": [
"Add configuration support",
"Move shell execution to petzku.util, and require the module"
],
"0.6.1": [
"Fix button ordering in GUI (0.6.0 messed this up, now back to audio-video-config-cancel)",
"Bump petzku.util dependency to 0.3.0"
],
"0.7.0": ["Add audio encoder setting to config menu"],
"0.7.1": ["Fix handling of spaces in mpv path"],
"0.8.0": [
"Allow user-selectable mpv executable",
"Notify user if non-ASCII filenames cause errors and suggest fixes (Windows)"
],
"0.8.1": ["Output audio-only clips as .m4a instead of .aac"],
"0.8.2": [
"Warn user when file not saved (if on appropriate aegisub versions)",
"Default to encoding video on Enter press"
],
"0.8.4": [
"Fix handling of audio when from a file different from the video"
],
"0.8.5": ["Bump util dependency to 0.4.1"],
"0.9.0": [
"Add option in config to default to a certain audio track ID (disabled by default)",
"Refactor command generation logic",
"Fix some issues with default mpv options",
"Change GUI Config and Cancel hotkeys (from G/C to C/N)",
"Improve Config GUI order and labels"
],
"1.0.0": [
"Proper 1.0 release finally! (it was about time)",
"Add dummy video encoding using lavfi (doesn't support checkerboard)"
]
],
"1.1.0": ["Option for adding short context to clips"]
}
},
"petzku.ExtrapolateMove": {
Expand Down
43 changes: 31 additions & 12 deletions macros/petzku.EncodeClip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ script_name = tr'Encode Clip'
script_description = tr'Encode various clips from the current selection'
script_author = 'petzku'
script_namespace = "petzku.EncodeClip"
script_version = '1.0.0'
script_version = '1.1.0'


local haveDepCtrl, DependencyControl, depctrl = pcall(require, "l0.DependencyControl")
Expand Down Expand Up @@ -94,23 +94,32 @@ If set, the value given to the right will be supplied to --aid.]]
Supplied to mpv as --aid, so this is indexed starting from 1. Supplying an out-of-bounds track ID will cause no audio to be included.
If you want to consistently select by language, just use --alang in the config sections above.]]
},
context_duration_label = {
class='label', label='Extra context duration for clips (in seconds):',
x=0, y=3, width=10, height=1
},
context_duration = {
class='floatedit', value=2, config=true, min=0, max=30,
x=10, y=3, width=10, height=1,
hint=[[Extra duration (in seconds) to add at the start and end of a clip. Limited to 30 seconds.]]
},
video_command_label = {
class='label', label='Custom mpv options for video clips:',
x=0, y=3, width=10, height=1
x=0, y=4, width=10, height=1
},
video_command = {
class='textbox', value="", config=true,
x=0, y=4, width=20, height=3,
x=0, y=5, width=20, height=3,
hint=[[Custom command line options passed to mpv when encoding video.
You can put options on separate lines, but all options must be prefixed with --. (e.g. "--aid=2" to pick the second audio track in the file)]]
},
audio_command_label = {
class='label', label='Custom mpv options for audio-only clips:',
x=0, y=7, width=10, height=1
x=0, y=8, width=10, height=1
},
audio_command = {
class='textbox', value="", config=true,
x=0, y=8, width=20, height=3,
x=0, y=9, width=20, height=3,
hint=[[Custom command line options passed to mpv when encoding only audio.
Options here do NOT get applied when encoding video, whether it has audio or not.]]
}
Expand All @@ -131,6 +140,11 @@ local GUI = {
class='checkbox', label=tr"&Audio", value=true, name='audio',
x=2, y=0,
hint=tr[[Enable audio in output]]
},
context = {
class='checkbox', label=tr"Conte&xt", value=false, name='context',
x=3, y=0,
hint=tr[[Include a few seconds of context at the start and end of the clip]]
}
},
-- constants for the buttons
Expand Down Expand Up @@ -225,12 +239,17 @@ local function get_base_outfile(t1, t2, ext)
return outfile, cant_hardsub
end

local function calc_start_end(subs, sel)
local function calc_start_end(subs, sel, ctx)
local t1, t2 = math.huge, 0
for _, i in ipairs(sel) do
t1 = math.min(t1, subs[i].start_time)
t2 = math.max(t2, subs[i].end_time)
end
if ctx then
local ctx_dur = math.floor(get_configuration().context_duration * 1000)
t1 = math.max(0, t1 - ctx_dur)
t2 = t2 + ctx_dur
end
return t1/1000, t2/1000
end

Expand Down Expand Up @@ -308,10 +327,10 @@ local function build_cmd(user_opts, ...)
return table.concat(cmd_table, ' ')
end

function make_clip(subs, sel, hardsub, audio)
function make_clip(subs, sel, hardsub, audio, context)
if audio == nil then audio = true end --encode with audio by default

local t1, t2 = calc_start_end(subs, sel)
local t1, t2 = calc_start_end(subs, sel, context)

local props = aegisub.project_properties()
local vidfile = props.video_file
Expand Down Expand Up @@ -387,8 +406,8 @@ Press Enter to proceed anyway, or Escape to cancel.]], "Encode &anyway") then
run_cmd(cmd)
end

function make_audio_clip(subs, sel)
local t1, t2 = calc_start_end(subs, sel)
function make_audio_clip(subs, sel, context)
local t1, t2 = calc_start_end(subs, sel, context)

local props = aegisub.project_properties()
local audiofile = props.audio_file
Expand Down Expand Up @@ -417,9 +436,9 @@ function show_dialog(subs, sel)
local btn, values = aegisub.dialog.display(GUI.main, buttons, {ok=GUI.BUTTONS.VIDEO, cancel=GUI.BUTTONS.CANCEL})

if btn == GUI.BUTTONS.AUDIO then
make_audio_clip(subs, sel)
make_audio_clip(subs, sel, values.context)
elseif btn == GUI.BUTTONS.VIDEO then
make_clip(subs, sel, values.subs, values.audio)
make_clip(subs, sel, values.subs, values.audio, values.context)
elseif btn == GUI.BUTTONS.CONFIG then
show_config_dialog()
-- once config is done, re-open this dialog
Expand Down

0 comments on commit 83c43be

Please sign in to comment.