doonbfez815298
2011-11-25 14:14
浏览 76
已采纳

用PhP接收VLC生成的MP4有点乱

Im using VCL to broadcast to my localhost, 127.0.0.1 with UDP (legacy) method. To catch the traffic, I use this code:

$address = '127.0.0.1';
$port = 1234;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($sock, $address, $port) or die('Could not bind to address');
$f = fopen ('output', 'w');
fclose ($f);
$sock = stream_socket_server('udp://127.0.0.1:1234', $errno, $errstr, STREAM_SERVER_BIND);
while(1)
{
    $a = stream_socket_recvfrom($sock, 65536);
    $f = fopen('output', 'a');
    fwrite ($f, $a);
    fclose ($f);
    @ob_flush();
}

this logs the packets and saves, I rename it to .MP4 and open - well, the result is a little messy. I can recognize the output, the top screen is visible, the lower part is not good. I tried to capture it with another VCL player, and there were no problem.

图片转代码服务由CSDN问答提供 功能建议

我使用VCL广播到我的localhost,127.0.0.1使用UDP(传统)方法。 为了捕获流量,我使用以下代码:

  $ address ='127.0.0.1'; 
 $ port = 1234; 
 $ sock = socket_create(AF_INET,SOCK_DGRAM  ,SOL_UDP); 
socket_bind($ sock,$ address,$ port)或die('无法绑定到地址'); 
 $ f = fopen('output','w'); 
 nclclose($ f)  ; 
 $ sock = stream_socket_server('udp://127.0.0.1:1234',$ errno,$ errstr,STREAM_SERVER_BIND); 
while(1)
 {
 $ a = stream_socket_recvfrom($ sock,65536);  
 $ f = fopen('output','a'); 
 fwrite($ f,$ a); 
 fclose($ f); 
 @ob_flush(); 
} 
   
 
 

这会记录数据包并保存,我将其重命名为.MP4​​并打开 - 好吧,结果有点乱。 我可以识别输出,顶部屏幕可见,下部不好。 我试图用另一个VCL播放器捕获它,没有问题。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjie3267 2011-11-25 14:37
    已采纳

    Here is your code with a lot of useless stuff removed and a few efficiency improvements. Try it out and see what happens. It may or may not fix the problem, but report back with what happens and we'll take it from there.

    // Settings
    $address = '127.0.0.1';
    $port = 1234;
    $outfile = "output.mp4";
    
    // Open pointers
    if (!$ofp = fopen($outfile, 'w'))
      exit("Could not open output file for writing");
    if (!$ifp = stream_socket_server("udp://$address:$port", $errno, $errstr, STREAM_SERVER_BIND))
      exit("Could not create listen socket ($errno: $errstr)");
    
    // Loop and fetch data
    // This method of looping is flawed and will cause problems because you are using
    // UDP. The socket will never be "closed", so the loop will never exit. But you
    // were looping infinitely before, so this is no different - we can address this
    // later
    while (!feof($ifp)) {
      if (!strlen($chunk = fread($ifp, 8192))) continue;
      fwrite($ofp, $chunk);
    }
    
    // Close file pointers
    fclose($ofp);
    @fclose($ifp);
    
    点赞 评论

相关推荐 更多相似问题