Live streaming doesn't work - php

I'm using FFmpeg to capture my screen:
ffmpeg -f dshow -i video="UScreenCapture" -r 5 -s 640x480 -acodec libmp3lame -ac 1 -vcodec mpeg 4 -vtag divx -q 10 -f mpegts tcp://127.0.0.1:1234
so let it stream to somewhere. The accepter script:
error_reporting(E_ALL); /* Allow the script to hang around waiting for connections. */
set_time_limit(30); /* Turn on implicit output flushing so we see what we're getting as it comes in. */
ob_implicit_flush();
$address = '127.0.0.1';
$port = 1234;
$outfile = dirname(__FILE__)."/output.flv";
$ofp = fopen($outfile, 'wb');
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; sleep (5); die; }
if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; sleep (5); die; }
if (socket_listen($sock, 5) === false) { echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; sleep (5); die; }
if (($msgsock = socket_accept($sock)) === false) { echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; sleep (5); break; }
do {
$a = '';
socket_recv ($msgsock, $a, 65536, MSG_WAITALL);
fwrite ($ofp, $a);
//echo strlen($a)."\r\n";
} while (true);
it seems to save the stuff to the disk OK. Now here comes the html:
I dont really know how to do this, but based on an example:
<video src="/output.flv"></video>
but it doesn't do anything. And if I want to stream the live incoming stuff, then what's the matter?

HTML 5 Video will not support the Flv format HTML5 will be support the following format video only
.mp4 = H.264 + AAC
.ogg/.ogv = Theora + Vorbis
.webm = VP8 + Vorbis
study the HTML5 video basics in the following site
HTML5 video basics
if you want to play the flv you have to use the flash or Flex program or some flv players like flowplayer

Related

SSH Executing via PHP interrupted

I've got something like Video uploader written in PHP. When application puts video into FTP server, application is executing ffmpeg to convert that video to .mp4 format:
if(ftp_put($connection, $file_name, $file, FTP_BINARY) == true) {
ftp_close($connection);
if($extension == 'mp4') {
return redirect('/');
} else {
$ssh = new SSH2($server->ip);
if (!$ssh->login($server->ssh_user, $server->ssh_pass)) {
exit('Login Failed');
} else {
$ssh->exec('cd /var/www/videos/' . $video->id . ' && ffmpeg -i ' . $file_name . ' -c:v libx264 ' . $file_name_we . '.mp4');
}
}
}
The problem is: video converting is being interuptted. How to make that ffmpeg is going to successfully convert a video to mp4 via PHP?

socket_recv I want readable data not byte count

