📜 ⬆️ ⬇️

We play videos from YouTube using Action script 3.0



And yet customers are not predictable people!

You work on the project, you work and everything seems to be fine, but he (the customer) a week before the project’s delivery: “I want the video to play on my flash-site. Yes, not simple, but from YouTube. ” Well, what can you do ... Nothing.
')
For clarification, I will say that the site is made in Flash CS4 using Action Script 3.0.

However, the problem, as it turned out not easy. And as always I had to get out.



Thanks for the karma! Transferred an article to the Adobe Flex blog.



Where to begin:



Search for articles on the topic of interest? And what pleases any, but the trouble is, as the author himself says, this method no longer works, and the promised update of the article is not yet available. Task…

You can, of course, download the entire player, but since it is written in Action Script 2, you can forget about controlling playback. And even such a simple action as scaling a video becomes, unbearably, challenging. And so this method also disappears.

What to do? To work, to work and to work again "And yes you will be rewarded for your labors." And so I write the decision in steps:

First step. We take out the YouTube player from the browser's cache (in my case, the FireFox browser is about: cache).

The second step. We look at the player code with the help of special programs (there are many of them, I use Sothink SWF Decompiler).

The third step. We are looking for in numerous classes the information we need, namely where and how we can take a direct link to the video. From this article, we know that in order to get a direct link to a video, we need to know two things: the video identification number (video_id) and a certain video identifier (the t parameter). With video_id, everything is simple, it is in the video request line http://www.youtube.com/watch?v=lIZVEnyHoGU and is called “v”. And here is where to get the “t”, and here we are helped by the player that we parsed into parts from which we learn that there is a certain request to the server with the ID of the video, in which we get information about it. The request is as follows http://youtube.com/get_video_info.php?video_id=lIZVEnyHoGU . If you insert this link in the browser line, then you will be prompted to download a file called “get_video_info.php”. Download and view the contents of the file. The file contains a line with variables of the form "status = ok & vq = None & author = guylevy ..., etc.". So, of the whole variety of variables, we need one, it is called “token” - in fact, this is the same “t” network that we were looking for.

Step Four. We have achieved what we wanted - we received a direct link to the video and now, if we add up everything received together http://www.youtube.com/pt_pdf_pdf.pdf.pdf.pcfvn_fc_flc_vc_v4_fb_gr8T6BHzSFMbwf6gW7h8B51Da_gr8T6BHzSFMbwf66W7h8B51Da_gr8T6BHzSFMbwf66WWH4B51Da_gr8T6BHzSFMbwf66WWH4B51Da_gr8T6BHzSFMbwf6. flv-movie with the name "video.flv", which in turn can either download or play in your player.

And so for the third step, we need to download as a text file “get_video_info.php” and extract from it the information we need. This was helped by the “URL.as” class, which I found on one of the forums, it helps me to get almost any information from the url, and after my refinement I also encode and decode the url with coded dots, dashes, ins and so on. d.
The whole process of getting a direct link, I implemented in the class "YouTubeVideoPlayer.as"

Also, using the article about 10 tricks for working with YouTube in the class “YouTubeVideoPlayer.as”, you can set the quality of the video received, provided that it exists.

Actually, these two classes that will “save the world” :)

(The code above is written for use in Flex)

package com
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

import mx.controls.VideoDisplay;
import mx.events.VideoEvent;

import com.URL;

/**
* YouTubeVideoPlayer.
* @author Aslanyan Tsolak Rubikovich
* @version 1.0
*/

public class YouTubeVideoPlayer extends VideoDisplay
{
private static var GET_VIDEO :String = "http://www.youtube.com/get_video.php";
private static var GET_VIDEO_INFO :String = "http://youtube.com/get_video_info.php";
private static var URL_PREFIX :String = "http://youtube.com?";
private static var QUALITY0 :String = "";
private static var QUALITY1 :String = "&fmt=18";
private static var QUALITY2 :String = "&fmt=22";
private static var QUALITY3 :String = "&ap=%2526fmt%3D18";
private static var QUALITY4 :String = "&ap=%2526fmt%3D22";

private var videoID :String;
private var videoToken :String;
private var videoQuality :String;
private var InfoLoader :URLLoader;

/**
* .
* @params source: www.youtube.com/watch?v=lIZVEnyHoGU
* @params quality: [0,1,2,3,4]
* @return void
*/

public function YouTubeVideoPlayer(source:String, quality:Number):void
{
super();

videoQuality = DefineQuality(quality);

LoadVideoInfo(source);
}

/**
* .
* @params quality: [0,1,2,3,4]
* @return
*/

private function DefineQuality(quality:Number):String
{
switch(quality)
{
case 0: return QUALITY0; break;
case 1: return QUALITY1; break;
case 2: return QUALITY2; break;
case 3: return QUALITY3; break;
case 4: return QUALITY4; break;
}

return QUALITY0;
}

/**
* .
* @params source: www.youtube.com/watch?v=lIZVEnyHoGU
* @return void
*/

private function LoadVideoInfo(source:String):void
{
videoID = GetVideoID(source);

var urlRequest :URLRequest = new URLRequest(GET_VIDEO_INFO + "?video_id=" + videoID);

InfoLoader = new URLLoader();
InfoLoader.addEventListener(Event.COMPLETE, OnInfoLoaderComplete);
InfoLoader.load(urlRequest);
}

/**
* source VideoID.
* @params source: www.youtube.com/watch?v=lIZVEnyHoGU
* @return VideoID
*/

private function GetVideoID(source:String):String
{
var _url:URL = new URL(source);

return _url.query.parsed.v;
}

/**
* get_video_info.php.
* @return void
*/

private function OnInfoLoaderComplete(e:Event):void
{
InfoLoader.removeEventListener(Event.COMPLETE, OnInfoLoaderComplete);

var str:String = URL_PREFIX + "?" + e.target.data.toString();

videoToken = GetVideoToken(str);

var fullVideoUrl:String = FlvUrlConstruct(videoID, videoToken);

InitVideoDisplay(fullVideoUrl);
}

/**
* source videoToken.
* @params source: get_video_info.php.
* @return videoToken
*/

private function GetVideoToken(source:String):String
{
var _url:URL = new URL(source);

return _url.query.parsed.token;
}

/**
* URL .
* @params video_id: id .
* @params token: id .
* @return videoToken
*/

private function FlvUrlConstruct(video_id:String, token:String):String
{
var fullUrl:String = GET_VIDEO + "?video_id=" + video_id + "&t=" + token + videoQuality;

return fullUrl;
}

/**
* VideoDisplay.
* @params source: .
* @return void
*/

private function InitVideoDisplay(source:String):void
{
this.autoPlay = true; //
this.source = source; //
this.width = 500; //
this.height = 281.25; //
this.autoRewind = true; //
this.maintainAspectRatio = true; //
this.playheadUpdateInterval = 100; // VideoEvent.PLAYHEAD_UPDATE
this.bufferTime = 5; //

this.addEventListener(VideoEvent.STATE_CHANGE, StateChange);
}

private function StateChange(e:VideoEvent):void
{
trace(e.state)
}
}
}


