3 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
7 -- This library is free software; you can redistribute it and/or
8 -- modify it under the terms of the GNU Lesser General Public
9 -- License as published by the Free Software Foundation; either
10 -- version 2.1 of the License, or (at your option) any later version.
12 -- This library is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 -- Lesser General Public License for more details.
17 -- You should have received a copy of the GNU Lesser General Public
18 -- License along with this library; if not, write to the Free Software
19 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 local YouTube
= {} -- Utility functions unique to this script
25 -- <http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs>
27 -- Identify the script.
29 package
.path
= self
.script_dir
.. '/?.lua'
30 local C
= require
'quvi/const'
32 r
.domain
= "youtube%.com"
33 r
.formats
= "default|best"
34 r
.categories
= C
.proto_http
35 self
.page_url
= YouTube
.normalize(self
.page_url
)
36 local U
= require
'quvi/util'
37 r
.handles
= U
.handles(self
.page_url
,
38 {r
.domain
}, {"/watch"}, {"v=[%w-_]+"})
42 -- Query available formats.
43 function query_formats(self
)
44 local config
,U
= YouTube
.get_config(self
)
45 local formats
= YouTube
.iter_formats(config
, U
)
48 for _
,v
in pairs(formats
) do
49 table.insert(t
, YouTube
.to_s(v
))
53 self
.formats
= table.concat(t
, "|")
60 self
.host_id
= "youtube"
62 local p_url
= YouTube
.normalize(self
.page_url
)
63 self
.start_time
= p_url
:match('#a?t=(.+)') or ''
65 return YouTube
.get_video_info(self
)
72 function YouTube
.normalize(s
)
73 if not s
then return s
end
74 local U
= require
'quvi/url'
76 if not t
.host
then return s
end
77 t
.host
= t
.host
:gsub('youtu%.be', 'youtube.com')
78 t
.host
= t
.host
:gsub('-nocookie', '')
80 local p
= {'/embed/([-_%w]+)', '/%w/([-_%w]+)', '/([-_%w]+)'}
81 for _
,v
in pairs(p
) do
82 local m
= t
.path
:match(v
)
83 if m
and #m
== 11 then
92 function YouTube
.get_config(self
)
93 local sch
= self
.page_url
:match('^(%w+)://')
94 or error("no match: scheme")
96 local p_url
= YouTube
.normalize(self
.page_url
)
98 self
.id
= p_url
:match("v=([%w-_]+)")
99 or error("no match: media ID")
101 local s_fmt
= "%s://www.youtube.com/get_video_info?&video_id=%s"
102 .. "&el=detailpage&ps=default&eurl=&gl=US&hl=en"
104 local c_url
= string.format(s_fmt
, sch
, self
.id
)
106 local U
= require
'quvi/util'
107 local c
= U
.decode(quvi
.fetch(c_url
, {fetch_type
='config'}))
110 local reason
= U
.unescape(c
['reason'])
111 local code
= c
['errorcode']
112 error(string.format("%s (code=%s)", reason
, code
))
118 function YouTube
.iter_formats(config
, U
)
119 local fmt_stream_map
= config
['url_encoded_fmt_stream_map']
120 or error("no match: url_encoded_fmt_stream_map")
122 fmt_stream_map
= U
.unescape(fmt_stream_map
) .. ","
125 for f
in fmt_stream_map
:gmatch('([^,]*),') do
126 local d
= U
.decode(f
)
127 if d
['itag'] and d
['url'] then
128 local uurl
= U
.unescape(d
['url'])
130 uurl
= uurl
.. "&signature=" .. U
.unescape(d
['sig'])
132 urls
[U
.unescape(d
['itag'])] = uurl
136 local fmt_map
= config
['fmt_list'] or error("no match: fmt_list")
137 fmt_map
= U
.unescape(fmt_map
)
140 for f
,w
,h
in fmt_map
:gmatch('(%d+)/(%d+)x(%d+)') do
142 table.insert(r
, {fmt_id
=tonumber(f
), url
=urls
[f
],
143 width
=tonumber(w
), height
=tonumber(h
)})
149 function YouTube
.get_video_info(self
)
150 local config
,U
= YouTube
.get_config(self
)
152 self
.title
= config
['title'] or error('no match: media title')
153 self
.title
= U
.unescape(self
.title
)
155 self
.thumbnail_url
= config
['thumbnail_url'] or ''
156 if #self
.thumbnail_url
> 0 then
157 self
.thumbnail_url
= U
.unescape(self
.thumbnail_url
)
160 self
.duration
= (config
['length_seconds'] or 0)*1000 -- to msec
162 self
.requested_format
=
163 YouTube
.convert_deprecated_id(self
.requested_format
)
165 local formats
= YouTube
.iter_formats(config
, U
)
166 local format = U
.choose_format(self
, formats
,
168 YouTube
.choose_default
,
170 or error("unable to choose format")
171 local url
= format.url
or error("no match: media url")
173 if url
and #self
.start_time
> 0 then
174 local min, sec
= self
.start_time
:match("^(%d+)m(%d+)s$")
175 min = tonumber(min) or 0
176 sec
= tonumber(sec
) or 0
177 local msec
= (min * 60000) + (sec
* 1000)
179 url
= url
.. "&begin=" .. msec
187 function YouTube
.choose_best(formats
) -- Highest quality available
188 local r
= {width
=0, height
=0, url
=nil}
189 local U
= require
'quvi/util'
190 for _
,v
in pairs(formats
) do
191 if U
.is_higher_quality(v
,r
) then
195 -- for k,v in pairs(r) do print(k,v) end
199 function YouTube
.choose_default(formats
)
200 local r
= formats
[1] -- Either whatever YouTube returns as the first.
201 for _
,v
in pairs(formats
) do
202 if v
.height
== 480 then -- Or, whichever is of 480p and found first.
210 YouTube
.conv_table
= { -- Deprecated.
222 function YouTube
.convert_deprecated_id(r_fmt
)
223 if YouTube
.conv_table
[r_fmt
] then
224 local s
= string.format("fmt%02d_", YouTube
.conv_table
[r_fmt
])
225 r_fmt
= r_fmt
:gsub("^(%w+)_", s
)
230 function YouTube
.to_s(t
)
231 return string.format("fmt%02d_%sp", t
.fmt_id
, t
.height
)
236 {u='http://youtu.be/3WSQH__H1XE', -- u=page url
237 e='http://youtube.com/watch?v=3WSQH__H1XE'}, -- e=expected url
238 {u='http://youtu.be/v/3WSQH__H1XE?hl=en',
239 e='http://youtube.com/watch?v=3WSQH__H1XE'},
240 {u='http://youtu.be/watch?v=3WSQH__H1XE',
241 e='http://youtube.com/watch?v=3WSQH__H1XE'},
242 {u='http://youtu.be/embed/3WSQH__H1XE',
243 e='http://youtube.com/watch?v=3WSQH__H1XE'},
244 {u='http://youtu.be/v/3WSQH__H1XE',
245 e='http://youtube.com/watch?v=3WSQH__H1XE'},
246 {u='http://youtu.be/e/3WSQH__H1XE',
247 e='http://youtube.com/watch?v=3WSQH__H1XE'},
248 {u='http://youtube.com/watch?v=3WSQH__H1XE',
249 e='http://youtube.com/watch?v=3WSQH__H1XE'},
250 {u='http://youtube.com/embed/3WSQH__H1XE',
251 e='http://youtube.com/watch?v=3WSQH__H1XE'},
252 {u='http://jp.youtube.com/watch?v=3WSQH__H1XE',
253 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
254 {u='http://jp.youtube-nocookie.com/e/3WSQH__H1XE',
255 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
256 {u='http://jp.youtube.com/embed/3WSQH__H1XE',
257 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
258 {u='http://youtube.com/3WSQH__H1XE', -- invalid page url
259 e='http://youtube.com/watch?v=3WSQH__H1XE'}
262 for i,v in pairs(a) do
263 local s = YouTube.normalize(v.u)
265 print('\n input: ' .. v.u .. " (#" .. i .. ")")
266 print('expected: ' .. v.e)
271 print((e == 0) and 'Tests OK' or ('\nerrors: ' .. e))
274 -- vim: set ts=4 sw=4 tw=72 expandtab: