Skip to content

Instantly share code, notes, and snippets.

@xenogenesi
Last active April 17, 2019 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xenogenesi/9361b6bc1520527dcffdea34c8fa5e45 to your computer and use it in GitHub Desktop.
Save xenogenesi/9361b6bc1520527dcffdea34c8fa5e45 to your computer and use it in GitHub Desktop.
lua extension for vlc to generate thumbnail of the current frame using ffmpeg
-- generate thumbnail jpeg of the current selected frame using ffmpeg
-- name convention for minidlna or synology
-- install in ~/.local/share/vlc/lua/extensions/Thumbnail2.lua
-- tested on linux only
function descriptor()
return {
title = "Thumbnail2 generator";
version = "1.0";
author = "Alex";
-- url = "";
shortdesc = "Thumbnail2 generator";
description = "Thumbnail2 generator call external program";
}
end
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hex_to_char)
end
local escape_shell_arg = function(url)
return "'"..url:gsub("'", "'\\''").."'"
end
function secondstostring(duration)
return string.format("%02d:%02d:%02d",
math.floor(duration/3600),
math.floor(duration/60)%60,
math.floor(duration%60))
end
function generate(uri, newuri)
local input = vlc.object.input()
if input then
local play_time = vlc.var.get(input, "time")
--local play_time = (vlc.var.get(input, "position") * vlc.var.get(input, "length"))
local play_time_ms = (play_time / 1000) % 1000
-- if tonumber(vlc.misc.version():match('^[0-9]*')) > 2 then
-- FIXME for some reason by default one hour is added (eg: 00:05 = 01:05)
--play_time = (play_time / 1000000) - ((1 * 60 * 60) + 1)
play_time = play_time / 1000000.
-- end
local play_time_string = secondstostring(play_time)..string.format(".%03d", play_time_ms)
--local play_time_string = os.date("%H:%M:%S", play_time)..string.format(".%03d", play_time_ms)
--vlc.msg.info("[Thumbnail2] elapsed time "..play_time)
vlc.msg.info("[Thumbnail2] elapsed time string "..play_time_string)
cmd = string.format('ffmpeg -y -ss %s -i %s -vframes 1 -q:v 2 -s 640x360 %s', play_time_string, escape_shell_arg(uri), escape_shell_arg(newuri))
vlc.msg.info("[Thumbnail2] executing: "..cmd)
assert(os.execute(cmd) == 0, cmd)
vlc.msg.info("[Thumbnail2] generated: "..newuri)
else
vlc.msg.info("[Thumbnail2] no input selected")
end
end
function click_generate()
local item = vlc.input.item()
local uri = item:uri()
uri = unescape(item:uri())
-- FIXME perhaps should check if the uri is file:// not assume
uri = string.gsub(uri, '^file://', '')
local newuri = uri..'.cover.jpg'
--uri = string.gsub(uri, '/', '\\')
vlc.msg.info("[Thumbnail2] item name "..uri)
generate(uri, newuri)
end
function click_generate2()
local item = vlc.input.item()
local uri = item:uri()
uri = unescape(item:uri())
-- FIXME perhaps should check if the uri is file:// not assume
uri = string.gsub(uri, '^file://', '')
local ext = uri:match("[^.]+$")
local newuri = uri:sub(0, string.len(uri)-string.len(ext))..'jpg'
--uri = string.gsub(uri, '/', '\\')
vlc.msg.info("[Thumbnail2] item name "..uri)
generate(uri, newuri)
end
function activate()
vlc.msg.info("[Thumbnail2] activate")
dlg = vlc.dialog(descriptor().title)
dlg:add_button("MiniDLNA (.cover.jpg)", click_generate,1,1,1,1)
dlg:add_button("replace ext .jpg", click_generate2,2,1,1,1)
end
function close()
vlc.msg.info("[Thumbnail2] close")
vlc.deactivate()
end
function deactivate()
vlc.msg.info("[Thumbnail2] deactivate")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment