1 ! Copyright (C) 2013 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: assocs http.client kernel make math.order sequences
5 splitting urls urls.encoding ;
9 TUPLE: encoding extension resolution video-codec profile
10 video-bitrate audio-codec audio-bitrate ;
12 CONSTANT: encodings H{
15 { 5 T{ encoding f "flv" "240p" "Sorenson H.263" f "0.25" "MP3" 64 } }
16 { 6 T{ encoding f "flv" "270p" "Sorenson H.263" f "0.8" "MP3" 64 } }
17 { 34 T{ encoding f "flv" "360p" "H.264" "Main" "0.5" "AAC" 128 } }
18 { 35 T{ encoding f "flv" "480p" "H.264" "Main" "0.8-1" "AAC" 128 } }
21 { 36 T{ encoding f "3gp" "240p" "MPEG-4 Visual" "Simple" "0.17" "AAC" 38 } }
22 { 13 T{ encoding f "3gp" f "MPEG-4 Visual" f "0.5" "AAC" f } }
23 { 17 T{ encoding f "3gp" "144p" "MPEG-4 Visual" "Simple" "0.05" "AAC" 24 } }
26 { 18 T{ encoding f "mp4" "360p" "H.264" "Baseline" "0.5" "AAC" 96 } }
27 { 22 T{ encoding f "mp4" "720p" "H.264" "High" "2-2.9" "AAC" 192 } }
28 { 37 T{ encoding f "mp4" "1080p" "H.264" "High" "3-4.3" "AAC" 192 } }
29 { 38 T{ encoding f "mp4" "3072p" "H.264" "High" "3.5-5" "AAC" 192 } }
30 { 82 T{ encoding f "mp4" "360p" "H.264" "3D" "0.5" "AAC" 96 } }
31 { 83 T{ encoding f "mp4" "240p" "H.264" "3D" "0.5" "AAC" 96 } }
32 { 84 T{ encoding f "mp4" "720p" "H.264" "3D" "2-2.9" "AAC" 152 } }
33 { 85 T{ encoding f "mp4" "520p" "H.264" "3D" "2-2.9" "AAC" 152 } }
36 { 43 T{ encoding f "webm" "360p" "VP8" f "0.5" "Vorbis" 128 } }
37 { 44 T{ encoding f "webm" "480p" "VP8" f "1" "Vorbis" 128 } }
38 { 45 T{ encoding f "webm" "720p" "VP8" f "2" "Vorbis" 192 } }
39 { 46 T{ encoding f "webm" "1080p" "VP8" f f "Vorbis" 192 } }
40 { 100 T{ encoding f "webm" "360p" "VP8" "3D" f "Vorbis" 128 } }
41 { 101 T{ encoding f "webm" "360p" "VP8" "3D" f "Vorbis" 192 } }
42 { 102 T{ encoding f "webm" "720p" "VP8" "3D" f "Vorbis" 192 } }
45 CONSTANT: video-info-url URL" http://www.youtube.com/get_video_info"
47 : get-video-info ( video-id -- video-info )
49 3 "asv" set-query-param
50 "detailpage" "el" set-query-param
51 "en_US" "hl" set-query-param
52 swap "video_id" set-query-param
53 http-get nip query>assoc ;
55 : video-formats ( video-info -- video-formats )
56 "url_encoded_fmt_stream_map" of "," split
59 : video-download-url ( video-format -- url )
60 [ "url" of ] [ "sig" of ] bi "&signature=" glue ;
62 : sanitize ( title -- title' )
63 [ 0 31 between? ] reject
64 [ "\"#$%'*,./:;<>?^|~\\" member? ] reject
67 : download-video ( video-id -- )
69 video-formats [ "type" of "video/mp4" head? ] find nip
72 "title" of sanitize ".mp4" append download-to