media/: Use socket.url
[libquvi-scripts.git] / share / media / youtube.lua
blob97a16835cc36c446d9dc1e07ce4dcf0abb5fcda0
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(qargs)
28 local Y = require 'quvi/youtube'
29 local u = Y.normalize(qargs.input_url)
30 return {
31 domains = table.concat({'youtube.com'}, ','),
32 can_parse_url = YouTube.can_parse_url(u)
34 end
36 -- Parse media properties.
37 function parse(qargs)
38 local Y = require 'quvi/youtube'
39 return YouTube.parse_properties(qargs, Y)
40 end
43 -- Utility functions
46 function YouTube.can_parse_url(url)
47 local U = require 'socket.url'
48 local t = U.parse(url)
49 if t and t.scheme and t.scheme:lower():match('^https?$')
50 and t.host and t.host:lower():match('youtube%.com$')
51 and t.query and t.query:lower():match('^v=[%w-_]+')
52 and t.path and t.path:lower():match('^/watch')
53 then
54 return true
55 else
56 return false
57 end
58 end
60 -- Parses the video info from the server.
61 function YouTube.parse_properties(qargs, Y)
62 local c, U = YouTube.get_data(qargs, Y)
64 qargs.duration_ms = (c['length_seconds'] or 0)*1000 -- to ms
65 qargs.thumb_url = U.unescape(c['thumbnail_url'] or '')
66 qargs.title = U.unescape(c['title'] or '')
67 qargs.streams = YouTube.iter_streams(c, U)
68 YouTube.append_begin_param(qargs)
70 return qargs
71 end
73 -- Queries the video data from the server.
74 function YouTube.get_data(qargs, Y)
75 local u = Y.normalize(qargs.input_url)
77 qargs.id = u:match('v=([%w-_]+)')
78 or error('no match: media ID')
80 local U = require 'socket.url'
81 local u = U.parse(u)
82 local s = u.scheme or error('no match: scheme')
84 local s_fmt = '%s://www.youtube.com/get_video_info?&video_id=%s'
85 .. '&el=detailpage&ps=default&eurl=&gl=US&hl=en'
86 local u = string.format(s_fmt, s, qargs.id)
88 local C = require 'quvi/const'
89 local U = require 'quvi/util'
91 local o = { [C.qfo_type] = C.qft_config }
92 local c = U.decode(quvi.fetch(u, o))
94 if c['reason'] then
95 local reason = U.unescape(c['reason'])
96 local code = c['errorcode']
97 error(string.format("%s (code=%s)", reason, code))
98 end
100 return c, U
103 -- Appends the &begin parameter to the media stream URL.
104 function YouTube.append_begin_param(qargs)
105 local m,s = qargs.input_url:match('t=(%d?%d?m?)(%d%d)s')
106 m = tonumber(((m or ''):gsub('%a',''))) or 0
107 s = tonumber(((s or ''):gsub('%a',''))) or 0
108 local ms = (m*60000) + (s*1000)
109 if ms >0 then
110 for i,v in ipairs(qargs.streams) do
111 local url = qargs.streams[i].url
112 qargs.streams[i].url = url .."&begin=".. ms
114 qargs.start_time_ms = ms
118 -- Iterates the available streams.
119 function YouTube.iter_streams(config, U)
121 -- Stream map. Holds many of the essential properties,
122 -- e.g. the media stream URL.
124 local stream_map = U.unescape(config['url_encoded_fmt_stream_map']
125 or error('no match: url_encoded_fmt_stream_map'))
126 .. ','
128 local smr = {}
129 for d in stream_map:gmatch('([^,]*),') do
130 local d = U.decode(d)
131 if d['url'] then
132 local ct = U.unescape(d['type'])
133 local v_enc,a_enc = ct:match('codecs="([%w.]+),%s+([%w.]+)"')
134 local itag = d['itag']
135 local cnt = (ct:match('/([%w-]+)')):gsub('x%-', '')
136 local t = {
137 url = U.unescape(d['url']) -- d['sig'] ? "&signature=val" : ""
138 .. (d['sig'] and ('&signature='..d['sig']) or ''),
139 quality = d['quality'],
140 container = cnt,
141 v_enc = v_enc,
142 a_enc = a_enc
144 smr[itag] = t
148 -- Format list. Combined with the above properties. This list is used
149 -- for collecting the video resolution.
151 local fmtl = U.unescape(config['fmt_list'] or error('no match: fmt_list'))
152 local S = require 'quvi/stream'
153 local r = {}
155 for itag,w,h in fmtl:gmatch('(%d+)/(%d+)x(%d+)') do
156 local smri = smr[itag]
157 local t = S.stream_new(smri.url)
159 t.video.encoding = smri.v_enc or ''
160 t.audio.encoding = smri.a_enc or ''
161 t.container = smri.container or ''
162 t.video.height = tonumber(h)
163 t.video.width = tonumber(w)
165 -- Do this after we have the video resolution, as the to_id
166 -- function uses the height property.
167 t.id = YouTube.to_id(t, itag, smri)
169 table.insert(r, t)
172 if #r >1 then
173 YouTube.ch_best(S, r) -- Pick one stream as the 'best' quality.
176 return r
179 -- Picks the stream with the highest video height property
180 -- as the best in quality.
181 function YouTube.ch_best(S, t)
182 local r = t[1] -- Make the first one the 'best' by default.
183 r.flags.best = true
184 for _,v in pairs(t) do
185 if v.video.height > r.video.height then
186 r = S.swap_best(r, v)
191 -- Return an ID for a stream.
192 function YouTube.to_id(t, itag, smri)
193 return string.format("%s_%s_i%02d_%sp",
194 smri.quality, t.container, itag, t.video.height)
197 -- vim: set ts=2 sw=2 tw=72 expandtab: