2012-04-09 93 views
6

从这一点开始:http://www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system 我试图创建一个应用程序来保存从移动摄像机到远程服务器的视频流。我发现了几个在谷歌Android代码部分的例子:ipcamera-for-android,spydroid-ipcamera等等。)从Android设备到LAMP服务器的视频流

我在这里和网络上看到了一些答案,但是找不到解决方案, “读取”并在服务器端保存数据流。我的Java知识很差,所以我宁愿能够在PHP中创建服务器端脚本(使用服务器套接字或其他东西)。有人可以帮助这部分?

使用IPCAMERA换Android和其nanohttp服务器使用服务器上使用我的工具,如mplayer的/ ffmpeg的mencorer我能够保存视频流千丈...例如UPDATE

方:

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端有问题)

+1

随着更新的信息,我的答案不是很有帮助。 ffmpeg/mplayer解决方案似乎最好。我会建议,局域网问题的一个可能的解决方案是建立VPN或SSH隧道...了解这是一次性的事情/只为你。 – TryTryAgain 2012-04-09 12:29:43

+0

无论如何,我希望能够在服务器端找到使用出站连接和stream_socket_server的解决方案 – 2012-04-09 22:36:06

回答

0

根据接收到的数据流(协议等),你已经结束了还是要最终使用:

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

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

当然,命令行唯一的版本VLC的可能是你想要的东西。我从来没有这样做,不知道这是如何工作的,我希望它不会安装一吨额外的软件包。 T his is something to look at可能涉及的问题。