[vlc-commits] VLSub: Allow a subtitle to be loaded without download it

Hugo Beauzée-Luyssen git at videolan.org
Thu Sep 21 17:49:53 CEST 2017


vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Thu Sep 21 13:59:47 2017 +0200| [d32f7fb5777bf7073162c61294a5aab33f87adbe] | committer: Hugo Beauzée-Luyssen

VLSub: Allow a subtitle to be loaded without download it

Taking advantage of stream extractors allows us not to care about
downloading the archive itself. The option of dumping the subtitle to
the disk is still present, but not mandatory anymore

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=d32f7fb5777bf7073162c61294a5aab33f87adbe
---

 share/lua/extensions/VLSub.lua | 132 +++++++++++++++++------------------------
 1 file changed, 53 insertions(+), 79 deletions(-)

diff --git a/share/lua/extensions/VLSub.lua b/share/lua/extensions/VLSub.lua
index 4ff2343f4d..bc760827cd 100644
--- a/share/lua/extensions/VLSub.lua
+++ b/share/lua/extensions/VLSub.lua
@@ -1064,6 +1064,7 @@ function SetDownloadBehaviours()
   openSub.conf.downloadBehaviours = nil
   openSub.conf.downloadBehaviours = {
     {'save', lang["int_dowload_save"]},
+    {'load', lang["int_dowload_load"]},
     {'manual', lang["int_dowload_manual"]}
   }
 end
@@ -1656,7 +1657,7 @@ function get_first_sel(list)
 end
 
 function find_subtitle_in_archive(archivePath, subfileExt)
-  local archive = vlc.directory_stream("file://" .. archivePath)
+  local archive = vlc.directory_stream(archivePath)
   local items = archive:readdir()
   if not items then
     return nil
@@ -1670,44 +1671,7 @@ function find_subtitle_in_archive(archivePath, subfileExt)
   return nil
 end
 
-function download_subtitles()
-  local index = get_first_sel(input_table["mainlist"])
-
-  if index == 0 then
-    setMessage(lang["mess_no_selection"])
-    return false
-  end
-
-  openSub.actionLabel = lang["mess_downloading"]
-
-  display_subtitles() -- reset selection
-
-  local item = openSub.itemStore[index]
-
-  if openSub.option.downloadBehaviour == 'manual'
-  or not openSub.file.hasInput then
-    local link = "<span style='color:#181'>"
-    link = link.."<b>"..lang["mess_dowload_link"]..":</b>"
-    link = link.."</span>  "
-    link = link.."</span>  <a href='"..
-      item.ZipDownloadLink.."'>"
-    link = link..item.MovieReleaseName.."</a>"
-
-    setMessage(link)
-    return false
-  end
-
-  local message = ""
-  local subfileName = openSub.file.name or ""
-
-  if openSub.option.langExt then
-    subfileName = subfileName.."."..item.SubLanguageID
-  end
-
-  subfileName = subfileName.."."..item.SubFormat
-  local tmp_dir
-  local file_target_access = true
-
+function dump_subtitle_file(item, subtitleMrl)
   if is_dir(openSub.file.dir) then
     tmp_dir = openSub.file.dir
   elseif openSub.conf.dirPath then
@@ -1723,20 +1687,14 @@ function download_subtitles()
     return false
   end
 
-  local tmpFileName = dump_zip(
-    item.ZipDownloadLink,
-    tmp_dir,
-    item.SubFileName)
-
-  vlc.msg.dbg("[VLsub] tmpFileName: "..tmpFileName)
-
-  local subtitleMrl = find_subtitle_in_archive(tmpFileName, item.SubFormat)
+  local subfileName = openSub.file.name or ""
 
-  if not subtitleMrl then
-    vlc.msg.err( "Failed to extract subtitle" )
-    return false
+  if openSub.option.langExt then
+    subfileName = subfileName.."."..item.SubLanguageID
   end
 
+  subfileName = subfileName.."."..item.SubFormat
+
   -- Determine if the path to the video file is accessible for writing
 
   local target = openSub.file.dir..subfileName
@@ -1753,7 +1711,7 @@ function download_subtitles()
       setError(lang["mess_save_fail"].."  "..
       "<a href='"..item.ZipDownloadLink.."'>"..
       lang["mess_click_link"].."</a>")
-      return false
+      return nil
     end
   end
 
@@ -1776,49 +1734,65 @@ function download_subtitles()
   stream = nil
   collectgarbage()
 
-  if not os.remove(tmpFileName) then
-    vlc.msg.err("[VLsub] Unable to remove temp: "..tmpFileName)
-  end
+  return target
+end
 
-  -- load subtitles
-  if add_sub(target) then
-    message = success_tag(lang["mess_loaded"]) .. message
-  else
-    message = error_tag(lang["mess_not_load"]) .. message
+function download_subtitles()
+  local index = get_first_sel(input_table["mainlist"])
+
+  if index == 0 then
+    setMessage(lang["mess_no_selection"])
+    return false
   end
 
-  setMessage(message)
-end
+  openSub.actionLabel = lang["mess_downloading"]
 
-function dump_zip(url, dir, subfileName)
-  -- Dump zipped data in a temporary file
-  setMessage(openSub.actionLabel..": "..progressBarContent(0))
-  local resp = get(url)
+  display_subtitles() -- reset selection
 
-  if not resp then
-    setError(lang["mess_no_response"])
+  local item = openSub.itemStore[index]
+
+  if openSub.option.downloadBehaviour == 'manual'
+  or not openSub.file.hasInput then
+    local link = "<span style='color:#181'>"
+    link = link.."<b>"..lang["mess_dowload_link"]..":</b>"
+    link = link.."</span>  "
+    link = link.."</span>  <a href='"..
+      item.ZipDownloadLink.."'>"
+    link = link..item.MovieReleaseName.."</a>"
+
+    setMessage(link)
     return false
   end
 
-  local tmpFileName = dir..subfileName..".gz"
-  if not file_touch(tmpFileName) then
+  local message = ""
+
+  local subtitleMrl = find_subtitle_in_archive(item.ZipDownloadLink, item.SubFormat)
+  if not subtitleMrl then
+    vlc.msg.err( "Failed to extract subtitle" )
     return false
   end
-  local tmpFile = assert(io.open(tmpFileName, "wb"))
 
-  tmpFile:write(resp)
-  tmpFile:flush()
-  tmpFile:close()
-  tmpFile = nil
-  collectgarbage()
+  if openSub.option.downloadBehaviour == 'save' then
+    local target = dump_subtitle_file(item, subtitleMrl)
+    if target then
+      subtitleMrl = vlc.strings.make_uri(target)
+    end
+  end
 
-  return tmpFileName
+  -- load subtitles
+  if add_sub(subtitleMrl) then
+    message = success_tag(lang["mess_loaded"]) .. message
+  else
+    message = error_tag(lang["mess_not_load"]) .. message
+  end
+
+  setMessage(message)
 end
 
-function add_sub(subPath)
+function add_sub(subMrl)
   if vlc.item or vlc.input.item() then
-    vlc.msg.dbg("[VLsub] Adding subtitle :" .. subPath)
-    return vlc.input.add_subtitle(subPath)
+    vlc.msg.dbg("[VLsub] Adding subtitle :" .. subMrl)
+    return vlc.input.add_subtitle_mrl(subMrl)
   end
   return false
 end



More information about the vlc-commits mailing list