从这一点开始:http: //www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system 我正在尝试创建一个应用程序来保存从移动摄像头到远程服务器的视频流。(我在 google 代码中为 android 部分找到了几个示例:ipcamera-for-android、spydroid-ipcamera 等。)
我在这里和网络上阅读了一些答案,但找不到关于如何“读取”和保存服务器端数据流的解决方案。
我对 java 的了解很差,所以我宁愿能够在 PHP 中创建服务器端脚本(使用服务器套接字或其他东西)。有人可以在这方面提供帮助吗?
更新
使用我对 mplayer / ffmpeg mencorer 等工具的了解,我能够保存视频流……例如,使用 ipcamera-for-android 及其在服务器端使用的 nanohttp 服务器:
ffmpeg-i "http://{ip of android phone}:8080/live.flv" /my/server/path/stream.flv
但是,只能在局域网中使用,我需要移动连接服务器,反之亦然。
更新 2
一些进展..在服务器端使用这个脚本
#!/usr/bin/php5
<?php
$handle = fopen("stream.3gp","w");
$socket = stream_socket_server("tcp://192.168.0.102:9000", $errno, $errstr);
if ($socket)
{
echo "start listening\n";
while ( $conn = stream_socket_accept($socket, 180))
{
echo "phone connected\n";
while ($chunk = stream_socket_recvfrom($conn, 1500))
{
fwrite($handle,$chunk);
}
}
}
fclose($handle);
fclose($socket);
?>
但是,3gp文件还不能播放..
更新 3
#!/usr/bin/php5
<?php
$socket = stream_socket_server("tcp://192.168.0.102:9000", $errno, $errstr);
$file = "saved.3gp";
$threegp_header = "\x00\x00\x00\x18\x66\x74\x79\x70\x33\x67\x70\x34\x00\x00\x03\x00\x33\x67\x70\x34\x33\x67\x70\x36";
$four_bytes = "\x00\x00\x00\x00";
if (!$socket) {
echo "$errstr ($errno)\n";
} else {
echo "server start listening\n";
while ( $conn = @stream_socket_accept($socket, 180))
{
echo "phone connected\n";
$handle = fopen($file,"w");
//mediaRecorder gives invalid stream header, so I replace it discarding first 32 byte, replacing with 28 good byte (standard 3gp header plus 4 empty bytes)
$discard = stream_get_contents($conn, 32);
fwrite($handle, $threegp_header);
fwrite($handle, $four_bytes);
//then confinue to write stream on file until phone stop streaming
while(!feof($conn))
{
fwrite($handle, stream_get_contents($conn, 1500));
}
echo "phone disconnected\n";
fclose($handle);
//then i had to update 3gp header (bytes 25 to 28) with the offset where moov atom starts
$handle = fopen($file,"c");
$output = shell_exec('grep -aobE "moov" '.$file);
$moov_pos = preg_replace('/moov:(\d+)/i', '\\1', $output);
$moov_pos_ex = strtoupper(str_pad(dechex($moov_pos - 24), 8, "0", STR_PAD_LEFT));
fwrite($handle, $threegp_header);
$tmp = '';
foreach(str_split($moov_pos_ex,2) as $hex)
{
$tmp .= pack('C*', hexdec($hex));
}
fwrite($handle, $tmp);
fclose($handle);
}
echo "phone disconnected\n";
}
@fclose($handle);
fclose($socket);
?>
经过一些实验,这次 vlc/mplayer 似乎可以播放它.. 音频仍然存在一些问题(但我认为我在 android 方面有问题)