views:

373

answers:

2

I'm using the FileReference class to upload flvs to a server. Is it possible to check the flv length not size before allowing an upload?

+4  A: 

Are you targeting Flash Player 10 alone or lower versions too? Because lower versions of Flash player (9 etc) do not allow the uploading SWF to read the contents of file (other than creationDate, creator (The Macintosh creator type of the file), modificationDate, name, size in bytes and type), so there is no way you are going to be able to do this on those players.

If you are targeting solely FP10 users, you can load the FLV into a ByteArray in your SWF and

  1. Play it using an FLV player and read the duration property from the player. But I couldn't find an FLV player that takes a ByteArray as input - and after reading this thread in SO, it seems like that is not possible at all.
  2. Parse the FLV file, and read the duration property from its metadata. The FLV file specification is open, but this isn't going to be easy.

Update to the comment:
Excerpts from the FLV file spec:

onMetaData
An FLV file can contain metadata with an “onMetaData” marker. Various stream properties are available to a running ActionScript program via the NetStream.onMetaData property. The available properties differ depending on the software used.
Common properties include:

  • duration: a DOUBLE indicating the total duration of the file in seconds
  • width: a DOUBLE indicating the width of the video in pixels
  • height: a DOUBLE indicating the height of the video in pixels
  • videodatarate: a DOUBLE indicating the video bit rate in kilobits per second
  • framerate: a DOUBLE indicating the number of frames per second
  • videocodecid: a DOUBLE indicating the video codec ID used in the file (see “Video tags” on page 8 for available CodecID values)
  • audiosamplerate: a DOUBLE indicating the frequency at which the audio stream is replayed
  • audiosamplesize: a DOUBLE indicating the resolution of a single audio sample
  • stereo: a BOOL indicating whether the data is stereo
  • audiocodecid: a DOUBLE indicating the audio codec ID used in the file (see “Audio tags” on page 6 for available SoundFormat values)
  • filesize: a DOUBLE indicating the total size of the file in bytes

FLV file can contain metadata - it doesn't say it will contain metadata. It also says that available properties can vary based on the software used to create FLV. So I guess there is no guarantee (as per specs) that the duration property will be present. That said, duration is one of the basic properties of FLV and it would be safe to assume that any reasonable software would include it.

Amarghosh
Thanks very much that helps a lot. I'm wondering whether they could be a case when the metadata for length doesn't exist. Have you come across this, or am I safe to assume the data will always be available?
Chin
Once again thanks a lot for your help. If you ever need any part time flex or .net work let me know.
Chin
+1  A: 

You can use Netstream.appendBytes to feed FileReference.data (after a call to browse, before a call to upload) to a NetStream used for playing a video. From there, the duration can be taken from the metadata, as described elsewhere on this thread. Note that at least Flash Player 10 is required for this approach.

JoeCoder