Skip to content

Commit

Permalink
Properly support different encoders and extensions
Browse files Browse the repository at this point in the history
Also fixes the output_template documentation, expects ${ext} in the template
and warns if it's missing.
  • Loading branch information
TheAMM committed May 9, 2018
1 parent a22bc4f commit 42152c8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
39 changes: 35 additions & 4 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ function expand_output_path(cropbox)
local filename = mp.get_property_native("filename")
local playback_time = mp.get_property_native("playback-time")

local filename_without_ext, extension = filename:match("^(.+)%.(.-)$")

local properties = {
path = mp.get_property_native("path"), -- Original path

filename = filename:match("^(.+)%..+$"), -- Filename without extension
file_ext = filename:gsub("^(.+)%..+$", ""), -- Original extension with leading dot
filename = filename_without_ext, -- Filename without extension
file_ext = extension or "", -- Original extension without leading dot

pos = mp.get_property_native("playback-time"),

Expand All @@ -60,7 +62,7 @@ function expand_output_path(cropbox)

unique = 0,

ext = option_values.output_format
ext = option_values.output_extension
}
local propex = PropertyExpander(MPVPropertySource(properties))

Expand Down Expand Up @@ -159,7 +161,8 @@ function screenshot(crop)
args = {
"mpv", input_path,
"--vf=crop=" .. crop_string,
"--frames=1", "--ovc=png",
"--frames=1",
"--ovc=" .. option_values.output_format,
"-o", output_path
}
}
Expand All @@ -186,6 +189,34 @@ end
-- Instances, binds --
----------------------

-- Sanity-check output_template
if option_values.warn_about_template and not option_values.output_template:find('%${ext}') then
msg.warn("Output template missing ${ext}! If this is desired, set warn_about_template=yes in config!")
end

-- Short list of extensions for encoders
local ENCODER_EXTENSION_MAP = {
png = "png",
mjpeg = "jpg",
targa = "tga",
tiff = "tiff",
gif = "gif", -- please don't
bmp = "bmp",
jpegls = "jpg",
ljpeg = "jpg",
jpeg2000 = "jp2",
}
-- Pick an extension if one was not provided
if option_values.output_extension == "" then
local extension = ENCODER_EXTENSION_MAP[option_values.output_format]
if not extension then
msg.error("Unrecognized output format '" .. option_values.output_format .. "', unable to pick an extension! Bailing!")
mp.osd_message("mpv_crop_script was unable to choose an extension, check your config", 3)
end
option_values.output_extension = extension
end


display_state = DisplayState()
asscropper = ASSCropper(display_state)
asscropper.overlay_transparency = option_values.overlay_transparency
Expand Down
26 changes: 23 additions & 3 deletions src/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,28 @@ local option_values = script_options.values
script_options:add_options({
{nil, nil, "mpv_crop_script.lua options and default values"},
{nil, nil, "Output options #", true},
{"output_template", "${filename} ${#pos:%02h.%02m.%06.3s} ${!full:${crop_w}x${crop_h} ${%unique:%03d}}.png",
"Directory to save (temporary) encode scripts in. Same default as with output_directory"},
{"output_template", "${filename} ${#pos:%02h.%02m.%06.3s} ${!full:${crop_w}x${crop_h} ${%unique:%03d}}.${ext}",
"Filename output template. See README.md for property expansion documentation."},
{nil, nil, [[Script-provided properties:
filename - filename without extension
file_ext - original extension without leading dot
path - original file path
pos - playback time
ext - output file extension without leading dot
crop_w - crop width
crop_h - crop height
crop_x - left
crop_y - top
crop_x2 - right
crop_y2 - bottom
full - boolean denoting a full (temporary) screenshot instead of crop
unique - counter that will increase per each existing filename, until a unique name is found]]},

{"output_format", "png",
"Format (encoder) to save final crops in"},
"Format (encoder) to save final crops in. For example, png, mjpeg, targa, bmp"},
{"output_extension", "",
"Output extension. Leave blank to try to choose from the encoder (if supported)"},

{"create_directories", false,
"Whether to create the directories in the final output path (defined by output_template)"},
{"skip_screenshot_for_images", true,
Expand All @@ -39,6 +57,8 @@ script_options:add_options({
"Try to check if video is light or dark upon opening crop tool, and invert the colors if necessary"},

{nil, nil, "Misc options #", true},
{"warn_about_template", true,
"Warn about output_template missing ${ext}, to ensure the extension is not missing"},
{"disable_keybind", false,
"Disable the built-in keybind"}
})
Expand Down

0 comments on commit 42152c8

Please sign in to comment.