php – 如何以编程方式启动/停止FFMPEG流转码
我有一个ip网络摄像头,提供MJPEG流.我可以在OSX下使用ffmpeg成功转码并保存该流.以下几乎给了我我想要的东西:
ffmpeg -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4 这将启动一个FFMPEG会话并开始将实时流保存到我的test.mp4文件中.按q将退出ffmpeg并保存文件. 我想以编程方式启动&使用PHP或Bash shell脚本停止录制.我尝试过以下方法: <?php $pid = pcntl_fork(); if($pid == -1){ die("could not fork"); }elseif($pid){ // we are the parent... print $pid.' started recording. waiting 10 seconds...'; sleep(10); // Wait 10 seconds print_r(shell_exec("kill ".$pid)); // Kill the child recording process echo 'done'; exit(); }else{ // we are the child process. call ffmpeg. exec('../lib/ffmpeg -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4'); } 但是有两个问题: > ffmpeg进程没有结束/死亡(可能是因为它再次分叉) 解决方法
所以我做错了.
对于初学者,我需要将输出格式ffmpeg推送到日志文件,并告诉它覆盖我的临时文件而不提示使用-y参数. 而不是 ffmpeg -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4 我现在正在使用 ffmpeg -y -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4 </dev/null >/dev/null 2>/tmp/ffmpeg.log & 第二个问题是我在将kill命令发送到ffmpeg之前没有等待足够长的时间,因此正在创建一个损坏的文件. 通过在1秒内添加-t(用于时间限制)参数,我确定ffmpeg平均需要15秒来记录1秒的视频.将时间限制增加到10秒使平均增加到25秒,所以似乎在我的服务器上至少有14秒的开销.通过将我的PHP脚本中的睡眠命令增加到30秒,我能够获得可用的视频文件. 因此,让PHP终止ffmpeg进程导致未知(或近似最佳)的记录时间长度,这完全取决于CPU功率,网络带宽等. 这有点令人失望,因为我希望能够根据一些外部变量增加记录长度(我有运动传感器输入数据库,我想记录直到运动停止). 无论如何,这是一个有效的PHP脚本,以防将来帮助某人: <?php print date('H:i:s')."nStarted recording. waiting 60 seconds...n"; exec('../lib/ffmpeg -y -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4 </dev/null >/dev/null 2>/tmp/ffmpeg.log &'); sleep(60); // Wait long enough before killing,otherwise the video will be corrupt! shell_exec('pkill ffmpeg'); // find and kill since we don't know the PID echo "donenn"; exit(); ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |