News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
access to media files on a LAN server
Author Message
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #1
RE: access to media files on a LAN server

The subtitle feature seems to be a little flaky on Windows smplayer, don't know about Mac. I have two movies that include *.srt files; both work on Linux mplayer, but one works on Windows smplayer.

The current version allows for an optional configuration file, so you can change the player and host once and not have to do it again every time the script is updated. The file's format is lua variables (the file is parsed as a lua chunk).

The current available identifiers are player, subtitle_flag, and host.
I have not found a way yet in Windows to use the standard path name (including stupid spaces) for player, so I'm using "Progra~1\\...." (also note the escaped backslash).
The subtitle_flag variable only comes into play when srt files are found, and it might not work as currently scripted for a player that uses the "flag=value" format. The whole subtitle feature is a bit messy at this time. It will probably cause problems with some players.
The host variable is a URL pointing to the directory containing the server's media files, not including the trailing slash (though I don't yet know how a trailing slash might affect the script).

Note, as I probably said before, that even little changes in the way a server indexes directories might determine whether or not this script fails. I have tried to improve that part a bit, but i'm sure it's far from perfect.

This version also adds an update feature. The way this feature works will probably seem strange (updates are retrieved from your own server), but it was added only so that I could more easily update the same script on two different OSs for testing. It does not get updates from the internet.

Code:
#!/usr/bin/env murgaLua

-- access media files on a file server & stream in a player
-- mikshaw 2010

local VERSION=3

-- check for configuration file
local myos,cfg=murgaLua.getHostOsName()
if myos=="windows" then cfg=os.getenv("USERPROFILE").."\\media.lua.cfg"
else cfg=os.getenv("HOME").."/.media.lua.cfg" end
local get_cfg=loadfile(cfg)
if get_cfg then get_cfg() end

-- apply configuration variables
if myos=="windows" then
player=player or "C:\\Progra~1\\SMPlayer\\smplayer.exe"
else
player=player or "mplayer"
end
subtitle_flag=subtitle_flag or "-sub"
host=host or "http://192.168.1.101/media"

function update_me()
local remote_file=host.."/media.lua"
local body,code,header=socket.http.request(remote_file)
if code~=200 then fltk.fl_alert("I couldn't check for updates for some reason.\nNo biggie.")
return end
local current_version=tonumber(string.match(body,"VERSION=([%d]*)"))
if current_version > VERSION then
  local ask=fltk.fl_choice("There is a newer version available.\nDo you want to update?","no","yes",NULL)
  if ask==1 then
    local outpoot=io.open(arg[0],"w")
    if outpoot then
      outpoot:write(body)
      outpoot:close()
      fltk.fl_message("Update successful.\nPlease restart the program.")
    else fltk.fl_alert("Sorry, but I couldn't update your script.\n You can try to download it yourself at "..remote_file) end
  end
else fltk.fl_message("You have the latest version.") end
end

ext= { "mp3", "ogg", "flac", "avi", "mp4", "mov", "divx", "flv",
"mkv", "asf", "mpg", "mpeg", "wmv", "wav" }

subtmp=os.tmpname()

function get_menu()
level=0
dir=host
get_files()
box:remove(1) -- get rid of the ../
while pos[1] do table.remove(pos) end
box:value(pos[level])
end

function get_files()
box:clear()
body,code,header=socket.http.request(dir)
for l in string.gmatch(body,"<a href=.-\n") do
filename=string.gsub(l,"<a href=\"(.-)\".*","%1")
filesize=string.gsub(l,".*>([%d%.]-[KMG])<.*","%1")
  if string.find(filename,"\/$") then box:add(filename) end
  for k,v in pairs(ext) do
    if string.find(string.lower(filename),"%."..v.."$") then
      box:add(filename.."\t"..filesize)
      break
    end
  end
end
fresh:activate()
if not pos[level] then pos[level]=1 end
box:value(pos[level])
end

function play()
realfile=string.gsub(box:text(box:value()),"\t.*","")
subfile=string.gsub(realfile,"%.%w-$","%.srt")
if string.find(body,"<a href=\""..subfile.."\"") then
subdata,y,z=socket.http.request(dir.."/"..subfile)
my_filename=io.open(subtmp,"w")
if my_filename then
  my_filename:write(subdata)
  my_filename:close()
end
subflag=" "..subtitle_flag.." "..subtmp.." "
else subflag=" "
end
win:iconize()
Fl:check()
os.execute(player..subflag..dir.."/"..realfile)
win:show()
end

function go_back()
dir=string.gsub(dir,"/[^/]-/$","")
level=level-1
if host==dir then get_menu()
else
get_files()
end
end

function go_forward()
dir=dir.."/"..box:text(box:value())
level=level+1
get_files()
end

function dubbleclik()
if Fl:event() == fltk.FL_RELEASE and Fl:event_clicks() > 0 then
navigate()
end
end

function navigate()
if box:value() > 0 then
pos[level]=box:value()
  if box:text(box:value())=="../" then go_back()
  elseif string.find(box:text(box:value()),"\/$") then go_forward()
  else play() end
end
end

function quit()
os.remove(subtmp)
os.exit()
end

ww,wh,bh=550,400,25
win=fltk:Fl_Double_Window(ww,wh,"Movies")
win:callback(quit)
box=fltk:Fl_Hold_Browser(0,0,ww,wh); box:callback(dubbleclik)
box:column_widths({ww-100,100,0})
go=fltk:Fl_Return_Button(0,0,0,0,"Open"); go:callback(navigate)
fresh=fltk:Fl_Button(0,0,0,0,"&Menu"); fresh:callback(get_menu)
update=fltk:Fl_Button(0,0,0,0,"&Update"); update:callback(update_me)

pos={}
pos[0]=1
get_menu()

win:resizable(box)
win:show()
Fl:run()

04-03-2010 11:57 PM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Messages In This Thread
RE: access to media files on a LAN server - mikshaw - 04-03-2010 11:57 PM

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: