Skip to content

Commit

Permalink
Optimize cropping image files (skip screenshot)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAMM committed May 8, 2018
1 parent 68f89f3 commit a22bc4f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
67 changes: 41 additions & 26 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ function expand_output_path(cropbox)
properties.unique = 1
if propex:expand(option_values.output_template) == test_path then
properties.full = true
local output_path_full = propex:expand(option_values.output_template)
return test_path, output_path_full
local temporary_screenshot_path = propex:expand(option_values.output_template)
return test_path, temporary_screenshot_path

else
-- Figure out an unique filename
Expand All @@ -82,8 +82,8 @@ function expand_output_path(cropbox)
-- Check if filename is free
if not path_exists(test_path) then
properties.full = true
local output_path_full = propex:expand(option_values.output_template)
return test_path, output_path_full
local temporary_screenshot_path = propex:expand(option_values.output_template)
return test_path, temporary_screenshot_path
else
-- Try the next one
properties.unique = properties.unique + 1
Expand All @@ -102,13 +102,13 @@ function screenshot(crop)
return
end

local output_path, output_path_full = expand_output_path(crop)
local output_path, temporary_screenshot_path = expand_output_path(crop)

-- Optionally create directories
if option_values.create_directories then
local paths = {}
paths[1] = split_path(output_path)
paths[2] = split_path(output_path_full)
paths[2] = split_path(temporary_screenshot_path)

-- Check if we can read the paths
for i, path in ipairs(paths) do
Expand All @@ -119,30 +119,45 @@ function screenshot(crop)
end
end

-- In case the full-size output path is identical to the crop path,
-- crudely make it different
if output_path_full == output_path then
output_path_full = output_path_full .. "_full.png"
end
local playback_time = mp.get_property_native("playback-time")
local duration = mp.get_property_native("duration")

-- Temporarily lower the PNG compression
local previous_png_compression = mp.get_property_native("screenshot-png-compression")
mp.set_property_native("screenshot-png-compression", 0)
-- Take the screenshot
mp.commandv("raw", "no-osd", "screenshot-to-file", output_path_full)
-- Return the previous value
mp.set_property_native("screenshot-png-compression", previous_png_compression)

if not path_exists(output_path_full) then
msg.error("Failed to take screenshot: " .. output_path_full)
mp.osd_message("Unable to save screenshot")
return
local input_path = nil

if option_values.skip_screenshot_for_images and duration == 0 and playback_time == 0 then
-- Seems to be an image (or at least static file)
input_path = mp.get_property_native("path")
temporary_screenshot_path = nil
else
-- Not an image, take a temporary screenshot

-- In case the full-size output path is identical to the crop path,
-- crudely make it different
if temporary_screenshot_path == output_path then
temporary_screenshot_path = temporary_screenshot_path .. "_full.png"
end

-- Temporarily lower the PNG compression
local previous_png_compression = mp.get_property_native("screenshot-png-compression")
mp.set_property_native("screenshot-png-compression", 0)
-- Take the screenshot
mp.commandv("raw", "no-osd", "screenshot-to-file", temporary_screenshot_path)
-- Return the previous value
mp.set_property_native("screenshot-png-compression", previous_png_compression)

if not path_exists(temporary_screenshot_path) then
msg.error("Failed to take screenshot: " .. temporary_screenshot_path)
mp.osd_message("Unable to save screenshot")
return
end

input_path = temporary_screenshot_path
end

local crop_string = string.format("%d:%d:%d:%d", crop.w, crop.h, crop.x, crop.y)
local cmd = {
args = {
"mpv", output_path_full,
"mpv", input_path,
"--vf=crop=" .. crop_string,
"--frames=1", "--ovc=png",
"-o", output_path
Expand All @@ -152,8 +167,8 @@ function screenshot(crop)
msg.info("Cropping: ", crop_string, output_path)
local ret = utils.subprocess(cmd)

if not option_values.keep_original then
os.remove(output_path_full)
if not option_values.keep_original and temporary_screenshot_path then
os.remove(temporary_screenshot_path)
end

if ret.error or ret.status ~= 0 then
Expand Down
2 changes: 2 additions & 0 deletions src/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ script_options:add_options({
"Format (encoder) to save final crops in"},
{"create_directories", false,
"Whether to create the directories in the final output path (defined by output_template)"},
{"skip_screenshot_for_images", true,
"If the current file is an image, skip taking a temporary screenshot and crop the image directly"},
{"keep_original", false,
"Keep the full-sized temporary screenshot as well"},

Expand Down

0 comments on commit a22bc4f

Please sign in to comment.