从Android设备到LAMP服务器的视频流传输

6
从这个链接开始:http://www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system。我正在尝试创建一个应用程序,将移动相机的视频流保存到远程服务器上。(我在Google Code中找到了几个与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文件目前还无法播放... 更新三
#!/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似乎可以播放它... 音频还有一些问题(但我认为是在安卓端出了问题)


1
有了更新的信息,我的以下答案并没有提供太多帮助。ffmpeg/mplayer 解决方案似乎是最好的选择。我建议,解决局域网问题的一个可能的解决方案是设置 VPN 或 SSH 隧道...需要理解这只是一次性的事情/只针对你。 - TryTryAgain
无论如何感谢,我希望能够使用服务器端的出站连接和stream_socket_server找到解决方案。 - Felice Ostuni
2个回答

3

0
根据您使用的传入流(协议等),您已经或想要使用的:
我不确定您愿意在LAMP上使用/安装什么,或者您更喜欢什么,但我知道VLC可以轻松捕获传入流。

http://wiki.videolan.org/Documentation:Streaming_HowTo/Receive_and_Save_a_Stream

当然,只有命令行版本的 VLC 可能是您想要的。我从未尝试过它,不确定它是如何工作的,但我希望它不会安装大量额外的包。这是需要关注的一些可能问题 (参见此处)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接