2 -- Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
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.
27 local Y
= require
'quvi/youtube'
31 -- Parse media properties.
33 return YouTube
.parse_properties(qargs
)
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
)
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'
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 U
= require
'quvi/util'
70 local c
= U
.decode(quvi
.http
.fetch(u
).data
)
73 local reason
= U
.unescape(c
['reason'])
74 local code
= c
['errorcode']
75 error(string.format("%s (code=%s)", reason
, code
))
81 -- Appends the &begin parameter to the media stream URL.
82 function YouTube
.append_begin_param(qargs
)
83 local m
,s
= qargs
.input_url
:match('t=(%d?%d?m?)(%d%d)s')
84 m
= tonumber(((m
or ''):gsub('%a',''))) or 0
85 s
= tonumber(((s
or ''):gsub('%a',''))) or 0
86 local ms
= (m
*60000) + (s
*1000)
88 for i
,v
in ipairs(qargs
.streams
) do
89 local url
= qargs
.streams
[i
].url
90 qargs
.streams
[i
].url
= url
.."&begin=".. ms
92 qargs
.start_time_ms
= ms
96 -- Iterates the available streams.
97 function YouTube
.iter_streams(config
, U
)
99 -- Stream map. Holds many of the essential properties,
100 -- e.g. the media stream URL.
102 local stream_map
= U
.unescape(config
['url_encoded_fmt_stream_map']
103 or error('no match: url_encoded_fmt_stream_map'))
107 for d
in stream_map
:gmatch('([^,]*),') do
108 local d
= U
.decode(d
)
110 local ct
= U
.unescape(d
['type'])
111 local v_enc
,a_enc
= ct
:match('codecs="([%w.]+),%s+([%w.]+)"')
112 local itag
= d
['itag']
113 local cnt
= (ct
:match('/([%w-]+)')):gsub('x%-', '')
115 url
= U
.unescape(d
['url']) -- d['sig'] ? "&signature=val" : ""
116 .. (d
['sig'] and ('&signature='..d
['sig']) or ''),
117 quality
= d
['quality'],
126 -- Format list. Combined with the above properties. This list is used
127 -- for collecting the video resolution.
129 local fmtl
= U
.unescape(config
['fmt_list'] or error('no match: fmt_list'))
130 local S
= require
'quvi/stream'
133 for itag
,w
,h
in fmtl
:gmatch('(%d+)/(%d+)x(%d+)') do
134 local smri
= smr
[itag
]
135 local t
= S
.stream_new(smri
.url
)
137 t
.video
.encoding
= smri
.v_enc
or ''
138 t
.audio
.encoding
= smri
.a_enc
or ''
139 t
.container
= smri
.container
or ''
140 t
.video
.height
= tonumber(h
)
141 t
.video
.width
= tonumber(w
)
143 -- Do this after we have the video resolution, as the to_id
144 -- function uses the height property.
145 t
.id
= YouTube
.to_id(t
, itag
, smri
)
151 YouTube
.ch_best(S
, r
) -- Pick one stream as the 'best' quality.
157 -- Picks the stream with the highest video height property
158 -- as the best in quality.
159 function YouTube
.ch_best(S
, t
)
160 local r
= t
[1] -- Make the first one the 'best' by default.
162 for _
,v
in pairs(t
) do
163 if v
.video
.height
> r
.video
.height
then
164 r
= S
.swap_best(r
, v
)
169 -- Return an ID for a stream.
170 function YouTube
.to_id(t
, itag
, smri
)
171 return string.format("%s_%s_i%02d_%sp",
172 smri
.quality
, t
.container
, itag
, t
.video
.height
)
175 -- vim: set ts=2 sw=2 tw=72 expandtab: