share/Makefile.am: Reorganize blocks, comments
[libquvi-scripts.git] / share / lua / media / youtube.lua
blob6e9e8b11ebd182efc284f33f41d405358201e450
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 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.qmspc_http
36 return r
37 end
39 -- Parse media properties.
40 function parse(qargs)
41 local Y = require 'quvi/youtube'
42 return YouTube.parse_properties(qargs, Y)
43 end
46 -- Utility functions
49 -- Parses the video info from the server.
50 function YouTube.parse_properties(qargs, Y)
51 local c, U = YouTube.get_data(qargs, Y)
53 qargs.duration_ms = (c['length_seconds'] or 0)*1000 -- to ms
54 qargs.thumb_url = U.unescape(c['thumbnail_url'] or '')
55 qargs.title = U.unescape(c['title'] or '')
56 qargs.streams = YouTube.iter_streams(c, U)
57 YouTube.append_begin_param(qargs)
59 return qargs
60 end
62 -- Queries the video data from the server.
63 function YouTube.get_data(qargs, Y)
64 local u = Y.normalize(qargs.input_url)
66 qargs.id = u:match('v=([%w-_]+)')
67 or error('no match: media ID')
69 local U = require 'quvi/url'
70 local u = U.parse(u)
71 local s = u.scheme or error('no match: scheme')
73 local s_fmt = '%s://www.youtube.com/get_video_info?&video_id=%s'
74 .. '&el=detailpage&ps=default&eurl=&gl=US&hl=en'
75 local u = string.format(s_fmt, s, qargs.id)
77 local C = require 'quvi/const'
78 local U = require 'quvi/util'
80 local o = { [C.qfo_type] = C.qft_config }
81 local c = U.decode(quvi.fetch(u, o))
83 if c['reason'] then
84 local reason = U.unescape(c['reason'])
85 local code = c['errorcode']
86 error(string.format("%s (code=%s)", reason, code))
87 end
89 return c, U
90 end
92 -- Appends the &begin parameter to the media stream URL.
93 function YouTube.append_begin_param(qargs)
94 local m,s = qargs.input_url:match('t=(%d?%d?m?)(%d%d)s')
95 m = tonumber(((m or ''):gsub('%a',''))) or 0
96 s = tonumber(((s or ''):gsub('%a',''))) or 0
97 local ms = (m*60000) + (s*1000)
98 if ms >0 then
99 for i,v in ipairs(qargs.streams) do
100 local url = qargs.streams[i].url
101 qargs.streams[i].url = url .."&begin=".. ms
103 qargs.start_time_ms = ms
107 -- Iterates the available streams.
108 function YouTube.iter_streams(config, U)
110 -- Stream map. Holds many of the essential properties,
111 -- e.g. the media stream URL.
113 local stream_map = U.unescape(config['url_encoded_fmt_stream_map']
114 or error('no match: url_encoded_fmt_stream_map'))
115 .. ','
117 local smr = {}
118 for d in stream_map:gmatch('([^,]*),') do
119 local d = U.decode(d)
120 if d['url'] then
121 local ct = U.unescape(d['type'])
122 local v_enc,a_enc = ct:match('codecs="([%w.]+),%s+([%w.]+)"')
123 local itag = d['itag']
124 local cnt = (ct:match('/([%w-]+)')):gsub('x%-', '')
125 local t = {
126 url = U.unescape(d['url']),
127 quality = d['quality'],
128 container = cnt,
129 v_enc = v_enc,
130 a_enc = a_enc
132 smr[itag] = t
136 -- Format list. Combined with the above properties. This list is used
137 -- for collecting the video resolution.
139 local fmtl = U.unescape(config['fmt_list'] or error('no match: fmt_list'))
140 local S = require 'quvi/stream'
141 local r = {}
143 for itag,w,h in fmtl:gmatch('(%d+)/(%d+)x(%d+)') do
144 local smri = smr[itag]
145 local t = S.stream_new(smri.url)
147 t.video.encoding = smri.v_enc or ''
148 t.audio.encoding = smri.a_enc or ''
149 t.container = smri.container or ''
150 t.video.height = tonumber(h)
151 t.video.width = tonumber(w)
153 -- Do this after we have the video resolution, as the to_id
154 -- function uses the height property.
155 t.id = YouTube.to_id(t, itag, smri)
157 table.insert(r, t)
160 if #r >1 then
161 YouTube.ch_best(S, r) -- Pick one stream as the 'best' quality.
164 return r
167 -- Picks the stream with the highest video height property
168 -- as the best in quality.
169 function YouTube.ch_best(S, t)
170 local r = t[1] -- Make the first one the 'best' by default.
171 r.flags.best = true
172 for _,v in pairs(t) do
173 if v.video.height > r.video.height then
174 r = S.swap_best(r, v)
179 -- Return an ID for a stream.
180 function YouTube.to_id(t, itag, smri)
181 return string.format("%s_%s_i%02d_%sp",
182 smri.quality, t.container, itag, t.video.height)
185 -- vim: set ts=2 sw=2 tw=72 expandtab: