您可能想考虑使用具有命令行功能的视频播放器,例如 VLC。您可以通过 PHP 的内在 'exec' 函数访问它并解析结果。或者,您也可以使用 FFMPEG 库打开流并确定它是否存在/是否可以播放。
FFMPEG: http: //ffmpeg-php.sourceforge.net/
VLC: http: //www.videolan.org/vlc/
我们公司专注于在线流媒体视频,我们遇到了一些相同的问题;应该给你一个很好的起点。
此外,这是我编写的一段非常糟糕的代码,用于帮助我们使用 PHP 的 socket_connect 验证 RTSP 流。你可能会从中得到一些用处。
final static public function validateRTSP($url)
{
$url_bits = parse_url($url);
$port = isset($url_bits['port']) ? $url_bits['port'] : 554;
if(false == isset($url_bits['host']))
{
throw new Exception("The URL `{$url}` does not have a valid host assignment.");
}
if(isset($url_bits['host']))
{
if(false === $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))
{
socket_close($socket);
throw new Exception('A socket could not be opened: ' . socket_strerror(socket_last_error($socket)));
}
if(false === socket_connect($socket, $url_bits['host'], $port))
{
socket_close($socket);
throw new Exception("A connection could not be established to {$url_bits['host']}: " . socket_strerror(socket_last_error($socket)));
}
$headers = array();
$headers[] = "DESCRIBE {$url} RTSP/1.0";
$headers[] = "User-Agent: WMPlayer/12.00.7600.16385 guid/3300AD50-2C39-46C0-AE0A-39E48EB3C868";
$headers[] = "Accept: application/sdp";
$headers[] = "Accept-Charset: UTF-8, *;q=0.1";
$headers[] = "X-Accept-Authentication: Negotiate, NTLM, Digest";
$headers[] = "Accept-Language: en-US, *;q=0.1";
$headers[] = "CSeq: 1";
$headers[] = "Supported: com.microsoft.wm.srvppair, com.microsoft.wm.sswitch, com.microsoft.wm.eosmsg, com.microsoft.wm.predstrm, com.microsoft.wm.fastcache, com.microsoft.wm.locid, com.microsoft.wm.rtp.asf, dlna.announce, dlna.rtx, dlna.rtx-dup, com.microsoft.wm.startupprofile";
$headerString = implode("\r\n", $headers) . "\r\n\r\n";
if(false === socket_write($socket, $headerString, strlen($headerString)))
{
socket_close($socket);
throw new Exception("Could not send headers to {$url_bits['host']}: " . socket_strerror(socket_last_error($socket)));
}
$response = '';
if(false === socket_recv($socket, $response, 2048, MSG_WAITALL))
{
socket_close($socket);
throw new Exception("Could not read response from {$url_bits['host']}: " . socket_strerror(socket_last_error($socket)));
}
socket_close($socket);
self::$passes[] = array
(
'request' => $headerString,
'response' => $response
);
if(preg_match_all('#^RTSP/.*\s+302+\s#i', $response, $match))
{
preg_match_all('#(Location:\s(.*))\r\n#i', $response, $redirect_match);
return self::url($redirect_match[2][0]);
}
if(false == preg_match('#^RTSP/.*\s+[200]+\s#i', $response))
{
throw new Exception("URL `{$url}` is invalid.");
}
if($attributes = array_pop(explode("\r\n\r\n", $response)))
{
preg_match_all("#([a-z]{1})={1}(.+)#i", $attributes, $all);
self::$attributes = $all[0];
}
return true;
}