Merge branch 'tg/add-videobash'
[quvi.git] / share / lua / website / youtube.lua
blobbee19c7cdd654657401fc477627081d820f13bf2
2 -- quvi
3 -- Copyright (C) 2010,2011 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of quvi <http://quvi.sourceforge.net/>.
6 --
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
20 -- 02110-1301 USA
23 local YouTube = {} -- Utility functions unique to this script
25 -- <http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs>
27 -- Identify the script.
28 function ident(self)
29 package.path = self.script_dir .. '/?.lua'
30 local C = require 'quvi/const'
31 local r = {}
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-_]+"})
39 return r
40 end
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)
47 local t = {}
48 for _,v in pairs(formats) do
49 table.insert(t, YouTube.to_s(v))
50 end
52 table.sort(t)
53 self.formats = table.concat(t, "|")
55 return self
56 end
58 -- Parse URL.
59 function parse(self)
60 self.host_id = "youtube"
61 local page_url = YouTube.normalize(self.page_url)
63 local _,_,s = page_url:find('#a?t=(.+)')
64 self.start_time = s or ''
66 return YouTube.get_video_info(self)
67 end
70 -- Utility functions
73 function YouTube.normalize(url)
74 if not url then return url end
75 url = url:gsub("-nocookie", "") -- youtube-nocookie.com
76 url = url:gsub("/v/", "/watch?v=") -- embedded
77 url = url:gsub("/embed/", "/watch?v=") -- embedded
78 url = url:gsub("/e/", "/watch?v=") -- embedded
79 url = url:gsub("youtu%.be/","youtube.com/watch?v=") -- youtu.be
80 return url
81 end
83 function YouTube.get_config(self)
84 local _,_,s = self.page_url:find('^(%w+)://')
85 local scheme = s or error("no match: scheme")
87 local page_url = YouTube.normalize(self.page_url)
89 local _,_,s = page_url:find("v=([%w-_]+)")
90 self.id = s or error("no match: media id")
92 local s_fmt = "%s://www.youtube.com/get_video_info?&video_id=%s"
93 .. "&el=detailpage&ps=default&eurl=&gl=US&hl=en"
95 local config_url = string.format(s_fmt, scheme, self.id)
97 local U = require 'quvi/util'
98 local config = U.decode(quvi.fetch(config_url, {fetch_type='config'}))
100 if config['reason'] then
101 local reason = U.unescape(config['reason'])
102 local code = config['errorcode']
103 error(string.format("%s (code=%s)", reason, code))
106 return config,U
109 function YouTube.iter_formats(config, U)
110 local fmt_stream_map = config['url_encoded_fmt_stream_map']
111 or error("no match: url_encoded_fmt_stream_map")
113 fmt_stream_map = U.unescape(fmt_stream_map) .. ","
115 local urls = {}
116 for f in fmt_stream_map:gfind('([^,]*),') do
117 local d = U.decode(f)
118 if d['itag'] and d['url'] then
119 urls[U.unescape(d['itag'])] = U.unescape(d['url'])
123 local fmt_map = config['fmt_list'] or error("no match: fmt_list")
124 fmt_map = U.unescape(fmt_map)
126 local r = {}
127 for f,w,h in fmt_map:gfind('(%d+)/(%d+)x(%d+)') do
128 -- print(f,w,h)
129 table.insert(r, {fmt_id=tonumber(f), url=urls[f],
130 width=tonumber(w), height=tonumber(h)})
133 return r
136 function YouTube.get_video_info(self)
137 local config,U = YouTube.get_config(self)
139 self.title = config['title'] or error('no match: media title')
140 self.title = U.unescape(self.title)
142 self.thumbnail_url = config['thumbnail_url'] or ''
143 if #self.thumbnail_url > 0 then
144 self.thumbnail_url = U.unescape(self.thumbnail_url)
147 self.duration = (config['length_seconds'] or 0)*1000 -- to msec
149 self.requested_format =
150 YouTube.convert_deprecated_id(self.requested_format)
152 local formats = YouTube.iter_formats(config, U)
153 local url = U.choose_format(self, formats,
154 YouTube.choose_best,
155 YouTube.choose_default,
156 YouTube.to_s).url
157 or error("no match: media url")
159 if url and #self.start_time > 0 then
160 local min, sec = self.start_time:match("^(%d+)m(%d+)s$")
161 min = tonumber(min) or 0
162 sec = tonumber(sec) or 0
163 local msec = (min * 60000) + (sec * 1000)
164 if msec > 0 then
165 url = url .. "&begin=" .. msec
169 self.url = {url}
171 return self
174 function YouTube.choose_best(formats) -- Highest quality available
175 local r = {width=0, height=0, url=nil}
176 local U = require 'quvi/util'
177 for _,v in pairs(formats) do
178 if U.is_higher_quality(v,r) then
179 r = v
182 -- for k,v in pairs(r) do print(k,v) end
183 return r
186 function YouTube.choose_default(formats) -- Lowest quality available
187 local r = {width=0xffff, height=0xffff, url=nil}
188 local U = require 'quvi/util'
189 for _,v in pairs(formats) do
190 if U.is_lower_quality(v,r) then
191 r = v
194 -- for k,v in pairs(r) do print(k,v) end
195 return r
198 YouTube.conv_table = { -- Deprecated.
199 -- flv
200 flv_240p = '5',
201 flv_360p = '34',
202 flv_480p = '35',
203 -- mp4
204 mp4_360p = '18',
205 mp4_720p = '22',
206 mp4_1080p = '37',
207 mp4_3072p = '38'
210 function YouTube.convert_deprecated_id(r_fmt)
211 if YouTube.conv_table[r_fmt] then
212 local s = string.format("fmt%02d_", YouTube.conv_table[r_fmt])
213 r_fmt = r_fmt:gsub("^(%w+)_", s)
215 return r_fmt
218 function YouTube.to_s(t)
219 return string.format("fmt%02d_%sp", t.fmt_id, t.height)
222 -- vim: set ts=4 sw=4 tw=72 expandtab: