在php代码

时间:2015-05-02 21:19:13

标签: php shell gstreamer pipeline

我在ubuntu中为gstreamer应用程序(管道)编写了一个脚本,通过网络摄像头和一个帧抓取器捕获2个视频流并将它们放入一个容器中:

 gst-launch -e mpegtsmux name="muxer" ! filesink location=TestHDMainCamera.ts \
 v4l2src device="/dev/video1" ! video/x-raw-yuv,  width=1280 ,height=720, \
  framerate=30/1  ! videorate ! ffmpegcolorspace ! x264enc ! muxer. \
 v4l2src device="/dev/video0" ! video/x-raw-yuv, width=1024 ,height=768, \
 framerate=30/1 ! x264enc !  muxer.  pulsesrc \
 device="alsa_input.usb-046d_Logitech_Webcam_C930e_AAF8A63E-02-C930e.analog-stereo" \
  ! audioconvert ! lamemp3enc target=1 cbr=true  ! muxer.

我只是将它放入.sh文件并通过终端执行。为了让生活更轻松我写了一个简单的PHP代码来创建一个简单的(!)网站界面(在本地主机上运行)来控制捕获过程(开始:运行.sh文件并停止:Ctrl + c)

包括两个额外的按钮,分别测试来自帧抓取器和网络摄像头的流,看看它们是否已连接:

网络摄像头测试预览:

  gst-launch v4l2src device=/dev/video1 ! ffmpegcolorspace ! xvimagesink

FrameGrabber的测试预览:

  gst-launch v4l2src device=/dev/video0 ! ffmpegcolorspace ! xvimagesink

以下是问题:

每次我加载页面并按下开始按钮时,似乎php执行.sh管道并立即终止它(我在屏幕上显示此消息:释放管道...)因此我得不到捕获的流( !)即使我尝试使用测试按钮来获得直播结果也是一样的......

你能帮助我并告诉我如何使用这个PHP代码运行gstreamer捕获的管道代码? (只有当我按下“停止”按钮才能执行Ctrl + c功能时,才需要终止gstreamer管道)

更多信息:

这是界面的main.php代码(它有点乱,抱歉):

 <html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
<?php
echo '<p>Hello</p>';
print_r($_GET);

if (isset($_GET['action'])) {
switch ($_GET['action']) {
    case 'Test Webcam':
        testWebcam();
        break;
    case 'Test Framegrabber':
        testFramegrabber();
        break;
}
}

function testFramegrabber() {
echo "The Test Framegrabber function is called.";
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
}

function testWebcam() {
echo "The Test Webcam function is called.";
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
}

//$output = shell_exec('ls -lart');
//echo "<pre>$output</pre>";
?>
<form action="main.php">
    <input type="submit" class="button" name="action" value="Test Webcam" />
<input type="submit" class="button" name="action" value="Test     Framegrabber" />
</form>
</body>
</html>

这里是按钮和其他细节的record.php:

<html>
<head>
<title>Recording page</title>
<link rel="stylesheet" type="text/css"     href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

<script type="text/javascript">
var w = null; // initialize variable

//function to start the timer
function startTimer()
{
// First check whether Web Workers are supported
if (typeof(Worker)!=="undefined"){
   // Check whether Web Worker has been created. If not, create a new Web     Worker based on the Javascript file simple-timer.js
   if (w==null){
      w = new Worker("simple-timer.js");
   }
   // Update timer div with output from Web Worker
   w.onmessage = function (event) {
      document.getElementById("timer").innerHTML = event.data;
   };
} else {
   // Web workers are not supported by your browser
   document.getElementById("timer").innerHTML = "Sorry, your browser does     not support Web Workers ...";
}
}

//function to stop the timer
function stopTimer()
{
w.terminate();
timerStart = true;
w = null;
}

function handleTimer() {
    document.getElementById("timerp").innerHTML = "start timer";
}
</script>
 </head>
<body>
 <?php
 echo '<p>Hello</p>';
 print_r($_GET);

if (isset($_GET['action'])) {
    switch ($_GET['action']) {
        case 'Test Webcam':
            testWebcam();
            break;
        case 'Test Framegrabber':
            testFramegrabber();
            break;
        case 'Record':
            record();
            break;
     }
}

function testFramegrabber() {
    echo "The Test Framegrabber function is called.";
    $output = shell_exec('ls -lart');
    echo "<pre>$output</pre>";
}

function testWebcam() {
    echo "The Test Webcam function is called.";
    $output = shell_exec('ls -lart');
    echo "<pre>$output</pre>";
 }

function record(){
echo "The Record function is called.";
echo "<script> startTimer(); </script>";
}
 function stop(){
    echo "The Stop function is called.";
    echo "<script> stopTimer(); </script>";
}
//$output = shell_exec('ls -lart');
//echo "<pre>$output</pre>";
?>
 <div class="header">A simple timer:</div>

   <div class="timer" id="timer">00:00</div>
   <div class="buttons">
      <button onclick="startTimer()" id="button1">Start</button>
      <button onclick="stopTimer()" id = "button2">Stop</button>
   </div>
 <form class="form-horizontal" action="record.php">
<fieldset>

