parse: Remove host_id
[libquvi-scripts.git] / share / lua / media / youtube.lua
blobf5585d6ffbdd92cee2a46e63ecbedcb52c8b9356
1 -- libquvi-scripts
2 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
6 -- This library is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU Lesser General Public
8 -- License as published by the Free Software Foundation; either
9 -- version 2.1 of the License, or (at your option) any later version.
11 -- This library is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 -- Lesser General Public License for more details.
16 -- You should have received a copy of the GNU Lesser General Public
17 -- License along with this library; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 -- 02110-1301 USA
22 local YouTube = {} -- Utility functions unique to this script
24 -- <http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs>
26 -- Identify the script.
27 function ident(self)
28 local A = require 'quvi/accepts'
29 local Y = require 'quvi/youtube'
30 local C = require 'quvi/const'
31 local u = Y.normalize(qargs.input_url)
32 local r = {
33 accepts = A.accepts(u, {"youtube%.com"}, {"/watch"}, {"v=[%w-_]+"}),
34 categories = C.proto_http
36 return r
37 end
39 -- Parse media properties.
40 function parse(self)
41 local p_url = YouTube.normalize(self.page_url)
42 self.start_time = p_url:match('#a?t=(.+)') or ''
44 return YouTube.get_video_info(self)
45 end
48 -- Utility functions
51 function YouTube.get_config(self)
52 local sch = self.page_url:match('^(%w+)://')
53 or error("no match: scheme")
55 local p_url = YouTube.normalize(self.page_url)
57 self.id = p_url:match("v=([%w-_]+)")
58 or error("no match: media ID")
60 local s_fmt = "%s://www.youtube.com/get_video_info?&video_id=%s"
61 .. "&el=detailpage&ps=default&eurl=&gl=US&hl=en"
63 local c_url = string.format(s_fmt, sch, self.id)
65 local U = require 'quvi/util'
66 local c = U.decode(quvi.fetch(c_url, {fetch_type='config'}))
68 if c['reason'] then
69 local reason = U.unescape(c['reason'])
70 local code = c['errorcode']
71 error(string.format("%s (code=%s)", reason, code))
72 end
74 return c, U
75 end
77 function YouTube.iter_formats(config, U)
78 local fmt_stream_map = config['url_encoded_fmt_stream_map']
79 or error("no match: url_encoded_fmt_stream_map")
81 fmt_stream_map = U.unescape(fmt_stream_map) .. ","
83 local urls = {}
84 for f in fmt_stream_map:gmatch('([^,]*),') do
85 local d = U.decode(f)
86 if d['itag'] and d['url'] then
87 urls[U.unescape(d['itag'])] = U.unescape(d['url'])
88 end
89 end
91 local fmt_map = config['fmt_list'] or error("no match: fmt_list")
92 fmt_map = U.unescape(fmt_map)
94 local r = {}
95 for f,w,h in fmt_map:gmatch('(%d+)/(%d+)x(%d+)') do
96 -- print(f,w,h)
97 table.insert(r, {fmt_id=tonumber(f), url=urls[f],
98 width=tonumber(w), height=tonumber(h)})
99 end
101 return r
104 function YouTube.get_video_info(self)
105 local config,U = YouTube.get_config(self)
107 self.title = config['title'] or error('no match: media title')
108 self.title = U.unescape(self.title)
110 self.thumbnail_url = config['thumbnail_url'] or ''
111 if #self.thumbnail_url > 0 then
112 self.thumbnail_url = U.unescape(self.thumbnail_url)
115 self.duration = (config['length_seconds'] or 0)*1000 -- to msec
117 self.requested_format =
118 YouTube.convert_deprecated_id(self.requested_format)
120 local formats = YouTube.iter_formats(config, U)
121 local format = U.choose_format(self, formats,
122 YouTube.choose_best,
123 YouTube.choose_default,
124 YouTube.to_s)
125 or error("unable to choose format")
126 local url = format.url or error("no match: media url")
128 if url and #self.start_time > 0 then
129 local min, sec = self.start_time:match("^(%d+)m(%d+)s$")
130 min = tonumber(min) or 0
131 sec = tonumber(sec) or 0
132 local msec = (min * 60000) + (sec * 1000)
133 if msec > 0 then
134 url = url .. "&begin=" .. msec
138 self.url = {url}
139 return self
142 function YouTube.choose_best(formats) -- Highest quality available
143 local r = {width=0, height=0, url=nil}
144 local U = require 'quvi/util'
145 for _,v in pairs(formats) do
146 if U.is_higher_quality(v,r) then
147 r = v
150 -- for k,v in pairs(r) do print(k,v) end
151 return r
154 function YouTube.choose_default(formats)
155 local r = formats[1] -- Either whatever YouTube returns as the first.
156 for _,v in pairs(formats) do
157 if v.height == 480 then -- Or, whichever is of 480p and found first.
158 r = v
159 break
162 return r
165 YouTube.conv_table = { -- Deprecated.
166 -- flv
167 flv_240p = '5',
168 flv_360p = '34',
169 flv_480p = '35',
170 -- mp4
171 mp4_360p = '18',
172 mp4_720p = '22',
173 mp4_1080p = '37',
174 mp4_3072p = '38'
177 function YouTube.convert_deprecated_id(r_fmt)
178 if YouTube.conv_table[r_fmt] then
179 local s = string.format("fmt%02d_", YouTube.conv_table[r_fmt])
180 r_fmt = r_fmt:gsub("^(%w+)_", s)
182 return r_fmt
185 function YouTube.to_s(t)
186 return string.format("fmt%02d_%sp", t.fmt_id, t.height)
189 --[[
190 local a = {
191 {u='http://youtu.be/3WSQH__H1XE', -- u=page url
192 e='http://youtube.com/watch?v=3WSQH__H1XE'}, -- e=expected url
193 {u='http://youtu.be/v/3WSQH__H1XE?hl=en',
194 e='http://youtube.com/watch?v=3WSQH__H1XE'},
195 {u='http://youtu.be/watch?v=3WSQH__H1XE',
196 e='http://youtube.com/watch?v=3WSQH__H1XE'},
197 {u='http://youtu.be/embed/3WSQH__H1XE',
198 e='http://youtube.com/watch?v=3WSQH__H1XE'},
199 {u='http://youtu.be/v/3WSQH__H1XE',
200 e='http://youtube.com/watch?v=3WSQH__H1XE'},
201 {u='http://youtu.be/e/3WSQH__H1XE',
202 e='http://youtube.com/watch?v=3WSQH__H1XE'},
203 {u='http://youtube.com/watch?v=3WSQH__H1XE',
204 e='http://youtube.com/watch?v=3WSQH__H1XE'},
205 {u='http://youtube.com/embed/3WSQH__H1XE',
206 e='http://youtube.com/watch?v=3WSQH__H1XE'},
207 {u='http://jp.youtube.com/watch?v=3WSQH__H1XE',
208 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
209 {u='http://jp.youtube-nocookie.com/e/3WSQH__H1XE',
210 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
211 {u='http://jp.youtube.com/embed/3WSQH__H1XE',
212 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
213 {u='http://youtube.com/3WSQH__H1XE', -- invalid page url
214 e='http://youtube.com/watch?v=3WSQH__H1XE'}
216 local e = 0
217 for i,v in pairs(a) do
218 local s = YouTube.normalize(v.u)
219 if s ~= v.e then
220 print('\n input: ' .. v.u .. " (#" .. i .. ")")
221 print('expected: ' .. v.e)
222 print(' got: ' .. s)
223 e = e + 1
226 print((e == 0) and 'Tests OK' or ('\nerrors: ' .. e))
227 ]]--
229 -- vim: set ts=4 sw=4 tw=72 expandtab: