Vous êtes sur la page 1sur 2

#!

/usr/bin/lua
require"strict"
require"lfs"

local sprintf=string.format

local ARGS=arg
local function u2d(path) return path:gsub("/","\\") end
local function d2u(path) return path:gsub("\\","/") end

MKVMERGE=[[D:\Programme\mkvtoolnix-amd64-6.9.1\mkvtoolnix\mkvmerge.exe]]

local function printf(...)


local ok,msg=pcall(sprintf,...)
io.write(msg)
io.flush()
end

local function Error(...)


local ok,msg=pcall(sprintf,...)
io.stderr:write(msg)
io.stderr:flush()
os.exit(1)
end

local function dirname(path)


return path:match("^(.+)/") or "."
end

local function is_dir(path)


if not path then return nil,"path is nil" end
if lfs.attributes(path,"mode")=="directory" then return path end
return nil,path.." is no dir"
end

local function save_file(path,...)


local fd=assert(io.open(path,"wb"))
fd:write(...)
fd:close()
end

local function do_cmd(...)


local ok,msg=pcall(sprintf,...)
if not ok then error(msg,2) end
printf(">%s\n",msg)
local fd=assert(io.popen(msg,"r"))
local lines={}
for line in fd:lines() do
lines[#lines+1]=line
printf("line=%q\n",line)
end
fd:close()
return lines
end

local function v2m(VIDEO_TS_DIR)


if not is_dir(VIDEO_TS_DIR) then Error("arg %q is no dir\n",VIDEO_TS_DIR) end
if not VIDEO_TS_DIR:match("/VIDEO_TS$") then Error("arg %q has not
'/VIDEO_TS'\n",VIDEO_TS_DIR) end
local DST_DIR=VIDEO_TS_DIR:gsub("/VIDEO_TS$","")
local entries={}
for entry in lfs.dir(VIDEO_TS_DIR) do
if entry~="." and entry~=".." then
entries[#entries+1]=entry
end
end
table.sort(entries)
for e,entry in ipairs(entries) do
local num=entry:match("VTS_(%d+)_1.VOB")
if num then
num=assert(tonumber(num,10))
printf("#%d entry=%q\n",num,entry)
-- local chapters=do_cmd('dvdxchap --title %d
"%s"',num,VIDEO_TS_DIR)
-- local chapter_file=sprintf("/tmp/chapters-%02d.txt",num)
-- save_file(chapter_file,table.concat(chapters,"\n"),"\n")
local mkv_file=sprintf("%s/title-%02d.mkv",DST_DIR,num)
--do_cmd('mkvmerge "%s/%s" --chapters %q -o
%q',VIDEO_TS_DIR,entry,chapter_file,mkv_file)
do_cmd('echo && "%s" "%s" -o "%s"
2>&1',MKVMERGE,u2d(VIDEO_TS_DIR.."/"..entry),u2d(mkv_file))
end
end
end

for a,arg in ipairs(ARGS) do


arg=d2u(arg)
local video_ts_dir =is_dir(arg:match("^(.*/VIDEO_TS)$"))
or is_dir(arg.."/VIDEO_TS")
or is_dir(dirname(arg).."/VIDEO_TS")
or Error("Could not find VIDEO_TS in %q\n",arg)

v2m(video_ts_dir)
end

printf("!! SCRIPT DONE !!\n")