An open API service indexing awesome lists of open source software.

https://github.com/sonsongithub/youtubegetvideoinfoapiparser

Swift Library to parse YouTube streaming data from get_video_info API.
https://github.com/sonsongithub/youtubegetvideoinfoapiparser

Last synced: 29 days ago
JSON representation

Swift Library to parse YouTube streaming data from get_video_info API.

Awesome Lists containing this project

README

        

[![Version](http://img.shields.io/cocoapods/v/YouTubeStreamingURLParser.svg?style=flat)](http://cocoadocs.org/docsets/YouTubeStreamingURLParser)
[![License](https://img.shields.io/cocoapods/l/YouTubeStreamingURLParser.svg?style=flat)](http://cocoadocs.org/docsets/YouTubeStreamingURLParser)
[![Platform](https://img.shields.io/cocoapods/p/YouTubeStreamingURLParser.svg?style=flat)](http://cocoadocs.org/docsets/YouTubeStreamingURLParser)

# YouTubeStreamingURLParser

## Overview
Swift Library to parse YouTube streaming data from `get_video_info` API.

You have to extract the raw media streaming URL of a YouTube movie when you want to play it using AVFoundation. For example, following code does not work.

```
// This code DOES NOT work.
let youtubeUrl = "https://www.youtube.com/watch?v=ROEIKn8OsGU"
if let url = URL(string: youtubeUrl) {
let item = AVPlayerItem(url: url)
let player = AVPlayer(playerItem: item)
....
}
```

You have to download a movie information via `get_video_info` API, `https://www.youtube.com/get_video_info?video_id=XXXXXXXXXXXXX`, before playing the movie.
While, downloading and parsing the information is very boaring.
YouTubeStreamingURLParser helps you to do it.

## YouTube streaming information

This API returns a streaming information with CGI parameter style.

```
token=&idpj=&as_launched_in_country=&iurlmq=&
view_count=&iurl=&iv_invideo_url=&thumbnail_url=&
sffb=&cc_font=&keywords=&fade_out_duration_milliseconds=&
ad_module=&csi_page_type=&shortform=&c=&
allowed_ads=&timestamp=&plid=&title=&
allow_html5_ads=&probe_url=&afv=&midroll_prefetch_size=&
account_playback_token=&adsense_video_doc_id=&iurlsd=&
of=&caption_tracks=&watermark=&use_cipher_signature=&
dbp=&default_audio_track_index=&midroll_freqcap=&
excluded_ads=&ad_flags=&allow_ratings=&no_get_video_log=&
cver=&cc_asr=&iurlmaxres=&caption_translation_languages=&
iv_load_policy=&fade_in_duration_milliseconds=&video_verticals=&
ptchn=&oid=&iurlhq=&gut_tag=&
apply_fade_on_midrolls=&instream_long=&adaptive_fmts=&
dashmpd=&cid=&muted=&subtitles_xlb=&
core_dbp=&cc3_module=&cl=&storyboard_spec=&
ptk=&iv_allow_in_place_switch=&vm=&video_id=&
ad_device=&url_encoded_fmt_stream_map=&iv_module=&
ad_logging_flag=&author=&pltype=&fade_in_start_milliseconds=&
mpvid=&caption_audio_tracks=&cc_module=&
cc_fonts_url=&ad_slots=&has_cc=&show_content_thumbnail=&
ldpj=&iv3_module=&length_seconds=&tmi=&
hl=&ypc_ad_indicator=&ttsurl=&enabled_engage_types=&
is_listed=&ucid=&eventid=&fade_out_start_milliseconds=&
fmt_list=&avg_rating=&fexp=&tag_for_child_directed=&
loeid=&afv_ad_tag=&allow_embed=&enablecsi=&status=ok
```

## Streaming URL in "url\_encoded\_fmt\_stream\_map"

An entry whose key is "url\_encoded\_fmt\_stream\_map" includes CGI parameter style text which is comma seperated. This parameter often has one more than entries. Each entry involves the movie's video type, raw media streaming URL, size and so on.

If you want to get the movie of raw streaming URL,

```
,,,,,,
```

```
fallback_host=tc.v17.cache7.googlevideo.com&
itag=22&
url=&
type=video%2Fmp4%3B+codecs%3D%22avc1.64001F%2C+mp4a.40.2%22&
quality=hd720
```

## How to use

```
let infoURL = NSURL(string:"https://www.youtube.com/get_video_info?video_id=\(youtubeContentID)") {
let request = NSMutableURLRequest(URL: infoURL)
let session = NSURLSession(configuration: sessionConfiguration)
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if let error = error {
print(error)
} else if let data = data, result = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
// Pattern 1
// Get streaming map directly
let maps = FormatStreamMapFromString(result)
if let map = maps.first {
print(map.url)
}
// Pattern 2
// Get streaming informaton
// You can access stream map from this object, too.
let streaming = YouTubeStreamingFromString(result)
print(streaming.title)
}
})
task.resume()

```