<!-- Form Name -->
<legend>Sreen recorder</legend>

 <table class="tg" width="100%">
  <tr>
    <th class="tg-031e"></th>
    <th class="tg-031e">fdsfs</th>
  </tr>
  <tr>
    <td class="tg-031e"></td>
    <td class="tg-031e"></td>
    <td class="tg-031e"></td>
  </tr>
  <tr>
    <td class="tg-031e"></td>
    <td class="tg-031e"></td>
    <td class="tg-031e"></td>
  </tr>
   <tr>
    <td class="tg-031e">
        <!-- Button -->
        <div class="control-group">
        <label class="control-label" for="recordbtn"></label>
        <div class="controls">
        <button id="recordbtn" name="action" class="btn btn-danger"     value="Record" onclick="startTimer()">Record</button>
        </div>
        </div>
    </td>
    <td class="tg-031e">
        <!-- Button -->
        <div class="control-group">
        <label class="control-label" for="stopbtn"></label>
        <div class="controls">
        <button id="stopbtn" name="action" class="btn btn-inverse"     value="Stop" onclick="stopTimer()">Stop</button>
        </div>
        </div>
    </td>
     <td class="tg-031e">
        <!-- Button -->
        <div class="control-group">
        <label class="control-label" for="nextbtn"></label>
        <div class="controls">
        <button id="nextbtn" name="action" class="btn btn-inverse">Next     recording</button>
        </div>
        </div>
    </td>
  </tr>
</table>



<!-- Button -->
<div class="control-group">
<label class="control-label" for="webcamTest"></label>
<div class="controls">
<button id="webcamTest" name="action" value="Test Webcam" class="btn     btn-info">Test Webcam</button>
</div>
</div>

<!-- Button -->
<div class="control-group">
<label class="control-label" for="testFramegrabber"></label>
<div class="controls">
<button id="testFramegrabber" name="action" value="Test Framegrabber"     class="btn btn-info">Test Framegrabber</button>
</div>
</div>

<!-- Select Basic -->
<div class="control-group">
<label class="control-label" for="recordPath">Path to record</label>
<div class="controls">
<select id="recordPath" name="recordPath" class="input-xlarge">
<option>/desktop</option>
<option>/home/var</option>
</select>
</div>
</div>



</fieldset>
</form>
</body>
</html>

只是在页面上捕获时显示的简单计时器:

    var timerStart = true;

function myTimer(d0)
{
   // get current time
   var d=(new Date()).valueOf();
   // calculate time difference between now and initial time
   var diff = d-d0;
   // calculate number of minutes
   var minutes = Math.floor(diff/1000/60);
   // calculate number of seconds
   var seconds = Math.floor(diff/1000)-minutes*60;
   var myVar = null;
   // if number of minutes less than 10, add a leading "0"
   minutes = minutes.toString();
   if (minutes.length == 1){
      minutes = "0"+minutes;
   }
   // if number of seconds less than 10, add a leading "0"
   seconds = seconds.toString();
   if (seconds.length == 1){
      seconds = "0"+seconds;
   }

   // return output to Web Worker
   postMessage(minutes+":"+seconds);
}

if (timerStart){
   // get current time
   var d0=(new Date()).valueOf();
   // repeat myTimer(d0) every 100 ms
   myVar=setInterval(function(){myTimer(d0)},100);
   // timer should not start anymore since it has been started
   timerStart = false;
    }

2 个答案:

答案 0 :(得分:0)

我建议检查你的apache用户。 你说你正在使用ubuntu,所以它将是“www-data”,它可能无法访问你的DISPLAY或“/ dev / video0”。

  • 尝试在终端中执行.sh作为www-data(您可能需要在/ etc / passwd中启用对www-data的shell访问)

  • 一个快速而肮脏的解决方案是以普通用户身份运行您的php。从终端写道:

    php -S 0.0.0.0:8080 -t your_directory /

答案 1 :(得分:0)

我遇到了同样的问题,我无法从PHP代码启动GStreamer管道。但是我可以从终端开始。

对我来说,解决方案是我必须在命令前使用'SUDO'。当我在Apache服务器上运行PHP代码时。 apache服务器在www-data用户上运行,并且需要SUDO访问权限才能运行命令。

我正在使用:(在PHP中)

    $start = shell_exec('usr/bin/gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw, format=(string)UYVY, width=(int)1920,height=(int)1080" ! nvvidconv ! "video/x-raw(memory:NVMM),format=(string)I420" ! omxh264enc bitrate=3000000 ! flvmux ! rtmpsink location="rtmp://127.0.0.1:1936/mycamera/mystream" 2>&1 &');

        echo "<pre>$start</pre>";

但是我必须在SUDO中使用以下'sudo':(在PHP中)

    $start = shell_exec('sudo /usr/bin/gst-launch-1.0 v4l2src device=/dev/video0 ! "video/x-raw, format=(string)UYVY, width=(int)1920,height=(int)1080" ! nvvidconv ! "video/x-raw(memory:NVMM),format=(string)I420" ! omxh264enc bitrate=3000000 ! flvmux ! rtmpsink location="rtmp://127.0.0.1:1936/webcam/mystream" 2>&1 &');

        echo "<pre>$start</pre>";
相关问题