Hello All hopefully you guys don't shoot me with virtual guns if already asked but, here goes.
I am socket_send to an ip/port i see the server receives and responds with data back. My code receives xxxx bytes. I want to know what those bytes entail IE: xml string back so I can parse and use the data back in my application. cURL is not doable in this situation because the server receives headers and does not respond at all.
**Code:**
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$port'...";
$result = socket_connect($socket, $address, $port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$in = $xml;
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
socket_shutdown($socket, 1);
echo "Reading response:\n\n";
$buf = '';
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
echo "Read $bytes bytes from socket_recv(). Closing socket...";
} else {
echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
socket_close($socket);
echo $buf . "\n";
echo "OK.\n\n";
?>
Response:
TCP/IP Connection
OK. OK. Attempting to connect to 'xxx.xxx.xxx.xxx' on port '9000'...OK. Sending HTTP HEAD request...OK. Reading response: Read 1365 bytes from socket_recv(). Closing socket... OK.
Firstly, you can use cURL and not send headers! Just an fyi.
Secondly, it's going to be tricky to determine exactly what type of content it is... Is the server returning any sort of "content type"? If so, you may be able to read that.
Afterwards, you can then use $buf and then attempt to use XML libraries to parse the string (such as PHP's SimpleXML Library).
If, for example, it doesn't parse the XML, then you know it's either a regular string or something else.
cURL should return the content type, however.
After crazy headaches and several medicine hits I found the issue: 1 line OMG
If any one else gets this issue with socket the line after socket_close($socket) I added echo html_entity_decode($buf).

PHP socket (socket_read)

I work on a personal project and i have a problem for managing socket on a multi client server.
So, according to php.net, it's possible to put in argument of socket_read() a socket create by socket_create or accepted by socket_accept...
If i understand, socket create by socket_create is a "local" socket and sockets send by socket_accept are sockets client, it's that ?
in this case, i would like to put my server socket in parameter of socket_read for wait a client among several write on this socket.
more clearly, i accept 4 clients and wait that one of them write on this socket, but i can't put in parameter a specific client socket because i don't know which of them will write...
And when i put the socket create by : socket_create, php return me an error
my native language is not English as you could see, but nobody could answer me...
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() a échoué : raison : " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $this->address, $this->port) === false) {
echo "socket_bind() a échoué : raison : " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() a échoué : raison : " . socket_strerror(socket_last_error($sock)) . "\n";
}
for($i=0;$i<$this->maxClient;$i++){
if(($client = socket_accept($sock)) === false){
echo "socket_accept() a échoué : raison : " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}else echo "<br/>Client $client[$i] has connected\n";
$this->joueurs[$i] = new Joueur($client,$i);
}
while($start){
$messageClient = socket_read($sock , 1024);
......
}
So,I have two problems, the first is, i need that socket_read() doesn't block my script and Scanning my sockets until it find something to read.
The second problem is that as my program does it not stopped, my client don't receive anything and when i stop my serveur my client receive the message...
Have you any idea ?
Thanks
Get familiar with socket_select - it allows for waiting on multiple sockets to become readable or/and writable, or have errors. A listening socket becomes readable when there's new connection pending/ready to be accept()-ed.
i found my problem :
for unlock socket_read()
socket_set_option($socket,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>1, "usec"=>0));
socket_set_nonblock($socket);
And for clear buffer i use "/n" without that i could receive message in my client only when the server stop.

Writing then reading back via sockets in PHP issues

