用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.

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