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?
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
- 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. - 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 secondswidth
: a DOUBLE indicating the width of the video in pixelsheight
: a DOUBLE indicating the height of the video in pixelsvideodatarate
: a DOUBLE indicating the video bit rate in kilobits per secondframerate
: a DOUBLE indicating the number of frames per secondvideocodecid
: 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 replayedaudiosamplesize
: a DOUBLE indicating the resolution of a single audio samplestereo
: a BOOL indicating whether the data is stereoaudiocodecid
: 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.
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.