2015-12-22 20:50



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??

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • duanjun7801 duanjun7801 5年前

    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(
        isset($parts['port']) ? $parts['port'] : 80,
      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);
    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);
      echo '{"success": true}';
    } else {
      echo '{"success": false}';
    点赞 评论 复制链接分享