Hello,
I need to find the length of a flash video hosted on any of the popular video sharing websites. Is this possible?
How does Blinkx determine length of the videos it has indexed?
Thanking you in advance.
Regards Mansoor
Hello,
I need to find the length of a flash video hosted on any of the popular video sharing websites. Is this possible?
How does Blinkx determine length of the videos it has indexed?
Thanking you in advance.
Regards Mansoor
An FLV or RTMP stream contains an onMetaData script that details the duration, if its known by the originator (e.g. you don't get it from 'live' streams).
You can read the FLV and RTMP specs and do your own dissecting, or you can play the video in Flash and wait for the onMetaData callback.
if in C#, I've done smth similar some time ago.
class FLVFileLength
{
private static Double ByteArrayToDouble(byte[] bytes, bool readInReverse)
{
if (bytes.Length != 8)
throw new Exception("bytes must be exactly 8 in Length");
if (readInReverse)
Array.Reverse(bytes);
return BitConverter.ToDouble(bytes, 0);
}
private static Double GetNextDouble(Stream fileStream, int offset, int length)
{
byte[] bytes = new byte[length];
// read bytes
int result = fileStream.Read(bytes, 0, length);
if (result != length)
return -1;
// convert to double (all flass values are written in reverse order)
return ByteArrayToDouble(bytes, true);
}
private static string ByteArrayToString(byte[] bytes)
{
string byteString = string.Empty;
foreach (byte b in bytes)
{
byteString += Convert.ToChar(b).ToString();
}
return byteString;
}
public double GetFLVlength(string URL)
{
try
{
HttpWebRequest http = HttpWebRequest.Create(URL) as HttpWebRequest;
http.Proxy = WebProxyManager.GetNextProxy();
http.UserAgent = UserAgentManager.GetNextUserAgent();
HttpWebResponse response = http.GetResponse() as HttpWebResponse;
Stream streamReader = response.GetResponseStream();
double duration = -1;
Stream fileStream = response.GetResponseStream();
byte[] bytes = new byte[1024];
string tag = "duration";
int length = fileStream.Read(bytes, 0, 1024);
string z = ByteArrayToString(bytes);
if (z.Contains(tag))
{
int pos = z.IndexOf(tag) + tag.Length + 1;
MemoryStream ms = new MemoryStream(bytes, pos, 8);
BinaryReader br = new BinaryReader(ms);
duration = ByteArrayToDouble(br.ReadBytes(8), true);
}
fileStream.Close();
return duration;
}
catch (Exception)
{
return -1;
}
}
}