复制视频流动作脚本3

时间:2016-10-10 09:27:53

标签: actionscript-3 video

早上好,

我正在制作视频课程,使用CRTMP服务器进行流式传输。这很好,但对于我的解决方案,我需要复制视频流(对于某些效果)。

我用Google搜索了重复的MovieClip并尝试复制这样的视频。

import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.media.*;
import flash.system.*;
import flash.utils.ByteArray;

public class Main extends MovieClip
{
    public var netStreamObj:NetStream;
    public var nc:NetConnection;
    public var vid:Video;
    public var vid2:Video;
    public var streamID:String;
    public var videoURL:String;
    public var metaListener:Object;

    public function Main()
    {
        init_RTMP();
    }

    private function init_RTMP():void
    {

        streamID = "szene3.f4v";
        videoURL = "rtmp://213.136.73.230/maya";

        vid = new Video(); //typo! was "vid = new video();"
        vid2 = new Video();
        nc = new NetConnection();
        nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
        nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
        nc.client = {onBWDone: function():void
        {
        }};
        nc.connect(videoURL);
    }

    private function onConnectionStatus(e:NetStatusEvent):void
    {
        if (e.info.code == "NetConnection.Connect.Success")
        {
            trace("Creating NetStream");
            netStreamObj = new NetStream(nc);

            metaListener = new Object();
            metaListener.onMetaData = received_Meta;
            netStreamObj.client = metaListener;

            netStreamObj.play(streamID);
            vid.attachNetStream(netStreamObj);
            //vid2.attachNetStream(netStreamObj); // wont work
            addChild(vid);
            // addChild(vid2); // wont work either

                //intervalID = setInterval(playback, 1000);
        }
    }

    private function asyncErrorHandler(event:AsyncErrorEvent):void
    {
        trace("asyncErrorHandler.." + "\r");
    }





    private function received_Meta(data:Object):void
    {
        var _stageW:int = stage.stageWidth;
        var _stageH:int = stage.stageHeight;

        var _videoW:int;
        var _videoH:int;
        var _aspectH:int;

        var Aspect_num:Number; //should be an "int" but that gives blank picture with sound
        Aspect_num = data.width / data.height;

        //Aspect ratio calculated here..
        _videoW = _stageW;
        _videoH = _videoW / Aspect_num;
        _aspectH = (_stageH - _videoH) / 2;

        vid.x = 0;
        vid.y = _aspectH;
        vid.width = _videoW;
        vid.height = _videoH;

        vid2.x = 0;
        vid2.y = _aspectH  ;
    }
} 

应该可以复制视频流。 2同一videoStream的实例。我做错了什么?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

  
      
  • “这意味着我必须加倍网络流量。这不是我想要的。”
  •   
  • “我尝试按照Bitmap.clone复制视频。但我收到了沙箱违规行为。”
  •   

您可以尝试此处建议的解决方法:Netstream Play(null) Bitmapdata Workaround

我将快速演示如何将其应用于您的代码。 此演示代码假设您的画布宽度= 550&高度= 400。如果按比例放大,请保持该比率。

package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.media.*;
    import flash.system.*;
    import flash.utils.ByteArray;

    import flash.geom.*;
    import flash.filters.ColorMatrixFilter;

    public class Main extends MovieClip
    {
        public var netStreamObj:NetStream;
        public var nc:NetConnection;
        public var vid:Video;

        public var streamID:String;
        public var videoURL:String;
        public var metaListener:Object;

        public var vid1_sprite : Sprite = new Sprite();
        public var vid2_sprite : Sprite = new Sprite();

        public var vid2_BMP : Bitmap;
        public var vid2_BMD : BitmapData;

        public var colMtx:Array = new Array(); //for ColorMatrix effects
        public var CMfilter:ColorMatrixFilter;


        public function Main()
        {
            init_RTMP();
        }

        private function init_RTMP():void
        {

            streamID = "szene3.f4v";
            videoURL = "rtmp://213.136.73.230/maya";

            vid = new Video();
            nc = new NetConnection();
            nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
            nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

            nc.client = { onBWDone: function():void { } };

            //nc.connect(null); //for file playback
            nc.connect(videoURL); //for RTMP streams

        }

        private function onConnectionStatus(e:NetStatusEvent):void
        {
            if (e.info.code == "NetConnection.Connect.Success")
            {
                trace("Creating NetStream");
                netStreamObj = new NetStream(nc);

                metaListener = new Object();
                metaListener.onMetaData = received_Meta;
                netStreamObj.client = metaListener;

                //netStreamObj.play("vid.flv"); //if File
                netStreamObj.play(streamID); //if RTMP

                vid.attachNetStream(netStreamObj);

           }
        }

        private function asyncErrorHandler(event:AsyncErrorEvent):void
        {
            trace("asyncErrorHandler.." + "\r");
        }

        private function received_Meta(data:Object):void
        {
            trace("Getting metadata");

            var _stageW:int = stage.stageWidth;
            var _stageH:int = stage.stageHeight;

            var _videoW:int = data.width;
            var _videoH:int = data.height;
            var _aspectH:int = 0;

            var Aspect_num:Number;
            Aspect_num = data.width / data.height;

            //Aspect ratio calculated here..
            _videoW = _stageW;
            _videoH = _videoW / Aspect_num;
            _aspectH = (_stageH - _videoH) / 2;

            trace("_videoW : " + _videoW);
            trace("_videoW : " + _videoH);
            trace("_aspectH : " + _aspectH);

            vid.x = 0;
            vid.y = 0;
            vid.width = _videoW;
            vid.height = _videoH;

            setup_Copy(); //# Do this after video resize 
        }

        public function setup_Copy () : void
        {
            vid2_BMD = new BitmapData(vid.width, vid.height, false, 0);
            vid2_BMP = new Bitmap( vid2_BMD );

            vid1_sprite.addChild(vid);
            vid1_sprite.x = 0;
            vid1_sprite.y = 0;
            addChild( vid1_sprite );

            vid2_sprite.addChild( vid2_BMP );
            vid2_sprite.x = 0;
            vid2_sprite.y = vid.height + 5;

            addChild( vid2_sprite );

            stage.addEventListener(Event.ENTER_FRAME, draw_Video);
        }

        public function draw_Video (evt:Event) : void
        {
            if ( netStreamObj.client.decodedFrames == netStreamObj.decodedFrames ) { return; } // Here we skip multiple readings

            //# Get bitmapdata directly from container of video
            if ( vid1_sprite.graphics.readGraphicsData().length > 0 ) 
            {
                vid2_BMD = GraphicsBitmapFill(vid1_sprite.graphics.readGraphicsData()[0]).bitmapData;
            }

            effect_BitmapData(); //# Do an effect to bitmapdata

        }

        public function effect_BitmapData ( ) : void
        {
            //# Matrix for Black & White effect
            colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // red
            colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // green
            colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // blue
            colMtx = colMtx.concat([0, 0, 0, 1, 0]); // alpha

            CMfilter = new ColorMatrixFilter(colMtx);
            vid2_BMP.bitmapData.applyFilter(vid2_BMD, new Rectangle(0, 0, vid.width, vid.height), new Point(0, 0), CMfilter);

        }


    } 
}