From 42152c818709caef10e16e5def7ba7da1bf5cce2 Mon Sep 17 00:00:00 2001 From: AMM Date: Wed, 9 May 2018 02:11:49 +0300 Subject: [PATCH] Properly support different encoders and extensions Also fixes the output_template documentation, expects ${ext} in the template and warns if it's missing. --- src/main.lua | 39 +++++++++++++++++++++++++++++++++++---- src/options.lua | 26 +++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/main.lua b/src/main.lua index bc3e1a9..833a6da 100644 --- a/src/main.lua +++ b/src/main.lua @@ -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"), @@ -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)) @@ -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 } } @@ -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 diff --git a/src/options.lua b/src/options.lua index fc72147..70e1251 100644 --- a/src/options.lua +++ b/src/options.lua @@ -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, @@ -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"} })