C ++ obtains the playback time of the FLV video file.

Source: Internet
Author: User
Tags flv file

Due to project requirements, you need to use C ++ to obtain the playback time of the FLV video file. The materials found on the Internet include PHP, Perl, and C # implementation. However, I tried it, the effect is not good. Some FLV files can read the playback time, but some cannot. The following are references:
1,
Http://flixforums.com/archive/index.php/t-149.html
This document is the most helpful, including the following:
I compute the FLV duration from the encoded FLV itself. the FLV file format is already ented at macromedias site. basically validate the FLV header and determine if there is a video stream or only audio. then seek to the end of the file and read the ui32 there which is the size of the last tag, then keep seeking backwards skipping over tags until you find a video (or audio if no video) Tag, skip forward a ui24 (TAG size) then read a ui24 and ui8-this is the last timestamp (a big endian ui24 followed by ui8 which is the upper byte) and this is the duration in milliseconds.
It wocould be nice if flixengine had an API to get this directly since it obviously knows.
(It probably means obtaining the timestamp in the last video tag segment is the video playback time)

2,
Description of the FLV file format on the Adobe Official Website:
Video_file_format_spec_v10.pdf
This is the most primitive information.
3,
Http://blog.csdn.net/eleele/archive/2009/09/06/4525457.aspx (this document does not help much to understand the structure of the FLV file, but it is not advisable to obtain the playback time, because the duration description in the script segment is not accurate)
(The above are personal opinions for your reference only)
======================================
C ++ obtains the FLV video playback time code:

// Indicates a file in FLV format.
Flvinfo flvinfo1;
Uint32 taglen, filelen, Len;
Type = FLV;

Ifile. seekg (0, IOS: Beg );

// Read the FLV Header
Ifile. Read (char *) data, 9 );
Memcpy (flvinfo1.signature, Data, 3 );
Flvinfo1.signature [3] = '/0 ';
Flvinfo1.version = data [3];
Flvinfo1.typeflag = data [4];
Flvinfo1.dataoffset = str2ulong (Data + 5 );

// Determine whether video information exists
If (flvinfo1.typeflag & 0x01)
{// Indicates that video data exists.
 
// Move the file pointer to the last 4 bytes at the end of the 0 file
Ifile. seekg (-4, IOS: End );
Filelen = ifile. tellp ();
Len = 4;
While (LEN <filelen)
{
Ifile. Read (char *) data, 4 );
Taglen = str2ulong (data );
Len = Len + taglen;

// Move the pointer to the beginning of the current tag segment
Ifile. seekg (-1 * Len, IOS: End );
Ifile. Read (char *) data, 11 );
If (* (data) = 9)
{
Fsize = str2uint24 (Data + 4)/1000;
Break;
}
// Correct pointer position
Ifile. seekg (-15, IOS: cur );
Len + = 4;
}
}
Else
{
Fsize = 0;
}
==========================================
The struct and functions used in the program are described as follows:
// Structure Description of the FLV File
Struct flvinfo
{
Uint8 signature [4];
Uint8 version;
Uint8 typeflag;
Uint32 dataoffset;
};
// FLV file tag Structure
Struct flvtag
{
Uint32 tagtype;
Uint32 datasize;
Uint32 timestamp;
Uint32 timestampextended;
Uint32 streamid;
};

// Store the high position in the low position
Static unsigned long str2ulong (unsigned char * Str)
{
Return (STR [3] | (STR [2] <8) | (STR [1] <16) | (STR [0] <24 ));
}
Static unsigned short str2usint (unsigned char * Str)
{
Return (STR [1] | (STR [0] <8 ));
}
Static unsigned long str2uint24 (unsigned char * Str)
{
Return (STR [2] | (STR [1] <8) | (STR [0] <16 ));
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.