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



我使用VCL广播到我的本地主机,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) 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();
}

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

这是您的代码,删除了许多无用的东西,并提高了一些效率。尝试一下,看看会发生什么。它可能解决问题,也可能不解决问题,但报告发生了什么,我们会从那里处理。

// 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);

相关内容

  • 没有找到相关文章

最新更新