计算flv视频文件的长度?使用纯PHP [英] calculate flv video file length ? using pure php

查看:128
本文介绍了计算flv视频文件的长度?使用纯PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有外部依赖关系(例如ffmpege)的情况下,使用php计算flv文件的长度的最佳方法是什么,因为客户端站点在共享主机上运行, itry http://code.google.com/p/flv4php/,但它会提取元数据,并非所有视频都包含元数据?

What is the best way to calculate the length of flv file using php with out external dependencies like ffmpege because client site run on shared hosting, itry http://code.google.com/p/flv4php/, but it extract metadata and not all video contain meta data ?

推荐答案

有一种不太复杂的方法.

There's a not too complicated way to do that.

FLV文件具有特定的数据结构,只要文件格式正确,就可以以相反的顺序对其进行解析.

FLV files have a specific data structure which allow them to be parsed in reverse order, assuming the file is well-formed.

只需打开文件并在文件末尾前查找4个字节即可.

Just fopen the file and seek 4 bytes before the end of the file.

您将获得一个大的endian 32位值,该值代表这些字节之前的标签大小(FLV文件由标签组成).您可以将unpack函数与'N'格式规范一起使用.

You will get a big endian 32 bit value that represents the size of the tag just before these bytes (FLV files are made of tags). You can use the unpack function with the 'N' format specification.

然后,您可以返回到刚刚找到的字节数,从而使您转到文件中最后一个标记的开头.

Then, you can seek back to the number of bytes that you just found, leading you to the start of the last tag in the file.

标签包含以下字段:

  • 一个字节表示标记的类型
  • 一个大的endian 24位整数,表示此标签的正文长度(应该是您之前找到的值,减去11 ...如果不是,则表示有问题)
  • 一个大字节序的24位整数,表示文件中标签的时间戳(以毫秒为单位),再加上一个8位整数,将时间戳扩展到32位.

因此,您所需要做的就是跳过前32位,然后解包('N',...)您读取的时间戳值.

So all you have to do is then skip the first 32 bits, and unpack('N', ...) the timestamp value you read.

由于FLV标签的持续时间通常很短,因此应该为文件提供相当准确的持续时间.

As FLV tag duration is usually very short, it should give a quite accurate duration for the file.

以下是一些示例代码:

$flv = fopen("flvfile.flv", "rb");
fseek($flv, -4, SEEK_END);
$arr = unpack('N', fread($flv, 4));
$last_tag_offset = $arr[1];
fseek($flv, -($last_tag_offset + 4), SEEK_END);
fseek($flv, 4, SEEK_CUR);
$t0 = fread($flv, 3);
$t1 = fread($flv, 1);
$arr = unpack('N', $t1 . $t0);
$milliseconds_duration = $arr[1];

最后两个fseek可以分解,但为了清楚起见,我将它们都保留了.

The two last fseek can be factorized, but I left them both for clarity.

经过一些测试后修正了代码

Fixed the code after some testing

这篇关于计算flv视频文件的长度?使用纯PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