Use renamed quvi.http.fetch
[libquvi-scripts.git] / share / media / youtube.lua
blob2cc2184a2df62a53e0bd0eadf3bb27b33ade2a6f
1 -- libquvi-scripts
2 -- Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program 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
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
21 local YouTube = {} -- Utility functions unique to this script
23 -- <http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs>
25 -- Identify the script.
26 function ident(qargs)
27 local Y = require 'quvi/youtube'
28 return Y.ident(qargs)
29 end
31 -- Parse media properties.
32 function parse(qargs)
33 return YouTube.parse_properties(qargs)
34 end
37 -- Utility functions
40 -- Parses the video info from the server.
41 function YouTube.parse_properties(qargs)
42 local c, U = YouTube.get_data(qargs)
44 qargs.duration_ms = (c['length_seconds'] or 0)*1000 -- to ms
45 qargs.thumb_url = U.unescape(c['thumbnail_url'] or '')
46 qargs.title = U.unescape(c['title'] or '')
47 qargs.streams = YouTube.iter_streams(c, U)
48 YouTube.append_begin_param(qargs)
50 return qargs
51 end
53 -- Queries the video data from the server.
54 function YouTube.get_data(qargs)
55 local Y = require 'quvi/youtube'
56 local u = Y.normalize(qargs.input_url)
58 qargs.id = u:match('v=([%w-_]+)')
59 or error('no match: media ID')
61 local U = require 'socket.url'
62 local u = U.parse(u)
63 local s = u.scheme or error('no match: scheme')
65 local s_fmt = '%s://www.youtube.com/get_video_info?&video_id=%s'
66 .. '&el=detailpage&ps=default&eurl=&gl=US&hl=en'
67 local u = string.format(s_fmt, s, qargs.id)
69 local C = require 'quvi/const'
70 local U = require 'quvi/util'
72 local o = { [C.qfo_type] = C.qft_config }
73 local c = U.decode(quvi.http.fetch(u,o).data)
75 if c['reason'] then
76 local reason = U.unescape(c['reason'])
77 local code = c['errorcode']
78 error(string.format("%s (code=%s)", reason, code))
79 end
81 return c, U
82 end
84 -- Appends the &begin parameter to the media stream URL.
85 function YouTube.append_begin_param(qargs)
86 local m,s = qargs.input_url:match('t=(%d?%d?m?)(%d%d)s')
87 m = tonumber(((m or ''):gsub('%a',''))) or 0
88 s = tonumber(((s or ''):gsub('%a',''))) or 0
89 local ms = (m*60000) + (s*1000)
90 if ms >0 then
91 for i,v in ipairs(qargs.streams) do
92 local url = qargs.streams[i].url
93 qargs.streams[i].url = url .."&begin=".. ms
94 end
95 qargs.start_time_ms = ms
96 end
97 end
99 -- Iterates the available streams.
100 function YouTube.iter_streams(config, U)
102 -- Stream map. Holds many of the essential properties,
103 -- e.g. the media stream URL.
105 local stream_map = U.unescape(config['url_encoded_fmt_stream_map']
106 or error('no match: url_encoded_fmt_stream_map'))
107 .. ','
109 local smr = {}
110 for d in stream_map:gmatch('([^,]*),') do
111 local d = U.decode(d)
112 if d['url'] then
113 local ct = U.unescape(d['type'])
114 local v_enc,a_enc = ct:match('codecs="([%w.]+),%s+([%w.]+)"')
115 local itag = d['itag']
116 local cnt = (ct:match('/([%w-]+)')):gsub('x%-', '')
117 local t = {
118 url = U.unescape(d['url']) -- d['sig'] ? "&signature=val" : ""
119 .. (d['sig'] and ('&signature='..d['sig']) or ''),
120 quality = d['quality'],
121 container = cnt,
122 v_enc = v_enc,
123 a_enc = a_enc
125 smr[itag] = t
129 -- Format list. Combined with the above properties. This list is used
130 -- for collecting the video resolution.
132 local fmtl = U.unescape(config['fmt_list'] or error('no match: fmt_list'))
133 local S = require 'quvi/stream'
134 local r = {}
136 for itag,w,h in fmtl:gmatch('(%d+)/(%d+)x(%d+)') do
137 local smri = smr[itag]
138 local t = S.stream_new(smri.url)
140 t.video.encoding = smri.v_enc or ''
141 t.audio.encoding = smri.a_enc or ''
142 t.container = smri.container or ''
143 t.video.height = tonumber(h)
144 t.video.width = tonumber(w)
146 -- Do this after we have the video resolution, as the to_id
147 -- function uses the height property.
148 t.id = YouTube.to_id(t, itag, smri)
150 table.insert(r, t)
153 if #r >1 then
154 YouTube.ch_best(S, r) -- Pick one stream as the 'best' quality.
157 return r
160 -- Picks the stream with the highest video height property
161 -- as the best in quality.
162 function YouTube.ch_best(S, t)
163 local r = t[1] -- Make the first one the 'best' by default.
164 r.flags.best = true
165 for _,v in pairs(t) do
166 if v.video.height > r.video.height then
167 r = S.swap_best(r, v)
172 -- Return an ID for a stream.
173 function YouTube.to_id(t, itag, smri)
174 return string.format("%s_%s_i%02d_%sp",
175 smri.quality, t.container, itag, t.video.height)
178 -- vim: set ts=2 sw=2 tw=72 expandtab: