Skip to content

Instantly share code, notes, and snippets.

@p3g4asus
Forked from Seneral/0 Warning.md
Last active January 4, 2023 18:20
Show Gist options
  • Save p3g4asus/597050997e01f8fd1fcf473fe6545a4f to your computer and use it in GitHub Desktop.
Save p3g4asus/597050997e01f8fd1fcf473fe6545a4f to your computer and use it in GitHub Desktop.
Modified VLC YouTube playlist parsing script. The original script was not working anymore because youtube removed the list_ajax interface. Unfortunately the new interface requires at least http GET method with proper headers and they are not supported by vlc. So this version makes use of an external program (youtube-dl). Disclaimer: this version…
--[[
Youtube playlist importer for VLC media player 1.1 and 2.0
Copyright 2012 Guillaume Le Maout
Authors: Guillaume Le Maout
Contact: http://addons.videolan.org/messages/?action=newmessage&username=exebetche
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
--]]
--[[
MODified by Kai Gillmann, 19.01.2013, kaigillmann@googlemail.com:
VLC HAS already a youtube importer, but not for playlists. IMO this mentioned one is
better than this one, because it opens the video in the best possible video resolution.
So i decided to remove all parts of the code which is not responsible for list handling.
Now this lua script parses the list, as wanted, but for each video opened, the vlc default
Youtube script is used, so the videos will be displayed properly.
--]]
--[[
Youtube playlist importer for VLC media player 1.1 and 2.0
Copyright 2012 Guillaume Le Maout
Authors: Guillaume Le Maout
Contact: http://addons.videolan.org/messages/?action=newmessage&username=exebetche
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
--]]
--[[
MODified by Kai Gillmann, 19.01.2013, kaigillmann@googlemail.com:
VLC HAS already a youtube importer, but not for playlists. IMO this mentioned one is
better than this one, because it opens the video in the best possible video resolution.
So i decided to remove all parts of the code which is not responsible for list handling.
Now this lua script parses the list, as wanted, but for each video opened, the vlc default
Youtube script is used, so the videos will be displayed properly.
--]]
--[[
Patched by Aaron Hill (https://github.com/seraku24), 2018-05-16:
The original script was failing in VLC 3.x due to an overzealous probe function.
This patch makes the probe function more restrictive to avoid false positives.
--]]
--[[
Patched by Matteo Federico Zazzetta (https://github.com/p3g4asus), 2020-02-17:
The original script was not working anymore because youtube removed the list_ajax interface.
Unfortunately the new interface requires at least http GET method with proper headers and
they are not supported by vlc. So this version makes use of an external program (youtube-dl).
Disclaimer: this version works only under Windows. It can be easily ported but I have no way to test
it on other OS at the moment.
Installation (under Windows): place this file in the lua playlist vlc folder together with JSON.lua
(http://regex.info/code/JSON.lua) and youtube-dl.exe (https://youtube-dl.org/latest)
--]]
-- Probe function.
function probe()
if vlc.access ~= "http" and vlc.access ~= "https" then
return false
end
return string.match(vlc.path:match("([^/]+)"),"%w+.youtube.com") and (
not string.match(vlc.path, "list_ajax") and string.match(vlc.path, "[?&]list="))
end
local function isdef(p,...)
if not p then
return false
else
local tb = p
for i,v in ipairs(arg) do
if tb[v]==nil then
return false
else
tb = tb[v]
end
end
return true
end
end
function Log(lm)
vlc.msg.info("[youtube_pl.lua] " .. tostring(lm))
end
function get_url_param( url, name )
local _, _, res = string.find( url, "[&?]"..name.."=([^&]*)" )
return res
end
function try_yt_dl(lst)
local info = debug.getinfo(1,'S');
local kk,jj,ll = string.match(string.sub(info.source,2), "(.-)([^\\/]-%.?([^%.\\/]*))$")
Log("Trying ytdl "..kk)
local link = vlc.access.."://www.youtube.com/playlist?list="..lst
local JSON = (loadfile (kk.."JSON.lua"))()
local bat_dir3 = "cmd /k \"\""..kk.."youtube-dl.exe\" -j --flat-playlist \""..link.."\"\""
local f = assert(io.popen (bat_dir3, 'r'))
local p = {}
Log("Exec "..bat_dir3)
-- local bat_dir2 = "> \""..kk.."youtube-dl.txt\" \""..kk.."youtube-dl.exe\" -j --flat-playlist \""..link.."\""
-- Log("Exec "..bat_dir2)
-- os.execute(bat_dir2)
-- local outfile = kk.."youtube-dl.txt"
-- Log("Open "..outfile)
local p = {}
for json in f:lines() do
Log(json)
local rv, pall = pcall(function () return JSON:decode(json) end)
if rv and isdef(pall,"id") then
local item = {}
item.path = "https://www.youtube.com/watch?v="..pall.id
if isdef(pall,"title") then
item.title = pall.title
end
if isdef(pall,"duration") then
item.duration = math.floor(tonumber(pall.duration))
end
table.insert (p, item)
end
end
f:close()
return p
end
-- Parse function.
function parse()
if string.match( vlc.path, "list=" ) then
local lst = get_url_param(vlc.path, "list")
return try_yt_dl(lst)
end
end
@Picoseconds
Copy link

I have updated VLC and it is better now, but most videos can't play from any music/copyright channels.

@Druxtt
Copy link

Druxtt commented Feb 23, 2021

I have updated VLC and it is better now, but most videos can't play from any music/copyright channels.

Yep you're right but man... That's what I've been using vlc for smh

@Seneral
Copy link

Seneral commented Feb 23, 2021

I have updated VLC and it is better now, but most videos can't play from any music/copyright channels.

Yeah my FlagPlayer broke as well on some videos, YouTube slightly changed formats for parts of their stream encoding. But that's not likely the issue you were having with youtube-dl, my older version works just fine on some of these videos.
Basically youtube checks if the IP that requested the webpage is the same as the one requesting the stream, for some copyrighted videos, which might be what you're experiencing - although I don't know why youtube-dl would be using a proxy, so not sure if that is actually the problem.
Can you share a link that currently does not work with youtube-dl?

@Picoseconds
Copy link

I installed the youtube-dl lua and then it is worse and 0 videos can play

@Daniel-Mendes
Copy link

Daniel-Mendes commented Jun 8, 2021

Hi, I worked on a version that use the code source of the playlist to extract the data (I use the ytInitialData from javascript to get the data), it works with /watch?v=XXX&list=XXXX and /playlist?list=XXXX urls and I want to add support of music.youtube.com/watch?list=XXX and music.youtube.com/playlist?list=XXX.

WARNING: if Youtube update there code, my parser will not work any more and will need an update.

https://gist.github.com/Daniel-Mendes/21cff1d94b584769fa05a4905bd272b5

Copy link

ghost commented Jun 15, 2021

I installed the youtube-dl lua and then it is worse and 0 videos can play

The YTDL LUA overrides this script. REMOVE IT.

@NeuroCPP
Copy link

I installed the youtube-dl lua and then it is worse and 0 videos can play

The YTDL LUA overrides this script. REMOVE IT.
Yes you said it right...but will it work without yt dl....while until then I am using this edition Made By @Daniel-Mendes
LINK TO FILE: https://gist.github.com/Daniel-Mendes/21cff1d94b584769fa05a4905bd272b5
NOTE::-- IT ONLY WORKS UPTO 100 videos in a playlist...sometimes it will discard the playlist after reading if the playlist if it has 100+ videos

@Killer-931
Copy link

Killer-931 commented Aug 13, 2021

Hey! I have used this extension before, by some Miracle I figured out how to get it to work. I did something recently, don't know what, I can't remember how to get the Extension to work, it took me hours last time and I am not a Code Genius.

Could someone make a step by step for absolutely brain dead people like me and others? We'd really appreciate it, a short video would be great!

Edit: Okay, I figured it out, Mostly. So, I reinstalled VLC, fully wiped all it's data. Went into AppData and Roaming, made the Lua and Extensions and Playlist folder. Putting it all in Playlist there, it Works, except it Doesn't.

So, it won't play 90% of the Songs, I guess it's a region Restriction thing, I'm from Australia though and normal browsing I have no Restrictions on the Songs that aren't playing Normally.

How do I go about making VLC play these 'Restricted' Songs?

@Seneral
Copy link

Seneral commented Aug 13, 2021

Not sure tbh but you could try if other custom software has similar issues with it. E.g. I made FlagPlayer and there are certain copyrighted videos it can't play if it is using a public proxy server (default, though you can easily host one locally). VLC should not have a problem with that due to not being a website, I would expect it to play back all videos. So if you find FlagPlayer can play them, I would file a bug with VLC (or the maintainer of the YouTube watch plugin, not this one). If it can't, it should give you some idea to why (you could even send me a link if it's not clear why and I'll test).
https://flagplayer.seneral.dev/ (Comments are broken again and there's a weird video length bug only on firefox but apart from that videos should play fine).
One thing I will say is that YouTube sometimes just doesn't load a video, both through FlagPlayer or the official website, until you reload. Not sure if YouTube detects foreign traffic by other programs and adds unreliability after some time, but got nothing to compare with to verify.

@vladscript
Copy link

Thanks! it solved an issue to me (no idea why or how) regarding error http 403 of mi AHK script to extract/insert URLsof playlist to VLC YouPlayTubeList

@czoins
Copy link

czoins commented Aug 22, 2021

For those unable to play most videos (403 HTTP error), i have updated the youtube.lua script to use youtube-dl to get the link. Updated script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment