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 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
)
76 local reason
= U
.unescape(c
['reason'])
77 local code
= c
['errorcode']
78 error(string.format("%s (code=%s)", reason
, code
))
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)
91 for i
,v
in ipairs(qargs
.streams
) do
92 local url
= qargs
.streams
[i
].url
93 qargs
.streams
[i
].url
= url
.."&begin=".. ms
95 qargs
.start_time_ms
= ms
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'))
110 for d
in stream_map
:gmatch('([^,]*),') do
111 local d
= U
.decode(d
)
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%-', '')
118 url
= U
.unescape(d
['url']) -- d['sig'] ? "&signature=val" : ""
119 .. (d
['sig'] and ('&signature='..d
['sig']) or ''),
120 quality
= d
['quality'],
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'
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
)
154 YouTube
.ch_best(S
, r
) -- Pick one stream as the 'best' quality.
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.
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: