dongsui0929 2015-12-22 20:50
浏览 146
已采纳

PHP:立即返回响应,但在后台运行多个冗长的shell_exec函数

I am creating a simple web service that accepts a video upload, runs multiple different encodes on the video (mp4, webm, ogv), and then uploads the newly created files to our video host.

Let's say I have multiple commands..

shell_exec('ffmpeg -i input.mp4 -f mp4 -c:v libx264 -preset slow -crf 24 -s 1280x720 -c:a libfdk_aac -profile:a aac_he -ar 22050 -b:a 64k -movflags +faststart output-1280x720.mp4');

shell_exec('ffmpeg -i input.mp4 -f mp4 -c:v libx264 -preset slow -crf 24 -s 1920x1080 -c:a libfdk_aac -profile:a aac_he -ar 22050 -b:a 64k -movflags +faststart output-1920x1080.mp4');

shell_exec('ffmpeg -i input.mp4 -f ogg -c:v libtheora -q:v 5 -s 1280x720 -c:a libvorbis -ar 22050 -b:a 64k -movflags +faststart output-1280x720.ogv');

shell_exec('ffmpeg -i input.mp4 -f ogg -c:v libtheora -q:v 5 -s 1920x1080 -c:a libvorbis -ar 22050 -b:a 64k -movflags +faststart output-1920x1080.ogv');

In summary, I want to...

  1. Print an immediate response: {success: true}
  2. Kick off multiple ffmpeg jobs synchronously.
  3. After each job is complete, send a POST to another server (one POST for each shell_exec).

It would also be nice to only send the POST if the job was successful, but I could easily work around that by just checking to see if the output file exists on the server.

I know that I can force shell_exec to run in the background by simply appending >/dev/null 2>/dev/null & to each command -- which would allow me to print a response immediately -- but I think by doing this, this would cause all of the jobs to run in parallel, and also, since this is diverting the output, I do not get any true callbacks when jobs are complete.

Any ideas??

  • 写回答

1条回答 默认 最新

  • duanjun7801 2016-01-15 17:09
    关注

    I decided to go with the idea of requesting the file asynchronously. To keep it clean, I was able to keep it all contained in a single file that "re-calls" itself with a different POST request.

    $root_path = $_SERVER['DOCUMENT_ROOT'] . '/';
    $root_http = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/';
    
    function request_async($url, $params, $type='POST', $timeout=30) {
      foreach ($params as $key => &$val) {
        if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
      }
      $post_string = implode('&', $post_params);
    
      $parts = parse_url($url);
    
      $fp = fsockopen(
        $parts['host'],
        isset($parts['port']) ? $parts['port'] : 80,
        $errno,
        $errstr,
        $timeout
      );
    
      if ('GET' == $type) $parts['path'] .= '?'.$post_string;
    
      $out = "$type ".$parts['path']." HTTP/1.1
    ";
      $out.= "Host: ".$parts['host']."
    ";
      $out.= "Content-Type: application/x-www-form-urlencoded
    ";
      $out.= "Content-Length: ".strlen($post_string)."
    ";
      $out.= "Connection: Close
    
    ";
    
      if ('POST' == $type && isset($post_string)) $out .= $post_string;
    
      fwrite($fp, $out);
      fclose($fp);
    }
    
    function get_http_path($path, $relative = FALSE){
      global $root_http, $root_path;
      return str_replace($root_path, $relative ? '/' : $root_http, $path);
    }
    
    $url = $_POST['url'];
    $media = $_POST['media'];
    
    if (isset($url)) {
      request_async(get_http_path(__FILE__), array(media => $url), 'POST', 1800);
      echo '{"success": true}';
    } else
    if (isset($media)) {
      $data = file_get_contents($media);
      // KICK OFF JOBS HERE
      echo '{"success": true}';
    } else {
      echo '{"success": false}';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路