and one more class


package com
{
/**
* Dschini.org - Manfred Weber
* manfred.dschini.org
* manfred.weber (at) gmail dot com
* Updated Tsolak Aslanyan
* version 1.1
*/

public class URL
{
private static const PATTERN:RegExp = /^([A-Za-z0-9_+.]{1,8}:\/\/)?([!-~]+@)?([^\/?#:]*)(:[0-9]*)?(\/[^?#]*)?(\?[^#]*)?(\#.*)?/i;

private var _url :String;
private var _scheme :String;
private var _userinfo :String;
private var _host :String;
private var _port :String;
private var _path :String;
private var _query :String;
private var _fragment :String;

/**
* Create new URL Object
* @params The url
*/

function URL(url:String):void
{
var result:Array = url.match(URL.PATTERN);

_url = result[0]; // user:pass@example.com:80/foo/bar.php?var1=foo&var2=bar#abc
_scheme = result[1]; // http://
_userinfo = result[2]; // user:pass@
_host = result[3]; // example.com
_port = result[4]; // :80
_path = result[5]; // /foo/bar.php
_query = result[6]; // ?var1=foo&var2=bar
_fragment = result[7]; // #abc
}

/**
* Get the url
*/

public function get url():String
{
return _url.length <= 0 ? undefined : _url;
}

/**
* Get the scheme
*/

public function get scheme():String
{
return _scheme.length <= 0 ? undefined : _scheme.substring(0 , _scheme.length - 3);
}

/**
* Get the userinfo
* Returns an object containing the user and/or password
*/

public function get userinfo():Object
{
var ret:Object = {user:undefined, pass:undefined};

if(_userinfo)
{
var arr:Array = _userinfo.substring(0, _userinfo.length - 1).split(':');

ret.user = arr[0] ? arr[0] : ret.user;
ret.pass = arr[1] ? arr[1] : ret.pas;
}

return ret;
}

/**
* Get the host
*/

public function get host():String
{
return _host.length <= 0 ? undefined : _host;
}

/**
* Get the port
*/

public function get port():int
{
return _port.length <= 0 ? undefined : int(_port.substring(1, _port.length));
}

/**
* Get the path
*/

public function get path():String
{
return _path.length <= 0 ? undefined : _path;
}

/**
* Get the query
* Returns an object containing the raw and parsed query string
*/

public function get query():Object
{
var ret:Object = {raw:undefined, parsed:undefined};

if(_query && _query.length > 0)
{
ret.raw = _query;

var _parse :String = _query.substring(1, _query.length);
var _intovars :Array = _parse.split("&");

ret.parsed = _intovars.length > 0 ? {} : undefined;

for(var i:int = 0; i < _intovars.length; i++)
{
var _kv:Array = _intovars[i].split("=");

ret.parsed[_kv[0]] = _kv[1];
}
}
return ret;
}

/**
* Get the fragment
*/

public function get fragment():String
{
return _fragment.length <= 0 ? undefined : _fragment;
}

/**
* Accepts an encoded string.
* Returns the decoded string.
*/

public function Unescape(value:String):String
{
return unescape(value);
}

/**
* Accepts an decoded string.
* Returns the encoded string.
*/

public function Escape(value:String):String
{
return escape(value);
}
}
}


Here is an example of using classes in Flex Builder:



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script><![CDATA[

import com.YouTubeVideoPlayer;

private var vp:YouTubeVideoPlayer;

private function GetVideo():void
{
if(vp != null)
{
vp.stop();
vp.close();
removeChild(vp);
}

var vp:YouTubeVideoPlayer = new YouTubeVideoPlayer(utUrl.text, 1);
vp.x = 119;
vp.y = 38;
addChild(vp);
}
]]></mx:Script>

<mx:Label x="10" y="12" text=" "/>
<mx:TextInput id="utUrl" x="119" y="10" width="600" text="http://www.youtube.com/watch?v=lIZVEnyHoGU"/>
<mx:Button x="727" y="10" label="" click="GetVideo()"/>

</mx:Application>


I hope this article will help someone, or at least will seem interesting.

PS
Thank you so much ayurganov for an invite !!!

Pps
No karma to transfer the topic: (

Source: https://habr.com/ru/post/69537/


All Articles