I writing a command and then reading back from a server via sockets in PHP. We have 20 servers that all run a Node JS script which can receive these commands and execute them. The Node JS script will return "ok" which PHP reads back to confirm the command has gone through.
The Node JS script listens on port 9000 and is set to allow half open.
Most of the time this works fine, but when a high volume of commands are sent we get errors back occasionally that say this:
Contents: Message received back from socket was 'Unexpected token {'
Transport endpoint is not connected
The transport endpoint message suggests to me that it has not connected successfully.
I am no expert in sockets so I don't know whether the implementation I have used is "correct". It does work most of the time but I am aware that there are functions like socket_bind and socket_listen that may work better, though I am not sure what they do.
Here is the PHP code that we are using. Any suggestions would be most appreciated.
public function sendDaemonCommand($address, $template_id, $params = array()) {
$hostname = $this->getHostnameFromPrivateIP($address);
$port = 9000;
$command = array('template_id' => $template_id, 'params' => $params);
$command = json_encode($command);
// Create a TCP Stream socket
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to create socket on " . $address . "\n\n" . socket_strerror(socket_last_error()) . "\n\nCommand:\n\n" . $command . "\n" . $this->functionTraceback());
return false;
}
// Connect to socket
if (socket_connect($sock, $address, $port) === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to connect to socket on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_close($sock);
return false;
}
// Write command to socket
$_command = $command;
$length = strlen($_command);
while (true) {
$sent = socket_write($sock, $_command, $length);
if ($sent === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to write command to socket on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_shutdown($sock, 2);
socket_close($sock);
return false;
}
if ($sent < $length) {
$_command = substr($_command, $sent);
$length -= $sent;
}
else {
break;
}
}
socket_shutdown($sock, 1);
// Read back from socket
if (($out = socket_read($sock, 1024)) !== false) {
#socket_shutdown($sock, 0);
$out = trim($out);
if ($out !== "ok") {
$this->mailError("Command Failed - " . $hostname, "Message received back from socket was '" . $out . "' on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_close($sock);
return false;
}
}
else {
$this->mailError("Command Failed - " . $hostname, "Failed to read from socket on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_shutdown($sock, 0);
socket_close($sock);
return false;
}
socket_close($sock);
return $out;
}
For a simple socket client such as this, I much prefer fsockopen() - it drastically reduces the complication of the client code and does not require the sockets extension, which is not available everywhere. The main disadvantage to this is that you lose the string error messages, but these are rarely that useful anyway - you still get an error string from creating the socket, and if read/write operations fail it's because the socket has become disconnected.
I'm also not sure how useful "allow half open" is in this context - I think it is likely to create more problems than it solves. The calls to socket_shutdown() are not doing anything useful here (and may be the source of your problems) - you would only use this if you are operating on a socket that will not be closed and may be operated on at a later point in time by some other code. Since you create a new socket and destroy it in the same routine, this is not the case.
Here is your code rewritten to use fsockopen() - as you can see, it is somewhat shorter and simpler. I have also modified it slightly so that it always returns a bool - your code returns either bool FALSE or string ok, and since there are only these two options it makes more sense for the function to always return a bool.
public function sendDaemonCommand($address, $template_id, $params = array()) {
// Prepare data
$hostname = $this->getHostnameFromPrivateIP($address);
$port = 9000;
$command = array('template_id' => $template_id, 'params' => $params);
$_command = $command = json_encode($command);
$length = strlen($_command);
// Connect to socket
if (!$sock = fsockopen($address, $port, $errNo, $errStr)) {
$this->mailError("Command Failed - " . $hostname, "Failed to connect to socket on " . $address . "\n\n" . $errStr . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
return false;
}
// Write command to socket
while (true) {
// Try and write data to socket
$sent = fwrite($sock, $_command, $length);
// If it failed, error out
if ($sent === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to write command to socket on " . $address . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
fclose($sock);
return false;
}
// If there is data left to send, try again
if ($sent < $length) {
$_command = substr($_command, $sent);
$length -= $sent;
continue;
}
// If we get here the write operation was successful
break;
}
// Read back from socket and close it
$out = fread($sock, 1024);
fclose($sock);
// Test the response from the server
if ($out === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to read from socket on " . $address . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
return false;
} else if (($out = trim($out)) !== "ok") {
$this->mailError("Command Failed - " . $hostname, "Message received back from socket was '" . $out . "' on " . $address . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
return false;
} else {
return true;
}
}

PHP: FLV to WMV conversion

How can I convert FLV to WMV? Is there any script around there or some way I can integrate this?
Thank you!!!
I don't think you can do this directly with PHP.
But, you can use external tools called form PHP (ffmpeg for example).
Here is a code sample:
<?php
$src = "file.flv";
$output = "file.wmv";
ffmpegPath = "/path/to/ffmpeg";
$flvtool2Path = "/path/to/flvtool2";
$ffmpegObj = new ffmpeg_movie($src);
$srcWidth = makeMultipleTwo($ffmpegObj->getFrameWidth());
$srcHeight = makeMultipleTwo($ffmpegObj->getFrameHeight());
$srcFPS = $ffmpegObj->getFrameRate();
$srcAB = intval($ffmpegObj->getAudioBitRate()/1000);
$srcAR = $ffmpegObj->getAudioSampleRate();
exec($ffmpegPath . " -i " . $src . " -ar " . $srcAR . " -ab " . $srcAB . " -vcodec wmv1 -acodec adpcm_ima_wav -s " . $srcWidth . "x" . $srcHeight . " " . $output. " | " . $flvtool2Path . " -U stdin " . $output);
// Make multiples function
function makeMultipleTwo ($value)
{
$sType = gettype($value/2);
if($sType == "integer")
{
return $value;
} else {
return ($value-1);
}
}
?>
Sources:
http://vexxhost.com/blog/2007/05/20/how-to-convertencode-files-to-flv-using-ffmpeg-php/
http://ubuntuforums.org/showpost.php?p=7315615&postcount=10
All solutions you will find are going to use ffmpeg, because that's easy to install on servers and even easier to utilize from PHP scripts. Most always you can just do:
exec("ffmpeg -i video.flv video.wmv");

Categories