Skip to main content
Inspiring
February 9, 2023
Answered

Function that returns the length of a video file in frames & seconds

  • February 9, 2023
  • 2 replies
  • 572 views

Hello all and thanks in advance for any help you can lend.  

 

I frequently use the VideoPlaybackComponent to play videos in my projectors... In this instance, I'm wanting to dynamically select a movie file and I need the script to return the length of an assigned video file.

 

I'm struggling to find a method to do this... 

 

Thanks!

 

Erik

    This topic has been closed for replies.
    Correct answer kglad

    with netstream use this:

     

    var clientObj:Object = new Object(); 
    clientObj.onMetaData = ns_onMetaData; 
    ns.client = clientObj;



    function ns_onMetaData(metaD:Object):void {
    trace(metaD.duration)
    }

    ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

    function asyncErrorHandler(e:AsyncErrorEvent):void {
    }

    2 replies

    kglad
    Community Expert
    February 9, 2023

    flv_pb.metadata.duration;  // in seconds

    flv_pb.metadata.duration*stage.frameRate;  // in frames

    Inspiring
    February 13, 2023

    Kglad, thanks for jumping in on this.

     

    I'm still struggling to get the script to return metadata for any parameters... The video loads and plays OK.  But I'm getting the following error re: metadata:

     

    "Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.NetStream was unable to invoke callback onMetaData. error=ReferenceError: Error #1069: Property onMetaData not found on flash.net.NetStream and there is no default value. at testingNetstream_fla::MainTimeline/frame1()".   

     

     I'll share the basic code I'm experimenting with using NetConnect/NetStream to load and play a video file:

     

    var nc:NetConnection = new NetConnection();
    nc.connect(null);

     

    var vid:Video = new Video();

    addChild(vid);

     

    var ns:NetStream = new NetStream(nc);

    vid.attachNetStream(ns);

     

    ns.play("qt_1880-516.mov"); //my local video file

     

    function ns_onMetaData(item:Object):void {
    trace("ns.metadata.duration")

    }

     

    kglad
    kgladCorrect answer
    Community Expert
    February 13, 2023

    with netstream use this:

     

    var clientObj:Object = new Object(); 
    clientObj.onMetaData = ns_onMetaData; 
    ns.client = clientObj;



    function ns_onMetaData(metaD:Object):void {
    trace(metaD.duration)
    }

    ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

    function asyncErrorHandler(e:AsyncErrorEvent):void {
    }

    Community Expert
    February 9, 2023

    @kglad and JoãoCésar are pretty good with code they might help you.