Home >Backend Development >PHP Tutorial >php sample code to get the time of FLV file

php sample code to get the time of FLV file

怪我咯
怪我咯Original
2017-07-12 11:54:441100browse

FLV is the abbreviation of FLASH VIDEO. The FLV streaming media format is a new video format, the full name is Flash Video. Because the files it forms are extremely small and the loading speed is extremely fast, it makes it possible to watch video files on the Internet. Its appearance effectively solves the problem that after the video files are imported into Flash, the exported SWF file is bulky and cannot be used well on the Internet. etc. Disadvantages

FLV is a video format developed with the launch of Flash MX. It is currently used by many new generation video sharing websites and is currently the fastest growing and most widespread video transmission format. It was developed based on the compression algorithm of Sorenson Company. The FLV format can not only be easily imported into Flash, it is extremely fast, it can also protect copyright, and the video can be played without using the local Microsoft or REAL player.
FLV is a new streaming video format that utilizes the Flash Player platform widely used on web pages to integrate video into Flash animation. In other words, as long as website visitors can watch Flash animations, they can naturally watch FLV format videos without having to install additional other video plug-ins. The use of FLV videos brings great convenience to video dissemination. .

This article mainly introduces the implementation of PHP to obtain the time of FLV files. This article directly gives the implementation code and usage methods. Friends in need can refer to

How does PHP obtain the time of FLV files? The answer is that after fopening the file, view the FLV file as HEX data and convert it to number.

The code is as follows:

<?php
functionBigEndian2Int($byte_word,$signed=false)
{
    $int_value    =0;
    $byte_wordlen=strlen($byte_word);
    for($i=0;$i<$byte_wordlen;$i++){
        $int_value+=ord($byte_word{$i})*pow(256,($byte_wordlen-1-$i));
    }
    if($signed){
        $sign_mask_bit=0x80<<(8*($byte_wordlen-1));
        if($int_value&$sign_mask_bit){
            $int_value=0-($int_value&($sign_mask_bit-1));
        }
    }
    return$int_value;
}
 
functiongetTime($name)
{
    if(!file_exists($name)){
        return;
    }
    $flv_data_length=filesize($name);
    $fp              =@fopen($name,&#39;rb&#39;);
    $flv_header      =fread($fp,5);
    fseek($fp,5,SEEK_SET);
    $frame_size_data_length  =BigEndian2Int(fread($fp,4));
    $flv_header_frame_length=9;
    if($frame_size_data_length>$flv_header_frame_length){
        fseek($fp,$frame_size_data_length-$flv_header_frame_length,SEEK_CUR);
    }
    $duration=0;
    while((ftell($fp)+1)<$flv_data_length){
        $this_tag_header=fread($fp,16);
        $data_length     =BigEndian2Int(substr($this_tag_header,5,3));
        $timestamp       =BigEndian2Int(substr($this_tag_header,8,3));
        $next_offset     =ftell($fp)-1+$data_length;
        if($timestamp>$duration){
            $duration=$timestamp;
        }
        fseek($fp,$next_offset,SEEK_SET);
    }
    fclose($fp);
    return$duration;
}
 
functionget_flv_file_time($time)
{
$time=getTime($time);
    $num=$time;
    $sec=intval($num/1000);
    $h   =intval($sec/3600);
    $m   =intval(($sec%3600)/60);
    $s   =intval(($sec%60));
    $tm  =$h.&#39;:&#39;.$m.&#39;:&#39;.$s;
    return$tm;
}
 
?>

Just use get_flv_file_time("your FLV.flv") directly.

The above is the detailed content of php sample code to get the time of FLV file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn