media/*.lua: Do not specify "categories"
[libquvi-scripts.git] / share / media / youtube.lua
blob8ef43fa581898c39dd2d5102b1c655518a71f243
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 A = require 'quvi/accepts'
29 local Y = require 'quvi/youtube'
30 local u = Y.normalize(qargs.input_url)
31 local r = {
32 accepts = A.accepts(u, {"youtube%.com"}, {"/watch"}, {"v=[%w-_]+"})
34 return r
35 end
37 -- Parse media properties.
38 function parse(qargs)
39 local Y = require 'quvi/youtube'
40 return YouTube.parse_properties(qargs, Y)
41 end
44 -- Utility functions
47 -- Parses the video info from the server.
48 function YouTube.parse_properties(qargs, Y)
49 local c, U = YouTube.get_data(qargs, Y)
51 qargs.duration_ms = (c['length_seconds'] or 0)*1000 -- to ms
52 qargs.thumb_url = U.unescape(c['thumbnail_url'] or '')
53 qargs.title = U.unescape(c['title'] or '')
54 qargs.streams = YouTube.iter_streams(c, U)
55 YouTube.append_begin_param(qargs)
57 return qargs
58 end
60 -- Queries the video data from the server.
61 function YouTube.get_data(qargs, Y)
62 local u = Y.normalize(qargs.input_url)
64 qargs.id = u:match('v=([%w-_]+)')
65 or error('no match: media ID')
67 local U = require 'quvi/url'
68 local u = U.parse(u)
69 local s = u.scheme or error('no match: scheme')
71 local s_fmt = '%s://www.youtube.com/get_video_info?&video_id=%s'
72 .. '&el=detailpage&ps=default&eurl=&gl=US&hl=en'
73 local u = string.format(s_fmt, s, qargs.id)
75 local C = require 'quvi/const'
76 local U = require 'quvi/util'
78 local o = { [C.qfo_type] = C.qft_config }
79 local c = U.decode(quvi.fetch(u, o))
81 if c['reason'] then
82 local reason = U.unescape(c['reason'])
83 local code = c['errorcode']
84 error(string.format("%s (code=%s)", reason, code))
85 end
87 return c, U
88 end
90 -- Appends the &begin parameter to the media stream URL.
91 function YouTube.append_begin_param(qargs)
92 local m,s = qargs.input_url:match('t=(%d?%d?m?)(%d%d)s')
93 m = tonumber(((m or ''):gsub('%a',''))) or 0
94 s = tonumber(((s or ''):gsub('%a',''))) or 0
95 local ms = (m*60000) + (s*1000)
96 if ms >0 then
97 for i,v in ipairs(qargs.streams) do
98 local url = qargs.streams[i].url
99 qargs.streams[i].url = url .."&begin=".. ms
101 qargs.start_time_ms = ms
105 -- Iterates the available streams.
106 function YouTube.iter_streams(config, U)
108 -- Stream map. Holds many of the essential properties,
109 -- e.g. the media stream URL.
111 local stream_map = U.unescape(config['url_encoded_fmt_stream_map']
112 or error('no match: url_encoded_fmt_stream_map'))
113 .. ','
115 local smr = {}
116 for d in stream_map:gmatch('([^,]*),') do
117 local d = U.decode(d)
118 if d['url'] then
119 local ct = U.unescape(d['type'])
120 local v_enc,a_enc = ct:match('codecs="([%w.]+),%s+([%w.]+)"')
121 local itag = d['itag']
122 local cnt = (ct:match('/([%w-]+)')):gsub('x%-', '')
123 local t = {
124 url = U.unescape(d['url']),
125 quality = d['quality'],
126 container = cnt,
127 v_enc = v_enc,
128 a_enc = a_enc
130 smr[itag] = t
134 -- Format list. Combined with the above properties. This list is used
135 -- for collecting the video resolution.
137 local fmtl = U.unescape(config['fmt_list'] or error('no match: fmt_list'))
138 local S = require 'quvi/stream'
139 local r = {}
141 for itag,w,h in fmtl:gmatch('(%d+)/(%d+)x(%d+)') do
142 local smri = smr[itag]
143 local t = S.stream_new(smri.url)
145 t.video.encoding = smri.v_enc or ''
146 t.audio.encoding = smri.a_enc or ''
147 t.container = smri.container or ''
148 t.video.height = tonumber(h)
149 t.video.width = tonumber(w)
151 -- Do this after we have the video resolution, as the to_id
152 -- function uses the height property.
153 t.id = YouTube.to_id(t, itag, smri)
155 table.insert(r, t)
158 if #r >1 then
159 YouTube.ch_best(S, r) -- Pick one stream as the 'best' quality.
162 return r
165 -- Picks the stream with the highest video height property
166 -- as the best in quality.
167 function YouTube.ch_best(S, t)
168 local r = t[1] -- Make the first one the 'best' by default.
169 r.flags.best = true
170 for _,v in pairs(t) do
171 if v.video.height > r.video.height then
172 r = S.swap_best(r, v)
177 -- Return an ID for a stream.
178 function YouTube.to_id(t, itag, smri)
179 return string.format("%s_%s_i%02d_%sp",
180 smri.quality, t.container, itag, t.video.height)
183 -- vim: set ts=2 sw=2 tw=72 expandtab: