Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2025
11
0
360 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. -- If you experience some problems with with the old buggy dkjson version, like in issue #3
  2. -- You need to download the latest version of dkjson until https://code.videolan.org/videolan/vlc/-/merge_requests/3318 is merged.
  3.  
  4. JSON = require "dkjson" -- load additional json routines
  5.  
  6. -- Probe function.
  7. function probe()
  8. if vlc.access == "http" or vlc.access == "https" then
  9. peeklen = 9
  10. s = ""
  11. while string.len(s) < 9 do
  12. s = string.lower(string.gsub(vlc.peek(peeklen), "%s", ""))
  13. peeklen = peeklen + 1
  14. end
  15. return s == "<!doctype"
  16. else
  17. return false
  18. end
  19. end
  20.  
  21. function _get_format_url(format)
  22. -- prefer streaming formats
  23. if format.manifest_url then
  24. return format.manifest_url
  25. else
  26. return format.url
  27. end
  28. end
  29.  
  30. -- Parse function.
  31. function parse()
  32. local url = vlc.access .. "://" .. vlc.path -- get full url
  33.  
  34. -- Function to execute command and return file handle or nil on failure
  35. local function execute_command(command)
  36. local file = io.popen(command, 'r')
  37. if file then
  38. local output = file:read("*a") -- Attempt to read something to check if command worked
  39. if output == "" then -- If nothing was read, assume command failed (like command not found)
  40. file:close() -- Important to close to avoid resource leaks
  41. return nil -- Indicate failure
  42. else
  43. file:close() -- Close and reopen for actual usage
  44. return io.popen(command, 'r') -- Reopen since we consumed the initial read
  45. end
  46. else
  47. return nil -- Command execution failed
  48. end
  49. end
  50.  
  51. -- Try executing youtube-dl command
  52. local file = execute_command('youtube-dl -j --flat-playlist "' .. url .. '"')
  53. if not file then
  54. -- If youtube-dl fails, try yt-dlp as a fallback
  55. file = assert(execute_command('yt-dlp -j --flat-playlist "' .. url .. '"'),
  56. "Both youtube-dl and yt-dlp failed to execute.")
  57. end
  58.  
  59.  
  60. local tracks = {}
  61. while true do
  62. local output = file:read('*l')
  63.  
  64. if not output then
  65. break
  66. end
  67.  
  68. local json = JSON.decode(output) -- decode the json-output from youtube-dl
  69.  
  70. if not json then
  71. break
  72. end
  73.  
  74. local outurl = json.url
  75. local out_includes_audio = true
  76. local audiourl = nil
  77. if not outurl then
  78. if json.requested_formats then
  79. for key, format in pairs(json.requested_formats) do
  80. if format.vcodec ~= (nil or "none") then
  81. outurl = _get_format_url(format)
  82. out_includes_audio = format.acodec ~= (nil or "none")
  83. end
  84.  
  85. if format.acodec ~= (nil or "none") then
  86. audiourl = _get_format_url(format)
  87. end
  88. end
  89. else
  90. -- choose best
  91. for key, format in pairs(json.formats) do
  92. outurl = _get_format_url(format)
  93. end
  94. -- prefer audio and video
  95. for key, format in pairs(json.formats) do
  96. if format.vcodec ~= (nil or "none") and format.acodec ~= (nil or "none") then
  97. outurl = _get_format_url(format)
  98. end
  99. end
  100. end
  101. end
  102.  
  103. if outurl then
  104. if (json._type == "url" or json._type == "url_transparent") and json.ie_key == "Youtube" then
  105. outurl = "https://www.youtube.com/watch?v=" .. outurl
  106. end
  107.  
  108. local category = nil
  109. if json.categories then
  110. category = json.categories[1]
  111. end
  112.  
  113. local year = nil
  114. if json.release_year then
  115. year = json.release_year
  116. elseif json.release_date then
  117. year = string.sub(json.release_date, 1, 4)
  118. elseif json.upload_date then
  119. year = string.sub(json.upload_date, 1, 4)
  120. end
  121.  
  122. local thumbnail = nil
  123. if json.thumbnails then
  124. thumbnail = json.thumbnails[#json.thumbnails].url
  125. end
  126.  
  127. jsoncopy = {}
  128. for k in pairs(json) do
  129. jsoncopy[k] = tostring(json[k])
  130. end
  131.  
  132. json = jsoncopy
  133.  
  134. item = {
  135. path = outurl,
  136. name = json.title,
  137. duration = json.duration,
  138.  
  139. -- for a list of these check vlc/modules/lua/libs/sd.c
  140. title = json.track or json.title,
  141. artist = json.artist or json.creator or json.uploader or json.playlist_uploader,
  142. genre = json.genre or category,
  143. copyright = json.license,
  144. album = json.album or json.playlist_title or json.playlist,
  145. tracknum = json.track_number or json.playlist_index,
  146. description = json.description,
  147. rating = json.average_rating,
  148. date = year,
  149. --setting
  150. url = json.webpage_url or url,
  151. --language
  152. --nowplaying
  153. --publisher
  154. --encodedby
  155. arturl = json.thumbnail or thumbnail,
  156. trackid = json.track_id or json.episode_id or json.id,
  157. tracktotal = json.n_entries,
  158. --director
  159. season = json.season or json.season_number or json.season_id,
  160. episode = json.episode or json.episode_number,
  161. show_name = json.series,
  162. --actors
  163.  
  164. meta = json,
  165. options = { "start-time=" .. (json.start_time or 0) },
  166. }
  167.  
  168. if not out_includes_audio and audiourl and outurl ~= audiourl then
  169. item['options'][':input-slave'] = ":input-slave=" .. audiourl;
  170. end
  171.  
  172. table.insert(tracks, item)
  173. end
  174. end
  175. file:close()
  176. return tracks
  177. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement