【问题标题】:Video streaming from Android device to LAMP Server从 Android 设备到 LAMP 服务器的视频流
【发布时间】:2012-04-21 17:47:05
【问题描述】:

从这里开始: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 端有问题)

【问题讨论】:

  • 有了更新的信息,我在下面的回答不是很有帮助。 ffmpeg/mplayer 解决方案似乎是最好的。我会建议,LAN 问题的一个可能解决方案是设置 VPN 或 SSH 隧道...理解这是一次性的事情/只适合您。
  • 还是谢谢,我希望在服务器端使用出站连接和stream_socket_server找到解决方案

标签: php android sockets video-streaming


【解决方案1】:

您可能会想要使用 PHP 的 server socket functionality

Here's a handy tutorial 完成您需要执行的操作以实现数据流。

【讨论】:

    【解决方案2】:

    根据您最终使用或想要使用的传入流(协议等):

    我不确定您愿意在 LAMP 上使用/安装什么,或者您喜欢什么,但我知道 VLC 可以轻松捕获传入的流。

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

    当然,只有命令行版本的 VLC 可能是您想要的。我从来没有做过,不确定它是如何工作的,我希望它不会安装一吨额外的软件包。 This is something to look at 关注可能存在的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      相关资源
      最近更新 更多